View Javadoc
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.math4.legacy.stat.regression;
18  
19  import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
20  import org.apache.commons.math4.legacy.exception.NullArgumentException;
21  import org.apache.commons.math4.legacy.linear.RealMatrix;
22  import org.apache.commons.math4.legacy.linear.RealVector;
23  import org.junit.Assert;
24  import org.junit.Before;
25  import org.junit.Test;
26  
27  
28  public abstract class MultipleLinearRegressionAbstractTest {
29  
30      protected AbstractMultipleLinearRegression regression;
31  
32      @Before
33      public void setUp(){
34          regression = createRegression();
35      }
36  
37      protected abstract AbstractMultipleLinearRegression createRegression();
38  
39      protected abstract int getNumberOfRegressors();
40  
41      protected abstract int getSampleSize();
42  
43      @Test
44      public void canEstimateRegressionParameters(){
45          double[] beta = regression.estimateRegressionParameters();
46          Assert.assertEquals(getNumberOfRegressors(), beta.length);
47      }
48  
49      @Test
50      public void canEstimateResiduals(){
51          double[] e = regression.estimateResiduals();
52          Assert.assertEquals(getSampleSize(), e.length);
53      }
54  
55      @Test
56      public void canEstimateRegressionParametersVariance(){
57          double[][] variance = regression.estimateRegressionParametersVariance();
58          Assert.assertEquals(getNumberOfRegressors(), variance.length);
59      }
60  
61      @Test
62      public void canEstimateRegressandVariance(){
63          if (getSampleSize() > getNumberOfRegressors()) {
64              double variance = regression.estimateRegressandVariance();
65              Assert.assertTrue(variance > 0.0);
66          }
67      }
68  
69      /**
70       * Verifies that newSampleData methods consistently insert unitary columns
71       * in design matrix.  Confirms the fix for MATH-411.
72       */
73      @Test
74      public void testNewSample() {
75          double[] design = new double[] {
76            1, 19, 22, 33,
77            2, 20, 30, 40,
78            3, 25, 35, 45,
79            4, 27, 37, 47
80          };
81          double[] y = new double[] {1, 2, 3, 4};
82          double[][] x = new double[][] {
83            {19, 22, 33},
84            {20, 30, 40},
85            {25, 35, 45},
86            {27, 37, 47}
87          };
88          AbstractMultipleLinearRegression regression = createRegression();
89          regression.newSampleData(design, 4, 3);
90          RealMatrix flatX = regression.getX().copy();
91          RealVector flatY = regression.getY().copy();
92          regression.newXSampleData(x);
93          regression.newYSampleData(y);
94          Assert.assertEquals(flatX, regression.getX());
95          Assert.assertEquals(flatY, regression.getY());
96  
97          // No intercept
98          regression.setNoIntercept(true);
99          regression.newSampleData(design, 4, 3);
100         flatX = regression.getX().copy();
101         flatY = regression.getY().copy();
102         regression.newXSampleData(x);
103         regression.newYSampleData(y);
104         Assert.assertEquals(flatX, regression.getX());
105         Assert.assertEquals(flatY, regression.getY());
106     }
107 
108     @Test(expected=NullArgumentException.class)
109     public void testNewSampleNullData() {
110         double[] data = null;
111         createRegression().newSampleData(data, 2, 3);
112     }
113 
114     @Test(expected=MathIllegalArgumentException.class)
115     public void testNewSampleInvalidData() {
116         double[] data = new double[] {1, 2, 3, 4};
117         createRegression().newSampleData(data, 2, 3);
118     }
119 
120     @Test(expected=MathIllegalArgumentException.class)
121     public void testNewSampleInsufficientData() {
122         double[] data = new double[] {1, 2, 3, 4};
123         createRegression().newSampleData(data, 1, 3);
124     }
125 
126     @Test(expected=NullArgumentException.class)
127     public void testXSampleDataNull() {
128         createRegression().newXSampleData(null);
129     }
130 
131     @Test(expected=NullArgumentException.class)
132     public void testYSampleDataNull() {
133         createRegression().newYSampleData(null);
134     }
135 }