commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From oe...@apache.org
Subject [2/2] [math] MATH-1277: Fixed incorrect Kendall's tau coefficient calculation due to internal integer overflow. Thanks to Marc Rosen.
Date Sun, 20 Sep 2015 08:12:59 GMT
MATH-1277: Fixed incorrect Kendall's tau coefficient calculation due to
internal integer overflow. Thanks to Marc Rosen.

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

Branch: refs/heads/MATH_3_X
Commit: 81ce1b183aa4fb56e7710ebe0274740c268118fa
Parents: c1c1a84
Author: Otmar Ertl <otmar.ertl@gmail.com>
Authored: Sun Sep 20 10:09:46 2015 +0200
Committer: Otmar Ertl <otmar.ertl@gmail.com>
Committed: Sun Sep 20 10:09:46 2015 +0200

----------------------------------------------------------------------
 src/changes/changes.xml                         |  3 +++
 .../stat/correlation/KendallsCorrelation.java   |  2 +-
 .../correlation/KendallsCorrelationTest.java    | 20 +++++++++++++++++++-
 3 files changed, 23 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/81ce1b18/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 7076a97..61240bc 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -51,6 +51,9 @@ 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="oertl" type="fix" issue="MATH-1277" due-to="Marc Rosen">
+        Fixed incorrect Kendall's tau coefficient calculation due to internal integer overflow.
+      </action>
       <action dev="oertl" type="update" issue="MATH-1274">
         Representation of Kolmogorov-Smirnov statistic as integral value.
       </action>

http://git-wip-us.apache.org/repos/asf/commons-math/blob/81ce1b18/src/main/java/org/apache/commons/math3/stat/correlation/KendallsCorrelation.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/stat/correlation/KendallsCorrelation.java
b/src/main/java/org/apache/commons/math3/stat/correlation/KendallsCorrelation.java
index 5d66c5e..42f4094 100644
--- a/src/main/java/org/apache/commons/math3/stat/correlation/KendallsCorrelation.java
+++ b/src/main/java/org/apache/commons/math3/stat/correlation/KendallsCorrelation.java
@@ -200,7 +200,7 @@ public class KendallsCorrelation {
         tiedXPairs += sum(consecutiveXTies - 1);
         tiedXYPairs += sum(consecutiveXYTies - 1);
 
-        int swaps = 0;
+        long swaps = 0;
         @SuppressWarnings("unchecked")
         Pair<Double, Double>[] pairsDestination = new Pair[n];
         for (int segmentSize = 1; segmentSize < n; segmentSize <<= 1) {

http://git-wip-us.apache.org/repos/asf/commons-math/blob/81ce1b18/src/test/java/org/apache/commons/math3/stat/correlation/KendallsCorrelationTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math3/stat/correlation/KendallsCorrelationTest.java
b/src/test/java/org/apache/commons/math3/stat/correlation/KendallsCorrelationTest.java
index 85723e7..7453a57 100644
--- a/src/test/java/org/apache/commons/math3/stat/correlation/KendallsCorrelationTest.java
+++ b/src/test/java/org/apache/commons/math3/stat/correlation/KendallsCorrelationTest.java
@@ -17,10 +17,11 @@
 package org.apache.commons.math3.stat.correlation;
 
 import java.util.Arrays;
-
 import org.apache.commons.math3.TestUtils;
 import org.apache.commons.math3.linear.BlockRealMatrix;
 import org.apache.commons.math3.linear.RealMatrix;
+import org.apache.commons.math3.random.RandomGenerator;
+import org.apache.commons.math3.random.Well1024a;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
@@ -258,4 +259,21 @@ public class KendallsCorrelationTest extends PearsonsCorrelationTest
{
 
         Assert.assertEquals(1.0, correlation.correlation(xArray, xArray), 1e-6);
     }
+
+    @Test
+    public void testMath1277() {
+        // example that led to a correlation coefficient outside of [-1, 1]
+        // due to the bug reported in MATH-1277
+        RandomGenerator rng = new Well1024a(0);
+        double[] xArray = new double[120000];
+        double[] yArray = new double[120000];
+        for (int i = 0; i < xArray.length; ++i) {
+            xArray[i] =  rng.nextDouble();
+        }
+        for (int i = 0; i < yArray.length; ++i) {
+            yArray[i] =  rng.nextDouble();
+        }
+        double coefficient = correlation.correlation(xArray, yArray);
+        Assert.assertTrue(1.0 >= coefficient && -1.0 <= coefficient);
+    }
 }


Mime
View raw message