Nonparametric

Rank tests

A rank test looks only at the order of the data, so it needs no assumption of normality. Its null distribution is a counting problem, and here it is counted: the p-value is an exact fraction, with ties handled by midranks, until the sample is large enough that the normal approximation takes over.

Paired data: sign and signed-rank tests

// Hamilton depression scores before and after treatment, nine patients
const before = [1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30];
const after  = [0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29];
import { signTest, wilcoxonTest, tTest } from "@ratmath/stats";

signTest(before, after).pValue;          // 23/128: 7 of 9 differences are positive
const w = wilcoxonTest(before, after);
w.statistic;                             // 40: the sum of the ranks of the positive differences
w.pValue;                                // 5/128 = 0.0390625, exact
w.estimate;                              // 23/50: the Hodges–Lehmann estimate of the shift
tTest(before, after, { paired: true });  // p = 0.016177 (certified), assuming normal differences

The sign test asks only whether each difference is positive. If the median difference is 0, the number of positives among the nn non-zero differences is binomial with p=1/2p = 1/2, so the p-value is a binomial tail. The signed-rank test also uses how large the differences are: rank the ∣di∣|d_i| and add the ranks of the positive ones,

W+=∑di>0rank⁡(∣di∣). W^+ = \sum_{d_i > 0} \operatorname{rank}(|d_i|).

If the differences are symmetric about 0, each sign is a fair coin independent of the ranks, so all 2n2^n sign patterns are equally likely. The distribution of W+W^+ over them is counted by dynamic programming: adding one rank rr either leaves the sum alone or adds rr. Ties take midranks, and doubling them makes every rank a whole number, so the count stays exact. Above n=60n = 60 the default is the normal approximation with the tie-corrected variance

Var⁡W+=n(n+1)(2n+1)24−148∑t(t3−t), \operatorname{Var} W^+ = \frac{n(n+1)(2n+1)}{24} - \frac{1}{48}\sum_t (t^3 - t),

where tt runs over the sizes of the tie groups; method: "exact" counts anyway.

Two groups: Mann–Whitney

import { mannWhitneyTest } from "@ratmath/stats";
const x = [0.80, 0.83, 1.89, 1.04, 1.45, 1.38, 1.91, 1.64, 0.73, 1.46];
const y = [1.15, 0.88, 0.90, 0.74, 1.21];
const m = mannWhitneyTest(x, y);
m.statistic;   // 35: pairs with x above y
m.pValue;      // 764/3003 ≈ 0.2544, exact over all C(15, 5) = 3003 splits
m.estimate;    // 61/200: the median of the 50 differences x − y
mannWhitneyTest(x, y, { method: "normal" }).pValue;   // 0.244624, with continuity correction

UU counts the pairs (xi,yj)(x_i, y_j) with xi>yjx_i > y_j, ties counting a half; it equals the rank sum of the xx's minus m(m+1)/2m(m+1)/2. Under the null hypothesis every way of choosing which mm of the N=m+nN = m + n pooled values are the xx's is equally likely, so the exact p-value is a count over (Nm)\binom{N}{m} choices. That count is done by dynamic programming over the observed midranks, so tied data get the exact conditional distribution rather than an approximation.

Several groups: Kruskal–Wallis

import { kruskalWallisTest } from "@ratmath/stats";
const k = kruskalWallisTest([[2.9, 3.0, 2.5, 2.6, 3.2], [3.8, 2.7, 4.0, 2.4], [2.8, 3.4, 3.7, 2.2, 2.0]]);
k.statistic;   // 27/35
k.meanRanks;   // [36/5, 9, 33/5]
k.pValue;      // 0.679965 from chi-square with 2 degrees of freedom

With RiR_i the rank sum of group ii, H=12N(N+1)∑iRi2ni−3(N+1)H = \frac{12}{N(N+1)}\sum_i \frac{R_i^2}{n_i} - 3(N+1), divided by 1−∑(t3−t)/(N3−N)1 - \sum(t^3 - t)/(N^3 - N) for ties. It is exact; the p-value uses the chi-square approximation with k−1k - 1 degrees of freedom, evaluated with certainty.

Rank correlation: Spearman and Kendall

import { spearmanTest, kendallTest } from "@ratmath/stats";
const a = [86, 97, 99, 100, 101, 103, 106, 110, 112, 113];
const b = [2, 20, 28, 27, 50, 29, 7, 17, 6, 12];
spearmanTest(a, b).statistic;   // −29/165: ρ = 1 − 6Σd²/(n(n² − 1)) without ties
kendallTest(a, b).statistic;    // −1/9
kendallTest(a, b).pValue;       // 0.727490, exact: permutations of 10 counted by inversions

Spearman's ρ\rho is Pearson's correlation of the ranks. For n≤9n \le 9 the p-value counts all n!n! orderings exactly; above that it uses t2=ρ2(n−2)/(1−ρ2)t^2 = \rho^2(n-2)/(1-\rho^2) against F(1,n−2)F(1, n-2). Kendall's τ\tau compares every pair: SS is concordant minus discordant pairs. Without ties, S=(n2)−2IS = \binom n2 - 2I where II is the number of inversions of a permutation, and the number of permutations of nn with II inversions satisfies a simple recurrence, so the exact distribution is cheap even for n=60n = 60.

Try itEdit the code and run it with @ratmath/stats and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs