Author: tn
Date: Sun Dec 1 19:17:22 2013
New Revision: 1546840
URL: http://svn.apache.org/r1546840
Log:
[MATH-1068] Avoid overflow in Kendalls correlation for large input arrays.
Modified:
commons/proper/math/trunk/src/changes/changes.xml
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/correlation/KendallsCorrelation.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/correlation/KendallsCorrelationTest.java
Modified: commons/proper/math/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/changes/changes.xml?rev=1546840&r1=1546839&r2=1546840&view=diff
==============================================================================
--- commons/proper/math/trunk/src/changes/changes.xml (original)
+++ commons/proper/math/trunk/src/changes/changes.xml Sun Dec 1 19:17:22 2013
@@ -51,6 +51,9 @@ If the output is not quite correct, chec
</properties>
<body>
<release version="3.3" date="TBD" description="TBD">
+ <action dev="tn" type="fix" issue="MATH-1068" due-to="Gal Lalouche">
+ Avoid overflow when calculating Kendall's correlation for large arrays.
+ </action>
<action dev="erans" type="fix" issue="MATH-1067" due-to="Florian Erhard">
Avoid infinite recursion in "Beta.regularizedBeta" (package
"o.a.c.m.special");
</action>
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/correlation/KendallsCorrelation.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/correlation/KendallsCorrelation.java?rev=1546840&r1=1546839&r2=1546840&view=diff
==============================================================================
---
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/correlation/KendallsCorrelation.java
(original)
+++
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/correlation/KendallsCorrelation.java
Sun Dec 1 19:17:22 2013
@@ -160,7 +160,7 @@ public class KendallsCorrelation {
}
final int n = xArray.length;
- final int numPairs = n * (n - 1) / 2;
+ final long numPairs = n * (n - 1l) / 2l;
@SuppressWarnings("unchecked")
Pair<Double, Double>[] pairs = new Pair[n];
@@ -254,7 +254,8 @@ public class KendallsCorrelation {
}
tiedYPairs += consecutiveYTies * (consecutiveYTies - 1) / 2;
- int concordantMinusDiscordant = numPairs - tiedXPairs - tiedYPairs +
tiedXYPairs - 2 * swaps;
- return concordantMinusDiscordant / FastMath.sqrt((numPairs -
tiedXPairs) * (numPairs - tiedYPairs));
+ final long concordantMinusDiscordant = numPairs - tiedXPairs -
tiedYPairs + tiedXYPairs - 2 * swaps;
+ final double nonTiedPairsMultiplied = (numPairs - tiedXPairs) *
(double) (numPairs - tiedYPairs);
+ return concordantMinusDiscordant /
FastMath.sqrt(nonTiedPairsMultiplied);
}
}
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/correlation/KendallsCorrelationTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/correlation/KendallsCorrelationTest.java?rev=1546840&r1=1546839&r2=1546840&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/correlation/KendallsCorrelationTest.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/correlation/KendallsCorrelationTest.java
Sun Dec 1 19:17:22 2013
@@ -16,6 +16,8 @@
*/
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;
@@ -248,4 +250,12 @@ public class KendallsCorrelationTest ext
new BlockRealMatrix(expected));
}
+ @Test
+ public void testLargeArray() {
+ // test integer overflow detected in MATH-1068
+ double[] xArray = new double[100000];
+ Arrays.fill(xArray, 0, 2500, 1.0);
+
+ Assert.assertEquals(1.0, correlation.correlation(xArray, xArray),
1e-6);
+ }
}