Tutorials › Introduction to Statistics › Calculating the Mann-Whitney U Statistic

Nonparametric Alternatives · Tutorial 959 of 996

Calculating the Mann-Whitney U Statistic

Work from pooled ranks to both U statistics, then use the exact null distribution to calculate p-values for small samples without ties.

Intermediate 10 min read

What You'll Learn

  • Pool two independent samples and assign ranks in ascending order.
  • Calculate each group’s rank sum and its Mann–Whitney U statistic.
  • Check the relationship between the two U statistics and the sample sizes.
  • Find exact p-values by counting possible allocations of pooled ranks.
  • Handle two-sided tail cutoffs correctly, including the midpoint case.

From Pooled Ranks to U

The Mann–Whitney test compares two independent samples by ranking all their observations together. This tutorial focuses on the calculation: how to obtain the rank sums and U statistics, and how to use the exact null distribution to find a p-value when the samples are small and there are no ties. The test uses ranks rather than the original measurement scale, but the calculation still depends on keeping the group labels attached to the observations.

Definition: For samples A and B of sizes \(n_A\) and \(n_B\), pool the observations and rank them from smallest to largest. Let \(R_A\) and \(R_B\) be the sums of the ranks assigned to the observations in each sample. The Mann–Whitney statistics are $$ U_A=R_A-\frac{n_A(n_A+1)}{2}, \qquad U_B=R_B-\frac{n_B(n_B+1)}{2}. $$ With no ties, \(U_A\) counts the pairs in which an observation from A is larger than one from B; \(U_B\) counts the pairs in which an observation from B is larger than one from A.

These definitions use ascending ranks: the smallest pooled observation receives rank 1. For each observation in A, its rank includes the observations below it in both groups. Subtracting the ranks that would come from observations in A below other A observations leaves the comparisons with B. Thus, each A observation contributes one count for every B observation it exceeds. With no ties, every cross-group pair has one larger member, so it contributes to exactly one of \(U_A\) or \(U_B\).

Key check: If the samples contain no ties, then $$ U_A+U_B=n_A n_B. $$ There are \(n_A n_B\) cross-group pairs in total. A result that does not satisfy this identity indicates an arithmetic or ranking error.

Assumptions for an Exact Calculation

The exact p-value calculation below treats the group labels as exchangeable under the null hypothesis: conditional on the pooled observations, every allocation of \(n_A\) labels to the \(n_A+n_B\) ranks is equally likely. This is appropriate when the two samples are independent and, under the null hypothesis, come from the same continuous distribution. The continuity condition rules out ties in the population; the worked examples also have no observed ties. If observations are paired, or the groups are not independent, this calculation does not apply.

The number of possible allocations is \(\binom{n_A+n_B}{n_A}\): choose which pooled ranks receive the A label, with the remaining ranks assigned to B. To obtain an exact p-value, calculate the statistic for every such allocation, or count how many allocations meet the relevant tail criterion. The observed data are one of these allocations.

Calculating the Statistics

Worked Example: Rank Sums and Both U Statistics

Suppose a hypothetical experiment records an outcome for three independent observations in group A and three in group B. The observed values are A: 4, 9, 12 and B: 2, 7, 10. All six values are distinct. Pooling and ranking them from smallest to largest gives:

B: 2 (rank 1); A: 4 (rank 2); B: 7 (rank 3); A: 9 (rank 4); B: 10 (rank 5); A: 12 (rank 6).

The rank sums are \(R_A=2+4+6=12\) and \(R_B=1+3+5=9\). Since each sample has size 3, $$ U_A=12-\frac{3(3+1)}{2}=12-6=6, \qquad U_B=9-\frac{3(3+1)}{2}=9-6=3. $$ There are \(3(3)=9\) cross-group pairs, and \(U_A+U_B=6+3=9\), as required.

