Mann–Whitney U Test

medium
Unit 13 · Statistics & Time Series0/3 to milestone

Session lengths and order values are heavily skewed, so a t-test's normality assumption is shaky. The Mann–Whitney U test compares two groups using only ranks.

  1. Pool both samples and rank them from 1, giving tied values the average of their ranks.
  2. RaR_a = the sum of the ranks belonging to sample a, and Ua=Ra−na(na+1)2U_a = R_a - \frac{n_a(n_a + 1)}{2}.
  3. For large samples, z=Ua−μUσUz = \frac{U_a - \mu_U}{\sigma_U} with μU=nanb2\mu_U = \frac{n_a n_b}{2} and σU=nanb(na+nb+1)12\sigma_U = \sqrt{\frac{n_a n_b (n_a + n_b + 1)}{12}} (ignore the tie correction).

Implement rankdata(values) (average ranks for ties), and mann_whitney_u(a, b) returning (U_a, z). Also implement prob_superiority(a, b): Ua/(nanb)U_a / (n_a n_b), the probability that a random value from a beats a random value from b (ties count half). It's the same quantity as ROC AUC.

#statistics#non-parametric#hypothesis-testing2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Mann–Whitney U Test · TechGradify