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 non-zero differences is binomial with , so the p-value is a binomial tail. The signed-rank test also uses how large the differences are: rank the and add the ranks of the positive ones,
If the differences are symmetric about 0, each sign is a fair coin independent of the ranks, so all sign patterns are equally likely. The distribution of over them is counted by dynamic programming: adding one rank either leaves the sum alone or adds . Ties take midranks, and doubling them makes every rank a whole number, so the count stays exact. Above the default is the normal approximation with the tie-corrected variance
where 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
counts the pairs with , ties counting a half; it equals the rank sum of the 's minus . Under the null hypothesis every way of choosing which of the pooled values are the 's is equally likely, so the exact p-value is a count over 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 the rank sum of group , , divided by for ties. It is exact; the p-value uses the chi-square approximation with 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 is Pearson's correlation of the ranks. For the p-value counts all orderings exactly; above that it uses against . Kendall's compares every pair: is concordant minus discordant pairs. Without ties, where is the number of inversions of a permutation, and the number of permutations of with inversions satisfies a simple recurrence, so the exact distribution is cheap even for .
@ratmath/stats and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.