As a direct check on the counting interpretation, compare each A value with the B values (2, 7, and 10): 4 exceeds one B value, 9 exceeds two, and 12 exceeds all three, so \(U_A=1+2+3=6\), in agreement with the rank calculation. In the other direction, 2 exceeds no A values, 7 exceeds one (4), and 10 exceeds two (4 and 9), giving \(U_B=0+1+2=3\).

A useful derivation verifies the relationship between rank sums and the total number of pairs. The ranks from 1 to \(N=n_A+n_B\) sum to \(N(N+1)/2\), so \(R_A+R_B=N(N+1)/2\). Substituting the U definitions gives $$ \begin{aligned} U_A+U_B &=\frac{N(N+1)}{2} -\frac{n_A(n_A+1)}{2} -\frac{n_B(n_B+1)}{2}\\ &=\frac{(n_A+n_B)(n_A+n_B+1)-n_A(n_A+1)-n_B(n_B+1)}{2}\\ &=n_A n_B. \end{aligned} $$ This identity provides a calculation check, not a separate assumption.

Exact P-Values by Counting Allocations

First state the direction of the alternative hypothesis. If larger outcomes in A are the direction of interest, large values of \(U_A\) are evidence in that direction, so the one-sided p-value is the probability under the null of \(U_A\) being at least its observed value. For a lower-direction alternative, count values at most the observed \(U_A\). The inequalities include equality because outcomes at least as extreme as the observed statistic are counted.

For a two-sided calculation, use the conventional doubled-tail rule, expressed as the union of the two equally extreme tails. Write \(M=n_A n_B\) and \(u_{\mathrm{obs}}=U_{A,\mathrm{obs}}\). The two-sided p-value is the probability of \(U_A\leq \min(u_{\mathrm{obs}},M-u_{\mathrm{obs}})\) or \(U_A\geq \max(u_{\mathrm{obs}},M-u_{\mathrm{obs}})\). Under the null, the distribution is symmetric around \(M/2\), since exchanging the group labels replaces \(U_A\) by \(M-U_A\).

Worked Example: An Exact Two-Sided P-Value

Consider a hypothetical untied dataset with A: 2, 5, 8 and B: 1, 3, 4, 7. The pooled ranks in ascending order are B: 1 (rank 1), A: 2 (rank 2), B: 3 (rank 3), B: 4 (rank 4), A: 5 (rank 5), B: 7 (rank 6), A: 8 (rank 7). Thus \(R_A=2+5+7=14\), and $$ U_A=14-\frac{3(3+1)}{2}=14-6=8. $$ Here \(M=3(4)=12\), and \(U_B=12-8=4\).

Under the exact null, there are \(\binom{7}{3}=35\) equally likely choices of the three ranks assigned to A. For any choice, \(U_A\) is the sum of A’s ranks minus 6. The observed value 8 has opposite extreme tail cutoffs \(U_A\leq4\) and \(U_A\geq8\). To count the lower tail, list the rank triples with rank sum at most 10, since \(U_A\leq4\) means the rank sum is at most \(4+6=10\). There are 11 such triples:

(1,2,3), (1,2,4), (1,2,5), (1,2,6), (1,2,7), (1,3,4), (1,3,5), (1,3,6), (1,4,5), (2,3,4), and (2,3,5).

The upper tail also has 11 allocations by symmetry. The tails are disjoint because the cutoffs 4 and 8 do not overlap. Therefore, $$ P(U_A\leq4\ \text{or}\ U_A\geq8)=\frac{11+11}{35} =\frac{22}{35}\approx0.6286. $$ The exact two-sided p-value is about 0.629. For the one-sided alternative that A tends to have larger outcomes, the p-value is \(P(U_A\geq8)=11/35\approx0.3143\). These calculations describe how unusual the observed statistic is under the stated null model; they do not establish that the null hypothesis is true or false.

1
Rank the pooled observations.
Keep each group label beside its observation. For the exact calculations here, verify that there are no ties.
2
Calculate rank sums and U.
Find \(R_A\) and \(R_B\), then apply the corresponding formula. Check that \(U_A+U_B=n_A n_B\).
3
Set the tail criterion.
Choose the lower or upper tail for a directional alternative. For a two-sided test, use the two opposite extreme cutoffs.
4
Count allocations.
Enumerate the \(\binom{n_A+n_B}{n_A}\) possible choices of ranks for A and divide the number meeting the criterion by that total.

