KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * --------------------
23  * RegressionTests.java
24  * --------------------
25  * (C) Copyright 2002, 2003 by Object Refinery Limited and Contributors.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: RegressionTests.java,v 1.3 2003/06/12 16:54:32 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 30-Sep-2002 : Version 1 (DG);
35  * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
36  *
37  */

38
39 package org.jfree.data.junit;
40
41 import junit.framework.Test;
42 import junit.framework.TestCase;
43 import junit.framework.TestSuite;
44
45 import org.jfree.data.Regression;
46 import org.jfree.data.XYDataset;
47 import org.jfree.data.XYSeries;
48 import org.jfree.data.XYSeriesCollection;
49
50 /**
51  * Tests for the {@link Regression} class.
52  *
53  * @author David Gilbert
54  */

55 public class RegressionTests extends TestCase {
56
57     /**
58      * Returns the tests as a test suite.
59      *
60      * @return the test suite.
61      */

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

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

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

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

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

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

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

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

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

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

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

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