Unit 6, lesson 51

Mann-Whitney Tests and Rank-Based Comparisons

How the Wilcoxon rank-sum test and the Mann-Whitney U statistic compare two independent samples through pooled ranks, what the exact permutation null assumes, and what the probability of superiority and a location shift mean.

  • Video 51
  • Rank-Based Tests

Group A has the outcomes 11, 33 and 55, and an independent group B has the outcomes 22, 44 and 66. Does one group tend to produce larger values? A rank comparison answers by pooling the six values, ranking them once and asking how the ranks split between the two labels, without a normal model for the measurements. This lesson builds the rank sum and the Mann-Whitney U statistic, computes the exact permutation p-value, and makes clear which null the test checks and why it is not automatically a test of medians.

Key terms

Rank sum

Definition Let a1,…,anAa_1, \dots, a_{n_A} be the sample from group A and b1,…,bnBb_1, \dots, b_{n_B} the sample from group B, with N=nA+nBN = n_A + n_B. Pool the NN values, sort them and give them the ranks 11 to NN. Tied values get the average of the positions they occupy, called a midrank. The rank sum RAR_A is the sum of the ranks of the A values, and RBR_B is the sum for the B values. Together they use every rank once, so

RA+RB=N(N+1)2.R_A + R_B = \frac{N(N + 1)}{2}.

In plain words There is one ranking for both groups together, and each rank then goes back to its group label. A small RAR_A means that the A values sit low in the pooled order. Ranks record order only, so applying one strictly increasing function to every value, such as the logarithm of positive values, leaves every rank and both rank sums unchanged.

Example The pooled values 11 to 66 have ranks equal to themselves, so RA=1+3+5=9R_A = 1 + 3 + 5 = 9 and RB=2+4+6=12R_B = 2 + 4 + 6 = 12, which add to 21=6⋅7/221 = 6 \cdot 7 / 2. With ties, take A with the values 11 and 22 and B with the values 22 and 33. The sorted values are 11, 22, 22 and 33, and the two values 22 share positions 22 and 33, so each gets rank 2.52.5. Then RA=1+2.5=3.5R_A = 1 + 2.5 = 3.5 and RB=2.5+4=6.5R_B = 2.5 + 4 = 6.5, which add to 10=4⋅5/210 = 4 \cdot 5 / 2.

Mann-Whitney U statistic

Definition Compare every A value with every B value, which makes nAnBn_A n_B cross-group pairs. Counting a pair as a win for A when the A value is larger and as half a win when the two values are equal,

UA=#{(i,j):ai>bj}+12 #{(i,j):ai=bj}.\begin{aligned} U_A &= \#\{(i, j) : a_i > b_j\} \\[0.4em] &\quad + \tfrac12 \, \#\{(i, j) : a_i = b_j\}. \end{aligned}

The statistic UBU_B counts wins for B in the same way. With midranks for ties,

UA=RA−nA(nA+1)2,UA+UB=nAnB.\begin{gathered} U_A = R_A - \frac{n_A(n_A + 1)}{2}, \\[0.4em] U_A + U_B = n_A n_B . \end{gathered}

So UAU_A runs from 00 to nAnBn_A n_B. Texts and software differ in orientation. Some report UBU_B, some the statistic of the first sample listed, and some the smaller of UAU_A and UBU_B, so a reported U needs its orientation stated.

In plain words When no values are tied, the rank of an A value counts the pooled values at or below it. Those include the A values at or below it, and removing them leaves the number of B values it beats. Adding over all A values removes 1+2+⋯+nA1 + 2 + \dots + n_A in total, which is where the subtraction comes from. A large UAU_A means that A values usually come out on top.

