MATH-1453: mannWhitneyU return minimum value for U

This commit is contained in:
Amar Prakash Pandey 2021-05-26 02:03:47 +05:30 committed by Gilles Sadowski
parent d92a3c6335
commit f29d5c97d3
2 changed files with 7 additions and 13 deletions

View File

@ -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);
}
}

View File

@ -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);
}