1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.math.stat.regression;
18  
19  import java.util.Random;
20  
21  import junit.framework.Test;
22  import junit.framework.TestCase;
23  import junit.framework.TestSuite;
24  /**
25   * Test cases for the TestStatistic class.
26   *
27   * @version $Revision: 566833 $ $Date: 2007-08-16 13:36:33 -0700 (Thu, 16 Aug 2007) $
28   */
29  
30  public final class SimpleRegressionTest extends TestCase {
31  
32      /* 
33       * NIST "Norris" refernce data set from 
34       * http://www.itl.nist.gov/div898/strd/lls/data/LINKS/DATA/Norris.dat
35       * Strangely, order is {y,x}
36       */
37      private double[][] data = { { 0.1, 0.2 }, {338.8, 337.4 }, {118.1, 118.2 }, 
38              {888.0, 884.6 }, {9.2, 10.1 }, {228.1, 226.5 }, {668.5, 666.3 }, {998.5, 996.3 }, 
39              {449.1, 448.6 }, {778.9, 777.0 }, {559.2, 558.2 }, {0.3, 0.4 }, {0.1, 0.6 }, {778.1, 775.5 }, 
40              {668.8, 666.9 }, {339.3, 338.0 }, {448.9, 447.5 }, {10.8, 11.6 }, {557.7, 556.0 }, 
41              {228.3, 228.1 }, {998.0, 995.8 }, {888.8, 887.6 }, {119.6, 120.2 }, {0.3, 0.3 }, 
42              {0.6, 0.3 }, {557.6, 556.8 }, {339.3, 339.1 }, {888.0, 887.2 }, {998.5, 999.0 }, 
43              {778.9, 779.0 }, {10.2, 11.1 }, {117.6, 118.3 }, {228.9, 229.2 }, {668.4, 669.1 }, 
44              {449.2, 448.9 }, {0.2, 0.5 }
45      };
46  
47      /* 
48       * Correlation example from 
49       * http://www.xycoon.com/correlation.htm
50       */
51      private double[][] corrData = { { 101.0, 99.2 }, {100.1, 99.0 }, {100.0, 100.0 }, 
52              {90.6, 111.6 }, {86.5, 122.2 }, {89.7, 117.6 }, {90.6, 121.1 }, {82.8, 136.0 }, 
53              {70.1, 154.2 }, {65.4, 153.6 }, {61.3, 158.5 }, {62.5, 140.6 }, {63.6, 136.2 }, 
54              {52.6, 168.0 }, {59.7, 154.3 }, {59.5, 149.0 }, {61.3, 165.5 }
55      };
56  
57      /*
58       * From Moore and Mcabe, "Introduction to the Practice of Statistics"
59       * Example 10.3 
60       */
61      private double[][] infData = { { 15.6, 5.2 }, {26.8, 6.1 }, {37.8, 8.7 }, {36.4, 8.5 },
62              {35.5, 8.8 }, {18.6, 4.9 }, {15.3, 4.5 }, {7.9, 2.5 }, {0.0, 1.1 }
63      };
64      
65      /*
66       * Data with bad linear fit
67       */
68      private double[][] infData2 = { { 1, 1 }, {2, 0 }, {3, 5 }, {4, 2 },
69              {5, -1 }, {6, 12 }
70      };
71  
72      public SimpleRegressionTest(String name) {
73          super(name);
74      }
75  
76      public void setUp() {
77      }
78  
79      public static Test suite() {
80          TestSuite suite = new TestSuite(SimpleRegressionTest.class);
81          suite.setName("BivariateRegression Tests");
82          return suite;
83      }
84  
85      public void testNorris() {
86          SimpleRegression regression = new SimpleRegression();
87          for (int i = 0; i < data.length; i++) {
88              regression.addData(data[i][1], data[i][0]);
89          }
90          // Tests against certified values from  
91          // http://www.itl.nist.gov/div898/strd/lls/data/LINKS/DATA/Norris.dat
92          assertEquals("slope", 1.00211681802045, regression.getSlope(), 10E-12);
93          assertEquals("slope std err", 0.429796848199937E-03,
94                  regression.getSlopeStdErr(),10E-12);
95          assertEquals("number of observations", 36, regression.getN());
96          assertEquals( "intercept", -0.262323073774029,
97              regression.getIntercept(),10E-12);
98          assertEquals("std err intercept", 0.232818234301152,
99              regression.getInterceptStdErr(),10E-12);
100         assertEquals("r-square", 0.999993745883712,
101             regression.getRSquare(), 10E-12);
102         assertEquals("SSR", 4255954.13232369,
103             regression.getRegressionSumSquares(), 10E-9);
104         assertEquals("MSE", 0.782864662630069,
105             regression.getMeanSquareError(), 10E-10);
106         assertEquals("SSE", 26.6173985294224,
107             regression.getSumSquaredErrors(),10E-9);
108         // ------------  End certified data tests
109           
110         assertEquals( "predict(0)",  -0.262323073774029,
111             regression.predict(0), 10E-12);
112         assertEquals("predict(1)", 1.00211681802045 - 0.262323073774029,
113             regression.predict(1), 10E-12);
114     }
115 
116     public void testCorr() {
117         SimpleRegression regression = new SimpleRegression();
118         regression.addData(corrData);
119         assertEquals("number of observations", 17, regression.getN());
120         assertEquals("r-square", .896123, regression.getRSquare(), 10E-6);
121         assertEquals("r", -0.94663767742, regression.getR(), 1E-10);
122     }
123 
124     public void testNaNs() {
125         SimpleRegression regression = new SimpleRegression();
126         assertTrue("intercept not NaN", Double.isNaN(regression.getIntercept()));
127         assertTrue("slope not NaN", Double.isNaN(regression.getSlope()));
128         assertTrue("slope std err not NaN", Double.isNaN(regression.getSlopeStdErr()));
129         assertTrue("intercept std err not NaN", Double.isNaN(regression.getInterceptStdErr()));
130         assertTrue("MSE not NaN", Double.isNaN(regression.getMeanSquareError()));
131         assertTrue("e not NaN", Double.isNaN(regression.getR()));
132         assertTrue("r-square not NaN", Double.isNaN(regression.getRSquare()));
133         assertTrue( "RSS not NaN", Double.isNaN(regression.getRegressionSumSquares()));
134         assertTrue("SSE not NaN",Double.isNaN(regression.getSumSquaredErrors()));
135         assertTrue("SSTO not NaN", Double.isNaN(regression.getTotalSumSquares()));
136         assertTrue("predict not NaN", Double.isNaN(regression.predict(0)));
137 
138         regression.addData(1, 2);
139         regression.addData(1, 3);
140 
141         // No x variation, so these should still blow...
142         assertTrue("intercept not NaN", Double.isNaN(regression.getIntercept()));
143         assertTrue("slope not NaN", Double.isNaN(regression.getSlope()));
144         assertTrue("slope std err not NaN", Double.isNaN(regression.getSlopeStdErr()));
145         assertTrue("intercept std err not NaN", Double.isNaN(regression.getInterceptStdErr()));
146         assertTrue("MSE not NaN", Double.isNaN(regression.getMeanSquareError()));
147         assertTrue("e not NaN", Double.isNaN(regression.getR()));
148         assertTrue("r-square not NaN", Double.isNaN(regression.getRSquare()));
149         assertTrue("RSS not NaN", Double.isNaN(regression.getRegressionSumSquares()));
150         assertTrue("SSE not NaN", Double.isNaN(regression.getSumSquaredErrors()));
151         assertTrue("predict not NaN", Double.isNaN(regression.predict(0)));
152 
153         // but SSTO should be OK
154         assertTrue("SSTO NaN", !Double.isNaN(regression.getTotalSumSquares()));
155 
156         regression = new SimpleRegression();
157 
158         regression.addData(1, 2);
159         regression.addData(3, 3);
160 
161         // All should be OK except MSE, s(b0), s(b1) which need one more df 
162         assertTrue("interceptNaN", !Double.isNaN(regression.getIntercept()));
163         assertTrue("slope NaN", !Double.isNaN(regression.getSlope()));
164         assertTrue ("slope std err not NaN", Double.isNaN(regression.getSlopeStdErr()));
165         assertTrue("intercept std err not NaN", Double.isNaN(regression.getInterceptStdErr()));
166         assertTrue("MSE not NaN", Double.isNaN(regression.getMeanSquareError()));
167         assertTrue("r NaN", !Double.isNaN(regression.getR()));
168         assertTrue("r-square NaN", !Double.isNaN(regression.getRSquare()));
169         assertTrue("RSS NaN", !Double.isNaN(regression.getRegressionSumSquares()));
170         assertTrue("SSE NaN", !Double.isNaN(regression.getSumSquaredErrors()));
171         assertTrue("SSTO NaN", !Double.isNaN(regression.getTotalSumSquares()));
172         assertTrue("predict NaN", !Double.isNaN(regression.predict(0)));
173 
174         regression.addData(1, 4);
175 
176         // MSE, MSE, s(b0), s(b1) should all be OK now
177         assertTrue("MSE NaN", !Double.isNaN(regression.getMeanSquareError()));
178         assertTrue("slope std err NaN", !Double.isNaN(regression.getSlopeStdErr()));
179         assertTrue("intercept std err NaN", !Double.isNaN(regression.getInterceptStdErr()));
180     }
181 
182     public void testClear() {
183         SimpleRegression regression = new SimpleRegression();
184         regression.addData(corrData);
185         assertEquals("number of observations", 17, regression.getN());
186         regression.clear();
187         assertEquals("number of observations", 0, regression.getN());
188         regression.addData(corrData);
189         assertEquals("r-square", .896123, regression.getRSquare(), 10E-6);
190         regression.addData(data);
191         assertEquals("number of observations", 53, regression.getN());
192     }
193 
194     public void testInference() throws Exception {
195         //----------  verified against R, version 1.8.1 -----
196         // infData
197         SimpleRegression regression = new SimpleRegression();
198         regression.addData(infData);
199         assertEquals("slope std err", 0.011448491,
200                 regression.getSlopeStdErr(), 1E-10);
201         assertEquals("std err intercept", 0.286036932,
202                 regression.getInterceptStdErr(),1E-8);
203         assertEquals("significance", 4.596e-07,
204                 regression.getSignificance(),1E-8);    
205         assertEquals("slope conf interval half-width", 0.0270713794287, 
206                 regression.getSlopeConfidenceInterval(),1E-8);
207         // infData2
208         regression = new SimpleRegression();
209         regression.addData(infData2);
210         assertEquals("slope std err", 1.07260253,
211                 regression.getSlopeStdErr(), 1E-8);
212         assertEquals("std err intercept",4.17718672,
213                 regression.getInterceptStdErr(),1E-8);
214         assertEquals("significance", 0.261829133982,
215                 regression.getSignificance(),1E-11);    
216         assertEquals("slope conf interval half-width", 2.97802204827, 
217                 regression.getSlopeConfidenceInterval(),1E-8);
218         //------------- End R-verified tests -------------------------------
219         
220         //FIXME: get a real example to test against with alpha = .01
221         assertTrue("tighter means wider",
222                 regression.getSlopeConfidenceInterval() < regression.getSlopeConfidenceInterval(0.01));
223      
224         try {
225             regression.getSlopeConfidenceInterval(1);
226             fail("expecting IllegalArgumentException for alpha = 1");
227         } catch (IllegalArgumentException ex) {
228             ;
229         }  
230 
231     }
232 
233     public void testPerfect() throws Exception {
234         SimpleRegression regression = new SimpleRegression();
235         int n = 100;
236         for (int i = 0; i < n; i++) {
237             regression.addData(((double) i) / (n - 1), i);
238         }
239         assertEquals(0.0, regression.getSignificance(), 1.0e-5);
240         assertTrue(regression.getSlope() > 0.0);
241         assertTrue(regression.getSumSquaredErrors() >= 0.0);
242     }
243 
244     public void testPerfectNegative() throws Exception {
245         SimpleRegression regression = new SimpleRegression();
246         int n = 100;
247         for (int i = 0; i < n; i++) {
248             regression.addData(- ((double) i) / (n - 1), i);
249         }
250    
251         assertEquals(0.0, regression.getSignificance(), 1.0e-5);
252         assertTrue(regression.getSlope() < 0.0);   
253     }
254 
255     public void testRandom() throws Exception {
256         SimpleRegression regression = new SimpleRegression();
257         Random random = new Random(1);
258         int n = 100;
259         for (int i = 0; i < n; i++) {
260             regression.addData(((double) i) / (n - 1), random.nextDouble());
261         }
262 
263         assertTrue( 0.0 < regression.getSignificance()
264                     && regression.getSignificance() < 1.0);       
265     }
266     
267     
268     // Jira MATH-85 = Bugzilla 39432
269     public void testSSENonNegative() {
270         double[] y = { 8915.102, 8919.302, 8923.502 };
271         double[] x = { 1.107178495E2, 1.107264895E2, 1.107351295E2 };
272         SimpleRegression reg = new SimpleRegression();
273         for (int i = 0; i < x.length; i++) {
274             reg.addData(x[i], y[i]);
275         }
276         assertTrue(reg.getSumSquaredErrors() >= 0.0);
277     } 
278 }