BCompare groups · Guide test 021
Mann–Whitney U or Wilcoxon rank sum test
Compares independent distributions through ranks; a pure median interpretation needs additional shape assumptions.
Do it in CodeSplash
In the app, set it up in Analyze with this method:
- Mann–Whitney U test — Do values in one group tend to be larger than in another (without assuming normality)?
Do it in CodeSplash Run this test on your own data, in Python or R.
Bench
Change the design, drag the data, move α. The statistic, its reference distribution and the verdict follow.
Bench · Mann–Whitney U / Wilcoxon rank-sum test
Alternative
Your data
Drag a point or use ↑/↓ to change itIf H₀ were true
- The reference: how W varies across samples when H₀ is true.
- Your statistic, from the data above.
- The p-value: the total probability of outcomes no more likely than yours (p = .034).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- ranks of group 1's values in the pooled sample
- the rank-sum statistic (R's W; scipy calls it U)
- W's center when H₀ is true
The verdict
W = 229, p = .034, r_rb = 0.41
p ≤ α = .050: reject H₀
Effect size: rank-biserial correlation r_rb = 0.414
It compares ranks, so it asks whether one group tends to be larger — not, in general, whether the medians differ. Small samples without ties use W's exact null distribution, as R does; with ties R switches to a normal approximation. This bench follows R's defaults. The CodeSplash method uses the tie-corrected normal approximation unless you choose Exact in its options. scipy and R report the first group's U; some textbooks count the other way, which gives n1n2 − U.
- Statistic
- 229.000
- p-value
- 0.0342
Mann–Whitney U / Wilcoxon rank-sum test on Group 1: n = 18; Group 2: n = 18: W = 229, p = .034, r_rb = 0.41. The p-value is below α = .050, so the data are hard to reconcile with H₀.
Question and hypotheses
H0: the two distributions are identical; under a common shape shift model, their location difference is zero.
Assumptions
Independent observations and independent groups; ordinal or quantitative measurements. The permutation null is identical group distributions. A location or median interpretation additionally needs comparable distribution shapes.
Formula
Parameters and notation
m,n are group sizes. U/(mn) estimates the probability that a random Y exceeds a random X, with half credit for ties. An equivalent statistic is a shifted rank sum.
Structure of the method
Pool observations, form midranks or equivalent pairwise comparisons, and calculate a rank-sum/U statistic. Under identical distributions, fixed group sizes define a conditional permutation distribution; a large-sample route uses its tie-adjusted variance and optional continuity correction. The signed orientation depends on which sample the reported U statistic counts as larger.
Estimated and nuisance parameters
The pairwise superiority probability is estimated from observed comparisons, with half credit for ties. No population means or variances are fitted for the basic statistic. Sample sizes and pooled tie counts determine calibration; a common-shape shift parameter is an additional model used only when giving a location interpretation.
Settings and design choices
Specify sample order, alternative direction, exact versus asymptotic/permutation method, continuity correction, tie convention, and multiplicity family. Scipy's exact U routine does not automatically supply a tie-corrected exact distribution; use an explicitly suitable permutation approach for small tied samples. Keep algorithmic convention separate from the statistical null.
Fitting and adaptation
Recompute pooled ranks when observations or group membership change; reusing ranks from a different comparison changes the statistic. If the scientific target is superiority probability under unequal shapes, consider a studentized procedure such as Brunner–Munzel. Independent confirmation is needed after choosing a favorable direction or subgroup from the same outcomes.
Sensitivity and robustness
Many ties reduce rank resolution. Dependence within subjects invalidates independent-group permutations. Unequal distributions with superiority probability one-half can still violate the strong exchangeability null needed for ordinary exact rank calibration. Both a median-shift claim and a distribution-free causal claim require additional assumptions beyond the test calculation.
How to use and calibrate
Use the exact conditional rank distribution for suitable small samples or a tie corrected normal approximation. Choose a directional alternative in advance.
Example application
Compare ordered pain scores in two independently randomized groups; report a probability of superiority and its interval.
Interpretation and pitfalls
Watch out
It is not a general test of equal medians. Equal superiority probability alone does not justify the usual permutation variance when shapes differ. See Brunner–Munzel.
What changed in edition 2
- New sections: structure of the method, estimated and nuisance parameters, settings and design choices, fitting and adaptation, sensitivity and robustness.
- Rewritten in the review: parameters and notation.
Parameters and notationdocs.scipy.org
Formula and returned first-sample statistic use complementary U conventions.
Corrected text
m,n are group sizes. U/(mn) estimates the probability that a random Y exceeds a random X, with half credit for ties. An equivalent statistic is a shifted rank sum. The displayed U counts X<Y. scipy.stats.mannwhitneyu(x,y) and R's shifted first-sample rank sum report the opposite orientation, equal to mn minus this displayed U. Two-sided p-values are unchanged; align the sign and alternative for directional interpretation.
Python and R
Rank tests generally concern distributions or relative effects; a location interpretation needs additional shape assumptions.
Validated — Executed successfully in the validation environment · needs numpy, scipy
import numpy as np
from scipy import stats
rng = np.random.default_rng(2026)
x = np.array([4.2,5.1,5.4,4.8,6.0,5.8,4.9,5.6,6.2,5.0])
y = np.array([4.0,4.4,4.8,5.0,5.1,4.3,4.7,4.6,5.2,4.9])
print(stats.mannwhitneyu(x,y,method="asymptotic",use_continuity=True))
Output
MannwhitneyuResult(statistic=np.float64(79.0), pvalue=np.float64(0.030955480699933292))
Validated — Executed successfully in the validation environment · needs Base/standard library
x <- c(4.2,5.1,5.4,4.8,6.0,5.8,4.9,5.6,6.2,5.0)
y <- c(4.0,4.4,4.8,5.0,5.1,4.3,4.7,4.6,5.2,4.9)
print(wilcox.test(x,y,exact=FALSE,correct=TRUE))
Output
Wilcoxon rank sum test with continuity correction data: x and y W = 79, p-value = 0.03096 alternative hypothesis: true location shift is not equal to 0
Sources
- SciPy Mann–Whitney testdocs.scipy.org
- scipy.stats.mannwhitneyudocs.scipy.org
- Statistics guide v4 (historical):
- R: Executed successfully in the validation environment
- Reference's record:
- Edition 2 review: Python: executed · R: not executed (Rscript unavailable). Python: Ran in the reference's review, on this exact code. R: Reviewed but not run: R wasn't available to the reference's authors.
From the CodeSplash statistics guide (v4), entry 021, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT021.