#318Mann-Whitney U TestMedium

Mann-Whitney U Test

Background

For skewed, heavy-tailed metrics (revenue per user, watch time), the Welch tt-test's normality assumption is shaky. The Mann-Whitney U is the non-parametric alternative: it tests whether one sample tends to produce larger values than another, using only ranks. No mean, no variance, no normality assumption.

Problem statement

Implement mann_whitney_u(sample_a, sample_b) returning the U-statistic for sample A.

UA  =  RA    nA(nA+1)2U_A \;=\; R_A \;-\; \frac{n_A(n_A + 1)}{2}

where RAR_A is the sum of A's ranks in the pooled sample. Ties get average ranks (the standard convention).

Input

  • sample_a, sample_b - array-likes of floats.

Output

  • float - the U statistic for sample A.

Examples

Input: a=[1,2,3], b=[4,5,6]
Output: 0.0   # A is entirely below B

Constraints

  • Both samples must be non-empty; ValueError otherwise.
  • Average ranks for ties.

Notes

  • U in [0, n_a * n_b]. UA+UB=nAnBU_A + U_B = n_A n_B, so UA=0U_A = 0 means A is entirely smaller, UA=nAnBU_A = n_A n_B means A is entirely larger.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • reference example: A entirely below B
  • sample: identical samples split the mass
  • reference: average ranks for ties