This is an automated email from the ASF dual-hosted git repository.
erans pushed a commit to branch modularized_master
in repository https://gitbox.apache.org/repos/asf/commons-math.git
The following commit(s) were added to refs/heads/modularized_master by this
push:
new f29d5c9 MATH-1453: mannWhitneyU return minimum value for U
f29d5c9 is described below
commit f29d5c97d38153c1ac62a751343d217e112f4af1
Author: Amar Prakash Pandey <[email protected]>
AuthorDate: Wed May 26 02:03:47 2021 +0530
MATH-1453: mannWhitneyU return minimum value for U
---
.../math4/legacy/stat/inference/MannWhitneyUTest.java | 18 ++++++------------
.../legacy/stat/inference/MannWhitneyUTestTest.java | 2 +-
2 files changed, 7 insertions(+), 13 deletions(-)
diff --git
a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/stat/inference/MannWhitneyUTest.java
b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/stat/inference/MannWhitneyUTest.java
index 760b5d0..53a93dc 100644
---
a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/stat/inference/MannWhitneyUTest.java
+++
b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/stat/inference/MannWhitneyUTest.java
@@ -26,6 +26,8 @@ import
org.apache.commons.math4.legacy.stat.ranking.NaturalRanking;
import org.apache.commons.math4.legacy.stat.ranking.TiesStrategy;
import org.apache.commons.math4.legacy.util.FastMath;
+import java.util.stream.IntStream;
+
/**
* An implementation of the Mann-Whitney U test (also called Wilcoxon rank-sum
test).
*
@@ -117,7 +119,7 @@ public class MannWhitneyUTest {
*
* @param x the first sample
* @param y the second sample
- * @return Mann-Whitney U statistic (maximum of U<sup>x</sup> and
U<sup>y</sup>)
+ * @return Mann-Whitney U statistic (minimum of U<sup>x</sup> and
U<sup>y</sup>)
* @throws NullArgumentException if {@code x} or {@code y} are {@code
null}.
* @throws NoDataException if {@code x} or {@code y} are zero-length.
*/
@@ -135,9 +137,7 @@ public class MannWhitneyUTest {
* The ranks for x is in the first x.length entries in ranks because x
* is in the first x.length entries in z
*/
- for (int i = 0; i < x.length; ++i) {
- sumRankX += ranks[i];
- }
+ sumRankX = IntStream.range(0, x.length).mapToDouble(i ->
ranks[i]).sum();
/*
* U1 = R1 - (n1 * (n1 + 1)) / 2 where R1 is sum of ranks for sample 1,
@@ -150,7 +150,7 @@ public class MannWhitneyUTest {
*/
final double U2 = (long) x.length * y.length - U1;
- return FastMath.max(U1, U2);
+ return FastMath.min(U1, U2);
}
/**
@@ -223,14 +223,8 @@ public class MannWhitneyUTest {
ensureDataConformance(x, y);
- final double Umax = mannWhitneyU(x, y);
-
- /*
- * It can be shown that U1 + U2 = n1 * n2
- */
- final double Umin = (long) x.length * y.length - Umax;
+ final double Umin = mannWhitneyU(x, y);
return calculateAsymptoticPValue(Umin, x.length, y.length);
}
-
}
diff --git
a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/inference/MannWhitneyUTestTest.java
b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/inference/MannWhitneyUTestTest.java
index c8469e6..1720611 100644
---
a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/inference/MannWhitneyUTestTest.java
+++
b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/inference/MannWhitneyUTestTest.java
@@ -42,7 +42,7 @@ public class MannWhitneyUTestTest {
final double x[] = {19, 22, 16, 29, 24};
final double y[] = {20, 11, 17, 12};
- Assert.assertEquals(17, testStatistic.mannWhitneyU(x, y), 1e-10);
+ Assert.assertEquals(3, testStatistic.mannWhitneyU(x, y), 1e-10);
Assert.assertEquals(0.08641, testStatistic.mannWhitneyUTest(x, y),
1e-5);
}