The Mann-Whitney U test compares two independent samples of continuous or ordinal data using ranks rather than the numerical size of each observation. It is a non-parametric option when a t-test’s distributional assumptions are unsuitable, including when data are non-normal or group variances differ. section 5.2.1 section 5.4
To calculate and interpret U:
- Rank the observations, then add the ranks within each group. section 5.2.1
- Calculate a U value from each group’s rank sum and select the lower value as the test statistic. section 5.2.1
- Compare U with the appropriate critical value: a U below that threshold is statistically significant. section 5.2.1
Comparing city temperatures
Six daily weather observations from New York City and six from Boston provide a small worked comparison. After ranking all 12 values, the lower U value is checked against a U-table critical value; the illustrated temperature difference is reported as highly significant. The conclusion concerns the groups’ relative positions, not the size of the temperature gap. section 5.2.1
import numpy as np #1
from scipy.stats import skewnorm, kruskal, mannwhitneyu
np.random.seed(99) #2
group_a = skewnorm.rvs(a=9, scale=2.2, size=99) + 4.5 #3
group_b = skewnorm.rvs(a=11, scale=1, size=99) + 4.6
H = kruskal(group_a, group_b) #4
U = mannwhitneyu(group_a, group_b)
print(f"Kruskal-Wallis Test, H={H[0]}, p={H[1]}") #5
print(f"Mann-Whitney U-test, U={U[0]}, p={U[1]}")
The Python listing generates two skewed samples and runs SciPy’s mannwhitneyu alongside a Kruskal-Wallis test, printing each statistic and p-value. It shows a tool-based route alongside the hand calculation. section 5.2.1
Match the test to the sampling design: use the Wilcoxon signed-rank test for two dependent samples, while the Kruskal-Wallis test accommodates two or more independent samples. When reporting a result, distinguish a rank-based difference from a difference in averages, and report non-significant results rather than treating significance as the only useful outcome. section 5.4 section 5.2.1
