001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * --------------------
028: * RegressionTests.java
029: * --------------------
030: * (C) Copyright 2002-2005 by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: RegressionTests.java,v 1.1.2.1 2006/10/03 15:41:41 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 30-Sep-2002 : Version 1 (DG);
040: * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
041: *
042: */
043:
044: package org.jfree.data.statistics.junit;
045:
046: import junit.framework.Test;
047: import junit.framework.TestCase;
048: import junit.framework.TestSuite;
049:
050: import org.jfree.data.statistics.Regression;
051: import org.jfree.data.xy.XYDataset;
052: import org.jfree.data.xy.XYSeries;
053: import org.jfree.data.xy.XYSeriesCollection;
054:
055: /**
056: * Tests for the {@link Regression} class.
057: */
058: public class RegressionTests extends TestCase {
059:
060: /**
061: * Returns the tests as a test suite.
062: *
063: * @return The test suite.
064: */
065: public static Test suite() {
066: return new TestSuite(RegressionTests.class);
067: }
068:
069: /**
070: * Constructs a new set of tests.
071: *
072: * @param name the name of the tests.
073: */
074: public RegressionTests(String name) {
075: super (name);
076: }
077:
078: /**
079: * Checks the results of an OLS regression on sample dataset 1.
080: */
081: public void testOLSRegression1a() {
082:
083: double[][] data = createSampleData1();
084: double[] result1 = Regression.getOLSRegression(data);
085: assertEquals(.25680930, result1[0], 0.0000001);
086: assertEquals(0.72792106, result1[1], 0.0000001);
087:
088: }
089:
090: /**
091: * Checks the results of an OLS regression on sample dataset 1 AFTER
092: * converting it to an XYSeries.
093: */
094: public void testOLSRegression1b() {
095:
096: double[][] data = createSampleData1();
097:
098: XYSeries series = new XYSeries("Test");
099: 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: }
|