You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2015/06/28 11:55:42 UTC

[2/2] [math] [MATH-1240] Fix calculation of ksSum in KolmogorovSmirnovTest for zero input.

[MATH-1240] Fix calculation of ksSum in KolmogorovSmirnovTest for zero input.


Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/a0191624
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/a0191624
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/a0191624

Branch: refs/heads/MATH_3_X
Commit: a019162481161757555c99f3cbce157cc9b1a41b
Parents: 6209fab
Author: Thomas Neidhart <th...@gmail.com>
Authored: Sun Jun 28 11:55:27 2015 +0200
Committer: Thomas Neidhart <th...@gmail.com>
Committed: Sun Jun 28 11:55:27 2015 +0200

----------------------------------------------------------------------
 src/changes/changes.xml                         |  5 +++++
 .../stat/inference/KolmogorovSmirnovTest.java   |  2 +-
 .../inference/KolmogorovSmirnovTestTest.java    | 20 ++++++++++++++++++++
 3 files changed, 26 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/a0191624/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index f2803f8..5d47406 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -51,6 +51,11 @@ If the output is not quite correct, check for invisible trailing spaces!
   </properties>
   <body>
     <release version="3.6" date="XXXX-XX-XX" description="">
+      <action dev="tn" type="fix" issue="MATH-1240">
+        "KolmogorovSmirnovTest#ksSum(...)" returned wrong result in case the provided
+        t-parameters was zero. This affected the calculation of "approximateP(...)" for
+        identical samples.
+      </action>
       <action dev="tn" type="fix" issue="MATH-1242" due-to="Otmar Ertl">
         Improved performance to calculate the two-sample Kolmogorov-Smirnov test
         via monte carlo simulation ("KolmogorovSmirnovTets#monteCarloP(...)").

http://git-wip-us.apache.org/repos/asf/commons-math/blob/a0191624/src/main/java/org/apache/commons/math3/stat/inference/KolmogorovSmirnovTest.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/stat/inference/KolmogorovSmirnovTest.java b/src/main/java/org/apache/commons/math3/stat/inference/KolmogorovSmirnovTest.java
index d7b6f5c..e0f5c7d 100644
--- a/src/main/java/org/apache/commons/math3/stat/inference/KolmogorovSmirnovTest.java
+++ b/src/main/java/org/apache/commons/math3/stat/inference/KolmogorovSmirnovTest.java
@@ -833,7 +833,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]

http://git-wip-us.apache.org/repos/asf/commons-math/blob/a0191624/src/test/java/org/apache/commons/math3/stat/inference/KolmogorovSmirnovTestTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math3/stat/inference/KolmogorovSmirnovTestTest.java b/src/test/java/org/apache/commons/math3/stat/inference/KolmogorovSmirnovTestTest.java
index 2705876..3d90e31 100644
--- a/src/test/java/org/apache/commons/math3/stat/inference/KolmogorovSmirnovTestTest.java
+++ b/src/test/java/org/apache/commons/math3/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.);
         }
     }