in commons-statistics-inference/src/main/java/org/apache/commons/statistics/inference/WilcoxonSignedRankTest.java [501:533]
private static double calculateAsymptoticPValue(double wPlus, int n, double z, double c,
AlternativeHypothesis alternative, boolean continuityCorrection) {
// E[W+] = n * (n + 1) / 4 - z * (z + 1) / 4
final double e = (n * (n + 1.0) - z * (z + 1.0)) * 0.25;
final double variance = ((n * (n + 1.0) * (2 * n + 1.0)) -
(z * (z + 1.0) * (2 * z + 1.0)) - c * 0.5) / 24;
double x = wPlus - e;
if (continuityCorrection) {
// +/- 0.5 is a continuity correction towards the expected.
if (alternative == AlternativeHypothesis.GREATER_THAN) {
x -= 0.5;
} else if (alternative == AlternativeHypothesis.LESS_THAN) {
x += 0.5;
} else {
// two-sided. Shift towards the expected of zero.
// Use of signum ignores x==0 (i.e. not copySign(0.5, z))
x -= Math.signum(x) * 0.5;
}
}
x /= Math.sqrt(variance);
final NormalDistribution standardNormal = NormalDistribution.of(0, 1);
if (alternative == AlternativeHypothesis.GREATER_THAN) {
return standardNormal.survivalProbability(x);
}
if (alternative == AlternativeHypothesis.LESS_THAN) {
return standardNormal.cumulativeProbability(x);
}
// two-sided
return 2 * standardNormal.survivalProbability(Math.abs(x));
}