#318Mann-Whitney U TestMediumStatisticsML System DesignAsked atMicrosoft · Meta · Google
Mann-Whitney U Test
Background
For skewed, heavy-tailed metrics (revenue per user, watch time), the Welch -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.
where 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]. , so means A is entirely smaller, 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