Mann-Whitney U test: how it works, with a worked example

Updated

The Mann–Whitney test (also called the Wilcoxon rank-sum test) compares two independent groups without assuming normal data. It pools the two samples, ranks all the values, and asks whether one group's ranks are systematically higher than the other's. It is the usual alternative to the 2-sample t-test when the data are skewed, have outliers or are only ordinal.

When to use it

  • Comparing two machines, shifts, suppliers or methods on a measurement that is skewed, such as times, particle counts or roughness.
  • Small samples where normality cannot be checked, or where one or two extreme values would dominate a t-test.
  • Ordinal data such as ratings or grades.
  • For three or more groups, use Kruskal–Wallis. For paired data, use the Wilcoxon signed-rank test on the differences.

How it works

Let group 1 have n₁ values and group 2 have n₂.

  • H₀: the two distributions are the same.
  • H₁: one distribution is shifted relative to the other (two-sided), or shifted in a stated direction.

If the two groups have the same shape and spread, H₀ and H₁ become statements about the medians: η₁ = η₂ against η₁ ≠ η₂.

Steps:

  1. Rank all N = n₁ + n₂ values together, from 1 (smallest) to N. Tied values get the average of their ranks.
  2. Add up the ranks of group 1: W = R₁.
  3. Convert to the U statistic, the number of pairs (one value from each group) in which the group 1 value is larger, with ties counting one half:
U₁ = R₁ − n₁(n₁ + 1)/2, U₂ = n₁n₂ − U₁

Under H₀, U₁ has mean n₁n₂/2. For small samples without ties, the exact distribution of U gives the p-value. Otherwise this normal approximation is used, with a correction for ties and a continuity correction of 0.5:

z = ( |U₁ − n₁n₂/2| − 0.5 ) / √( n₁n₂/12 · [ (N + 1) − Σ(t³ − t) / (N(N − 1)) ] )

where t is the number of values in each group of ties. Ties reduce the variance, so the correction makes the test slightly more sensitive.

Estimate and interval. The matching estimate of the shift is the Hodges–Lehmann estimate: the median of all n₁ × n₂ differences between a value from group 2 and a value from group 1. The confidence interval runs between two of these sorted differences, chosen from the distribution of U.

Assumptions: independent observations within and between groups; a continuous or at least ordinal measurement. For the median interpretation, the two distributions should have similar shapes.

Example

A hospital laboratory tracks turnaround time for urgent potassium tests, from receipt of the specimen to the reported result. The night shift has fewer staff, and the lab manager wants to know whether urgent results take longer at night. She samples 12 urgent tests from day shifts and 11 from night shifts, in minutes:

ShiftTurnaround time (min)
Day38, 42, 35, 51, 40, 37, 45, 39, 48, 36, 41, 44
Night47, 52, 44, 63, 49, 58, 45, 71, 50, 46, 55
Turnaround time of urgent potassium tests by shift40506070Turnaround time (min)DayNight
Turnaround time of urgent potassium tests by shift

Turnaround times are skewed to the right (a few long delays), so the manager uses the Mann–Whitney test, two-sided, α = 0.05.

  • Medians: day 40.5 min, night 50.0 min.
  • Ranking all 23 values (44 and 45 each appear twice, so those ranks are averaged): R_day = 90, R_night = 186.
  • U_day = 90 − 12 × 13/2 = 12, and U_night = 12 × 11 − 12 = 120. In only 12 of the 132 day–night pairs is the day value the larger one.
  • Mean of U under H₀: 66. With the ties and continuity corrections, z = 3.29 and the two-sided p-value is 0.0010.
  • Hodges–Lehmann estimate of the night − day difference: 10.0 min.
  • 95% confidence interval for the difference: 5 to 17 min (achieved confidence 95.6%, from the exact distribution of U without ties; the two small ties barely affect it).

How to read the results

  • The p-value of 0.001 says the night shift's turnaround times are systematically longer. This is not a chance pattern in a small sample.
  • The estimate says a typical urgent potassium result takes about 10 minutes longer at night, and plausibly 5 to 17 minutes longer.
  • In lab terms, if the target for urgent potassium is 45 minutes, the day shift meets it most of the time (10 of 12 here), and the night shift rarely does (2 of 11). The next step is to map the night workflow: specimen transport, centrifuge batching, and who verifies results.
  • Look at the spread too. The night shift also has the longest delays (63 and 71 minutes). A median shift and a longer tail often have different causes.

Common mistakes

  • Calling it a test of medians without checking the shapes. If one group is much more spread out, the test can reject H₀ even when the medians are equal. Look at the boxplot.
  • Using it on paired data. If the same specimens were timed under two conditions, use a paired method.
  • Ignoring ties. Many tied values (from rounding times to 5 minutes, for example) make the uncorrected approximation conservative. Use the ties correction, which is standard.
  • Assuming it has no assumptions. It still needs independent observations. Turnaround times from the same busy hour are often related.

Frequently asked questions

Are the Mann–Whitney test and the Wilcoxon rank-sum test the same?

Yes. The Wilcoxon rank-sum statistic W is the rank sum of one group, and the Mann–Whitney U is W minus n₁(n₁ + 1)/2. They give the same p-value.

Does the Mann–Whitney test compare medians?

Only when the two distributions have the same shape and differ by a shift. In general it tests whether a value from one group tends to be larger than a value from the other.

How does the test handle ties?

Tied values get the average of the ranks they occupy, and the variance of U is reduced by the term Σ(t³ − t)/(N(N − 1)). With many ties, the exact distribution is replaced by the corrected normal approximation.

How much power do I lose compared with the t-test?

Very little. With normal data the Mann–Whitney test has about 95% of the efficiency of the t-test, and with skewed or heavy-tailed data it is often much more powerful.

Try it with your own data

StatLab SPC is free, open-source statistics for engineers. It runs in your browser, works offline, and never uploads your data.

Open StatLab