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 under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * --------------------
  25. * RegressionTests.java
  26. * --------------------
  27. * (C) Copyright 2002-2005 by Object Refinery Limited and Contributors.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: RegressionTests.java,v 1.2 2005/01/11 17:50:16 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 30-Sep-2002 : Version 1 (DG);
  37. * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  38. *
  39. */
  40. package org.jfree.data.statistics.junit;
  41. import junit.framework.Test;
  42. import junit.framework.TestCase;
  43. import junit.framework.TestSuite;
  44. import org.jfree.data.statistics.Regression;
  45. import org.jfree.data.xy.XYDataset;
  46. import org.jfree.data.xy.XYSeries;
  47. import org.jfree.data.xy.XYSeriesCollection;
  48. /**
  49. * Tests for the {@link Regression} class.
  50. */
  51. public class RegressionTests extends TestCase {
  52. /**
  53. * Returns the tests as a test suite.
  54. *
  55. * @return The test suite.
  56. */
  57. public static Test suite() {
  58. return new TestSuite(RegressionTests.class);
  59. }
  60. /**
  61. * Constructs a new set of tests.
  62. *
  63. * @param name the name of the tests.
  64. */
  65. public RegressionTests(String name) {
  66. super(name);
  67. }
  68. /**
  69. * Checks the results of an OLS regression on sample dataset 1.
  70. */
  71. public void testOLSRegression1a() {
  72. double[][] data = createSampleData1();
  73. double[] result1 = Regression.getOLSRegression(data);
  74. assertEquals(.25680930, result1[0], 0.0000001);
  75. assertEquals(0.72792106, result1[1], 0.0000001);
  76. }
  77. /**
  78. * Checks the results of an OLS regression on sample dataset 1 AFTER converting it to
  79. * an XYSeries.
  80. */
  81. public void testOLSRegression1b() {
  82. double[][] data = createSampleData1();
  83. XYSeries series = new XYSeries("Test");
  84. for (int i = 0; i < 11; i++) {
  85. series.add(data[i][0], data[i][1]);
  86. }
  87. XYDataset ds = new XYSeriesCollection(series);
  88. double[] result2 = Regression.getOLSRegression(ds, 0);
  89. assertEquals(.25680930, result2[0], 0.0000001);
  90. assertEquals(0.72792106, result2[1], 0.0000001);
  91. }
  92. /**
  93. * Checks the results of a power regression on sample dataset 1.
  94. */
  95. public void testPowerRegression1a() {
  96. double[][] data = createSampleData1();
  97. double[] result = Regression.getPowerRegression(data);
  98. assertEquals(0.91045813, result[0], 0.0000001);
  99. assertEquals(0.88918346, result[1], 0.0000001);
  100. }
  101. /**
  102. * Checks the results of a power regression on sample dataset 1 AFTER converting it to
  103. * an XYSeries.
  104. */
  105. public void testPowerRegression1b() {
  106. double[][] data = createSampleData1();
  107. XYSeries series = new XYSeries("Test");
  108. for (int i = 0; i < 11; i++) {
  109. series.add(data[i][0], data[i][1]);
  110. }
  111. XYDataset ds = new XYSeriesCollection(series);
  112. double[] result = Regression.getPowerRegression(ds, 0);
  113. assertEquals(0.91045813, result[0], 0.0000001);
  114. assertEquals(0.88918346, result[1], 0.0000001);
  115. }
  116. /**
  117. * Checks the results of an OLS regression on sample dataset 2.
  118. */
  119. public void testOLSRegression2a() {
  120. double[][] data = createSampleData2();
  121. double[] result = Regression.getOLSRegression(data);
  122. assertEquals(53.9729697, result[0], 0.0000001);
  123. assertEquals(-4.1823030, result[1], 0.0000001);
  124. }
  125. /**
  126. * Checks the results of an OLS regression on sample dataset 2 AFTER converting it to
  127. * an XYSeries.
  128. */
  129. public void testOLSRegression2b() {
  130. double[][] data = createSampleData2();
  131. XYSeries series = new XYSeries("Test");
  132. for (int i = 0; i < 10; i++) {
  133. series.add(data[i][0], data[i][1]);
  134. }
  135. XYDataset ds = new XYSeriesCollection(series);
  136. double[] result = Regression.getOLSRegression(ds, 0);
  137. assertEquals(53.9729697, result[0], 0.0000001);
  138. assertEquals(-4.1823030, result[1], 0.0000001);
  139. }
  140. /**
  141. * Checks the results of a power regression on sample dataset 2.
  142. */
  143. public void testPowerRegression2a() {
  144. double[][] data = createSampleData2();
  145. double[] result = Regression.getPowerRegression(data);
  146. assertEquals(106.1241681, result[0], 0.0000001);
  147. assertEquals(-0.8466615, result[1], 0.0000001);
  148. }
  149. /**
  150. * Checks the results of a power regression on sample dataset 2 AFTER converting it to
  151. * an XYSeries.
  152. */
  153. public void testPowerRegression2b() {
  154. double[][] data = createSampleData2();
  155. XYSeries series = new XYSeries("Test");
  156. for (int i = 0; i < 10; i++) {
  157. series.add(data[i][0], data[i][1]);
  158. }
  159. XYDataset ds = new XYSeriesCollection(series);
  160. double[] result = Regression.getPowerRegression(ds, 0);
  161. assertEquals(106.1241681, result[0], 0.0000001);
  162. assertEquals(-0.8466615, result[1], 0.0000001);
  163. }
  164. /**
  165. * Creates and returns a sample dataset.
  166. * <P>
  167. * The data is taken from Table 11.2, page 313 of "Understanding Statistics" by Ott and
  168. * Mendenhall (Duxbury Press).
  169. *
  170. * @return the sample data.
  171. */
  172. private double[][] createSampleData1() {
  173. double[][] result = new double[11][2];
  174. result[0][0] = 2.00;
  175. result[0][1] = 1.60;
  176. result[1][0] = 2.25;
  177. result[1][1] = 2.00;
  178. result[2][0] = 2.60;
  179. result[2][1] = 1.80;
  180. result[3][0] = 2.65;
  181. result[3][1] = 2.80;
  182. result[4][0] = 2.80;
  183. result[4][1] = 2.10;
  184. result[5][0] = 3.10;
  185. result[5][1] = 2.00;
  186. result[6][0] = 2.90;
  187. result[6][1] = 2.65;
  188. result[7][0] = 3.25;
  189. result[7][1] = 2.25;
  190. result[8][0] = 3.30;
  191. result[8][1] = 2.60;
  192. result[9][0] = 3.60;
  193. result[9][1] = 3.00;
  194. result[10][0] = 3.25;
  195. result[10][1] = 3.10;
  196. return result;
  197. }
  198. /**
  199. * Creates a sample data set.
  200. *
  201. * @return the sample data.
  202. */
  203. private double[][] createSampleData2() {
  204. double[][] result = new double[10][2];
  205. result[0][0] = 2;
  206. result[0][1] = 56.27;
  207. result[1][0] = 3;
  208. result[1][1] = 41.32;
  209. result[2][0] = 4;
  210. result[2][1] = 31.45;
  211. result[3][0] = 5;
  212. result[3][1] = 30.05;
  213. result[4][0] = 6;
  214. result[4][1] = 24.69;
  215. result[5][0] = 7;
  216. result[5][1] = 19.78;
  217. result[6][0] = 8;
  218. result[6][1] = 20.94;
  219. result[7][0] = 9;
  220. result[7][1] = 16.73;
  221. result[8][0] = 10;
  222. result[8][1] = 14.21;
  223. result[9][0] = 11;
  224. result[9][1] = 12.44;
  225. return result;
  226. }
  227. }