Example For A with 11, 33 and 55 and B with 22, 44 and 66, the value 11 beats no B value, 33 beats 22, and 55 beats 22 and 44. So UA=0+1+2=3U_A = 0 + 1 + 2 = 3 out of 99 pairs, and the rank formula agrees, since 9−3⋅4/2=39 - 3 \cdot 4 / 2 = 3. The other orientation gives UB=9−3=6U_B = 9 - 3 = 6. In the tied example, the A value 11 loses both comparisons, and the A value 22 ties the B value 22 and loses to 33. So UA=0.5U_A = 0.5, matching 3.5−2⋅3/2=0.53.5 - 2 \cdot 3 / 2 = 0.5.

Wilcoxon rank-sum test

Definition Suppose a1,…,anAa_1, \dots, a_{n_A} are independent observations from a population with distribution function FAF_A, b1,…,bnBb_1, \dots, b_{n_B} are independent observations from a population with distribution function FBF_B, and the two samples are independent of each other. The Wilcoxon rank-sum test of

H0:FA=FBH_0 : F_A = F_B

compares the observed RAR_A with its distribution under H0H_0, which is the exact permutation distribution described below or, for larger samples, a normal approximation. Without ties the null mean and variance are

E0(RA)=nA(N+1)2,Var⁡0(RA)=nAnB(N+1)12.\begin{gathered} E_0(R_A) = \frac{n_A(N + 1)}{2}, \\[0.4em] \operatorname{Var}_0(R_A) = \frac{n_A n_B (N + 1)}{12}. \end{gathered}

A two-sided test counts values of RAR_A far from the center in either direction as evidence against H0H_0. Because UAU_A differs from RAR_A by a constant, the Mann-Whitney test based on UAU_A is the same test with the same p-value, and the two names are often joined as the Wilcoxon-Mann-Whitney test. In a randomized experiment that assigns nAn_A of NN units to A completely at random, the random assignment justifies the same reference under the sharp null hypothesis that every unit would give the same response under either treatment.

In plain words If both groups come from one population, the labels A and B carry no information about the values, and any split of the pooled ranks is as likely as the observed one. The null is that the whole distributions are equal, which says more than equal medians or equal means. The test needs independent units. It is a different procedure from the Wilcoxon signed-rank test, which analyzes the within-pair differences of paired data.

Example For the running example N=6N = 6, so the center is E0(RA)=3⋅7/2=10.5E_0(R_A) = 3 \cdot 7 / 2 = 10.5, and the observed RA=9R_A = 9 lies 1.51.5 below it. On the U scale the center is 9/2=4.59/2 = 4.5, and UA=3U_A = 3 also lies 1.51.5 below it. The null variance of either statistic is 3⋅3⋅7/12=5.253 \cdot 3 \cdot 7 / 12 = 5.25. Whether a distance of 1.51.5 is unusual is settled by the exact permutation distribution.

Exact permutation distribution

Definition Fix the NN pooled observations and their ranks. Under H0:FA=FBH_0 : F_A = F_B with the sampling conditions above, the NN observations are exchangeable. Given the pooled values, every one of the (NnA)\binom{N}{n_A} ways of choosing which observations carry the label A is then equally likely. Computing UAU_A for every allocation gives the exact permutation distribution of UAU_A. The p-value is the fraction of allocations whose statistic is at least as extreme as the observed one, under a rule for extremeness fixed before the data are seen. Without ties this distribution depends only on nAn_A and nBn_B, which is why it can be tabulated. With ties it depends on the pattern of ties, so the allocations are enumerated on the actual midranks, or a tie-corrected approximation is used.

In plain words The reference distribution lists what the statistic would be under every relabeling of the pooled values, all equally likely when the labels carry no information. The number of possible allocations also limits how small the p-value can be.

Example For nA=nB=3n_A = n_B = 3 there are (63)=20\binom{6}{3} = 20 allocations. The table shows how many give each low value of UAU_A, and the counts for UA=5,6,7,8,9U_A = 5, 6, 7, 8, 9 mirror those for 4,3,2,1,04, 3, 2, 1, 0.

UAU_A0011223344
Allocations1111223333

The center is 4.54.5 and the observed UA=3U_A = 3 is 1.51.5 below it, so the two-sided rule counts allocations with UA≤3U_A \le 3 or UA≥6U_A \ge 6, which is 77 on each side, and

