RegressionTest.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2013, 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. * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  25. * Other names may be trademarks of their respective owners.]
  26. *
  27. * -------------------
  28. * RegressionTest.java
  29. * -------------------
  30. * (C) Copyright 2002-2013, by Object Refinery Limited and Contributors.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes
  36. * -------
  37. * 30-Sep-2002 : Version 1 (DG);
  38. * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  39. *
  40. */
  41. package org.jfree.data.statistics;
  42. import static org.junit.Assert.assertEquals;
  43. import org.jfree.data.xy.XYDataset;
  44. import org.jfree.data.xy.XYSeries;
  45. import org.jfree.data.xy.XYSeriesCollection;
  46. import org.junit.Test;
  47. /**
  48. * Tests for the {@link Regression} class.
  49. */
  50. public class RegressionTest {
  51. /**
  52. * Checks the results of an OLS regression on sample dataset 1.
  53. */
  54. @Test
  55. public void testOLSRegression1a() {
  56. double[][] data = createSampleData1();
  57. double[] result1 = Regression.getOLSRegression(data);
  58. assertEquals(.25680930, result1[0], 0.0000001);
  59. assertEquals(0.72792106, result1[1], 0.0000001);
  60. }
  61. /**
  62. * Checks the results of an OLS regression on sample dataset 1 AFTER
  63. * converting it to an XYSeries.
  64. */
  65. @Test
  66. public void testOLSRegression1b() {
  67. double[][] data = createSampleData1();
  68. XYSeries series = new XYSeries("Test");
  69. for (int i = 0; i < 11; i++) {
  70. series.add(data[i][0], data[i][1]);
  71. }
  72. XYDataset ds = new XYSeriesCollection(series);
  73. double[] result2 = Regression.getOLSRegression(ds, 0);
  74. assertEquals(.25680930, result2[0], 0.0000001);
  75. assertEquals(0.72792106, result2[1], 0.0000001);
  76. }
  77. /**
  78. * Checks the results of a power regression on sample dataset 1.
  79. */
  80. @Test
  81. public void testPowerRegression1a() {
  82. double[][] data = createSampleData1();
  83. double[] result = Regression.getPowerRegression(data);
  84. assertEquals(0.91045813, result[0], 0.0000001);
  85. assertEquals(0.88918346, result[1], 0.0000001);
  86. }
  87. /**
  88. * Checks the results of a power regression on sample dataset 1 AFTER
  89. * converting it to an XYSeries.
  90. */
  91. @Test
  92. public void testPowerRegression1b() {
  93. double[][] data = createSampleData1();
  94. XYSeries series = new XYSeries("Test");
  95. for (int i = 0; i < 11; i++) {
  96. series.add(data[i][0], data[i][1]);
  97. }
  98. XYDataset ds = new XYSeriesCollection(series);
  99. double[] result = Regression.getPowerRegression(ds, 0);
  100. assertEquals(0.91045813, result[0], 0.0000001);
  101. assertEquals(0.88918346, result[1], 0.0000001);
  102. }
  103. /**
  104. * Checks the results of an OLS regression on sample dataset 2.
  105. */
  106. @Test
  107. public void testOLSRegression2a() {
  108. double[][] data = createSampleData2();
  109. double[] result = Regression.getOLSRegression(data);
  110. assertEquals(53.9729697, result[0], 0.0000001);
  111. assertEquals(-4.1823030, result[1], 0.0000001);
  112. }
  113. /**
  114. * Checks the results of an OLS regression on sample dataset 2 AFTER
  115. * converting it to an XYSeries.
  116. */
  117. @Test
  118. public void testOLSRegression2b() {
  119. double[][] data = createSampleData2();
  120. XYSeries series = new XYSeries("Test");
  121. for (int i = 0; i < 10; i++) {
  122. series.add(data[i][0], data[i][1]);
  123. }
  124. XYDataset ds = new XYSeriesCollection(series);
  125. double[] result = Regression.getOLSRegression(ds, 0);
  126. assertEquals(53.9729697, result[0], 0.0000001);
  127. assertEquals(-4.1823030, result[1], 0.0000001);
  128. }
  129. /**
  130. * Checks the results of a power regression on sample dataset 2.
  131. */
  132. @Test
  133. public void testPowerRegression2a() {
  134. double[][] data = createSampleData2();
  135. double[] result = Regression.getPowerRegression(data);
  136. assertEquals(106.1241681, result[0], 0.0000001);
  137. assertEquals(-0.8466615, result[1], 0.0000001);
  138. }
  139. /**
  140. * Checks the results of a power regression on sample dataset 2 AFTER
  141. * converting it to an XYSeries.
  142. */
  143. @Test
  144. public void testPowerRegression2b() {
  145. double[][] data = createSampleData2();
  146. XYSeries series = new XYSeries("Test");
  147. for (int i = 0; i < 10; i++) {
  148. series.add(data[i][0], data[i][1]);
  149. }
  150. XYDataset ds = new XYSeriesCollection(series);
  151. double[] result = Regression.getPowerRegression(ds, 0);
  152. assertEquals(106.1241681, result[0], 0.0000001);
  153. assertEquals(-0.8466615, result[1], 0.0000001);
  154. }
  155. /**
  156. * Creates and returns a sample dataset.
  157. * <P>
  158. * The data is taken from Table 11.2, page 313 of "Understanding Statistics"
  159. * by Ott and Mendenhall (Duxbury Press).
  160. *
  161. * @return The sample data.
  162. */
  163. private double[][] createSampleData1() {
  164. double[][] result = new double[11][2];
  165. result[0][0] = 2.00;
  166. result[0][1] = 1.60;
  167. result[1][0] = 2.25;
  168. result[1][1] = 2.00;
  169. result[2][0] = 2.60;
  170. result[2][1] = 1.80;
  171. result[3][0] = 2.65;
  172. result[3][1] = 2.80;
  173. result[4][0] = 2.80;
  174. result[4][1] = 2.10;
  175. result[5][0] = 3.10;
  176. result[5][1] = 2.00;
  177. result[6][0] = 2.90;
  178. result[6][1] = 2.65;
  179. result[7][0] = 3.25;
  180. result[7][1] = 2.25;
  181. result[8][0] = 3.30;
  182. result[8][1] = 2.60;
  183. result[9][0] = 3.60;
  184. result[9][1] = 3.00;
  185. result[10][0] = 3.25;
  186. result[10][1] = 3.10;
  187. return result;
  188. }
  189. /**
  190. * Creates a sample data set.
  191. *
  192. * @return The sample data.
  193. */
  194. private double[][] createSampleData2() {
  195. double[][] result = new double[10][2];
  196. result[0][0] = 2;
  197. result[0][1] = 56.27;
  198. result[1][0] = 3;
  199. result[1][1] = 41.32;
  200. result[2][0] = 4;
  201. result[2][1] = 31.45;
  202. result[3][0] = 5;
  203. result[3][1] = 30.05;
  204. result[4][0] = 6;
  205. result[4][1] = 24.69;
  206. result[5][0] = 7;
  207. result[5][1] = 19.78;
  208. result[6][0] = 8;
  209. result[6][1] = 20.94;
  210. result[7][0] = 9;
  211. result[7][1] = 16.73;
  212. result[8][0] = 10;
  213. result[8][1] = 14.21;
  214. result[9][0] = 11;
  215. result[9][1] = 12.44;
  216. return result;
  217. }
  218. }