This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git
The following commit(s) were added to refs/heads/master by this push:
new 700d22442 MATH-1698: Avoid boxing in KendallsCorrelation and document
signed zeros (#357)
700d22442 is described below
commit 700d22442c38652042cb1d28a9e17bf758d12406
Author: insaf021 <[email protected]>
AuthorDate: Fri Nov 20 03:39:33 2026 +0530
MATH-1698: Avoid boxing in KendallsCorrelation and document signed zeros
(#357)
Store the observation pairs as double[] and order them with
Double.compare instead of boxing into Pair<Double, Double>.
Double.compare matches the previous compareTo/equals semantics, so
results are unchanged.
Document that values are ordered by Double.compare, so -0.0 and 0.0
are distinct and not counted as a tie, and add a test for it.
---
.../stat/correlation/KendallsCorrelation.java | 40 ++++++++++------------
.../stat/correlation/KendallsCorrelationTest.java | 10 ++++++
2 files changed, 28 insertions(+), 22 deletions(-)
diff --git
a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/stat/correlation/KendallsCorrelation.java
b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/stat/correlation/KendallsCorrelation.java
index 862949336..c8c3c74a4 100644
---
a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/stat/correlation/KendallsCorrelation.java
+++
b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/stat/correlation/KendallsCorrelation.java
@@ -17,14 +17,12 @@
package org.apache.commons.math4.legacy.stat.correlation;
import java.util.Arrays;
-import java.util.Comparator;
import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
import org.apache.commons.math4.legacy.linear.BlockRealMatrix;
import org.apache.commons.math4.legacy.linear.MatrixUtils;
import org.apache.commons.math4.legacy.linear.RealMatrix;
import org.apache.commons.math4.core.jdkmath.JdkMath;
-import org.apache.commons.math4.legacy.core.Pair;
/**
* Implementation of Kendall's Tau-b rank correlation.
@@ -39,6 +37,10 @@ import org.apache.commons.math4.legacy.core.Pair;
* or y<sub>1</sub> = y<sub>2</sub>, the pair is neither concordant nor
* discordant.
* <p>
+ * Values are ordered using {@link Double#compare(double, double)}. Signed
+ * zeros are therefore distinct: {@code -0.0} is less than {@code 0.0} and a
+ * pair with these values is not counted as a tie.
+ * <p>
* Kendall's Tau-b is defined as:
* <div style="white-space: pre"><code>
* tau<sub>b</sub> = (n<sub>c</sub> - n<sub>d</sub>) / sqrt((n<sub>0</sub> -
n<sub>1</sub>) * (n<sub>0</sub> - n<sub>2</sub>))
@@ -161,31 +163,26 @@ public class KendallsCorrelation {
final int n = xArray.length;
final long numPairs = sum(n - 1);
- @SuppressWarnings("unchecked")
- Pair<Double, Double>[] pairs = new Pair[n];
+ double[][] pairs = new double[n][];
for (int i = 0; i < n; i++) {
- pairs[i] = new Pair<>(xArray[i], yArray[i]);
+ pairs[i] = new double[] {xArray[i], yArray[i]};
}
- Arrays.sort(pairs, new Comparator<Pair<Double, Double>>() {
- /** {@inheritDoc} */
- @Override
- public int compare(Pair<Double, Double> pair1, Pair<Double,
Double> pair2) {
- int compareFirst =
pair1.getFirst().compareTo(pair2.getFirst());
- return compareFirst != 0 ? compareFirst :
pair1.getSecond().compareTo(pair2.getSecond());
- }
+ Arrays.sort(pairs, (pair1, pair2) -> {
+ final int compareFirst = Double.compare(pair1[0], pair2[0]);
+ return compareFirst != 0 ? compareFirst : Double.compare(pair1[1],
pair2[1]);
});
long tiedXPairs = 0;
long tiedXYPairs = 0;
long consecutiveXTies = 1;
long consecutiveXYTies = 1;
- Pair<Double, Double> prev = pairs[0];
+ double[] prev = pairs[0];
for (int i = 1; i < n; i++) {
- final Pair<Double, Double> curr = pairs[i];
- if (curr.getFirst().equals(prev.getFirst())) {
+ final double[] curr = pairs[i];
+ if (Double.compare(curr[0], prev[0]) == 0) {
consecutiveXTies++;
- if (curr.getSecond().equals(prev.getSecond())) {
+ if (Double.compare(curr[1], prev[1]) == 0) {
consecutiveXYTies++;
} else {
tiedXYPairs += sum(consecutiveXYTies - 1);
@@ -203,8 +200,7 @@ public class KendallsCorrelation {
tiedXYPairs += sum(consecutiveXYTies - 1);
long swaps = 0;
- @SuppressWarnings("unchecked")
- Pair<Double, Double>[] pairsDestination = new Pair[n];
+ double[][] pairsDestination = new double[n][];
for (int segmentSize = 1; segmentSize < n; segmentSize <<= 1) {
for (int offset = 0; offset < n; offset += 2 * segmentSize) {
int i = offset;
@@ -216,7 +212,7 @@ public class KendallsCorrelation {
while (i < iEnd || j < jEnd) {
if (i < iEnd) {
if (j < jEnd) {
- if
(pairs[i].getSecond().compareTo(pairs[j].getSecond()) <= 0) {
+ if (Double.compare(pairs[i][1], pairs[j][1]) <= 0)
{
pairsDestination[copyLocation] = pairs[i];
i++;
} else {
@@ -235,7 +231,7 @@ public class KendallsCorrelation {
copyLocation++;
}
}
- final Pair<Double, Double>[] pairsTemp = pairs;
+ final double[][] pairsTemp = pairs;
pairs = pairsDestination;
pairsDestination = pairsTemp;
}
@@ -244,8 +240,8 @@ public class KendallsCorrelation {
long consecutiveYTies = 1;
prev = pairs[0];
for (int i = 1; i < n; i++) {
- final Pair<Double, Double> curr = pairs[i];
- if (curr.getSecond().equals(prev.getSecond())) {
+ final double[] curr = pairs[i];
+ if (Double.compare(curr[1], prev[1]) == 0) {
consecutiveYTies++;
} else {
tiedYPairs += sum(consecutiveYTies - 1);
diff --git
a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/correlation/KendallsCorrelationTest.java
b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/correlation/KendallsCorrelationTest.java
index 722ffdc45..b666fd293 100644
---
a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/correlation/KendallsCorrelationTest.java
+++
b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/correlation/KendallsCorrelationTest.java
@@ -216,6 +216,16 @@ public class KendallsCorrelationTest extends
PearsonsCorrelationTest {
Assert.assertEquals(-1.0, correlation.correlation(xArray, yArray),
Double.MIN_VALUE);
}
+ @Test
+ public void testSignedZeros() {
+ // -0.0 is ordered below 0.0 and the two are not a tie
+ final double[] xArray = new double[] {0.0, -0.0, 1.0};
+ final double[] yArray = new double[] {1.0, 2.0, 3.0};
+ Assert.assertEquals(1.0 / 3.0, correlation.correlation(xArray,
yArray), 1e-15);
+ Assert.assertEquals(1.0 / 3.0, correlation.correlation(yArray,
xArray), 1e-15);
+ Assert.assertEquals(-1.0, correlation.correlation(new double[] {0.0,
-0.0}, new double[] {1.0, 2.0}), 0);
+ }
+
@Test
public void test2dDoubleArray() {
final double[][] input = new double[][] {