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