Repository: commons-math Updated Branches: refs/heads/MATH_3_X c1c1a8429 -> 81ce1b183 refs/heads/master 6fe2094e3 -> fb0078159
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/fb007815 Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/fb007815 Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/fb007815 Branch: refs/heads/master Commit: fb0078159d2463da149de54018fca79a9447153e Parents: 6fe2094 Author: Otmar Ertl <[email protected]> Authored: Sun Sep 20 10:03:29 2015 +0200 Committer: Otmar Ertl <[email protected]> Committed: Sun Sep 20 10:04:44 2015 +0200 ---------------------------------------------------------------------- src/changes/changes.xml | 3 +++ .../stat/correlation/KendallsCorrelation.java | 2 +- .../correlation/KendallsCorrelationTest.java | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-math/blob/fb007815/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 27a5ae1..b1284e1 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -54,6 +54,9 @@ If the output is not quite correct, check for invisible trailing spaces! </release> <release version="4.0" date="XXXX-XX-XX" description=""> + <action dev="oertl" type="fix" issue="MATH-1277" due-to="Marc Rosen"> <!-- backported to 3.6 --> + Fixed incorrect Kendall's tau coefficient calculation due to internal integer overflow. + </action> <action dev="oertl" type="update" issue="MATH-1274"> <!-- backported to 3.6 --> Representation of Kolmogorov-Smirnov statistic as integral value. </action> http://git-wip-us.apache.org/repos/asf/commons-math/blob/fb007815/src/main/java/org/apache/commons/math4/stat/correlation/KendallsCorrelation.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/stat/correlation/KendallsCorrelation.java b/src/main/java/org/apache/commons/math4/stat/correlation/KendallsCorrelation.java index 77b7d22..125083e 100644 --- a/src/main/java/org/apache/commons/math4/stat/correlation/KendallsCorrelation.java +++ b/src/main/java/org/apache/commons/math4/stat/correlation/KendallsCorrelation.java @@ -201,7 +201,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/fb007815/src/test/java/org/apache/commons/math4/stat/correlation/KendallsCorrelationTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/stat/correlation/KendallsCorrelationTest.java b/src/test/java/org/apache/commons/math4/stat/correlation/KendallsCorrelationTest.java index ffc9cbb..a3f312c 100644 --- a/src/test/java/org/apache/commons/math4/stat/correlation/KendallsCorrelationTest.java +++ b/src/test/java/org/apache/commons/math4/stat/correlation/KendallsCorrelationTest.java @@ -21,6 +21,8 @@ import java.util.Arrays; import org.apache.commons.math4.TestUtils; import org.apache.commons.math4.linear.BlockRealMatrix; import org.apache.commons.math4.linear.RealMatrix; +import org.apache.commons.math4.random.RandomGenerator; +import org.apache.commons.math4.random.Well1024a; import org.apache.commons.math4.stat.correlation.KendallsCorrelation; import org.junit.Assert; import org.junit.Before; @@ -259,4 +261,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 a 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); + } }