The Midpoint and Overlapping Tails

The two-sided tail events must be counted as a union, not automatically added without checking whether they overlap. When \(u_{\mathrm{obs}}\ne M/2\), the cutoffs \(\min(u_{\mathrm{obs}},M-u_{\mathrm{obs}})\) and \(\max(u_{\mathrm{obs}},M-u_{\mathrm{obs}})\) differ, and the lower and upper tail events are disjoint. When \(u_{\mathrm{obs}}=M/2\), which is possible when \(M\) is even, the cutoffs are equal. Both events include the midpoint outcomes, so adding their probabilities counts those outcomes twice. Their union is the entire set of possible U values and has probability 1. Equivalently, the doubled-tail rule is capped at 1.

Worked Example: Two-Sided P-Value at the Midpoint

Suppose two independent groups each contain two untied observations. Consider hypothetical pooled values with ranks 1 and 4 assigned to A and ranks 2 and 3 assigned to B. Then \(R_A=1+4=5\), so $$ U_A=5-\frac{2(2+1)}{2}=5-3=2, \qquad M=2(2)=4. $$ The observed statistic is exactly the midpoint \(M/2=2\).

There are \(\binom{4}{2}=6\) possible choices of ranks for A. The rank pairs 12, 13, 14, 23, 24, and 34 give U values 0, 1, 2, 2, 3, and 4, respectively. The two-sided cutoff events are \(U_A\leq2\) and \(U_A\geq2\). Each has probability \(4/6\), and they overlap for the two allocations with \(U_A=2\). Their union contains all six allocations, so the exact two-sided p-value is 1—not \(8/6\). The doubled-tail calculation likewise gives \(\min(1,2(4/6))=1\).

Common Mistakes and Why the Calculation Matters

  • Using the wrong rank direction. With ascending ranks, \(U_A\) counts A observations larger than B observations. Reversing the ranking direction without changing the interpretation reverses which U statistic represents larger A values.
  • Subtracting the wrong quantity. The rank-sum correction for A is \(n_A(n_A+1)/2\), not a correction based on the total sample size or on \(n_B\).
  • Forgetting to check the pair total. With no ties, \(U_A+U_B\) must equal \(n_A n_B\). If it does not, recheck the ranks, sums, and subtractions.
  • Adding inclusive two-sided tails when they overlap. At the midpoint, the events \(U_A\leq M/2\) and \(U_A\geq M/2\) both include \(U_A=M/2\). Count their union once, or use the doubled-tail rule capped at 1.
  • Using the exact allocation calculation for a different design. The method assumes independent groups and exchangeable labels under the null. It is not a substitute for an analysis designed for paired observations.

Ranking makes the test usable when a normal model for the original measurements is not appropriate, while the exact allocation calculation avoids relying on a large-sample approximation for small samples. For a small dataset, careful enumeration is practical and makes the source of the p-value transparent: it is the fraction of permitted label allocations at least as extreme as the observed result under the chosen tail rule. Ties require additional care in ranking and in the null distribution; the calculations here are deliberately restricted to untied data.

Check Your Understanding

Use ascending ranks and assume independent groups with no ties unless a question says otherwise.

  1. Group A has ranks 2, 5, and 7. Calculate \(R_A\) and \(U_A\).
  2. If \(n_A=4\) and \(n_B=5\), what must \(U_A+U_B\) equal when there are no ties?
  3. Why are there \(\binom{n_A+n_B}{n_A}\) allocations in the exact null calculation?
  4. For \(M=10\) and \(u_{\mathrm{obs}}=3\), state the two cutoff events used for the two-sided p-value.
  5. When \(M=4\) and \(u_{\mathrm{obs}}=2\), why must the inclusive lower- and upper-tail probabilities not simply be added?