From 5cfd99f0dcd0d10689e480f0854e859079f125d0 Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Sun, 28 Jun 2015 11:58:19 +0200 Subject: [PATCH] [MATH-1240] Fix calculation of ksSum in KolmogorovSmirnovTest for zero input. --- src/changes/changes.xml | 5 +++++ .../stat/inference/KolmogorovSmirnovTest.java | 2 +- .../inference/KolmogorovSmirnovTestTest.java | 20 +++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index ad6f88a54..e201c3b4e 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -54,6 +54,11 @@ If the output is not quite correct, check for invisible trailing spaces! + + "KolmogorovSmirnovTest#ksSum(...)" returned wrong result in case the provided + t-parameters was zero. This affected the calculation of "approximateP(...)" for + identical samples. + Improved performance to calculate the two-sample Kolmogorov-Smirnov test via monte carlo simulation ("KolmogorovSmirnovTets#monteCarloP(...)"). diff --git a/src/main/java/org/apache/commons/math4/stat/inference/KolmogorovSmirnovTest.java b/src/main/java/org/apache/commons/math4/stat/inference/KolmogorovSmirnovTest.java index 4e52cd672..f2ccb5cc9 100644 --- a/src/main/java/org/apache/commons/math4/stat/inference/KolmogorovSmirnovTest.java +++ b/src/main/java/org/apache/commons/math4/stat/inference/KolmogorovSmirnovTest.java @@ -832,7 +832,7 @@ public class KolmogorovSmirnovTest { */ public double ksSum(double t, double tolerance, int maxIterations) { if (t == 0.0) { - return 1.0; + return 0.0; } // TODO: for small t (say less than 1), the alternative expansion in part 3 of [1] diff --git a/src/test/java/org/apache/commons/math4/stat/inference/KolmogorovSmirnovTestTest.java b/src/test/java/org/apache/commons/math4/stat/inference/KolmogorovSmirnovTestTest.java index f18640b5d..e369c2a84 100644 --- a/src/test/java/org/apache/commons/math4/stat/inference/KolmogorovSmirnovTestTest.java +++ b/src/test/java/org/apache/commons/math4/stat/inference/KolmogorovSmirnovTestTest.java @@ -400,11 +400,31 @@ public class KolmogorovSmirnovTestTest { @Test public void testTwoSamplesAllEqual() { + int iterations = 10_000; final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(); for (int i = 2; i < 30; ++i) { + // testing values with ties double[] values = new double[i]; Arrays.fill(values, i); + // testing values without ties + double[] ascendingValues = new double[i]; + for (int j = 0; j < ascendingValues.length; j++) { + ascendingValues[j] = j; + } + Assert.assertEquals(0., test.kolmogorovSmirnovStatistic(values, values), 0.); + Assert.assertEquals(0., test.kolmogorovSmirnovStatistic(ascendingValues, ascendingValues), 0.); + + if (i < 10) { + Assert.assertEquals(1.0, test.exactP(0, values.length, values.length, true), 0.); + Assert.assertEquals(1.0, test.exactP(0, values.length, values.length, false), 0.); + } + + Assert.assertEquals(1.0, test.monteCarloP(0, values.length, values.length, true, iterations), 0.); + Assert.assertEquals(1.0, test.monteCarloP(0, values.length, values.length, false, iterations), 0.); + + Assert.assertEquals(1.0, test.approximateP(0, values.length, values.length), 0.); + Assert.assertEquals(1.0, test.approximateP(0, values.length, values.length), 0.); } }