MATH-1277: Fixed incorrect Kendall's tau coefficient calculation due to

internal integer overflow. Thanks to Marc Rosen.
This commit is contained in:
Otmar Ertl 2015-09-20 10:03:29 +02:00
parent 6fe2094e30
commit fb0078159d
3 changed files with 23 additions and 1 deletions

View File

@ -54,6 +54,9 @@ If the output is not quite correct, check for invisible trailing spaces!
</release> </release>
<release version="4.0" date="XXXX-XX-XX" description=""> <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 --> <action dev="oertl" type="update" issue="MATH-1274"> <!-- backported to 3.6 -->
Representation of Kolmogorov-Smirnov statistic as integral value. Representation of Kolmogorov-Smirnov statistic as integral value.
</action> </action>

View File

@ -201,7 +201,7 @@ public class KendallsCorrelation {
tiedXPairs += sum(consecutiveXTies - 1); tiedXPairs += sum(consecutiveXTies - 1);
tiedXYPairs += sum(consecutiveXYTies - 1); tiedXYPairs += sum(consecutiveXYTies - 1);
int swaps = 0; long swaps = 0;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Pair<Double, Double>[] pairsDestination = new Pair[n]; Pair<Double, Double>[] pairsDestination = new Pair[n];
for (int segmentSize = 1; segmentSize < n; segmentSize <<= 1) { for (int segmentSize = 1; segmentSize < n; segmentSize <<= 1) {

View File

@ -21,6 +21,8 @@ import java.util.Arrays;
import org.apache.commons.math4.TestUtils; import org.apache.commons.math4.TestUtils;
import org.apache.commons.math4.linear.BlockRealMatrix; import org.apache.commons.math4.linear.BlockRealMatrix;
import org.apache.commons.math4.linear.RealMatrix; 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.apache.commons.math4.stat.correlation.KendallsCorrelation;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
@ -259,4 +261,21 @@ public class KendallsCorrelationTest extends PearsonsCorrelationTest {
Assert.assertEquals(1.0, correlation.correlation(xArray, xArray), 1e-6); 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);
}
} }