Under the null hypothesis a rank statistic has the same distribution for every continuous population, so its law can be computed once and for all. The signed-rank statistic W of n observations has the law of 1·B1 + 2·B2 + … + n·Bn with fair coins Bj: its probabilities are the coefficients of (1 + x)(1 + x2)···(1 + xn)/2n. The rank sum W′ of n observations among n + m is the sum of a random n-subset of the ranks. Both are built here by their recursions. Spikes are the exact probabilities, the curve is the normal approximation, pink spikes form the exact critical region, and the gold line is the observed value.
code/ch14_nonparametric_statistics.py (signed_rank_counts, signed_rank_conditional, rank_sum_counts, rank_sum_conditional); data: data/ch14-demos.json.