liveBook · Manning

Mann-whitney u test

As explained in Effective Data Analysis

The Mann-Whitney U test is a non-parametric statistical test for comparing the relative ranks or distributions of two independent samples.

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

How Effective Data Analysis covers it

How deep it goes
Works through rank sums, selection of U, and critical-value comparison, with a hand-calculated example and a Python implementation.
Examples
Uses New York City and Boston temperatures and simulated skewed samples tested with SciPy.
What it assumes
Assumes observations can be identified as continuous or ordinal and the two samples as independent; introduces the rank-sum calculation.
What you can do after
Apply it via SciPy’s mannwhitneyu or calculate U from ranks, then interpret the result as a group comparison.
How it connects
Contrasts with the t-test for parametric comparisons, the Wilcoxon signed-rank test for dependent samples, and Kruskal-Wallis for independent groups.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Effective Data Analysis. The section links point to the parts of the book it draws on.