Method · Statistics Library

Mann-Whitney U test

The Mann-Whitney U test compares two independent groups' distributions using ranks rather than raw values, the nonparametric alternative to the two-sample t-test when normality is doubtful or outliers are present.

Topic indexA reference reading outside the guided chapter sequence

Understand the idea

Instead of comparing two groups' averages directly, the Mann-Whitney test pools every observation from both groups together, ranks them all from lowest to highest, and compares the total rank each group accumulated. If one group's values consistently rank higher than the other's, that group's rank total will be noticeably larger than chance would produce, and that's what the test detects. Because it uses only ranks, extreme outliers and non-bell-shaped distributions barely affect it, unlike a test built on raw means.

The rank-biserial correlation is the natural effect-size companion here: it runs from -1 to +1 and directly answers 'what fraction of cross-group pairs favor one group over the other, minus the fraction that favor the reverse?' A positive value means the first group (alphabetically) tends to rank higher; negative means the second group does.

There are actually two different mathematical routes to computing this test's p-value: an EXACT calculation (enumerating literally every possible way the observations could have split between the groups, used automatically for small samples with no tied values) and a NORMAL APPROXIMATION (used for larger samples or whenever ties are present). The card always names which route it used, because the two can give slightly different p-values on the same data, and knowing which one produced your number matters for reproducing it elsewhere.

The Mann-Whitney U test is also called the Wilcoxon rank-sum test; the two names describe the same test. The Mann-Whitney test is the most common nonparametric two-sample test: it works from ranks and does not assume the data follow a normal distribution.

Practitioner explanation

KernelStats computes U1 = R1 - n1(n1+1)/2 (R1 the rank sum of the first, alphabetically-sorted group) via scipy.stats.mannwhitneyu, with rank-biserial correlation r_rb = (U1-U2)/(n1*n2) = 2*U1/(n1*n2) - 1 as the effect size, using Kerby's simple-difference formulation. The sign convention is explicit: U1 belongs to the FIRST (alphabetically first) group, so a positive r means that first group tends to rank higher, and this is stated directly rather than left for the analyst to infer from the raw statistic.

The exact-versus-approximation p-value route follows the same rule as SciPy's own implementation (exact permutation ONLY when a group has n<=8 AND the pooled values carry no ties, normal approximation otherwise), and the card prints the route it used by name. Continuity correction exists only within the normal-approximation route, so when the exact route is used the continuity-correction setting has no effect on the result. The card always states plainly which of the three possible labels applies: exact permutation p-value, normal approximation with continuity correction, or normal approximation without it.

The product frames the test's hypotheses carefully as being about STOCHASTIC DOMINANCE (one distribution's values tend to rank higher than the other's) rather than specifically about medians, and only under the ADDITIONAL assumption that both distributions have a similar SHAPE does a significant result cleanly translate into a statement about differing medians; this distinction is the exact content of a documented, referenced misconception about this test.

When to use it

Consider it when

Comparing two INDEPENDENT groups on a continuous or ordinal response when normality is doubtful, samples are small, or outliers are present.

Use caution when

The data are roughly normal (the two-sample/Welch's t-test is more powerful in that case), the two measurements are paired or matched on the same subjects (use the Wilcoxon signed-rank test instead), or more than two groups are being compared (use Kruskal-Wallis).

Formulas and symbols

Mann-Whitney U statistic

U1=R1−n1(n1+1)2U_1 = R_1 - \dfrac{n_1(n_1+1)}{2}

R1 the sum of ranks of group 1 in the pooled ranking; n1 the size of group 1. U2 = n1*n2 - U1.

Rank-biserial correlation

rrb=2U1n1n2−1=U1−U2n1n2r_{rb} = \dfrac{2U_1}{n_1 n_2} - 1 = \dfrac{U_1-U_2}{n_1 n_2}

Ranges -1 to +1. Positive means the first (alphabetically first) group tends to rank higher.

Assumptions and checks

  • Independence of observations. Mann-Whitney assumes observations in and across groups are independent; violating this invalidates the test regardless of the shape check.

    In KernelStats: Not checked by KernelStats: assumed from the study design; stated directly as a standing reminder.

  • Mann-Whitney's rank-sum result is always a valid test of whether one group tends to rank higher than the other, but it can only be read as a difference in MEDIANS specifically when the two groups' distributions have similar shape and spread; markedly different spreads make the median interpretation misleading even though the test itself remains valid.

    In KernelStats: Checked directly: the card compares the two groups' spread (IQR, or SD when IQR is degenerate) via a Brown-Forsythe-style test and flags a material, statistically-supported difference in spread as a warning against the median interpretation, reported honestly as 'assumed, not checked' when a group is too small or has zero spread to compare.

Alternatives

  • Two-Sample t-Test. The data are roughly normal, where the parametric t-test is more powerful.
  • Wilcoxon Signed-Rank Test. The two measurements are paired or matched on the same subjects rather than independent groups.
  • Kruskal-Wallis Test. More than two independent groups are being compared.

Worked example

Scenario

Comparing satisfaction scores between customers who received staff training and those who did not (n1=42, n2=38).

Result

U = 1102, normal-approximation p-value (with continuity correction) = .003; rank-biserial r = 0.38 (medium effect), median1 = 8, median2 = 6.

Interpretation

Customers who received staff training tend to rank higher on satisfaction than those who did not, with a medium-sized effect. Because both groups exceed 8 observations, the normal-approximation route applies automatically rather than the exact permutation route, and the card names this explicitly so the p-value's provenance is never ambiguous.

Common misconceptions

Misconception

A significant Mann-Whitney test always means the two groups' medians differ.

Correction

The Mann-Whitney test's null hypothesis is that the two distributions are IDENTICAL, and its alternative is about STOCHASTIC DOMINANCE (one group's values tend to rank higher). A significant result cleanly implies a difference in MEDIANS specifically only when the two distributions have a similar shape; with differently-shaped distributions, a significant result can occur even when the medians are similar, driven instead by a difference in spread or skew.

Read more: Misconception: the Mann-Whitney/Wilcoxon test always compares medians

Misconception

The continuity-correction option always changes the reported p-value when it is turned on.

Correction

The continuity correction exists only within the NORMAL-APPROXIMATION route: when the exact permutation p-value is used instead (small samples, no ties), the continuity-correction setting has no effect on the result at all, even though it may still display as 'on'. The card explicitly names which route was actually used so this is never left ambiguous.

References

  1. On a test of whether one of two random variables is stochastically larger than the other, Mann, H. B. and Whitney, D. R. (1947), Annals of Mathematical Statistics, 18(1), 50-60.
  2. NIST/SEMATECH e-Handbook of Statistical Methods, §1.3.5.13 Wilcoxon rank-sum test, NIST/SEMATECH (2012), §1.3.5.13.