Nonparametric

Kolmogorov–Smirnov

How far is the empirical distribution of a sample from a claimed one, or from another sample's? The Kolmogorov–Smirnov statistic is the largest vertical gap between the two step functions, and its distribution under the null hypothesis can be computed exactly.

One sample

import { ksTest, normal, kolmogorovCdf } from "@ratmath/stats";

const u = ksTest([0.1, 0.2, 0.35, 0.5, 0.9], (x) => x);   // against the uniform cdf F(x) = x
u.statistic;   // 3/10
u.pValue;      // 83/125, exact

const z = ksTest([0.61, 0.29, 0.06, 0.59, -1.73, -0.74, 0.51, -0.56, 0.39, 1.64, 0.05, -0.06, 0.64,
                  -0.82, 0.37, 1.77, 1.09, -1.28, 2.36, 1.31, 1.05, -0.32, -0.40, 1.06, -2.47], normal());
z.statistic;   // an enclosure around 0.174092: the normal cdf is certified, not exact
z.pValue;      // an enclosure around 0.389741 (certified)

With the sample sorted, x(1)≤⋯≤x(n)x_{(1)} \le \dots \le x_{(n)}, the statistic is

Dn=max⁡imax⁡(in−F(x(i)),  F(x(i))−i−1n). D_n = \max_i \max\Bigl(\tfrac{i}{n} - F(x_{(i)}),\; F(x_{(i)}) - \tfrac{i-1}{n}\Bigr).

For a continuous FF, DnD_n has the same law whatever FF is. Marsaglia, Tsang and Wang showed that P(Dn<d)=n!nn(Hn)kkP(D_n < d) = \frac{n!}{n^n} (H^n)_{kk} for an explicit (2k−1)(2k-1)-square matrix HH with k=⌈nd⌉k = \lceil nd \rceil, whose entries are rational when dd is. So kolmogorovCdf(n, d) is exact: kolmogorovCdf(10, "0.274") is a fraction whose decimal expansion begins 0.6284796154565043, the value in their paper. When FF is certified, DnD_n is an enclosure and so is the p-value, since P(Dn≥d)P(D_n \ge d) falls as dd grows.

Far in the tail the matrix grows, and there a sharper route is cheaper: the one-sided statistic Dn+D_n^+ has the exact Smirnov–Birnbaum–Tingey tail

P(Dn+≥d)=d∑j=0⌊n(1−d)⌋(nj)(1−d−jn)n−j(d+jn)j−1, P(D_n^+ \ge d) = d\sum_{j=0}^{\lfloor n(1-d)\rfloor} \binom{n}{j}\Bigl(1 - d - \tfrac jn\Bigr)^{n-j}\Bigl(d + \tfrac jn\Bigr)^{j-1},

and P+≤P(Dn≥d)≤2P+P^+ \le P(D_n \ge d) \le 2P^+. When 2P+≤1/10002P^+ \le 1/1000 that enclosure is what is returned (method "one-sided bound"): it is tiny and decides any usual level. Above n=100n = 100 the p-value is simulated from a seed instead.

Two samples

const k = ksTest([1, 2, 3, 4, 5, 6], [3.5, 4.5, 6.5, 7.5, 8.5, 9.5]);
k.statistic;   // 2/3
k.pValue;      // 1/7, exact
ksTest([1, 2, 3, 4, 5, 6], [3.5, 4.5, 6.5, 7.5, 8.5, 9.5], { alternative: "less" }).pValue;   // 1/14

Under the null hypothesis every interleaving of the mm and nn values is equally likely. An interleaving is a lattice path from (0,0)(0,0) to (m,n)(m,n), and the event D<dD < d says the path stays inside a band around the diagonal. Counting the paths that stay inside, by dynamic programming over the grid, gives the exact p-value. With ties the band is only checked where the pooled value changes, which gives the exact conditional distribution. One-sided alternatives use one edge of the band; "greater" means xx tends to be larger, so its empirical cdf lies below.

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