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.Assert;
30 import org.junit.Test;
31
32
33
34
35 public class MillerUpdatingRegressionTest {
36
37 public MillerUpdatingRegressionTest() {
38 }
39
40
41
42 private final static double[][] airdata = {
43 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},
44 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},
45 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},
46 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},
47 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},
48 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}
49 };
50
51
52
53
54 @Test
55 public void testHasIntercept() {
56 MillerUpdatingRegression instance = new MillerUpdatingRegression(10, false);
57 if (instance.hasIntercept()) {
58 Assert.fail("Should not have intercept");
59 }
60 instance = new MillerUpdatingRegression(10, true);
61 if (!instance.hasIntercept()) {
62 Assert.fail("Should have intercept");
63 }
64 }
65
66
67
68
69 @Test
70 public void testAddObsGetNClear() {
71 MillerUpdatingRegression instance = new MillerUpdatingRegression(3, true);
72 double[][] xAll = new double[airdata[0].length][];
73 double[] y = new double[airdata[0].length];
74 for (int i = 0; i < airdata[0].length; i++) {
75 xAll[i] = new double[3];
76 xAll[i][0] = FastMath.log(airdata[3][i]);
77 xAll[i][1] = FastMath.log(airdata[4][i]);
78 xAll[i][2] = airdata[5][i];
79 y[i] = FastMath.log(airdata[2][i]);
80 }
81 instance.addObservations(xAll, y);
82 if (instance.getN() != xAll.length) {
83 Assert.fail("Number of observations not correct in bulk addition");
84 }
85 instance.clear();
86 for (int i = 0; i < xAll.length; i++) {
87 instance.addObservation(xAll[i], y[i]);
88 }
89 if (instance.getN() != xAll.length) {
90 Assert.fail("Number of observations not correct in drip addition");
91 }
92 return;
93 }
94
95 @Test
96 public void testNegativeTestAddObs() {
97 MillerUpdatingRegression instance = new MillerUpdatingRegression(3, true);
98 try {
99 instance.addObservation(new double[]{1.0}, 0.0);
100 Assert.fail("Should throw MathIllegalArgumentException");
101 } catch (MathIllegalArgumentException iae) {
102 } catch (Exception e) {
103 Assert.fail("Should throw MathIllegalArgumentException");
104 }
105 try {
106 instance.addObservation(new double[]{1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}, 0.0);
107 Assert.fail("Should throw MathIllegalArgumentException");
108 } catch (MathIllegalArgumentException iae) {
109 } catch (Exception e) {
110 Assert.fail("Should throw MathIllegalArgumentException");
111 }
112 try {
113 instance.addObservation(new double[]{1.0, 1.0, 1.0}, 0.0);
114 } catch (Exception e) {
115 Assert.fail("Should throw MathIllegalArgumentException");
116 }
117
118
119 instance = new MillerUpdatingRegression(3, false);
120 try {
121 instance.addObservation(new double[]{1.0}, 0.0);
122 Assert.fail("Should throw MathIllegalArgumentException [NOINTERCEPT]");
123 } catch (MathIllegalArgumentException iae) {
124 } catch (Exception e) {
125 Assert.fail("Should throw MathIllegalArgumentException [NOINTERCEPT]");
126 }
127 try {
128 instance.addObservation(new double[]{1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}, 0.0);
129 Assert.fail("Should throw MathIllegalArgumentException [NOINTERCEPT]");
130 } catch (MathIllegalArgumentException iae) {
131 } catch (Exception e) {
132 Assert.fail("Should throw MathIllegalArgumentException [NOINTERCEPT]");
133 }
134 try {
135 instance.addObservation(new double[]{1.0, 1.0, 1.0}, 0.0);
136 } catch (Exception e) {
137 Assert.fail("Should throw MathIllegalArgumentException [NOINTERCEPT]");
138 }
139 }
140
141 @Test
142 public void testNegativeTestAddMultipleObs() {
143 MillerUpdatingRegression instance = new MillerUpdatingRegression(3, true);
144 try {
145 double[][] tst = {{1.0, 1.0, 1.0}, {1.20, 2.0, 2.1}};
146 double[] y = {1.0};
147 instance.addObservations(tst, y);
148
149 Assert.fail("Should throw MathIllegalArgumentException");
150 } catch (MathIllegalArgumentException iae) {
151 } catch (Exception e) {
152 Assert.fail("Should throw MathIllegalArgumentException");
153 }
154
155 try {
156 double[][] tst = {{1.0, 1.0, 1.0}, {1.20, 2.0, 2.1}};
157 double[] y = {1.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
158 instance.addObservations(tst, y);
159
160 Assert.fail("Should throw MathIllegalArgumentException");
161 } catch (MathIllegalArgumentException iae) {
162 } catch (Exception e) {
163 Assert.fail("Should throw MathIllegalArgumentException");
164 }
165 }
166
167
168
169
170 @Test
171 public void testRegressAirlineConstantExternal() {
172 MillerUpdatingRegression instance = new MillerUpdatingRegression(4, false);
173 double[][] x = new double[airdata[0].length][];
174 double[] y = new double[airdata[0].length];
175 for (int i = 0; i < airdata[0].length; i++) {
176 x[i] = new double[4];
177 x[i][0] = 1.0;
178 x[i][1] = FastMath.log(airdata[3][i]);
179 x[i][2] = FastMath.log(airdata[4][i]);
180 x[i][3] = airdata[5][i];
181 y[i] = FastMath.log(airdata[2][i]);
182 }
183
184 instance.addObservations(x, y);
185 try {
186 RegressionResults result = instance.regress();
187 Assert.assertNotNull("The test case is a prototype.", result);
188 UnitTestUtils.assertEquals(
189 new double[]{9.5169, 0.8827, 0.4540, -1.6275},
190 result.getParameterEstimates(), 1e-4);
191
192
193 UnitTestUtils.assertEquals(
194 new double[]{.2292445, .0132545, .0203042, .345302},
195 result.getStdErrorOfEstimates(), 1.0e-4);
196
197 UnitTestUtils.assertEquals(0.01552839, result.getMeanSquareError(), 1.0e-8);
198 } catch (Exception e) {
199 Assert.fail("Should not throw exception but does");
200 }
201 }
202
203 @Test
204 public void testRegressAirlineConstantInternal() {
205 MillerUpdatingRegression instance = new MillerUpdatingRegression(3, true);
206 double[][] x = new double[airdata[0].length][];
207 double[] y = new double[airdata[0].length];
208 for (int i = 0; i < airdata[0].length; i++) {
209 x[i] = new double[3];
210 x[i][0] = FastMath.log(airdata[3][i]);
211 x[i][1] = FastMath.log(airdata[4][i]);
212 x[i][2] = airdata[5][i];
213 y[i] = FastMath.log(airdata[2][i]);
214 }
215
216 instance.addObservations(x, y);
217 try {
218 RegressionResults result = instance.regress();
219 Assert.assertNotNull("The test case is a prototype.", result);
220 UnitTestUtils.assertEquals(
221 new double[]{9.5169, 0.8827, 0.4540, -1.6275},
222 result.getParameterEstimates(), 1e-4);
223
224
225 UnitTestUtils.assertEquals(
226 new double[]{.2292445, .0132545, .0203042, .345302},
227 result.getStdErrorOfEstimates(), 1.0e-4);
228
229 UnitTestUtils.assertEquals(0.9883, result.getRSquared(), 1.0e-4);
230 UnitTestUtils.assertEquals(0.01552839, result.getMeanSquareError(), 1.0e-8);
231 } catch (Exception e) {
232 Assert.fail("Should not throw exception but does");
233 }
234 }
235
236 @Test
237 public void testFilippelli() {
238 double[] data = new double[]{
239 0.8116, -6.860120914,
240 0.9072, -4.324130045,
241 0.9052, -4.358625055,
242 0.9039, -4.358426747,
243 0.8053, -6.955852379,
244 0.8377, -6.661145254,
245 0.8667, -6.355462942,
246 0.8809, -6.118102026,
247 0.7975, -7.115148017,
248 0.8162, -6.815308569,
249 0.8515, -6.519993057,
250 0.8766, -6.204119983,
251 0.8885, -5.853871964,
252 0.8859, -6.109523091,
253 0.8959, -5.79832982,
254 0.8913, -5.482672118,
255 0.8959, -5.171791386,
256 0.8971, -4.851705903,
257 0.9021, -4.517126416,
258 0.909, -4.143573228,
259 0.9139, -3.709075441,
260 0.9199, -3.499489089,
261 0.8692, -6.300769497,
262 0.8872, -5.953504836,
263 0.89, -5.642065153,
264 0.891, -5.031376979,
265 0.8977, -4.680685696,
266 0.9035, -4.329846955,
267 0.9078, -3.928486195,
268 0.7675, -8.56735134,
269 0.7705, -8.363211311,
270 0.7713, -8.107682739,
271 0.7736, -7.823908741,
272 0.7775, -7.522878745,
273 0.7841, -7.218819279,
274 0.7971, -6.920818754,
275 0.8329, -6.628932138,
276 0.8641, -6.323946875,
277 0.8804, -5.991399828,
278 0.7668, -8.781464495,
279 0.7633, -8.663140179,
280 0.7678, -8.473531488,
281 0.7697, -8.247337057,
282 0.77, -7.971428747,
283 0.7749, -7.676129393,
284 0.7796, -7.352812702,
285 0.7897, -7.072065318,
286 0.8131, -6.774174009,
287 0.8498, -6.478861916,
288 0.8741, -6.159517513,
289 0.8061, -6.835647144,
290 0.846, -6.53165267,
291 0.8751, -6.224098421,
292 0.8856, -5.910094889,
293 0.8919, -5.598599459,
294 0.8934, -5.290645224,
295 0.894, -4.974284616,
296 0.8957, -4.64454848,
297 0.9047, -4.290560426,
298 0.9129, -3.885055584,
299 0.9209, -3.408378962,
300 0.9219, -3.13200249,
301 0.7739, -8.726767166,
302 0.7681, -8.66695597,
303 0.7665, -8.511026475,
304 0.7703, -8.165388579,
305 0.7702, -7.886056648,
306 0.7761, -7.588043762,
307 0.7809, -7.283412422,
308 0.7961, -6.995678626,
309 0.8253, -6.691862621,
310 0.8602, -6.392544977,
311 0.8809, -6.067374056,
312 0.8301, -6.684029655,
313 0.8664, -6.378719832,
314 0.8834, -6.065855188,
315 0.8898, -5.752272167,
316 0.8964, -5.132414673,
317 0.8963, -4.811352704,
318 0.9074, -4.098269308,
319 0.9119, -3.66174277,
320 0.9228, -3.2644011
321 };
322 MillerUpdatingRegression model = new MillerUpdatingRegression(10, true);
323 int off = 0;
324 double[] tmp = new double[10];
325 int nobs = 82;
326 for (int i = 0; i < nobs; i++) {
327 tmp[0] = data[off + 1];
328
329
330
331
332
333
334
335
336
337 tmp[1] = tmp[0] * tmp[0];
338 tmp[2] = tmp[0] * tmp[1];
339 tmp[3] = tmp[0] * tmp[2];
340 tmp[4] = tmp[0] * tmp[3];
341 tmp[5] = tmp[0] * tmp[4];
342 tmp[6] = tmp[0] * tmp[5];
343 tmp[7] = tmp[0] * tmp[6];
344 tmp[8] = tmp[0] * tmp[7];
345 tmp[9] = tmp[0] * tmp[8];
346 model.addObservation(tmp, data[off]);
347 off += 2;
348 }
349 RegressionResults result = model.regress();
350 double[] betaHat = result.getParameterEstimates();
351 UnitTestUtils.assertEquals(betaHat,
352 new double[]{
353 -1467.48961422980,
354 -2772.17959193342,
355 -2316.37108160893,
356 -1127.97394098372,
357 -354.478233703349,
358 -75.1242017393757,
359 -10.8753180355343,
360 -1.06221498588947,
361 -0.670191154593408E-01,
362 -0.246781078275479E-02,
363 -0.402962525080404E-04
364 }, 1E-5);
365
366 double[] se = result.getStdErrorOfEstimates();
367 UnitTestUtils.assertEquals(se,
368 new double[]{
369 298.084530995537,
370 559.779865474950,
371 466.477572127796,
372 227.204274477751,
373 71.6478660875927,
374 15.2897178747400,
375 2.23691159816033,
376 0.221624321934227,
377 0.142363763154724E-01,
378 0.535617408889821E-03,
379 0.896632837373868E-05
380 }, 1E-5);
381
382 UnitTestUtils.assertEquals(0.996727416185620, result.getRSquared(), 1.0e-8);
383 UnitTestUtils.assertEquals(0.112091743968020E-04, result.getMeanSquareError(), 1.0e-10);
384 UnitTestUtils.assertEquals(0.795851382172941E-03, result.getErrorSumSquares(), 1.0e-10);
385
386 }
387
388 @Test
389 public void testWampler1() {
390 double[] data = new double[]{
391 1, 0,
392 6, 1,
393 63, 2,
394 364, 3,
395 1365, 4,
396 3906, 5,
397 9331, 6,
398 19608, 7,
399 37449, 8,
400 66430, 9,
401 111111, 10,
402 177156, 11,
403 271453, 12,
404 402234, 13,
405 579195, 14,
406 813616, 15,
407 1118481, 16,
408 1508598, 17,
409 2000719, 18,
410 2613660, 19,
411 3368421, 20};
412
413 MillerUpdatingRegression model = new MillerUpdatingRegression(5, true);
414 int off = 0;
415 double[] tmp = new double[5];
416 int nobs = 21;
417 for (int i = 0; i < nobs; i++) {
418 tmp[0] = data[off + 1];
419 tmp[1] = tmp[0] * tmp[0];
420 tmp[2] = tmp[0] * tmp[1];
421 tmp[3] = tmp[0] * tmp[2];
422 tmp[4] = tmp[0] * tmp[3];
423 model.addObservation(tmp, data[off]);
424 off += 2;
425 }
426 RegressionResults result = model.regress();
427 double[] betaHat = result.getParameterEstimates();
428 UnitTestUtils.assertEquals(betaHat,
429 new double[]{1.0,
430 1.0, 1.0,
431 1.0, 1.0,
432 1.0}, 1E-8);
433
434 double[] se = result.getStdErrorOfEstimates();
435 UnitTestUtils.assertEquals(se,
436 new double[]{0.0,
437 0.0, 0.0,
438 0.0, 0.0,
439 0.0}, 1E-8);
440
441 UnitTestUtils.assertEquals(1.0, result.getRSquared(), 1.0e-10);
442 UnitTestUtils.assertEquals(0, result.getMeanSquareError(), 1.0e-7);
443 UnitTestUtils.assertEquals(0.00, result.getErrorSumSquares(), 1.0e-6);
444
445 return;
446 }
447
448 @Test
449 public void testWampler2() {
450 double[] data = new double[]{
451 1.00000, 0,
452 1.11111, 1,
453 1.24992, 2,
454 1.42753, 3,
455 1.65984, 4,
456 1.96875, 5,
457 2.38336, 6,
458 2.94117, 7,
459 3.68928, 8,
460 4.68559, 9,
461 6.00000, 10,
462 7.71561, 11,
463 9.92992, 12,
464 12.75603, 13,
465 16.32384, 14,
466 20.78125, 15,
467 26.29536, 16,
468 33.05367, 17,
469 41.26528, 18,
470 51.16209, 19,
471 63.00000, 20};
472
473 MillerUpdatingRegression model = new MillerUpdatingRegression(5, true);
474 int off = 0;
475 double[] tmp = new double[5];
476 int nobs = 21;
477 for (int i = 0; i < nobs; i++) {
478 tmp[0] = data[off + 1];
479 tmp[1] = tmp[0] * tmp[0];
480 tmp[2] = tmp[0] * tmp[1];
481 tmp[3] = tmp[0] * tmp[2];
482 tmp[4] = tmp[0] * tmp[3];
483 model.addObservation(tmp, data[off]);
484 off += 2;
485 }
486 RegressionResults result = model.regress();
487 double[] betaHat = result.getParameterEstimates();
488 UnitTestUtils.assertEquals(betaHat,
489 new double[]{1.0,
490 1.0e-1, 1.0e-2,
491 1.0e-3, 1.0e-4,
492 1.0e-5}, 1E-8);
493
494 double[] se = result.getStdErrorOfEstimates();
495 UnitTestUtils.assertEquals(se,
496 new double[]{0.0,
497 0.0, 0.0,
498 0.0, 0.0,
499 0.0}, 1E-8);
500
501 UnitTestUtils.assertEquals(1.0, result.getRSquared(), 1.0e-10);
502 UnitTestUtils.assertEquals(0, result.getMeanSquareError(), 1.0e-7);
503 UnitTestUtils.assertEquals(0.00, result.getErrorSumSquares(), 1.0e-6);
504 return;
505 }
506
507 @Test
508 public void testWampler3() {
509 double[] data = new double[]{
510 760, 0,
511 -2042, 1,
512 2111, 2,
513 -1684, 3,
514 3888, 4,
515 1858, 5,
516 11379, 6,
517 17560, 7,
518 39287, 8,
519 64382, 9,
520 113159, 10,
521 175108, 11,
522 273291, 12,
523 400186, 13,
524 581243, 14,
525 811568, 15,
526 1121004, 16,
527 1506550, 17,
528 2002767, 18,
529 2611612, 19,
530 3369180, 20};
531 MillerUpdatingRegression model = new MillerUpdatingRegression(5, true);
532 int off = 0;
533 double[] tmp = new double[5];
534 int nobs = 21;
535 for (int i = 0; i < nobs; i++) {
536 tmp[0] = data[off + 1];
537 tmp[1] = tmp[0] * tmp[0];
538 tmp[2] = tmp[0] * tmp[1];
539 tmp[3] = tmp[0] * tmp[2];
540 tmp[4] = tmp[0] * tmp[3];
541 model.addObservation(tmp, data[off]);
542 off += 2;
543 }
544 RegressionResults result = model.regress();
545 double[] betaHat = result.getParameterEstimates();
546 UnitTestUtils.assertEquals(betaHat,
547 new double[]{1.0,
548 1.0, 1.0,
549 1.0, 1.0,
550 1.0}, 1E-8);
551 double[] se = result.getStdErrorOfEstimates();
552 UnitTestUtils.assertEquals(se,
553 new double[]{2152.32624678170,
554 2363.55173469681, 779.343524331583,
555 101.475507550350, 5.64566512170752,
556 0.112324854679312}, 1E-8);
557
558 UnitTestUtils.assertEquals(.999995559025820, result.getRSquared(), 1.0e-10);
559 UnitTestUtils.assertEquals(5570284.53333333, result.getMeanSquareError(), 1.0e-7);
560 UnitTestUtils.assertEquals(83554268.0000000, result.getErrorSumSquares(), 1.0e-6);
561 return;
562 }
563
564
565 public void testWampler4() {
566 double[] data = new double[]{
567 75901, 0,
568 -204794, 1,
569 204863, 2,
570 -204436, 3,
571 253665, 4,
572 -200894, 5,
573 214131, 6,
574 -185192, 7,
575 221249, 8,
576 -138370, 9,
577 315911, 10,
578 -27644, 11,
579 455253, 12,
580 197434, 13,
581 783995, 14,
582 608816, 15,
583 1370781, 16,
584 1303798, 17,
585 2205519, 18,
586 2408860, 19,
587 3444321, 20};
588 MillerUpdatingRegression model = new MillerUpdatingRegression(5, true);
589 int off = 0;
590 double[] tmp = new double[5];
591 int nobs = 21;
592 for (int i = 0; i < nobs; i++) {
593 tmp[0] = data[off + 1];
594 tmp[1] = tmp[0] * tmp[0];
595 tmp[2] = tmp[0] * tmp[1];
596 tmp[3] = tmp[0] * tmp[2];
597 tmp[4] = tmp[0] * tmp[3];
598 model.addObservation(tmp, data[off]);
599 off += 2;
600 }
601 RegressionResults result = model.regress();
602 double[] betaHat = result.getParameterEstimates();
603 UnitTestUtils.assertEquals(betaHat,
604 new double[]{1.0,
605 1.0, 1.0,
606 1.0, 1.0,
607 1.0}, 1E-8);
608
609 double[] se = result.getStdErrorOfEstimates();
610 UnitTestUtils.assertEquals(se,
611 new double[]{215232.624678170,
612 236355.173469681, 77934.3524331583,
613 10147.5507550350, 564.566512170752,
614 11.2324854679312}, 1E-8);
615
616 UnitTestUtils.assertEquals(.957478440825662, result.getRSquared(), 1.0e-10);
617 UnitTestUtils.assertEquals(55702845333.3333, result.getMeanSquareError(), 1.0e-4);
618 UnitTestUtils.assertEquals(835542680000.000, result.getErrorSumSquares(), 1.0e-3);
619
620 return;
621 }
622
623
624
625
626
627
628
629
630
631
632
633 @Test
634 public void testLongly() {
635
636
637 double[] design = new double[]{
638 60323, 83.0, 234289, 2356, 1590, 107608, 1947,
639 61122, 88.5, 259426, 2325, 1456, 108632, 1948,
640 60171, 88.2, 258054, 3682, 1616, 109773, 1949,
641 61187, 89.5, 284599, 3351, 1650, 110929, 1950,
642 63221, 96.2, 328975, 2099, 3099, 112075, 1951,
643 63639, 98.1, 346999, 1932, 3594, 113270, 1952,
644 64989, 99.0, 365385, 1870, 3547, 115094, 1953,
645 63761, 100.0, 363112, 3578, 3350, 116219, 1954,
646 66019, 101.2, 397469, 2904, 3048, 117388, 1955,
647 67857, 104.6, 419180, 2822, 2857, 118734, 1956,
648 68169, 108.4, 442769, 2936, 2798, 120445, 1957,
649 66513, 110.8, 444546, 4681, 2637, 121950, 1958,
650 68655, 112.6, 482704, 3813, 2552, 123366, 1959,
651 69564, 114.2, 502601, 3931, 2514, 125368, 1960,
652 69331, 115.7, 518173, 4806, 2572, 127852, 1961,
653 70551, 116.9, 554894, 4007, 2827, 130081, 1962
654 };
655
656 final int nobs = 16;
657 final int nvars = 6;
658
659
660 MillerUpdatingRegression model = new MillerUpdatingRegression(6, true);
661 int off = 0;
662 double[] tmp = new double[6];
663 for (int i = 0; i < nobs; i++) {
664 System.arraycopy(design, off + 1, tmp, 0, nvars);
665 model.addObservation(tmp, design[off]);
666 off += nvars + 1;
667 }
668
669
670 RegressionResults result = model.regress();
671 double[] betaHat = result.getParameterEstimates();
672 UnitTestUtils.assertEquals(betaHat,
673 new double[]{-3482258.63459582, 15.0618722713733,
674 -0.358191792925910E-01, -2.02022980381683,
675 -1.03322686717359, -0.511041056535807E-01,
676 1829.15146461355}, 1E-8);
677
678
679 double[] errors = result.getStdErrorOfEstimates();
680 UnitTestUtils.assertEquals(new double[]{890420.383607373,
681 84.9149257747669,
682 0.334910077722432E-01,
683 0.488399681651699,
684 0.214274163161675,
685 0.226073200069370,
686 455.478499142212}, errors, 1E-6);
687
688
689 UnitTestUtils.assertEquals(0.995479004577296, result.getRSquared(), 1E-12);
690 UnitTestUtils.assertEquals(0.992465007628826, result.getAdjustedRSquared(), 1E-12);
691
692
693
694 model = new MillerUpdatingRegression(6, false);
695 off = 0;
696 for (int i = 0; i < nobs; i++) {
697 System.arraycopy(design, off + 1, tmp, 0, nvars);
698 model.addObservation(tmp, design[off]);
699 off += nvars + 1;
700 }
701
702 result = model.regress();
703 betaHat = result.getParameterEstimates();
704 UnitTestUtils.assertEquals(betaHat,
705 new double[]{-52.99357013868291, 0.07107319907358,
706 -0.42346585566399, -0.57256866841929,
707 -0.41420358884978, 48.41786562001326}, 1E-11);
708
709
710 errors = result.getStdErrorOfEstimates();
711 UnitTestUtils.assertEquals(new double[]{129.54486693117232, 0.03016640003786,
712 0.41773654056612, 0.27899087467676, 0.32128496193363,
713 17.68948737819961}, errors, 1E-11);
714
715
716
717 UnitTestUtils.assertEquals(0.9999670130706, result.getRSquared(), 1E-12);
718 UnitTestUtils.assertEquals(0.999947220913, result.getAdjustedRSquared(), 1E-12);
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
760
761
762 @Test
763 public void testOneRedundantColumn() {
764 MillerUpdatingRegression instance = new MillerUpdatingRegression(4, false);
765 MillerUpdatingRegression instance2 = new MillerUpdatingRegression(5, false);
766 double[][] x = new double[airdata[0].length][];
767 double[][] x2 = new double[airdata[0].length][];
768 double[] y = new double[airdata[0].length];
769 for (int i = 0; i < airdata[0].length; i++) {
770 x[i] = new double[4];
771 x2[i] = new double[5];
772 x[i][0] = 1.0;
773 x[i][1] = FastMath.log(airdata[3][i]);
774 x[i][2] = FastMath.log(airdata[4][i]);
775 x[i][3] = airdata[5][i];
776
777 x2[i][0] = x[i][0];
778 x2[i][1] = x[i][1];
779 x2[i][2] = x[i][2];
780 x2[i][3] = x[i][3];
781 x2[i][4] = x[i][3];
782
783 y[i] = FastMath.log(airdata[2][i]);
784 }
785
786 instance.addObservations(x, y);
787 RegressionResults result = instance.regress();
788 Assert.assertNotNull("Could not estimate initial regression", result);
789
790 instance2.addObservations(x2, y);
791 RegressionResults resultRedundant = instance2.regress();
792 Assert.assertNotNull("Could not estimate redundant regression", resultRedundant);
793 double[] beta = result.getParameterEstimates();
794 double[] betar = resultRedundant.getParameterEstimates();
795 double[] se = result.getStdErrorOfEstimates();
796 double[] ser = resultRedundant.getStdErrorOfEstimates();
797
798 for (int i = 0; i < beta.length; i++) {
799 if (FastMath.abs(beta[i] - betar[i]) > 1.0e-8) {
800 Assert.fail("Parameters not correctly estimated");
801 }
802 if (FastMath.abs(se[i] - ser[i]) > 1.0e-8) {
803 Assert.fail("Standard errors not correctly estimated");
804 }
805 for (int j = 0; j < i; j++) {
806 if (FastMath.abs(result.getCovarianceOfParameters(i, j)
807 - resultRedundant.getCovarianceOfParameters(i, j)) > 1.0e-8) {
808 Assert.fail("Variance Covariance not correct");
809 }
810 }
811 }
812
813
814 UnitTestUtils.assertEquals(result.getAdjustedRSquared(), resultRedundant.getAdjustedRSquared(), 1.0e-8);
815 UnitTestUtils.assertEquals(result.getErrorSumSquares(), resultRedundant.getErrorSumSquares(), 1.0e-8);
816 UnitTestUtils.assertEquals(result.getMeanSquareError(), resultRedundant.getMeanSquareError(), 1.0e-8);
817 UnitTestUtils.assertEquals(result.getRSquared(), resultRedundant.getRSquared(), 1.0e-8);
818 return;
819 }
820
821 @Test
822 public void testThreeRedundantColumn() {
823
824 MillerUpdatingRegression instance = new MillerUpdatingRegression(4, false);
825 MillerUpdatingRegression instance2 = new MillerUpdatingRegression(7, false);
826 double[][] x = new double[airdata[0].length][];
827 double[][] x2 = new double[airdata[0].length][];
828 double[] y = new double[airdata[0].length];
829 for (int i = 0; i < airdata[0].length; i++) {
830 x[i] = new double[4];
831 x2[i] = new double[7];
832 x[i][0] = 1.0;
833 x[i][1] = FastMath.log(airdata[3][i]);
834 x[i][2] = FastMath.log(airdata[4][i]);
835 x[i][3] = airdata[5][i];
836
837 x2[i][0] = x[i][0];
838 x2[i][1] = x[i][0];
839 x2[i][2] = x[i][1];
840 x2[i][3] = x[i][2];
841 x2[i][4] = x[i][1];
842 x2[i][5] = x[i][3];
843 x2[i][6] = x[i][2];
844
845 y[i] = FastMath.log(airdata[2][i]);
846 }
847
848 instance.addObservations(x, y);
849 RegressionResults result = instance.regress();
850 Assert.assertNotNull("Could not estimate initial regression", result);
851
852 instance2.addObservations(x2, y);
853 RegressionResults resultRedundant = instance2.regress();
854 Assert.assertNotNull("Could not estimate redundant regression", resultRedundant);
855 double[] beta = result.getParameterEstimates();
856 double[] betar = resultRedundant.getParameterEstimates();
857 double[] se = result.getStdErrorOfEstimates();
858 double[] ser = resultRedundant.getStdErrorOfEstimates();
859
860 if (FastMath.abs(beta[0] - betar[0]) > 1.0e-8) {
861 Assert.fail("Parameters not correct after reorder (0,3)");
862 }
863 if (FastMath.abs(beta[1] - betar[2]) > 1.0e-8) {
864 Assert.fail("Parameters not correct after reorder (1,2)");
865 }
866 if (FastMath.abs(beta[2] - betar[3]) > 1.0e-8) {
867 Assert.fail("Parameters not correct after reorder (2,1)");
868 }
869 if (FastMath.abs(beta[3] - betar[5]) > 1.0e-8) {
870 Assert.fail("Parameters not correct after reorder (3,0)");
871 }
872
873 if (FastMath.abs(se[0] - ser[0]) > 1.0e-8) {
874 Assert.fail("Se not correct after reorder (0,3)");
875 }
876 if (FastMath.abs(se[1] - ser[2]) > 1.0e-8) {
877 Assert.fail("Se not correct after reorder (1,2)");
878 }
879 if (FastMath.abs(se[2] - ser[3]) > 1.0e-8) {
880 Assert.fail("Se not correct after reorder (2,1)");
881 }
882 if (FastMath.abs(se[3] - ser[5]) > 1.0e-8) {
883 Assert.fail("Se not correct after reorder (3,0)");
884 }
885
886 if (FastMath.abs(result.getCovarianceOfParameters(0, 0)
887 - resultRedundant.getCovarianceOfParameters(0, 0)) > 1.0e-8) {
888 Assert.fail("VCV not correct after reorder (0,0)");
889 }
890 if (FastMath.abs(result.getCovarianceOfParameters(0, 1)
891 - resultRedundant.getCovarianceOfParameters(0, 2)) > 1.0e-8) {
892 Assert.fail("VCV not correct after reorder (0,1)<->(0,2)");
893 }
894 if (FastMath.abs(result.getCovarianceOfParameters(0, 2)
895 - resultRedundant.getCovarianceOfParameters(0, 3)) > 1.0e-8) {
896 Assert.fail("VCV not correct after reorder (0,2)<->(0,1)");
897 }
898 if (FastMath.abs(result.getCovarianceOfParameters(0, 3)
899 - resultRedundant.getCovarianceOfParameters(0, 5)) > 1.0e-8) {
900 Assert.fail("VCV not correct after reorder (0,3)<->(0,3)");
901 }
902 if (FastMath.abs(result.getCovarianceOfParameters(1, 0)
903 - resultRedundant.getCovarianceOfParameters(2, 0)) > 1.0e-8) {
904 Assert.fail("VCV not correct after reorder (1,0)<->(2,0)");
905 }
906 if (FastMath.abs(result.getCovarianceOfParameters(1, 1)
907 - resultRedundant.getCovarianceOfParameters(2, 2)) > 1.0e-8) {
908 Assert.fail("VCV not correct (1,1)<->(2,1)");
909 }
910 if (FastMath.abs(result.getCovarianceOfParameters(1, 2)
911 - resultRedundant.getCovarianceOfParameters(2, 3)) > 1.0e-8) {
912 Assert.fail("VCV not correct (1,2)<->(2,2)");
913 }
914
915 if (FastMath.abs(result.getCovarianceOfParameters(2, 0)
916 - resultRedundant.getCovarianceOfParameters(3, 0)) > 1.0e-8) {
917 Assert.fail("VCV not correct (2,0)<->(1,0)");
918 }
919 if (FastMath.abs(result.getCovarianceOfParameters(2, 1)
920 - resultRedundant.getCovarianceOfParameters(3, 2)) > 1.0e-8) {
921 Assert.fail("VCV not correct (2,1)<->(1,2)");
922 }
923
924 if (FastMath.abs(result.getCovarianceOfParameters(3, 3)
925 - resultRedundant.getCovarianceOfParameters(5, 5)) > 1.0e-8) {
926 Assert.fail("VCV not correct (3,3)<->(3,2)");
927 }
928
929 UnitTestUtils.assertEquals(result.getAdjustedRSquared(), resultRedundant.getAdjustedRSquared(), 1.0e-8);
930 UnitTestUtils.assertEquals(result.getErrorSumSquares(), resultRedundant.getErrorSumSquares(), 1.0e-8);
931 UnitTestUtils.assertEquals(result.getMeanSquareError(), resultRedundant.getMeanSquareError(), 1.0e-8);
932 UnitTestUtils.assertEquals(result.getRSquared(), resultRedundant.getRSquared(), 1.0e-8);
933 return;
934 }
935
936 @Test
937 public void testPCorr() {
938 MillerUpdatingRegression instance = new MillerUpdatingRegression(4, false);
939 double[][] x = new double[airdata[0].length][];
940 double[] y = new double[airdata[0].length];
941 double[] cp = new double[10];
942 double[] yxcorr = new double[4];
943 double[] diag = new double[4];
944 double sumysq = 0.0;
945 int off = 0;
946 for (int i = 0; i < airdata[0].length; i++) {
947 x[i] = new double[4];
948 x[i][0] = 1.0;
949 x[i][1] = FastMath.log(airdata[3][i]);
950 x[i][2] = FastMath.log(airdata[4][i]);
951 x[i][3] = airdata[5][i];
952 y[i] = FastMath.log(airdata[2][i]);
953 off = 0;
954 for (int j = 0; j < 4; j++) {
955 double tmp = x[i][j];
956 for (int k = 0; k <= j; k++, off++) {
957 cp[off] += tmp * x[i][k];
958 }
959 yxcorr[j] += tmp * y[i];
960 }
961 sumysq += y[i] * y[i];
962 }
963 PearsonsCorrelation pearson = new PearsonsCorrelation(x);
964 RealMatrix corr = pearson.getCorrelationMatrix();
965 off = 0;
966 for (int i = 0; i < 4; i++, off += (i + 1)) {
967 diag[i] = FastMath.sqrt(cp[off]);
968 }
969
970 instance.addObservations(x, y);
971 double[] pc = instance.getPartialCorrelations(0);
972 int idx = 0;
973 off = 0;
974 int off2 = 6;
975 for (int i = 0; i < 4; i++) {
976 for (int j = 0; j < i; j++) {
977 if (FastMath.abs(pc[idx] - cp[off] / (diag[i] * diag[j])) > 1.0e-8) {
978 Assert.fail("Failed cross products... i = " + i + " j = " + j);
979 }
980 ++idx;
981 ++off;
982 }
983 ++off;
984 if (FastMath.abs(pc[i+off2] - yxcorr[ i] / (FastMath.sqrt(sumysq) * diag[i])) > 1.0e-8) {
985 Assert.fail("Assert.failed cross product i = " + i + " y");
986 }
987 }
988 double[] pc2 = instance.getPartialCorrelations(1);
989
990 idx = 0;
991
992 for (int i = 1; i < 4; i++) {
993 for (int j = 1; j < i; j++) {
994 if (FastMath.abs(pc2[idx] - corr.getEntry(j, i)) > 1.0e-8) {
995 Assert.fail("Failed cross products... i = " + i + " j = " + j);
996 }
997 ++idx;
998 }
999 }
1000 double[] pc3 = instance.getPartialCorrelations(2);
1001 if (pc3 == null) {
1002 Assert.fail("Should not be null");
1003 }
1004 return;
1005 }
1006
1007 @Test
1008 public void testHdiag() {
1009 MillerUpdatingRegression instance = new MillerUpdatingRegression(4, false);
1010 double[][] x = new double[airdata[0].length][];
1011 double[] y = new double[airdata[0].length];
1012 for (int i = 0; i < airdata[0].length; i++) {
1013 x[i] = new double[4];
1014 x[i][0] = 1.0;
1015 x[i][1] = FastMath.log(airdata[3][i]);
1016 x[i][2] = FastMath.log(airdata[4][i]);
1017 x[i][3] = airdata[5][i];
1018 y[i] = FastMath.log(airdata[2][i]);
1019 }
1020 instance.addObservations(x, y);
1021 OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression();
1022 ols.setNoIntercept(true);
1023 ols.newSampleData(y, x);
1024
1025 RealMatrix rm = ols.calculateHat();
1026 for (int i = 0; i < x.length; i++) {
1027 UnitTestUtils.assertEquals(instance.getDiagonalOfHatMatrix(x[i]), rm.getEntry(i, i), 1.0e-8);
1028 }
1029 return;
1030 }
1031 @Test
1032 public void testHdiagConstant() {
1033 MillerUpdatingRegression instance = new MillerUpdatingRegression(3, true);
1034 double[][] x = new double[airdata[0].length][];
1035 double[] y = new double[airdata[0].length];
1036 for (int i = 0; i < airdata[0].length; i++) {
1037 x[i] = new double[3];
1038 x[i][0] = FastMath.log(airdata[3][i]);
1039 x[i][1] = FastMath.log(airdata[4][i]);
1040 x[i][2] = airdata[5][i];
1041 y[i] = FastMath.log(airdata[2][i]);
1042 }
1043 instance.addObservations(x, y);
1044 OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression();
1045 ols.setNoIntercept(false);
1046 ols.newSampleData(y, x);
1047
1048 RealMatrix rm = ols.calculateHat();
1049 for (int i = 0; i < x.length; i++) {
1050 UnitTestUtils.assertEquals(instance.getDiagonalOfHatMatrix(x[i]), rm.getEntry(i, i), 1.0e-8);
1051 }
1052 return;
1053 }
1054
1055
1056 @Test
1057 public void testSubsetRegression() {
1058
1059 MillerUpdatingRegression instance = new MillerUpdatingRegression(3, true);
1060 MillerUpdatingRegression redRegression = new MillerUpdatingRegression(2, true);
1061 double[][] x = new double[airdata[0].length][];
1062 double[][] xReduced = new double[airdata[0].length][];
1063 double[] y = new double[airdata[0].length];
1064 for (int i = 0; i < airdata[0].length; i++) {
1065 x[i] = new double[3];
1066 x[i][0] = FastMath.log(airdata[3][i]);
1067 x[i][1] = FastMath.log(airdata[4][i]);
1068 x[i][2] = airdata[5][i];
1069
1070 xReduced[i] = new double[2];
1071 xReduced[i][0] = FastMath.log(airdata[3][i]);
1072 xReduced[i][1] = FastMath.log(airdata[4][i]);
1073
1074 y[i] = FastMath.log(airdata[2][i]);
1075 }
1076
1077 instance.addObservations(x, y);
1078 redRegression.addObservations(xReduced, y);
1079
1080 RegressionResults resultsInstance = instance.regress( new int[]{0,1,2} );
1081 RegressionResults resultsReduced = redRegression.regress();
1082
1083 UnitTestUtils.assertEquals(resultsInstance.getParameterEstimates(), resultsReduced.getParameterEstimates(), 1.0e-12);
1084 UnitTestUtils.assertEquals(resultsInstance.getStdErrorOfEstimates(), resultsReduced.getStdErrorOfEstimates(), 1.0e-12);
1085 }
1086
1087
1088 }