1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.stat.regression;
23
24 import org.hipparchus.exception.MathIllegalArgumentException;
25 import org.hipparchus.exception.NullArgumentException;
26 import org.hipparchus.linear.RealMatrix;
27 import org.hipparchus.linear.RealVector;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31
32
33 public abstract class MultipleLinearRegressionAbstractTest {
34
35 protected AbstractMultipleLinearRegression regression;
36
37 @Before
38 public void setUp(){
39 regression = createRegression();
40 }
41
42 protected abstract AbstractMultipleLinearRegression createRegression();
43
44 protected abstract int getNumberOfRegressors();
45
46 protected abstract int getSampleSize();
47
48 @Test
49 public void canEstimateRegressionParameters(){
50 double[] beta = regression.estimateRegressionParameters();
51 Assert.assertEquals(getNumberOfRegressors(), beta.length);
52 }
53
54 @Test
55 public void canEstimateResiduals(){
56 double[] e = regression.estimateResiduals();
57 Assert.assertEquals(getSampleSize(), e.length);
58 }
59
60 @Test
61 public void canEstimateRegressionParametersVariance(){
62 double[][] variance = regression.estimateRegressionParametersVariance();
63 Assert.assertEquals(getNumberOfRegressors(), variance.length);
64 }
65
66 @Test
67 public void canEstimateRegressandVariance(){
68 if (getSampleSize() > getNumberOfRegressors()) {
69 double variance = regression.estimateRegressandVariance();
70 Assert.assertTrue(variance > 0.0);
71 }
72 }
73
74
75
76
77
78 @Test
79 public void testNewSample() {
80 double[] design = new double[] {
81 1, 19, 22, 33,
82 2, 20, 30, 40,
83 3, 25, 35, 45,
84 4, 27, 37, 47
85 };
86 double[] y = new double[] {1, 2, 3, 4};
87 double[][] x = new double[][] {
88 {19, 22, 33},
89 {20, 30, 40},
90 {25, 35, 45},
91 {27, 37, 47}
92 };
93 AbstractMultipleLinearRegression regression = createRegression();
94 regression.newSampleData(design, 4, 3);
95 RealMatrix flatX = regression.getX().copy();
96 RealVector flatY = regression.getY().copy();
97 regression.newXSampleData(x);
98 regression.newYSampleData(y);
99 Assert.assertEquals(flatX, regression.getX());
100 Assert.assertEquals(flatY, regression.getY());
101
102
103 regression.setNoIntercept(true);
104 regression.newSampleData(design, 4, 3);
105 flatX = regression.getX().copy();
106 flatY = regression.getY().copy();
107 regression.newXSampleData(x);
108 regression.newYSampleData(y);
109 Assert.assertEquals(flatX, regression.getX());
110 Assert.assertEquals(flatY, regression.getY());
111 }
112
113 @Test(expected=NullArgumentException.class)
114 public void testNewSampleNullData() {
115 double[] data = null;
116 createRegression().newSampleData(data, 2, 3);
117 }
118
119 @Test(expected=MathIllegalArgumentException.class)
120 public void testNewSampleInvalidData() {
121 double[] data = new double[] {1, 2, 3, 4};
122 createRegression().newSampleData(data, 2, 3);
123 }
124
125 @Test(expected=MathIllegalArgumentException.class)
126 public void testNewSampleInsufficientData() {
127 double[] data = new double[] {1, 2, 3, 4};
128 createRegression().newSampleData(data, 1, 3);
129 }
130
131 @Test(expected=NullArgumentException.class)
132 public void testXSampleDataNull() {
133 createRegression().newXSampleData(null);
134 }
135
136 @Test(expected=NullArgumentException.class)
137 public void testYSampleDataNull() {
138 createRegression().newYSampleData(null);
139 }
140
141 }