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.UnitTestUtils;
25 import org.hipparchus.exception.MathIllegalArgumentException;
26 import org.hipparchus.linear.RealMatrix;
27 import org.hipparchus.stat.correlation.PearsonsCorrelation;
28 import org.hipparchus.util.FastMath;
29 import org.junit.jupiter.api.Test;
30
31 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
32 import static org.junit.jupiter.api.Assertions.assertNotNull;
33 import static org.junit.jupiter.api.Assertions.fail;
34
35
36
37
38 public class MillerUpdatingRegressionTest {
39
40 public MillerUpdatingRegressionTest() {
41 }
42
43
44
45 private final static double[][] airdata = {
46 new double[]{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6},
47 new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
48 new double[]{1140640, 1215690, 1309570, 1511530, 1676730, 1823740, 2022890, 2314760, 2639160, 3247620, 3787750, 3867750, 3996020, 4282880, 4748320, 569292, 640614, 777655, 999294, 1203970, 1358100, 1501350, 1709270, 2025400, 2548370, 3137740, 3557700, 3717740, 3962370, 4209390, 286298, 309290, 342056, 374595, 450037, 510412, 575347, 669331, 783799, 913883, 1041520, 1125800, 1096070, 1198930, 1170470, 145167, 170192, 247506, 309391, 354338, 373941, 420915, 474017, 532590, 676771, 880438, 1052020, 1193680, 1303390, 1436970, 91361, 95428, 98187, 115967, 138382, 156228, 183169, 210212, 274024, 356915, 432344, 524294, 530924, 581447, 610257, 68978, 74904, 83829, 98148, 118449, 133161, 145062, 170711, 199775, 276797, 381478, 506969, 633388, 804388, 1009500},
49 new double[]{0.952757, 0.986757, 1.09198, 1.17578, 1.16017, 1.17376, 1.29051, 1.39067, 1.61273, 1.82544, 1.54604, 1.5279, 1.6602, 1.82231, 1.93646, 0.520635, 0.534627, 0.655192, 0.791575, 0.842945, 0.852892, 0.922843, 1, 1.19845, 1.34067, 1.32624, 1.24852, 1.25432, 1.37177, 1.38974, 0.262424, 0.266433, 0.306043, 0.325586, 0.345706, 0.367517, 0.409937, 0.448023, 0.539595, 0.539382, 0.467967, 0.450544, 0.468793, 0.494397, 0.493317, 0.086393, 0.09674, 0.1415, 0.169715, 0.173805, 0.164272, 0.170906, 0.17784, 0.192248, 0.242469, 0.256505, 0.249657, 0.273923, 0.371131, 0.421411, 0.051028, 0.052646, 0.056348, 0.066953, 0.070308, 0.073961, 0.084946, 0.095474, 0.119814, 0.150046, 0.144014, 0.1693, 0.172761, 0.18667, 0.213279, 0.037682, 0.039784, 0.044331, 0.050245, 0.055046, 0.052462, 0.056977, 0.06149, 0.069027, 0.092749, 0.11264, 0.154154, 0.186461, 0.246847, 0.304013},
50 new double[]{106650, 110307, 110574, 121974, 196606, 265609, 263451, 316411, 384110, 569251, 871636, 997239, 938002, 859572, 823411, 103795, 111477, 118664, 114797, 215322, 281704, 304818, 348609, 374579, 544109, 853356, 1003200, 941977, 856533, 821361, 118788, 123798, 122882, 131274, 222037, 278721, 306564, 356073, 378311, 555267, 850322, 1015610, 954508, 886999, 844079, 114987, 120501, 121908, 127220, 209405, 263148, 316724, 363598, 389436, 547376, 850418, 1011170, 951934, 881323, 831374, 118222, 116223, 115853, 129372, 243266, 277930, 317273, 358794, 397667, 566672, 848393, 1005740, 958231, 872924, 844622, 117112, 119420, 116087, 122997, 194309, 307923, 323595, 363081, 386422, 564867, 874818, 1013170, 930477, 851676, 819476},
51 new double[]{0.534487, 0.532328, 0.547736, 0.540846, 0.591167, 0.575417, 0.594495, 0.597409, 0.638522, 0.676287, 0.605735, 0.61436, 0.633366, 0.650117, 0.625603, 0.490851, 0.473449, 0.503013, 0.512501, 0.566782, 0.558133, 0.558799, 0.57207, 0.624763, 0.628706, 0.58915, 0.532612, 0.526652, 0.540163, 0.528775, 0.524334, 0.537185, 0.582119, 0.579489, 0.606592, 0.60727, 0.582425, 0.573972, 0.654256, 0.631055, 0.56924, 0.589682, 0.587953, 0.565388, 0.577078, 0.432066, 0.439669, 0.488932, 0.484181, 0.529925, 0.532723, 0.549067, 0.55714, 0.611377, 0.645319, 0.611734, 0.580884, 0.572047, 0.59457, 0.585525, 0.442875, 0.462473, 0.519118, 0.529331, 0.557797, 0.556181, 0.569327, 0.583465, 0.631818, 0.604723, 0.587921, 0.616159, 0.605868, 0.594688, 0.635545, 0.448539, 0.475889, 0.500562, 0.500344, 0.528897, 0.495361, 0.510342, 0.518296, 0.546723, 0.554276, 0.517766, 0.580049, 0.556024, 0.537791, 0.525775}
52 };
53
54
55
56
57 @Test
58 void testHasIntercept() {
59 MillerUpdatingRegression instance = new MillerUpdatingRegression(10, false);
60 if (instance.hasIntercept()) {
61 fail("Should not have intercept");
62 }
63 instance = new MillerUpdatingRegression(10, true);
64 if (!instance.hasIntercept()) {
65 fail("Should have intercept");
66 }
67 }
68
69
70
71
72 @Test
73 void testAddObsGetNClear() {
74 MillerUpdatingRegression instance = new MillerUpdatingRegression(3, true);
75 double[][] xAll = new double[airdata[0].length][];
76 double[] y = new double[airdata[0].length];
77 for (int i = 0; i < airdata[0].length; i++) {
78 xAll[i] = new double[3];
79 xAll[i][0] = FastMath.log(airdata[3][i]);
80 xAll[i][1] = FastMath.log(airdata[4][i]);
81 xAll[i][2] = airdata[5][i];
82 y[i] = FastMath.log(airdata[2][i]);
83 }
84 instance.addObservations(xAll, y);
85 if (instance.getN() != xAll.length) {
86 fail("Number of observations not correct in bulk addition");
87 }
88 instance.clear();
89 for (int i = 0; i < xAll.length; i++) {
90 instance.addObservation(xAll[i], y[i]);
91 }
92 if (instance.getN() != xAll.length) {
93 fail("Number of observations not correct in drip addition");
94 }
95 return;
96 }
97
98 @Test
99 void testNegativeTestAddObs() {
100 MillerUpdatingRegression instance = new MillerUpdatingRegression(3, true);
101 try {
102 instance.addObservation(new double[]{1.0}, 0.0);
103 fail("Should throw MathIllegalArgumentException");
104 } catch (MathIllegalArgumentException iae) {
105 } catch (Exception e) {
106 fail("Should throw MathIllegalArgumentException");
107 }
108 try {
109 instance.addObservation(new double[]{1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}, 0.0);
110 fail("Should throw MathIllegalArgumentException");
111 } catch (MathIllegalArgumentException iae) {
112 } catch (Exception e) {
113 fail("Should throw MathIllegalArgumentException");
114 }
115 MillerUpdatingRegression finalInstance = instance;
116 assertDoesNotThrow(() -> {
117 finalInstance.addObservation(new double[]{ 1.0, 1.0, 1.0}, 0.0);
118 }, "Should throw MathIllegalArgumentException");
119
120
121 instance = new MillerUpdatingRegression(3, false);
122 try {
123 instance.addObservation(new double[]{1.0}, 0.0);
124 fail("Should throw MathIllegalArgumentException [NOINTERCEPT]");
125 } catch (MathIllegalArgumentException iae) {
126 } catch (Exception e) {
127 fail("Should throw MathIllegalArgumentException [NOINTERCEPT]");
128 }
129 try {
130 instance.addObservation(new double[]{1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}, 0.0);
131 fail("Should throw MathIllegalArgumentException [NOINTERCEPT]");
132 } catch (MathIllegalArgumentException iae) {
133 } catch (Exception e) {
134 fail("Should throw MathIllegalArgumentException [NOINTERCEPT]");
135 }
136 MillerUpdatingRegression finalInstance1 = instance;
137 assertDoesNotThrow(() -> {
138 finalInstance1.addObservation(new double[]{ 1.0, 1.0, 1.0}, 0.0);
139 }, "Should throw MathIllegalArgumentException [NOINTERCEPT]");
140 }
141
142 @Test
143 void testNegativeTestAddMultipleObs() {
144 MillerUpdatingRegression instance = new MillerUpdatingRegression(3, true);
145 try {
146 double[][] tst = {{1.0, 1.0, 1.0}, {1.20, 2.0, 2.1}};
147 double[] y = {1.0};
148 instance.addObservations(tst, y);
149
150 fail("Should throw MathIllegalArgumentException");
151 } catch (MathIllegalArgumentException iae) {
152 } catch (Exception e) {
153 fail("Should throw MathIllegalArgumentException");
154 }
155
156 try {
157 double[][] tst = {{1.0, 1.0, 1.0}, {1.20, 2.0, 2.1}};
158 double[] y = {1.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
159 instance.addObservations(tst, y);
160
161 fail("Should throw MathIllegalArgumentException");
162 } catch (MathIllegalArgumentException iae) {
163 } catch (Exception e) {
164 fail("Should throw MathIllegalArgumentException");
165 }
166 }
167
168
169
170
171 @Test
172 void testRegressAirlineConstantExternal() {
173 MillerUpdatingRegression instance = new MillerUpdatingRegression(4, false);
174 double[][] x = new double[airdata[0].length][];
175 double[] y = new double[airdata[0].length];
176 for (int i = 0; i < airdata[0].length; i++) {
177 x[i] = new double[4];
178 x[i][0] = 1.0;
179 x[i][1] = FastMath.log(airdata[3][i]);
180 x[i][2] = FastMath.log(airdata[4][i]);
181 x[i][3] = airdata[5][i];
182 y[i] = FastMath.log(airdata[2][i]);
183 }
184
185 instance.addObservations(x, y);
186 assertDoesNotThrow(() -> {
187 RegressionResults result = instance.regress();
188 assertNotNull(result, "The test case is a prototype.");
189 UnitTestUtils.customAssertEquals(
190 new double[]{9.5169, 0.8827, 0.4540, -1.6275},
191 result.getParameterEstimates(), 1e-4);
192
193
194 UnitTestUtils.customAssertEquals(
195 new double[]{.2292445, .0132545, .0203042, .345302},
196 result.getStdErrorOfEstimates(), 1.0e-4);
197
198 UnitTestUtils.customAssertEquals(0.01552839, result.getMeanSquareError(), 1.0e-8);
199 }, "Should not throw exception but does");
200 }
201
202 @Test
203 void testRegressAirlineConstantInternal() {
204 MillerUpdatingRegression instance = new MillerUpdatingRegression(3, true);
205 double[][] x = new double[airdata[0].length][];
206 double[] y = new double[airdata[0].length];
207 for (int i = 0; i < airdata[0].length; i++) {
208 x[i] = new double[3];
209 x[i][0] = FastMath.log(airdata[3][i]);
210 x[i][1] = FastMath.log(airdata[4][i]);
211 x[i][2] = airdata[5][i];
212 y[i] = FastMath.log(airdata[2][i]);
213 }
214
215 instance.addObservations(x, y);
216 assertDoesNotThrow(() -> {
217 RegressionResults result = instance.regress();
218 assertNotNull(result, "The test case is a prototype.");
219 UnitTestUtils.customAssertEquals(
220 new double[]{9.5169, 0.8827, 0.4540, -1.6275},
221 result.getParameterEstimates(), 1e-4);
222
223
224 UnitTestUtils.customAssertEquals(
225 new double[]{.2292445, .0132545, .0203042, .345302},
226 result.getStdErrorOfEstimates(), 1.0e-4);
227
228 UnitTestUtils.customAssertEquals(0.9883, result.getRSquared(), 1.0e-4);
229 UnitTestUtils.customAssertEquals(0.01552839, result.getMeanSquareError(), 1.0e-8);
230 }, "Should not throw exception but does");
231 }
232
233 @Test
234 void testFilippelli() {
235 double[] data = new double[]{
236 0.8116, -6.860120914,
237 0.9072, -4.324130045,
238 0.9052, -4.358625055,
239 0.9039, -4.358426747,
240 0.8053, -6.955852379,
241 0.8377, -6.661145254,
242 0.8667, -6.355462942,
243 0.8809, -6.118102026,
244 0.7975, -7.115148017,
245 0.8162, -6.815308569,
246 0.8515, -6.519993057,
247 0.8766, -6.204119983,
248 0.8885, -5.853871964,
249 0.8859, -6.109523091,
250 0.8959, -5.79832982,
251 0.8913, -5.482672118,
252 0.8959, -5.171791386,
253 0.8971, -4.851705903,
254 0.9021, -4.517126416,
255 0.909, -4.143573228,
256 0.9139, -3.709075441,
257 0.9199, -3.499489089,
258 0.8692, -6.300769497,
259 0.8872, -5.953504836,
260 0.89, -5.642065153,
261 0.891, -5.031376979,
262 0.8977, -4.680685696,
263 0.9035, -4.329846955,
264 0.9078, -3.928486195,
265 0.7675, -8.56735134,
266 0.7705, -8.363211311,
267 0.7713, -8.107682739,
268 0.7736, -7.823908741,
269 0.7775, -7.522878745,
270 0.7841, -7.218819279,
271 0.7971, -6.920818754,
272 0.8329, -6.628932138,
273 0.8641, -6.323946875,
274 0.8804, -5.991399828,
275 0.7668, -8.781464495,
276 0.7633, -8.663140179,
277 0.7678, -8.473531488,
278 0.7697, -8.247337057,
279 0.77, -7.971428747,
280 0.7749, -7.676129393,
281 0.7796, -7.352812702,
282 0.7897, -7.072065318,
283 0.8131, -6.774174009,
284 0.8498, -6.478861916,
285 0.8741, -6.159517513,
286 0.8061, -6.835647144,
287 0.846, -6.53165267,
288 0.8751, -6.224098421,
289 0.8856, -5.910094889,
290 0.8919, -5.598599459,
291 0.8934, -5.290645224,
292 0.894, -4.974284616,
293 0.8957, -4.64454848,
294 0.9047, -4.290560426,
295 0.9129, -3.885055584,
296 0.9209, -3.408378962,
297 0.9219, -3.13200249,
298 0.7739, -8.726767166,
299 0.7681, -8.66695597,
300 0.7665, -8.511026475,
301 0.7703, -8.165388579,
302 0.7702, -7.886056648,
303 0.7761, -7.588043762,
304 0.7809, -7.283412422,
305 0.7961, -6.995678626,
306 0.8253, -6.691862621,
307 0.8602, -6.392544977,
308 0.8809, -6.067374056,
309 0.8301, -6.684029655,
310 0.8664, -6.378719832,
311 0.8834, -6.065855188,
312 0.8898, -5.752272167,
313 0.8964, -5.132414673,
314 0.8963, -4.811352704,
315 0.9074, -4.098269308,
316 0.9119, -3.66174277,
317 0.9228, -3.2644011
318 };
319 MillerUpdatingRegression model = new MillerUpdatingRegression(10, true);
320 int off = 0;
321 double[] tmp = new double[10];
322 int nobs = 82;
323 for (int i = 0; i < nobs; i++) {
324 tmp[0] = data[off + 1];
325
326
327
328
329
330
331
332
333
334 tmp[1] = tmp[0] * tmp[0];
335 tmp[2] = tmp[0] * tmp[1];
336 tmp[3] = tmp[0] * tmp[2];
337 tmp[4] = tmp[0] * tmp[3];
338 tmp[5] = tmp[0] * tmp[4];
339 tmp[6] = tmp[0] * tmp[5];
340 tmp[7] = tmp[0] * tmp[6];
341 tmp[8] = tmp[0] * tmp[7];
342 tmp[9] = tmp[0] * tmp[8];
343 model.addObservation(tmp, data[off]);
344 off += 2;
345 }
346 RegressionResults result = model.regress();
347 double[] betaHat = result.getParameterEstimates();
348 UnitTestUtils.customAssertEquals(betaHat,
349 new double[]{
350 -1467.48961422980,
351 -2772.17959193342,
352 -2316.37108160893,
353 -1127.97394098372,
354 -354.478233703349,
355 -75.1242017393757,
356 -10.8753180355343,
357 -1.06221498588947,
358 -0.670191154593408E-01,
359 -0.246781078275479E-02,
360 -0.402962525080404E-04
361 }, 1E-5);
362
363 double[] se = result.getStdErrorOfEstimates();
364 UnitTestUtils.customAssertEquals(se,
365 new double[]{
366 298.084530995537,
367 559.779865474950,
368 466.477572127796,
369 227.204274477751,
370 71.6478660875927,
371 15.2897178747400,
372 2.23691159816033,
373 0.221624321934227,
374 0.142363763154724E-01,
375 0.535617408889821E-03,
376 0.896632837373868E-05
377 }, 1E-5);
378
379 UnitTestUtils.customAssertEquals(0.996727416185620, result.getRSquared(), 1.0e-8);
380 UnitTestUtils.customAssertEquals(0.112091743968020E-04, result.getMeanSquareError(), 1.0e-10);
381 UnitTestUtils.customAssertEquals(0.795851382172941E-03, result.getErrorSumSquares(), 1.0e-10);
382
383 }
384
385 @Test
386 void testWampler1() {
387 double[] data = new double[]{
388 1, 0,
389 6, 1,
390 63, 2,
391 364, 3,
392 1365, 4,
393 3906, 5,
394 9331, 6,
395 19608, 7,
396 37449, 8,
397 66430, 9,
398 111111, 10,
399 177156, 11,
400 271453, 12,
401 402234, 13,
402 579195, 14,
403 813616, 15,
404 1118481, 16,
405 1508598, 17,
406 2000719, 18,
407 2613660, 19,
408 3368421, 20};
409
410 MillerUpdatingRegression model = new MillerUpdatingRegression(5, true);
411 int off = 0;
412 double[] tmp = new double[5];
413 int nobs = 21;
414 for (int i = 0; i < nobs; i++) {
415 tmp[0] = data[off + 1];
416 tmp[1] = tmp[0] * tmp[0];
417 tmp[2] = tmp[0] * tmp[1];
418 tmp[3] = tmp[0] * tmp[2];
419 tmp[4] = tmp[0] * tmp[3];
420 model.addObservation(tmp, data[off]);
421 off += 2;
422 }
423 RegressionResults result = model.regress();
424 double[] betaHat = result.getParameterEstimates();
425 UnitTestUtils.customAssertEquals(betaHat,
426 new double[]{1.0,
427 1.0, 1.0,
428 1.0, 1.0,
429 1.0}, 1E-8);
430
431 double[] se = result.getStdErrorOfEstimates();
432 UnitTestUtils.customAssertEquals(se,
433 new double[]{0.0,
434 0.0, 0.0,
435 0.0, 0.0,
436 0.0}, 1E-8);
437
438 UnitTestUtils.customAssertEquals(1.0, result.getRSquared(), 1.0e-10);
439 UnitTestUtils.customAssertEquals(0, result.getMeanSquareError(), 1.0e-7);
440 UnitTestUtils.customAssertEquals(0.00, result.getErrorSumSquares(), 1.0e-6);
441
442 return;
443 }
444
445 @Test
446 void testWampler2() {
447 double[] data = new double[]{
448 1.00000, 0,
449 1.11111, 1,
450 1.24992, 2,
451 1.42753, 3,
452 1.65984, 4,
453 1.96875, 5,
454 2.38336, 6,
455 2.94117, 7,
456 3.68928, 8,
457 4.68559, 9,
458 6.00000, 10,
459 7.71561, 11,
460 9.92992, 12,
461 12.75603, 13,
462 16.32384, 14,
463 20.78125, 15,
464 26.29536, 16,
465 33.05367, 17,
466 41.26528, 18,
467 51.16209, 19,
468 63.00000, 20};
469
470 MillerUpdatingRegression model = new MillerUpdatingRegression(5, true);
471 int off = 0;
472 double[] tmp = new double[5];
473 int nobs = 21;
474 for (int i = 0; i < nobs; i++) {
475 tmp[0] = data[off + 1];
476 tmp[1] = tmp[0] * tmp[0];
477 tmp[2] = tmp[0] * tmp[1];
478 tmp[3] = tmp[0] * tmp[2];
479 tmp[4] = tmp[0] * tmp[3];
480 model.addObservation(tmp, data[off]);
481 off += 2;
482 }
483 RegressionResults result = model.regress();
484 double[] betaHat = result.getParameterEstimates();
485 UnitTestUtils.customAssertEquals(betaHat,
486 new double[]{1.0,
487 1.0e-1, 1.0e-2,
488 1.0e-3, 1.0e-4,
489 1.0e-5}, 1E-8);
490
491 double[] se = result.getStdErrorOfEstimates();
492 UnitTestUtils.customAssertEquals(se,
493 new double[]{0.0,
494 0.0, 0.0,
495 0.0, 0.0,
496 0.0}, 1E-8);
497
498 UnitTestUtils.customAssertEquals(1.0, result.getRSquared(), 1.0e-10);
499 UnitTestUtils.customAssertEquals(0, result.getMeanSquareError(), 1.0e-7);
500 UnitTestUtils.customAssertEquals(0.00, result.getErrorSumSquares(), 1.0e-6);
501 return;
502 }
503
504 @Test
505 void testWampler3() {
506 double[] data = new double[]{
507 760, 0,
508 -2042, 1,
509 2111, 2,
510 -1684, 3,
511 3888, 4,
512 1858, 5,
513 11379, 6,
514 17560, 7,
515 39287, 8,
516 64382, 9,
517 113159, 10,
518 175108, 11,
519 273291, 12,
520 400186, 13,
521 581243, 14,
522 811568, 15,
523 1121004, 16,
524 1506550, 17,
525 2002767, 18,
526 2611612, 19,
527 3369180, 20};
528 MillerUpdatingRegression model = new MillerUpdatingRegression(5, true);
529 int off = 0;
530 double[] tmp = new double[5];
531 int nobs = 21;
532 for (int i = 0; i < nobs; i++) {
533 tmp[0] = data[off + 1];
534 tmp[1] = tmp[0] * tmp[0];
535 tmp[2] = tmp[0] * tmp[1];
536 tmp[3] = tmp[0] * tmp[2];
537 tmp[4] = tmp[0] * tmp[3];
538 model.addObservation(tmp, data[off]);
539 off += 2;
540 }
541 RegressionResults result = model.regress();
542 double[] betaHat = result.getParameterEstimates();
543 UnitTestUtils.customAssertEquals(betaHat,
544 new double[]{1.0,
545 1.0, 1.0,
546 1.0, 1.0,
547 1.0}, 1E-8);
548 double[] se = result.getStdErrorOfEstimates();
549 UnitTestUtils.customAssertEquals(se,
550 new double[]{2152.32624678170,
551 2363.55173469681, 779.343524331583,
552 101.475507550350, 5.64566512170752,
553 0.112324854679312}, 1E-8);
554
555 UnitTestUtils.customAssertEquals(.999995559025820, result.getRSquared(), 1.0e-10);
556 UnitTestUtils.customAssertEquals(5570284.53333333, result.getMeanSquareError(), 1.0e-7);
557 UnitTestUtils.customAssertEquals(83554268.0000000, result.getErrorSumSquares(), 1.0e-6);
558 return;
559 }
560
561
562 public void testWampler4() {
563 double[] data = new double[]{
564 75901, 0,
565 -204794, 1,
566 204863, 2,
567 -204436, 3,
568 253665, 4,
569 -200894, 5,
570 214131, 6,
571 -185192, 7,
572 221249, 8,
573 -138370, 9,
574 315911, 10,
575 -27644, 11,
576 455253, 12,
577 197434, 13,
578 783995, 14,
579 608816, 15,
580 1370781, 16,
581 1303798, 17,
582 2205519, 18,
583 2408860, 19,
584 3444321, 20};
585 MillerUpdatingRegression model = new MillerUpdatingRegression(5, true);
586 int off = 0;
587 double[] tmp = new double[5];
588 int nobs = 21;
589 for (int i = 0; i < nobs; i++) {
590 tmp[0] = data[off + 1];
591 tmp[1] = tmp[0] * tmp[0];
592 tmp[2] = tmp[0] * tmp[1];
593 tmp[3] = tmp[0] * tmp[2];
594 tmp[4] = tmp[0] * tmp[3];
595 model.addObservation(tmp, data[off]);
596 off += 2;
597 }
598 RegressionResults result = model.regress();
599 double[] betaHat = result.getParameterEstimates();
600 UnitTestUtils.customAssertEquals(betaHat,
601 new double[]{1.0,
602 1.0, 1.0,
603 1.0, 1.0,
604 1.0}, 1E-8);
605
606 double[] se = result.getStdErrorOfEstimates();
607 UnitTestUtils.customAssertEquals(se,
608 new double[]{215232.624678170,
609 236355.173469681, 77934.3524331583,
610 10147.5507550350, 564.566512170752,
611 11.2324854679312}, 1E-8);
612
613 UnitTestUtils.customAssertEquals(.957478440825662, result.getRSquared(), 1.0e-10);
614 UnitTestUtils.customAssertEquals(55702845333.3333, result.getMeanSquareError(), 1.0e-4);
615 UnitTestUtils.customAssertEquals(835542680000.000, result.getErrorSumSquares(), 1.0e-3);
616
617 return;
618 }
619
620
621
622
623
624
625
626
627
628
629
630 @Test
631 void testLongly() {
632
633
634 double[] design = new double[]{
635 60323, 83.0, 234289, 2356, 1590, 107608, 1947,
636 61122, 88.5, 259426, 2325, 1456, 108632, 1948,
637 60171, 88.2, 258054, 3682, 1616, 109773, 1949,
638 61187, 89.5, 284599, 3351, 1650, 110929, 1950,
639 63221, 96.2, 328975, 2099, 3099, 112075, 1951,
640 63639, 98.1, 346999, 1932, 3594, 113270, 1952,
641 64989, 99.0, 365385, 1870, 3547, 115094, 1953,
642 63761, 100.0, 363112, 3578, 3350, 116219, 1954,
643 66019, 101.2, 397469, 2904, 3048, 117388, 1955,
644 67857, 104.6, 419180, 2822, 2857, 118734, 1956,
645 68169, 108.4, 442769, 2936, 2798, 120445, 1957,
646 66513, 110.8, 444546, 4681, 2637, 121950, 1958,
647 68655, 112.6, 482704, 3813, 2552, 123366, 1959,
648 69564, 114.2, 502601, 3931, 2514, 125368, 1960,
649 69331, 115.7, 518173, 4806, 2572, 127852, 1961,
650 70551, 116.9, 554894, 4007, 2827, 130081, 1962
651 };
652
653 final int nobs = 16;
654 final int nvars = 6;
655
656
657 MillerUpdatingRegression model = new MillerUpdatingRegression(6, true);
658 int off = 0;
659 double[] tmp = new double[6];
660 for (int i = 0; i < nobs; i++) {
661 System.arraycopy(design, off + 1, tmp, 0, nvars);
662 model.addObservation(tmp, design[off]);
663 off += nvars + 1;
664 }
665
666
667 RegressionResults result = model.regress();
668 double[] betaHat = result.getParameterEstimates();
669 UnitTestUtils.customAssertEquals(betaHat,
670 new double[]{-3482258.63459582, 15.0618722713733,
671 -0.358191792925910E-01, -2.02022980381683,
672 -1.03322686717359, -0.511041056535807E-01,
673 1829.15146461355}, 1E-8);
674
675
676 double[] errors = result.getStdErrorOfEstimates();
677 UnitTestUtils.customAssertEquals(new double[]{ 890420.383607373,
678 84.9149257747669,
679 0.334910077722432E-01,
680 0.488399681651699,
681 0.214274163161675,
682 0.226073200069370,
683 455.478499142212}, errors, 1E-6);
684
685
686 UnitTestUtils.customAssertEquals(0.995479004577296, result.getRSquared(), 1E-12);
687 UnitTestUtils.customAssertEquals(0.992465007628826, result.getAdjustedRSquared(), 1E-12);
688
689
690
691 model = new MillerUpdatingRegression(6, false);
692 off = 0;
693 for (int i = 0; i < nobs; i++) {
694 System.arraycopy(design, off + 1, tmp, 0, nvars);
695 model.addObservation(tmp, design[off]);
696 off += nvars + 1;
697 }
698
699 result = model.regress();
700 betaHat = result.getParameterEstimates();
701 UnitTestUtils.customAssertEquals(betaHat,
702 new double[]{-52.99357013868291, 0.07107319907358,
703 -0.42346585566399, -0.57256866841929,
704 -0.41420358884978, 48.41786562001326}, 1E-11);
705
706
707 errors = result.getStdErrorOfEstimates();
708 UnitTestUtils.customAssertEquals(new double[]{ 129.54486693117232, 0.03016640003786,
709 0.41773654056612, 0.27899087467676, 0.32128496193363,
710 17.68948737819961}, errors, 1E-11);
711
712
713
714 UnitTestUtils.customAssertEquals(0.9999670130706, result.getRSquared(), 1E-12);
715 UnitTestUtils.customAssertEquals(0.999947220913, result.getAdjustedRSquared(), 1E-12);
716
717 }
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759 @Test
760 void testOneRedundantColumn() {
761 MillerUpdatingRegression instance = new MillerUpdatingRegression(4, false);
762 MillerUpdatingRegression instance2 = new MillerUpdatingRegression(5, false);
763 double[][] x = new double[airdata[0].length][];
764 double[][] x2 = new double[airdata[0].length][];
765 double[] y = new double[airdata[0].length];
766 for (int i = 0; i < airdata[0].length; i++) {
767 x[i] = new double[4];
768 x2[i] = new double[5];
769 x[i][0] = 1.0;
770 x[i][1] = FastMath.log(airdata[3][i]);
771 x[i][2] = FastMath.log(airdata[4][i]);
772 x[i][3] = airdata[5][i];
773
774 x2[i][0] = x[i][0];
775 x2[i][1] = x[i][1];
776 x2[i][2] = x[i][2];
777 x2[i][3] = x[i][3];
778 x2[i][4] = x[i][3];
779
780 y[i] = FastMath.log(airdata[2][i]);
781 }
782
783 instance.addObservations(x, y);
784 RegressionResults result = instance.regress();
785 assertNotNull(result, "Could not estimate initial regression");
786
787 instance2.addObservations(x2, y);
788 RegressionResults resultRedundant = instance2.regress();
789 assertNotNull(resultRedundant, "Could not estimate redundant regression");
790 double[] beta = result.getParameterEstimates();
791 double[] betar = resultRedundant.getParameterEstimates();
792 double[] se = result.getStdErrorOfEstimates();
793 double[] ser = resultRedundant.getStdErrorOfEstimates();
794
795 for (int i = 0; i < beta.length; i++) {
796 if (FastMath.abs(beta[i] - betar[i]) > 1.0e-8) {
797 fail("Parameters not correctly estimated");
798 }
799 if (FastMath.abs(se[i] - ser[i]) > 1.0e-8) {
800 fail("Standard errors not correctly estimated");
801 }
802 for (int j = 0; j < i; j++) {
803 if (FastMath.abs(result.getCovarianceOfParameters(i, j)
804 - resultRedundant.getCovarianceOfParameters(i, j)) > 1.0e-8) {
805 fail("Variance Covariance not correct");
806 }
807 }
808 }
809
810
811 UnitTestUtils.customAssertEquals(result.getAdjustedRSquared(), resultRedundant.getAdjustedRSquared(), 1.0e-8);
812 UnitTestUtils.customAssertEquals(result.getErrorSumSquares(), resultRedundant.getErrorSumSquares(), 1.0e-8);
813 UnitTestUtils.customAssertEquals(result.getMeanSquareError(), resultRedundant.getMeanSquareError(), 1.0e-8);
814 UnitTestUtils.customAssertEquals(result.getRSquared(), resultRedundant.getRSquared(), 1.0e-8);
815 return;
816 }
817
818 @Test
819 void testThreeRedundantColumn() {
820
821 MillerUpdatingRegression instance = new MillerUpdatingRegression(4, false);
822 MillerUpdatingRegression instance2 = new MillerUpdatingRegression(7, false);
823 double[][] x = new double[airdata[0].length][];
824 double[][] x2 = new double[airdata[0].length][];
825 double[] y = new double[airdata[0].length];
826 for (int i = 0; i < airdata[0].length; i++) {
827 x[i] = new double[4];
828 x2[i] = new double[7];
829 x[i][0] = 1.0;
830 x[i][1] = FastMath.log(airdata[3][i]);
831 x[i][2] = FastMath.log(airdata[4][i]);
832 x[i][3] = airdata[5][i];
833
834 x2[i][0] = x[i][0];
835 x2[i][1] = x[i][0];
836 x2[i][2] = x[i][1];
837 x2[i][3] = x[i][2];
838 x2[i][4] = x[i][1];
839 x2[i][5] = x[i][3];
840 x2[i][6] = x[i][2];
841
842 y[i] = FastMath.log(airdata[2][i]);
843 }
844
845 instance.addObservations(x, y);
846 RegressionResults result = instance.regress();
847 assertNotNull(result, "Could not estimate initial regression");
848
849 instance2.addObservations(x2, y);
850 RegressionResults resultRedundant = instance2.regress();
851 assertNotNull(resultRedundant, "Could not estimate redundant regression");
852 double[] beta = result.getParameterEstimates();
853 double[] betar = resultRedundant.getParameterEstimates();
854 double[] se = result.getStdErrorOfEstimates();
855 double[] ser = resultRedundant.getStdErrorOfEstimates();
856
857 if (FastMath.abs(beta[0] - betar[0]) > 1.0e-8) {
858 fail("Parameters not correct after reorder (0,3)");
859 }
860 if (FastMath.abs(beta[1] - betar[2]) > 1.0e-8) {
861 fail("Parameters not correct after reorder (1,2)");
862 }
863 if (FastMath.abs(beta[2] - betar[3]) > 1.0e-8) {
864 fail("Parameters not correct after reorder (2,1)");
865 }
866 if (FastMath.abs(beta[3] - betar[5]) > 1.0e-8) {
867 fail("Parameters not correct after reorder (3,0)");
868 }
869
870 if (FastMath.abs(se[0] - ser[0]) > 1.0e-8) {
871 fail("Se not correct after reorder (0,3)");
872 }
873 if (FastMath.abs(se[1] - ser[2]) > 1.0e-8) {
874 fail("Se not correct after reorder (1,2)");
875 }
876 if (FastMath.abs(se[2] - ser[3]) > 1.0e-8) {
877 fail("Se not correct after reorder (2,1)");
878 }
879 if (FastMath.abs(se[3] - ser[5]) > 1.0e-8) {
880 fail("Se not correct after reorder (3,0)");
881 }
882
883 if (FastMath.abs(result.getCovarianceOfParameters(0, 0)
884 - resultRedundant.getCovarianceOfParameters(0, 0)) > 1.0e-8) {
885 fail("VCV not correct after reorder (0,0)");
886 }
887 if (FastMath.abs(result.getCovarianceOfParameters(0, 1)
888 - resultRedundant.getCovarianceOfParameters(0, 2)) > 1.0e-8) {
889 fail("VCV not correct after reorder (0,1)<->(0,2)");
890 }
891 if (FastMath.abs(result.getCovarianceOfParameters(0, 2)
892 - resultRedundant.getCovarianceOfParameters(0, 3)) > 1.0e-8) {
893 fail("VCV not correct after reorder (0,2)<->(0,1)");
894 }
895 if (FastMath.abs(result.getCovarianceOfParameters(0, 3)
896 - resultRedundant.getCovarianceOfParameters(0, 5)) > 1.0e-8) {
897 fail("VCV not correct after reorder (0,3)<->(0,3)");
898 }
899 if (FastMath.abs(result.getCovarianceOfParameters(1, 0)
900 - resultRedundant.getCovarianceOfParameters(2, 0)) > 1.0e-8) {
901 fail("VCV not correct after reorder (1,0)<->(2,0)");
902 }
903 if (FastMath.abs(result.getCovarianceOfParameters(1, 1)
904 - resultRedundant.getCovarianceOfParameters(2, 2)) > 1.0e-8) {
905 fail("VCV not correct (1,1)<->(2,1)");
906 }
907 if (FastMath.abs(result.getCovarianceOfParameters(1, 2)
908 - resultRedundant.getCovarianceOfParameters(2, 3)) > 1.0e-8) {
909 fail("VCV not correct (1,2)<->(2,2)");
910 }
911
912 if (FastMath.abs(result.getCovarianceOfParameters(2, 0)
913 - resultRedundant.getCovarianceOfParameters(3, 0)) > 1.0e-8) {
914 fail("VCV not correct (2,0)<->(1,0)");
915 }
916 if (FastMath.abs(result.getCovarianceOfParameters(2, 1)
917 - resultRedundant.getCovarianceOfParameters(3, 2)) > 1.0e-8) {
918 fail("VCV not correct (2,1)<->(1,2)");
919 }
920
921 if (FastMath.abs(result.getCovarianceOfParameters(3, 3)
922 - resultRedundant.getCovarianceOfParameters(5, 5)) > 1.0e-8) {
923 fail("VCV not correct (3,3)<->(3,2)");
924 }
925
926 UnitTestUtils.customAssertEquals(result.getAdjustedRSquared(), resultRedundant.getAdjustedRSquared(), 1.0e-8);
927 UnitTestUtils.customAssertEquals(result.getErrorSumSquares(), resultRedundant.getErrorSumSquares(), 1.0e-8);
928 UnitTestUtils.customAssertEquals(result.getMeanSquareError(), resultRedundant.getMeanSquareError(), 1.0e-8);
929 UnitTestUtils.customAssertEquals(result.getRSquared(), resultRedundant.getRSquared(), 1.0e-8);
930 return;
931 }
932
933 @Test
934 void testPCorr() {
935 MillerUpdatingRegression instance = new MillerUpdatingRegression(4, false);
936 double[][] x = new double[airdata[0].length][];
937 double[] y = new double[airdata[0].length];
938 double[] cp = new double[10];
939 double[] yxcorr = new double[4];
940 double[] diag = new double[4];
941 double sumysq = 0.0;
942 int off = 0;
943 for (int i = 0; i < airdata[0].length; i++) {
944 x[i] = new double[4];
945 x[i][0] = 1.0;
946 x[i][1] = FastMath.log(airdata[3][i]);
947 x[i][2] = FastMath.log(airdata[4][i]);
948 x[i][3] = airdata[5][i];
949 y[i] = FastMath.log(airdata[2][i]);
950 off = 0;
951 for (int j = 0; j < 4; j++) {
952 double tmp = x[i][j];
953 for (int k = 0; k <= j; k++, off++) {
954 cp[off] += tmp * x[i][k];
955 }
956 yxcorr[j] += tmp * y[i];
957 }
958 sumysq += y[i] * y[i];
959 }
960 PearsonsCorrelation pearson = new PearsonsCorrelation(x);
961 RealMatrix corr = pearson.getCorrelationMatrix();
962 off = 0;
963 for (int i = 0; i < 4; i++, off += (i + 1)) {
964 diag[i] = FastMath.sqrt(cp[off]);
965 }
966
967 instance.addObservations(x, y);
968 double[] pc = instance.getPartialCorrelations(0);
969 int idx = 0;
970 off = 0;
971 int off2 = 6;
972 for (int i = 0; i < 4; i++) {
973 for (int j = 0; j < i; j++) {
974 if (FastMath.abs(pc[idx] - cp[off] / (diag[i] * diag[j])) > 1.0e-8) {
975 fail("Failed cross products... i = " + i + " j = " + j);
976 }
977 ++idx;
978 ++off;
979 }
980 ++off;
981 if (FastMath.abs(pc[i+off2] - yxcorr[ i] / (FastMath.sqrt(sumysq) * diag[i])) > 1.0e-8) {
982 fail("Assertions.failed cross product i = " + i + " y");
983 }
984 }
985 double[] pc2 = instance.getPartialCorrelations(1);
986
987 idx = 0;
988
989 for (int i = 1; i < 4; i++) {
990 for (int j = 1; j < i; j++) {
991 if (FastMath.abs(pc2[idx] - corr.getEntry(j, i)) > 1.0e-8) {
992 fail("Failed cross products... i = " + i + " j = " + j);
993 }
994 ++idx;
995 }
996 }
997 double[] pc3 = instance.getPartialCorrelations(2);
998 if (pc3 == null) {
999 fail("Should not be null");
1000 }
1001 return;
1002 }
1003
1004 @Test
1005 void testHdiag() {
1006 MillerUpdatingRegression instance = new MillerUpdatingRegression(4, false);
1007 double[][] x = new double[airdata[0].length][];
1008 double[] y = new double[airdata[0].length];
1009 for (int i = 0; i < airdata[0].length; i++) {
1010 x[i] = new double[4];
1011 x[i][0] = 1.0;
1012 x[i][1] = FastMath.log(airdata[3][i]);
1013 x[i][2] = FastMath.log(airdata[4][i]);
1014 x[i][3] = airdata[5][i];
1015 y[i] = FastMath.log(airdata[2][i]);
1016 }
1017 instance.addObservations(x, y);
1018 OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression();
1019 ols.setNoIntercept(true);
1020 ols.newSampleData(y, x);
1021
1022 RealMatrix rm = ols.calculateHat();
1023 for (int i = 0; i < x.length; i++) {
1024 UnitTestUtils.customAssertEquals(instance.getDiagonalOfHatMatrix(x[i]), rm.getEntry(i, i), 1.0e-8);
1025 }
1026 return;
1027 }
1028
1029 @Test
1030 void testHdiagConstant() {
1031 MillerUpdatingRegression instance = new MillerUpdatingRegression(3, true);
1032 double[][] x = new double[airdata[0].length][];
1033 double[] y = new double[airdata[0].length];
1034 for (int i = 0; i < airdata[0].length; i++) {
1035 x[i] = new double[3];
1036 x[i][0] = FastMath.log(airdata[3][i]);
1037 x[i][1] = FastMath.log(airdata[4][i]);
1038 x[i][2] = airdata[5][i];
1039 y[i] = FastMath.log(airdata[2][i]);
1040 }
1041 instance.addObservations(x, y);
1042 OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression();
1043 ols.setNoIntercept(false);
1044 ols.newSampleData(y, x);
1045
1046 RealMatrix rm = ols.calculateHat();
1047 for (int i = 0; i < x.length; i++) {
1048 UnitTestUtils.customAssertEquals(instance.getDiagonalOfHatMatrix(x[i]), rm.getEntry(i, i), 1.0e-8);
1049 }
1050 return;
1051 }
1052
1053
1054 @Test
1055 void testSubsetRegression() {
1056
1057 MillerUpdatingRegression instance = new MillerUpdatingRegression(3, true);
1058 MillerUpdatingRegression redRegression = new MillerUpdatingRegression(2, true);
1059 double[][] x = new double[airdata[0].length][];
1060 double[][] xReduced = new double[airdata[0].length][];
1061 double[] y = new double[airdata[0].length];
1062 for (int i = 0; i < airdata[0].length; i++) {
1063 x[i] = new double[3];
1064 x[i][0] = FastMath.log(airdata[3][i]);
1065 x[i][1] = FastMath.log(airdata[4][i]);
1066 x[i][2] = airdata[5][i];
1067
1068 xReduced[i] = new double[2];
1069 xReduced[i][0] = FastMath.log(airdata[3][i]);
1070 xReduced[i][1] = FastMath.log(airdata[4][i]);
1071
1072 y[i] = FastMath.log(airdata[2][i]);
1073 }
1074
1075 instance.addObservations(x, y);
1076 redRegression.addObservations(xReduced, y);
1077
1078 RegressionResults resultsInstance = instance.regress( new int[]{0,1,2} );
1079 RegressionResults resultsReduced = redRegression.regress();
1080
1081 UnitTestUtils.customAssertEquals(resultsInstance.getParameterEstimates(), resultsReduced.getParameterEstimates(), 1.0e-12);
1082 UnitTestUtils.customAssertEquals(resultsInstance.getStdErrorOfEstimates(), resultsReduced.getStdErrorOfEstimates(), 1.0e-12);
1083 }
1084
1085
1086 }