p=1420=0.7.p = \frac{14}{20} = 0.7.

This large p-value does not show that the two distributions are equal. It says only that the observed split of ranks is not unusual under the null, and three observations per group carry little information. For the tied samples, A with 11 and 22 and B with 22 and 33, the six allocations of the midranks 11, 2.52.5, 2.52.5 and 44 give UA=0.5U_A = 0.5, 22 and 3.53.5 twice each, while untied samples of two and two give 00, 11, 22, 22, 33 and 44. The observed UA=0.5U_A = 0.5 is 1.51.5 from the center 22, and 44 of the 66 allocations are that far away, so p=2/3p = 2/3.

Probability of superiority

Definition For independent draws XX from population A and YY from population B, the probability of superiority of A over B is

θ=P(X>Y)+12P(X=Y).\begin{aligned} \theta &= P(X > Y) \\[0.4em] &\quad + \tfrac12 P(X = Y). \end{aligned}

Under independent random sampling, each of the nAnBn_A n_B cross-group pairs scores 11 for an A win, 12\tfrac12 for a tie and 00 otherwise, with expected score θ\theta, so

θ^=UAnAnB\hat{\theta} = \frac{U_A}{n_A n_B}

is an unbiased estimate of θ\theta. If FA=FBF_A = F_B, then P(X>Y)=P(Y>X)P(X > Y) = P(Y > X) and θ=12\theta = \tfrac12. The converse fails, because different distributions can also give θ=12\theta = \tfrac12. For this reason the ordinary rank-sum test of FA=FBF_A = F_B is not automatically a valid test of the weaker null θ=12\theta = \tfrac12 when the shapes differ. Procedures built for that null, such as the Brunner-Munzel test, use a different standard error.

In plain words The probability θ\theta says how often a randomly chosen A outcome beats an independently chosen B outcome, with ties counted as half. A value of 0.750.75 does not mean that three quarters of the A units beat every B unit, and θ\theta is neither a difference in medians nor a difference in means.

Example For the running example θ^=3/9=1/3\hat{\theta} = 3/9 = 1/3, so an A value beat a B value in one third of the cross-group pairs, an estimate with large uncertainty at three values per group. For the tied samples θ^=0.5/4=1/8\hat{\theta} = 0.5/4 = 1/8. For two different distributions with θ=12\theta = \tfrac12, let A be normal with mean 00 and variance 11 and B normal with mean 00 and variance 99. The difference X−YX - Y is continuous and symmetric about 00, so θ=12\theta = \tfrac12 although the spreads differ.

Location-shift model

Definition Two distributions follow a location-shift model if there is a constant δ\delta with

FA(x)=FB(x−δ)F_A(x) = F_B(x - \delta)

for every xx, which means that A has the same distribution as B+δB + \delta. Every quantile of A then equals the same quantile of B plus δ\delta, so the medians differ by δ\delta, and so do the means when they exist. Within this model and with a continuous FBF_B, the null FA=FBF_A = F_B is the same as δ=0\delta = 0. The rank-sum test then becomes a test of δ=0\delta = 0, and θ>12\theta > \tfrac12 exactly when δ>0\delta > 0.

In plain words The shared shape is what lets a rank result speak about medians. Without it, two groups can have the same median but differ in spread or skewness, and the rank-sum test can then reject for reasons that have nothing to do with the median. A rejection by itself does not show that the medians differ. That reading needs the shift model or another argument.

Example Let B be uniform on (0,1)(0, 1) and A uniform on (0.5,1.5)(0.5, 1.5), so that A has the distribution of B+0.5B + 0.5. The medians differ by δ=0.5\delta = 0.5, and so do the means. Write X=B1+0.5X = B_1 + 0.5 and Y=B2Y = B_2 with B1B_1 and B2B_2 independent and uniform on (0,1)(0, 1). Then X≤YX \le Y exactly when B2−B1≥0.5B_2 - B_1 \ge 0.5, which has probability (1−0.5)2/2=0.125(1 - 0.5)^2 / 2 = 0.125, so θ=0.875\theta = 0.875.

