When samples are small, skewed or full of outliers, a t-test's normality assumption is shaky. Rank tests compare groups using only the ordering of the values. Implement the two standard ones, each with a tie-corrected normal approximation and an effect size.
Ties always get the average of the ranks they span (ranks start at 1). Two-sided p-values are 2(1−Φ(∣z∣)) with Φ the standard normal CDF. No continuity correction is used.
1. mann_whitney_u(x, y) for two independent samples of sizes n1,n2≥1. Rank all N=n1+n2 values together and let R1 be the rank sum of x. Return (u, z, p, r):
- u=R1−n1(n1+1)/2
- z=(u−n1n2/2)/σU with
σU2=12n1n2[(N+1)−N(N−1)∑j(tj3−tj)],
where tj is the size of the j-th group of tied values (use 0 for the correction term when N=1).
p: the two-sided p-value
- r=2u/(n1n2)−1, the rank-biserial correlation (positive when
x tends to be larger)
2. wilcoxon_signed_rank(d) for paired differences di (for example, model A's error minus model B's on each example). Drop zeros, leaving n values. Rank ∣di∣ and let W+ and W− be the rank sums of the positive and negative differences. Return (w_plus, z, p, r):
w_plus =W+
- z=(W+−n(n+1)/4)/σW with σW2=24n(n+1)(2n+1)−48∑j(tj3−tj), ties counted among the ∣di∣
p: the two-sided p-value
- r=(W+−W−)/(W++W−), the matched-pairs rank-biserial correlation
Notes
- If σ=0 (every value tied, or no nonzero differences) return z=0.0 and p=1.0. With no nonzero differences return
(0.0, 0.0, 1.0, 0.0).
- Return plain floats.