24 / Inference
Rank-based comparisons and Mann–Whitney U
Rank-based tests compare the ordering of observations rather than assuming that the original measurements follow a normal distribution. Mann–Whitney U is a common independent-group method; its interpretation must match the question and the assumptions, especially when group shapes or spreads differ.
The business question
Do two independent groups show a distributional difference when raw-value assumptions are difficult?
- Compare independent supplier lead times with a skewed distribution.
- Analyse ordered service ratings when equal numerical gaps between ratings are not defensible.
- Investigate whether observations from one process tend to be larger than those from another.
How the method works
Combine both groups, sort the values and assign ranks. Tied values receive average ranks. The sum of one group’s ranks can be converted to a U statistic, which also counts pairwise comparisons: the number of times an observation in that group exceeds an observation in the other, with half credit for a tie.
The ordinary two-sided null concerns the equality of the underlying distributions. With additional location-shift and equal-shape assumptions, a location interpretation may be appropriate. Without them, a rejection can reflect differences in spread or shape and should not be described automatically as a test of equal medians. Plotting the distributions is therefore part of interpreting the result.
Small untied samples can use an exact reference distribution. The live teaching calculation and the Python example here use a normal approximation with tie and continuity adjustments, so their p-values are explicitly approximate. Independence, measurement quality and sampling design still matter. Replacing values with ranks does not repair confounding, clustering, selective sampling or unreliable category definitions.
The calculation
| Symbol | Meaning |
|---|---|
| R_A | Sum of pooled ranks for group A, using average ranks for ties. |
| n_A, n_B | Independent observations in the two groups. |
| U_A | A-over-B pairwise wins, with ties counted as half. |
| superiority | Estimated P(A>B) + 0.5P(A=B), not a causal probability. |
A worked business example
ILLUSTRATIVE SCENARIO
Illustrative delivery-day samples from two independent spare-parts suppliers.
Inputs
- Supplier A: 4, 5, 6, 7, 8, 9 days.
- Supplier B: 6, 7, 8, 9, 10, 11 days.
- Use a two-sided asymptotic test with tie correction and the default continuity correction.
Work through the numbers
- The sample medians are 6.5 and 8.5 days, but these medians are descriptive summaries rather than the complete test definition.
- Pooling and ranking gives U_A = 8 out of 6×6 = 36 pairwise comparisons.
- The superiority estimate is 8/36 ≈0.222, including half-ties; A observations tend to be smaller in this sample.
- The tie-corrected normal approximation gives p≈0.125530.
This does not show the suppliers are equivalent. The observed tendency may still matter operationally, and the sample is small. A purchasing decision needs uncertainty, tail delays, price and a larger representative comparison.
Explore the decision
Compare two groups by rank. The live explorer has its own example inputs; it is separate from the worked calculation above.
Enable JavaScript to use this calculator. The worked example and executable Python remain available.
Apply it at work
- Choose a distributional or location question that rank-based analysis can answer.
- Verify independence and inspect each group’s distribution, ties and measurement scale.
- Compute U, the specified exact or approximate p-value and an interpretable effect summary.
- Interpret the result together with service-level risks, costs and a plan to collect more informative data.
Check these assumptions
- Observations within and between groups are independent.
- The ordering of the measured outcome is meaningful and consistently recorded.
- The test method correctly handles ties and the sample sizes.
- A median or pure location-shift interpretation needs additional equal-shape assumptions beyond the basic rank comparison.
Common mistakes and how to avoid them
- Calling every nonparametric procedure a median test can produce the wrong business conclusion.
- Mixing exact, permutation and asymptotic p-values without labelling the method makes comparisons misleading.
- Using a non-significant result as proof of equality confuses weak evidence with demonstrated equivalence.
How this can help the business
- A buyer can investigate typical ordering and delay tendencies without relying only on an unstable average.
- A service analyst can analyse ordered ratings while avoiding unjustified claims about precise numerical distances.
Match the rank-based hypothesis to the decision, report the actual distributional pattern, and require a practical and adequately supported difference before acting.
When to use another approach
Use a paired method for matched data, quantile regression for a specific percentile, or a robust mean comparison when the mean remains the business target.
Reproduce the worked example in Python
The code is self-contained for this example. Dependencies and run instructions are included with the downloadable notebook.
Show executable code and recorded output
import numpy as np
from scipy import stats
a = np.array([4,5,6,7,8,9])
b = np.array([6,7,8,9,10,11])
test = stats.mannwhitneyu(a, b, alternative="two-sided", method="asymptotic")
superiority = test.statistic/(len(a)*len(b))
print("Median days A/B:", np.median(a), np.median(b))
print("U and tie-corrected approximate p:", round(test.statistic, 3), round(test.pvalue, 6))
print("P(A exceeds B), counting ties half:", round(superiority, 3))Recorded output
Median days A/B: 6.5 8.5 U and tie-corrected approximate p: 8.0 0.12553 P(A exceeds B), counting ties half: 0.222
Sources and further reading
Concepts are linked to primary teaching or technical sources. The business examples are illustrative applications, not reported client results.