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.inference;
23
24 import org.hipparchus.distribution.continuous.NormalDistribution;
25 import org.hipparchus.exception.MathIllegalArgumentException;
26 import org.hipparchus.exception.NullArgumentException;
27 import org.hipparchus.stat.descriptive.StreamingStatistics;
28 import org.hipparchus.util.FastMath;
29 import org.junit.jupiter.api.Test;
30
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import static org.junit.jupiter.api.Assertions.assertEquals;
35 import static org.junit.jupiter.api.Assertions.assertFalse;
36 import static org.junit.jupiter.api.Assertions.assertTrue;
37 import static org.junit.jupiter.api.Assertions.fail;
38
39
40
41
42
43 class InferenceTestUtilsTest {
44
45 private double[] classA = { 93.0, 103.0, 95.0, 101.0 };
46 private double[] classB = { 99.0, 92.0, 102.0, 100.0, 102.0 };
47 private double[] classC = { 110.0, 115.0, 111.0, 117.0, 128.0 };
48
49 private List<double[]> classes = new ArrayList<double[]>();
50 private OneWayAnova oneWayAnova = new OneWayAnova();
51
52
53 @Test
54 void testChiSquare() {
55
56
57
58
59
60
61 long[] observed = {10, 9, 11};
62 double[] expected = {10, 10, 10};
63 assertEquals(0.2,
64 InferenceTestUtils.chiSquare(expected, observed), 10E-12, "chi-square statistic");
65 assertEquals(0.904837418036,
66 InferenceTestUtils.chiSquareTest(expected, observed), 1E-10, "chi-square p-value");
67
68 long[] observed1 = { 500, 623, 72, 70, 31 };
69 double[] expected1 = { 485, 541, 82, 61, 37 };
70 assertEquals(9.023307936427388,
71 InferenceTestUtils.chiSquare(expected1, observed1), 1E-10, "chi-square test statistic");
72 assertEquals(0.06051952647453607,
73 InferenceTestUtils.chiSquareTest(expected1, observed1), 1E-9, "chi-square p-value");
74 assertTrue(InferenceTestUtils.chiSquareTest(expected1, observed1, 0.07),
75 "chi-square test reject");
76 assertFalse(InferenceTestUtils.chiSquareTest(expected1, observed1, 0.05), "chi-square test accept");
77
78 try {
79 InferenceTestUtils.chiSquareTest(expected1, observed1, 95);
80 fail("alpha out of range, MathIllegalArgumentException expected");
81 } catch (MathIllegalArgumentException ex) {
82
83 }
84
85 long[] tooShortObs = { 0 };
86 double[] tooShortEx = { 1 };
87 try {
88 InferenceTestUtils.chiSquare(tooShortEx, tooShortObs);
89 fail("arguments too short, MathIllegalArgumentException expected");
90 } catch (MathIllegalArgumentException ex) {
91
92 }
93
94
95 long[] unMatchedObs = { 0, 1, 2, 3 };
96 double[] unMatchedEx = { 1, 1, 2 };
97 try {
98 InferenceTestUtils.chiSquare(unMatchedEx, unMatchedObs);
99 fail("arrays have different lengths, MathIllegalArgumentException expected");
100 } catch (MathIllegalArgumentException ex) {
101
102 }
103
104
105 expected[0] = 0;
106 try {
107 InferenceTestUtils.chiSquareTest(expected, observed, .01);
108 fail("bad expected count, MathIllegalArgumentException expected");
109 } catch (MathIllegalArgumentException ex) {
110
111 }
112
113
114 expected[0] = 1;
115 observed[0] = -1;
116 try {
117 InferenceTestUtils.chiSquareTest(expected, observed, .01);
118 fail("bad expected count, MathIllegalArgumentException expected");
119 } catch (MathIllegalArgumentException ex) {
120
121 }
122
123 }
124
125 @Test
126 void testChiSquareIndependence() {
127
128
129
130 long[][] counts = { {40, 22, 43}, {91, 21, 28}, {60, 10, 22}};
131 assertEquals( 22.709027688, InferenceTestUtils.chiSquare(counts), 1E-9, "chi-square test statistic");
132 assertEquals(0.000144751460134, InferenceTestUtils.chiSquareTest(counts), 1E-9, "chi-square p-value");
133 assertTrue(InferenceTestUtils.chiSquareTest(counts, 0.0002), "chi-square test reject");
134 assertFalse(InferenceTestUtils.chiSquareTest(counts, 0.0001), "chi-square test accept");
135
136 long[][] counts2 = {{10, 15}, {30, 40}, {60, 90} };
137 assertEquals( 0.168965517241, InferenceTestUtils.chiSquare(counts2), 1E-9, "chi-square test statistic");
138 assertEquals(0.918987499852, InferenceTestUtils.chiSquareTest(counts2), 1E-9, "chi-square p-value");
139 assertFalse(InferenceTestUtils.chiSquareTest(counts2, 0.1), "chi-square test accept");
140
141
142 long[][] counts3 = { {40, 22, 43}, {91, 21, 28}, {60, 10}};
143 try {
144 InferenceTestUtils.chiSquare(counts3);
145 fail("Expecting MathIllegalArgumentException");
146 } catch (MathIllegalArgumentException ex) {
147
148 }
149
150
151 long[][] counts4 = {{40, 22, 43}};
152 try {
153 InferenceTestUtils.chiSquare(counts4);
154 fail("Expecting MathIllegalArgumentException");
155 } catch (MathIllegalArgumentException ex) {
156
157 }
158 long[][] counts5 = {{40}, {40}, {30}, {10}};
159 try {
160 InferenceTestUtils.chiSquare(counts5);
161 fail("Expecting MathIllegalArgumentException");
162 } catch (MathIllegalArgumentException ex) {
163
164 }
165
166
167 long[][] counts6 = {{10, -2}, {30, 40}, {60, 90} };
168 try {
169 InferenceTestUtils.chiSquare(counts6);
170 fail("Expecting MathIllegalArgumentException");
171 } catch (MathIllegalArgumentException ex) {
172
173 }
174
175
176 try {
177 InferenceTestUtils.chiSquareTest(counts, 0);
178 fail("Expecting MathIllegalArgumentException");
179 } catch (MathIllegalArgumentException ex) {
180
181 }
182 }
183
184 @Test
185 void testChiSquareLargeTestStatistic() {
186 double[] exp = new double[] {
187 3389119.5, 649136.6, 285745.4, 25357364.76,
188 11291189.78, 543628.0, 232921.0, 437665.75
189 };
190
191 long[] obs = new long[] { 2372383, 584222, 257170, 17750155, 7903832, 489265, 209628, 393899 };
192
193 ChiSquareTest csti = new ChiSquareTest();
194 double cst = csti.chiSquareTest(exp, obs);
195 assertEquals(0.0, cst, 1E-3, "chi-square p-value");
196 assertEquals(114875.90421929007,
197 InferenceTestUtils.chiSquare(exp, obs), 1E-9, "chi-square test statistic");
198 }
199
200
201 @Test
202 void testChiSquareZeroCount() {
203
204 long[][] counts = { {40, 0, 4}, {91, 1, 2}, {60, 2, 0}};
205 assertEquals( 9.67444662263, InferenceTestUtils.chiSquare(counts), 1E-9, "chi-square test statistic");
206 assertEquals(0.0462835770603, InferenceTestUtils.chiSquareTest(counts), 1E-9, "chi-square p-value");
207 }
208
209 private double[] tooShortObs = { 1.0 };
210 private double[] emptyObs = {};
211 private StreamingStatistics emptyStats = new StreamingStatistics();
212
213 @Test
214 void testOneSampleT() {
215 double[] observed = {
216 93.0, 103.0, 95.0, 101.0, 91.0, 105.0, 96.0, 94.0,
217 101.0, 88.0, 98.0, 94.0, 101.0, 92.0, 95.0
218 };
219 double mu = 100.0;
220 StreamingStatistics sampleStats = new StreamingStatistics();
221 for (int i = 0; i < observed.length; i++) {
222 sampleStats.addValue(observed[i]);
223 }
224
225
226 assertEquals(-2.81976445346, InferenceTestUtils.t(mu, observed), 10E-10, "t statistic");
227 assertEquals(-2.81976445346, InferenceTestUtils.t(mu, sampleStats), 10E-10, "t statistic");
228 assertEquals(0.0136390585873, InferenceTestUtils.tTest(mu, observed), 10E-10, "p value");
229 assertEquals(0.0136390585873, InferenceTestUtils.tTest(mu, sampleStats), 10E-10, "p value");
230
231 try {
232 InferenceTestUtils.t(mu, (double[]) null);
233 fail("arguments too short, NullArgumentException expected");
234 } catch (NullArgumentException ex) {
235
236 }
237
238 try {
239 InferenceTestUtils.t(mu, (StreamingStatistics) null);
240 fail("arguments too short, NullArgumentException expected");
241 } catch (NullArgumentException ex) {
242
243 }
244
245 try {
246 InferenceTestUtils.t(mu, emptyObs);
247 fail("arguments too short, MathIllegalArgumentException expected");
248 } catch (MathIllegalArgumentException ex) {
249
250 }
251
252 try {
253 InferenceTestUtils.t(mu, emptyStats);
254 fail("arguments too short, MathIllegalArgumentException expected");
255 } catch (MathIllegalArgumentException ex) {
256
257 }
258
259 try {
260 InferenceTestUtils.t(mu, tooShortObs);
261 fail("insufficient data to compute t statistic, MathIllegalArgumentException expected");
262 } catch (MathIllegalArgumentException ex) {
263
264 }
265 try {
266 InferenceTestUtils.tTest(mu, tooShortObs);
267 fail("insufficient data to perform t test, MathIllegalArgumentException expected");
268 } catch (MathIllegalArgumentException ex) {
269
270 }
271
272 try {
273 InferenceTestUtils.t(mu, (StreamingStatistics) null);
274 fail("insufficient data to compute t statistic, NullArgumentException expected");
275 } catch (NullArgumentException ex) {
276
277 }
278 try {
279 InferenceTestUtils.tTest(mu, (StreamingStatistics) null);
280 fail("insufficient data to perform t test, NullArgumentException expected");
281 } catch (NullArgumentException ex) {
282
283 }
284 }
285
286 @Test
287 void testOneSampleTTest() {
288 double[] oneSidedP = {
289 2d, 0d, 6d, 6d, 3d, 3d, 2d, 3d, -6d, 6d,
290 6d, 6d, 3d, 0d, 1d, 1d, 0d, 2d, 3d, 3d
291 };
292 StreamingStatistics oneSidedPStats = new StreamingStatistics();
293 for (int i = 0; i < oneSidedP.length; i++) {
294 oneSidedPStats.addValue(oneSidedP[i]);
295 }
296
297 assertEquals(3.86485535541, InferenceTestUtils.t(0d, oneSidedP), 10E-10, "one sample t stat");
298 assertEquals(3.86485535541, InferenceTestUtils.t(0d, oneSidedPStats),1E-10,"one sample t stat");
299 assertEquals(0.000521637019637, InferenceTestUtils.tTest(0d, oneSidedP) / 2d, 10E-10, "one sample p value");
300 assertEquals(0.000521637019637, InferenceTestUtils.tTest(0d, oneSidedPStats) / 2d, 10E-5, "one sample p value");
301 assertTrue(InferenceTestUtils.tTest(0d, oneSidedP, 0.01), "one sample t-test reject");
302 assertTrue(InferenceTestUtils.tTest(0d, oneSidedPStats, 0.01), "one sample t-test reject");
303 assertFalse(InferenceTestUtils.tTest(0d, oneSidedP, 0.0001), "one sample t-test accept");
304 assertFalse(InferenceTestUtils.tTest(0d, oneSidedPStats, 0.0001), "one sample t-test accept");
305
306 try {
307 InferenceTestUtils.tTest(0d, oneSidedP, 95);
308 fail("alpha out of range, MathIllegalArgumentException expected");
309 } catch (MathIllegalArgumentException ex) {
310
311 }
312
313 try {
314 InferenceTestUtils.tTest(0d, oneSidedPStats, 95);
315 fail("alpha out of range, MathIllegalArgumentException expected");
316 } catch (MathIllegalArgumentException ex) {
317
318 }
319
320 }
321
322 @Test
323 void testTwoSampleTHeterscedastic() {
324 double[] sample1 = { 7d, -4d, 18d, 17d, -3d, -5d, 1d, 10d, 11d, -2d };
325 double[] sample2 = { -1d, 12d, -1d, -3d, 3d, -5d, 5d, 2d, -11d, -1d, -3d };
326 StreamingStatistics sampleStats1 = new StreamingStatistics();
327 for (int i = 0; i < sample1.length; i++) {
328 sampleStats1.addValue(sample1[i]);
329 }
330 StreamingStatistics sampleStats2 = new StreamingStatistics();
331 for (int i = 0; i < sample2.length; i++) {
332 sampleStats2.addValue(sample2[i]);
333 }
334
335
336 assertEquals(1.60371728768,
337 InferenceTestUtils.t(sample1, sample2), 1E-10, "two sample heteroscedastic t stat");
338 assertEquals(1.60371728768,
339 InferenceTestUtils.t(sampleStats1, sampleStats2), 1E-10, "two sample heteroscedastic t stat");
340 assertEquals(0.128839369622,
341 InferenceTestUtils.tTest(sample1, sample2), 1E-10, "two sample heteroscedastic p value");
342 assertEquals(0.128839369622,
343 InferenceTestUtils.tTest(sampleStats1, sampleStats2), 1E-10, "two sample heteroscedastic p value");
344 assertTrue(InferenceTestUtils.tTest(sample1, sample2, 0.2),
345 "two sample heteroscedastic t-test reject");
346 assertTrue(InferenceTestUtils.tTest(sampleStats1, sampleStats2, 0.2),
347 "two sample heteroscedastic t-test reject");
348 assertFalse(InferenceTestUtils.tTest(sample1, sample2, 0.1), "two sample heteroscedastic t-test accept");
349 assertFalse(InferenceTestUtils.tTest(sampleStats1, sampleStats2, 0.1), "two sample heteroscedastic t-test accept");
350
351 try {
352 InferenceTestUtils.tTest(sample1, sample2, .95);
353 fail("alpha out of range, MathIllegalArgumentException expected");
354 } catch (MathIllegalArgumentException ex) {
355
356 }
357
358 try {
359 InferenceTestUtils.tTest(sampleStats1, sampleStats2, .95);
360 fail("alpha out of range, MathIllegalArgumentException expected");
361 } catch (MathIllegalArgumentException ex) {
362
363 }
364
365 try {
366 InferenceTestUtils.tTest(sample1, tooShortObs, .01);
367 fail("insufficient data, MathIllegalArgumentException expected");
368 } catch (MathIllegalArgumentException ex) {
369
370 }
371
372 try {
373 InferenceTestUtils.tTest(sampleStats1, (StreamingStatistics) null, .01);
374 fail("insufficient data, NullArgumentException expected");
375 } catch (NullArgumentException ex) {
376
377 }
378
379 try {
380 InferenceTestUtils.tTest(sample1, tooShortObs);
381 fail("insufficient data, MathIllegalArgumentException expected");
382 } catch (MathIllegalArgumentException ex) {
383
384 }
385
386 try {
387 InferenceTestUtils.tTest(sampleStats1, (StreamingStatistics) null);
388 fail("insufficient data, NullArgumentException expected");
389 } catch (NullArgumentException ex) {
390
391 }
392
393 try {
394 InferenceTestUtils.t(sample1, tooShortObs);
395 fail("insufficient data, MathIllegalArgumentException expected");
396 } catch (MathIllegalArgumentException ex) {
397
398 }
399
400 try {
401 InferenceTestUtils.t(sampleStats1, (StreamingStatistics) null);
402 fail("insufficient data, NullArgumentException expected");
403 } catch (NullArgumentException ex) {
404
405 }
406 }
407
408 @Test
409 void testTwoSampleTHomoscedastic() {
410 double[] sample1 ={2, 4, 6, 8, 10, 97};
411 double[] sample2 = {4, 6, 8, 10, 16};
412 StreamingStatistics sampleStats1 = new StreamingStatistics();
413 for (int i = 0; i < sample1.length; i++) {
414 sampleStats1.addValue(sample1[i]);
415 }
416 StreamingStatistics sampleStats2 = new StreamingStatistics();
417 for (int i = 0; i < sample2.length; i++) {
418 sampleStats2.addValue(sample2[i]);
419 }
420
421
422 assertEquals(0.73096310086,
423 InferenceTestUtils.homoscedasticT(sample1, sample2), 10E-11, "two sample homoscedastic t stat");
424 assertEquals(0.4833963785,
425 InferenceTestUtils.homoscedasticTTest(sampleStats1, sampleStats2), 1E-10, "two sample homoscedastic p value");
426 assertTrue(InferenceTestUtils.homoscedasticTTest(sample1, sample2, 0.49),
427 "two sample homoscedastic t-test reject");
428 assertFalse(InferenceTestUtils.homoscedasticTTest(sample1, sample2, 0.48), "two sample homoscedastic t-test accept");
429 }
430
431 @Test
432 void testSmallSamples() {
433 double[] sample1 = {1d, 3d};
434 double[] sample2 = {4d, 5d};
435
436
437 assertEquals(-2.2360679775, InferenceTestUtils.t(sample1, sample2), 1E-10);
438 assertEquals(0.198727388935, InferenceTestUtils.tTest(sample1, sample2), 1E-10);
439 }
440
441 @Test
442 void testPaired() {
443 double[] sample1 = {1d, 3d, 5d, 7d};
444 double[] sample2 = {0d, 6d, 11d, 2d};
445 double[] sample3 = {5d, 7d, 8d, 10d};
446
447
448 assertEquals(-0.3133, InferenceTestUtils.pairedT(sample1, sample2), 1E-4);
449 assertEquals(0.774544295819, InferenceTestUtils.pairedTTest(sample1, sample2), 1E-10);
450 assertEquals(0.001208, InferenceTestUtils.pairedTTest(sample1, sample3), 1E-6);
451 assertFalse(InferenceTestUtils.pairedTTest(sample1, sample3, .001));
452 assertTrue(InferenceTestUtils.pairedTTest(sample1, sample3, .002));
453 }
454
455 @Test
456 void testOneWayAnovaUtils() {
457 classes.add(classA);
458 classes.add(classB);
459 classes.add(classC);
460 assertEquals(oneWayAnova.anovaFValue(classes),
461 InferenceTestUtils.oneWayAnovaFValue(classes), 10E-12);
462 assertEquals(oneWayAnova.anovaPValue(classes),
463 InferenceTestUtils.oneWayAnovaPValue(classes), 10E-12);
464 assertEquals(oneWayAnova.anovaTest(classes, 0.01),
465 InferenceTestUtils.oneWayAnovaTest(classes, 0.01));
466 }
467
468 @Test
469 void testGTestGoodnesOfFit() throws Exception {
470 double[] exp = new double[] { 0.54d, 0.40d, 0.05d, 0.01d };
471 long[] obs = new long[] { 70, 79, 3, 4 };
472
473 assertEquals(13.144799, InferenceTestUtils.g(exp, obs), 1E-5, "G test statistic");
474 double p_gtgf = InferenceTestUtils.gTest(exp, obs);
475 assertEquals(0.004333, p_gtgf, 1E-5, "g-Test p-value");
476 assertTrue(InferenceTestUtils.gTest(exp, obs, 0.05));
477 }
478
479 @Test
480 void testGTestIndependance() throws Exception {
481 long[] obs1 = new long[] { 268, 199, 42 };
482 long[] obs2 = new long[] { 807, 759, 184 };
483
484 double g = InferenceTestUtils.gDataSetsComparison(obs1, obs2);
485
486 assertEquals(7.3008170, g, 1E-4, "G test statistic");
487 double p_gti = InferenceTestUtils.gTestDataSetsComparison(obs1, obs2);
488
489 assertEquals(0.0259805, p_gti, 1E-4, "g-Test p-value");
490 assertTrue(InferenceTestUtils.gTestDataSetsComparison(obs1, obs2, 0.05));
491 }
492
493 @Test
494 void testRootLogLikelihood() {
495
496 assertTrue(InferenceTestUtils.rootLogLikelihoodRatio(904, 21060, 1144, 283012) > 0.0);
497
498
499 assertTrue(InferenceTestUtils.rootLogLikelihoodRatio(36, 21928, 60280, 623876) < 0.0);
500
501 assertEquals(FastMath.sqrt(2.772589), InferenceTestUtils.rootLogLikelihoodRatio(1, 0, 0, 1), 0.000001);
502 assertEquals(-FastMath.sqrt(2.772589), InferenceTestUtils.rootLogLikelihoodRatio(0, 1, 1, 0), 0.000001);
503 assertEquals(FastMath.sqrt(27.72589), InferenceTestUtils.rootLogLikelihoodRatio(10, 0, 0, 10), 0.00001);
504
505 assertEquals(FastMath.sqrt(39.33052), InferenceTestUtils.rootLogLikelihoodRatio(5, 1995, 0, 100000), 0.00001);
506 assertEquals(-FastMath.sqrt(39.33052), InferenceTestUtils.rootLogLikelihoodRatio(0, 100000, 5, 1995), 0.00001);
507
508 assertEquals(FastMath.sqrt(4730.737), InferenceTestUtils.rootLogLikelihoodRatio(1000, 1995, 1000, 100000), 0.001);
509 assertEquals(-FastMath.sqrt(4730.737), InferenceTestUtils.rootLogLikelihoodRatio(1000, 100000, 1000, 1995), 0.001);
510
511 assertEquals(FastMath.sqrt(5734.343), InferenceTestUtils.rootLogLikelihoodRatio(1000, 1000, 1000, 100000), 0.001);
512 assertEquals(FastMath.sqrt(5714.932), InferenceTestUtils.rootLogLikelihoodRatio(1000, 1000, 1000, 99000), 0.001);
513 }
514
515 @Test
516 void testKSOneSample() throws Exception {
517 final NormalDistribution unitNormal = new NormalDistribution(0d, 1d);
518 final double[] sample = KolmogorovSmirnovTestTest.gaussian;
519 final double tol = KolmogorovSmirnovTestTest.TOLERANCE;
520 assertEquals(0.3172069207622391, InferenceTestUtils.kolmogorovSmirnovTest(unitNormal, sample), tol);
521 assertEquals(0.0932947561266756, InferenceTestUtils.kolmogorovSmirnovStatistic(unitNormal, sample), tol);
522 }
523
524 @Test
525 void testKSTwoSample() throws Exception {
526 final double tol = KolmogorovSmirnovTestTest.TOLERANCE;
527 final double[] smallSample1 = { 6, 7, 9, 13, 19, 21, 22, 23, 24 };
528 final double[] smallSample2 = { 10, 11, 12, 16, 20, 27, 28, 32, 44, 54 };
529
530 assertEquals(0.105577085453247,
531 InferenceTestUtils.kolmogorovSmirnovTest(smallSample1, smallSample2, false), tol);
532 final double d = InferenceTestUtils.kolmogorovSmirnovStatistic(smallSample1, smallSample2);
533 assertEquals(0.5, d, tol);
534 assertEquals(0.105577085453247,
535 InferenceTestUtils.exactP(d, smallSample1.length,smallSample2.length, false), tol);
536 }
537 }