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 the relative ranks of values in two independent samples. It is a non-parametric option for continuous or ordinal data, including data that are not normally distributed. section 5.2.1 section 5.4

To calculate the statistic by hand, rank the combined observations and sum the ranks within each group. Calculate a U value from each group’s rank sum, select the lower value, and compare it with the appropriate critical value in a U table. A lower U than the threshold is statistically significant. section 5.2.1

The test focuses on relative positions rather than the numerical size of differences. A significant result indicates a difference in ranks; it should not automatically be described as a difference in medians. Explain the result in terms that match the data and the decision at hand. section 5.2.1

Choose a test that matches the group design:

  • Use Mann–Whitney U for two independent samples. section 5.4
  • Consider the Wilcoxon signed-rank test for two dependent samples, or the Kruskal–Wallis test for two or more independent samples. section 5.4

Comparing city temperatures

A hand calculation compares six daily weather values each from New York City and Boston. The ranks yield a lower U for New York City, which is compared with a table value to assess significance. 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]}")

This Python listing compares two simulated skewed groups using SciPy’s Mann–Whitney U and Kruskal–Wallis tests. section 5.2.1

How Effective Data Analysis covers it

How deep it goes
Works through rank sums, U calculation, and comparison with a critical value.
Examples
Illustrates hand calculation with city temperatures and compares simulated skewed groups in Python.
What you can do after
Apply the test via SciPy’s mannwhitneyu and interpret its rank-based comparison.
How it connects
Connects rank-based comparisons with t-tests, Wilcoxon signed-rank tests, and Kruskal–Wallis tests.

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.