Check yourself

  1. Take A with the values 11, 22 and 33 and B with the values 44, 55 and 66. Find RAR_A, UAU_A, θ^\hat{\theta} and the exact two-sided p-value. Does complete separation guarantee rejection at the 55 percent level?
Show the answer

The ranks equal the values, so RA=1+2+3=6R_A = 1 + 2 + 3 = 6 and UA=6−3⋅4/2=0U_A = 6 - 3 \cdot 4 / 2 = 0. No A value beats any B value, so θ^=0\hat{\theta} = 0. The observed UA=0U_A = 0 is 4.54.5 from the center 4.54.5, and only the allocations with UA=0U_A = 0 or UA=9U_A = 9 are that far away, one each, so

p=220=0.1.p = \frac{2}{20} = 0.1.

Complete separation does not guarantee rejection. With three values per group the smallest attainable two-sided p-value is 0.10.1, so this test cannot reject at the 55 percent level for any data. Failing to reject does not show that the distributions are equal either. The report should give the ordering, the sample sizes and θ^\hat{\theta} together.

  1. Eight runners are timed before and after a training block. An analyst treats the 88 before times and the 88 after times as two groups and runs a rank-sum test. What is wrong, and what should be done instead?
Show the answer

The two groups are the same eight runners, so the samples are not independent. A runner’s before and after times are linked, and much of the variation in the times typically comes from differences between runners. The rank-sum reference shuffles labels across all 1616 times as if they were exchangeable, which ignores the matching, so the reference does not describe how the data arose, and the comparison is also diluted by the between-runner variation.

A paired analysis works with the eight within-runner differences, after minus before. The sign test fits a question about the median difference, the Wilcoxon signed-rank test needs differences that are symmetric under the null, and the paired t test targets the mean difference. When a runner’s before and after times are exchangeable under the null, the valid relabeling swaps them within each runner, which flips the sign of that runner’s difference. Without a control group, a before-after comparison also cannot by itself attribute a change to the training.

  1. Population A is uniform on (−1,1)(-1, 1). Population B also has median 00, but its lower half is more spread out. With probability 12\tfrac12 a B value is uniform on (−3,0)(-3, 0), and with probability 12\tfrac12 it is uniform on (0,1)(0, 1). Find θ\theta. With large samples, would the rank-sum test tend to reject, and would a rejection show that the medians differ?
Show the answer

Let XX come from A and YY from B, independently. Ties have probability 00, so θ=P(X>Y)\theta = P(X > Y). When YY is uniform on (−3,0)(-3, 0), an A value xx in (−1,0)(-1, 0) loses with probability −x/3-x/3, and an A value above 00 always wins. The value XX lies in (−1,0)(-1, 0) half the time, and there the loss probability −x/3-x/3 averages 16\tfrac16, so XX loses with probability 12⋅16=112\tfrac12 \cdot \tfrac16 = \tfrac{1}{12}. When YY is uniform on (0,1)(0, 1), an A value xx in (0,1)(0, 1) wins with probability xx, which averages 12\tfrac12 there, and an A value below 00 never wins, so XX wins with probability 12⋅12=14\tfrac12 \cdot \tfrac12 = \tfrac14. Hence

θ=12⋅1112+12⋅14=712.\theta = \frac12 \cdot \frac{11}{12} + \frac12 \cdot \frac14 = \frac{7}{12}.

Because θ≠12\theta \ne \tfrac12, the two distributions differ, and as both samples grow θ^\hat{\theta} settles near 712\tfrac{7}{12}, so the rejection probability approaches 11. A rejection correctly signals that FA≠FBF_A \ne F_B, but both medians are 00. The difference lies in the shape of B, so reporting that the medians differ would be wrong. Without a location-shift model, a rank-sum result should be described as a difference in distributions, with θ^\hat{\theta} as its effect estimate.

How this lesson connects