Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5217317DF7 for ; Sun, 20 Sep 2015 08:12:59 +0000 (UTC) Received: (qmail 74286 invoked by uid 500); 20 Sep 2015 08:12:59 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 73988 invoked by uid 500); 20 Sep 2015 08:12:58 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 73972 invoked by uid 99); 20 Sep 2015 08:12:58 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 20 Sep 2015 08:12:58 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id A17A8DFE5F; Sun, 20 Sep 2015 08:12:58 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: oertl@apache.org To: commits@commons.apache.org Date: Sun, 20 Sep 2015 08:12:58 -0000 Message-Id: <135086e737f041c8bd76432718293562@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [1/2] [math] MATH-1277: Fixed incorrect Kendall's tau coefficient calculation due to internal integer overflow. Thanks to Marc Rosen. 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 Authored: Sun Sep 20 10:03:29 2015 +0200 Committer: Otmar Ertl 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! + + Fixed incorrect Kendall's tau coefficient calculation due to internal integer overflow. + Representation of Kolmogorov-Smirnov statistic as integral value. 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[] 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); + } }