KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > statistics > junit > RegressionTests


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * --------------------
28  * RegressionTests.java
29  * --------------------
30  * (C) Copyright 2002-2005 by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: RegressionTests.java,v 1.1.2.1 2006/10/03 15:41:41 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 30-Sep-2002 : Version 1 (DG);
40  * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
41  *
42  */

43
44 package org.jfree.data.statistics.junit;
45
46 import junit.framework.Test;
47 import junit.framework.TestCase;
48 import junit.framework.TestSuite;
49
50 import org.jfree.data.statistics.Regression;
51 import org.jfree.data.xy.XYDataset;
52 import org.jfree.data.xy.XYSeries;
53 import org.jfree.data.xy.XYSeriesCollection;
54
55 /**
56  * Tests for the {@link Regression} class.
57  */

58 public class RegressionTests extends TestCase {
59
60     /**
61      * Returns the tests as a test suite.
62      *
63      * @return The test suite.
64      */

65     public static Test suite() {
66         return new TestSuite(RegressionTests.class);
67     }
68
69     /**
70      * Constructs a new set of tests.
71      *
72      * @param name the name of the tests.
73      */

74     public RegressionTests(String JavaDoc name) {
75         super(name);
76     }
77
78     /**
79      * Checks the results of an OLS regression on sample dataset 1.
80      */

81     public void testOLSRegression1a() {
82
83         double[][] data = createSampleData1();
84         double[] result1 = Regression.getOLSRegression(data);
85         assertEquals(.25680930, result1[0], 0.0000001);
86         assertEquals(0.72792106, result1[1], 0.0000001);
87
88     }
89
90     /**
91      * Checks the results of an OLS regression on sample dataset 1 AFTER
92      * converting it to an XYSeries.
93      */

94     public void testOLSRegression1b() {
95
96         double[][] data = createSampleData1();
97
98         XYSeries series = new XYSeries("Test");
99         for (int i = 0; i < 11; i++) {
100             series.add(data[i][0], data[i][1]);
101         }
102         XYDataset ds = new XYSeriesCollection(series);
103         double[] result2 = Regression.getOLSRegression(ds, 0);
104
105         assertEquals(.25680930, result2[0], 0.0000001);
106         assertEquals(0.72792106, result2[1], 0.0000001);
107
108     }
109
110     /**
111      * Checks the results of a power regression on sample dataset 1.
112      */

113     public void testPowerRegression1a() {
114
115         double[][] data = createSampleData1();
116         double[] result = Regression.getPowerRegression(data);
117         assertEquals(0.91045813, result[0], 0.0000001);
118         assertEquals(0.88918346, result[1], 0.0000001);
119
120     }
121
122     /**
123      * Checks the results of a power regression on sample dataset 1 AFTER
124      * converting it to an XYSeries.
125      */

126     public void testPowerRegression1b() {
127
128         double[][] data = createSampleData1();
129
130         XYSeries series = new XYSeries("Test");
131         for (int i = 0; i < 11; i++) {
132             series.add(data[i][0], data[i][1]);
133         }
134         XYDataset ds = new XYSeriesCollection(series);
135         double[] result = Regression.getPowerRegression(ds, 0);
136
137         assertEquals(0.91045813, result[0], 0.0000001);
138         assertEquals(0.88918346, result[1], 0.0000001);
139
140     }
141
142     /**
143      * Checks the results of an OLS regression on sample dataset 2.
144      */

145     public void testOLSRegression2a() {
146
147         double[][] data = createSampleData2();
148         double[] result = Regression.getOLSRegression(data);
149         assertEquals(53.9729697, result[0], 0.0000001);
150         assertEquals(-4.1823030, result[1], 0.0000001);
151
152     }
153
154     /**
155      * Checks the results of an OLS regression on sample dataset 2 AFTER
156      * converting it to an XYSeries.
157      */

158     public void testOLSRegression2b() {
159
160         double[][] data = createSampleData2();
161
162         XYSeries series = new XYSeries("Test");
163         for (int i = 0; i < 10; i++) {
164             series.add(data[i][0], data[i][1]);
165         }
166         XYDataset ds = new XYSeriesCollection(series);
167         double[] result = Regression.getOLSRegression(ds, 0);
168
169         assertEquals(53.9729697, result[0], 0.0000001);
170         assertEquals(-4.1823030, result[1], 0.0000001);
171
172     }
173
174     /**
175      * Checks the results of a power regression on sample dataset 2.
176      */

177     public void testPowerRegression2a() {
178
179         double[][] data = createSampleData2();
180         double[] result = Regression.getPowerRegression(data);
181         assertEquals(106.1241681, result[0], 0.0000001);
182         assertEquals(-0.8466615, result[1], 0.0000001);
183
184     }
185
186     /**
187      * Checks the results of a power regression on sample dataset 2 AFTER
188      * converting it to an XYSeries.
189      */

190     public void testPowerRegression2b() {
191
192         double[][] data = createSampleData2();
193
194         XYSeries series = new XYSeries("Test");
195         for (int i = 0; i < 10; i++) {
196             series.add(data[i][0], data[i][1]);
197         }
198         XYDataset ds = new XYSeriesCollection(series);
199         double[] result = Regression.getPowerRegression(ds, 0);
200
201         assertEquals(106.1241681, result[0], 0.0000001);
202         assertEquals(-0.8466615, result[1], 0.0000001);
203
204     }
205
206     /**
207      * Creates and returns a sample dataset.
208      * <P>
209      * The data is taken from Table 11.2, page 313 of "Understanding Statistics"
210      * by Ott and Mendenhall (Duxbury Press).
211      *
212      * @return The sample data.
213      */

214     private double[][] createSampleData1() {
215
216         double[][] result = new double[11][2];
217
218         result[0][0] = 2.00;
219         result[0][1] = 1.60;
220         result[1][0] = 2.25;
221         result[1][1] = 2.00;
222         result[2][0] = 2.60;
223         result[2][1] = 1.80;
224         result[3][0] = 2.65;
225         result[3][1] = 2.80;
226         result[4][0] = 2.80;
227         result[4][1] = 2.10;
228         result[5][0] = 3.10;
229         result[5][1] = 2.00;
230         result[6][0] = 2.90;
231         result[6][1] = 2.65;
232         result[7][0] = 3.25;
233         result[7][1] = 2.25;
234         result[8][0] = 3.30;
235         result[8][1] = 2.60;
236         result[9][0] = 3.60;
237         result[9][1] = 3.00;
238         result[10][0] = 3.25;
239         result[10][1] = 3.10;
240
241         return result;
242
243     }
244
245     /**
246      * Creates a sample data set.
247      *
248      * @return The sample data.
249      */

250     private double[][] createSampleData2() {
251
252         double[][] result = new double[10][2];
253
254         result[0][0] = 2;
255         result[0][1] = 56.27;
256         result[1][0] = 3;
257         result[1][1] = 41.32;
258         result[2][0] = 4;
259         result[2][1] = 31.45;
260         result[3][0] = 5;
261         result[3][1] = 30.05;
262         result[4][0] = 6;
263         result[4][1] = 24.69;
264         result[5][0] = 7;
265         result[5][1] = 19.78;
266         result[6][0] = 8;
267         result[6][1] = 20.94;
268         result[7][0] = 9;
269         result[7][1] = 16.73;
270         result[8][0] = 10;
271         result[8][1] = 14.21;
272         result[9][0] = 11;
273         result[9][1] = 12.44;
274
275         return result;
276
277     }
278
279 }
280
Popular Tags