in commons-statistics-inference/src/main/java/org/apache/commons/statistics/inference/MannWhitneyUTest.java [344:377]
private double calculateAsymptoticPValue(double u, int n1, int n2, double c) {
// Use long to avoid overflow
final long n1n2 = (long) n1 * n2;
final long n = (long) n1 + n2;
// https://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U_test#Normal_approximation_and_tie_correction
final double e = n1n2 * 0.5;
final double variance = (n1n2 / 12.0) * ((n + 1.0) - c / n / (n - 1));
double z = u - e;
if (continuityCorrection) {
// +/- 0.5 is a continuity correction towards the expected.
if (alternative == AlternativeHypothesis.GREATER_THAN) {
z -= 0.5;
} else if (alternative == AlternativeHypothesis.LESS_THAN) {
z += 0.5;
} else {
// two-sided. Shift towards the expected of zero.
// Use of signum ignores x==0 (i.e. not copySign(0.5, z))
z -= Math.signum(z) * 0.5;
}
}
z /= Math.sqrt(variance);
final NormalDistribution standardNormal = NormalDistribution.of(0, 1);
if (alternative == AlternativeHypothesis.GREATER_THAN) {
return standardNormal.survivalProbability(z);
}
if (alternative == AlternativeHypothesis.LESS_THAN) {
return standardNormal.cumulativeProbability(z);
}
// two-sided
return 2 * standardNormal.survivalProbability(Math.abs(z));
}