Exact

Exact tests

Tests whose null distribution is finite, so the p-value is a fraction found by counting. No normal approximation, and no minimum sample size.

The binomial test

import { binomialTest, proportionInterval, show } from "@ratmath/stats";

const t = binomialTest(14, 20, "1/2");       // 14 successes in 20 trials; is p = 1/2?
String(t);              // "exact binomial test: p = 0.115318 (exact, two-sided)"
t.pValue;               // 15115/131072
t.decision("0.05");     // "keep"
t.reject("0.05");       // false
binomialTest(14, 20, "1/2", { alternative: "greater" }).pValue;   // 15115/262144

Under H0H_0 the count is binomial(n,p0)(n, p_0). The one-sided p-values are tail sums. The two-sided p-value adds the probability of every count that is no more likely than the one observed,

p=∑k : P(k)≤P(kobs)P(k), p = \sum_{k \,:\, P(k) \le P(k_{\text{obs}})} P(k),

which is exact for any p0p_0, symmetric or not. Comparing fractions for "no more likely" is exact here; in floating point, ties are decided by rounding error.

An interval for a proportion

const ci = proportionInterval(14, 20);       // Clopper and Pearson's interval, 95%
show(ci.low, 6);        // "0.457211"
show(ci.high, 6);       // "0.881068"
proportionInterval(14, 20, { method: "wilson" });   // 0.481027 to 0.854523
proportionInterval(14, 20, { method: "wald", level: "0.99" });   // 0.4361 to 0.9639

The "exact" interval is the set of p0p_0 that the two one-sided binomial tests do not reject at α/2\alpha/2 each. Its ends are quantiles of beta distributions,

low=B−1 ⁣(α2; k, n−k+1),high=B−1 ⁣(1−α2; k+1, n−k), \text{low} = B^{-1}\!\left(\tfrac{\alpha}{2};\, k,\, n-k+1\right), \qquad \text{high} = B^{-1}\!\left(1-\tfrac{\alpha}{2};\, k+1,\, n-k\right),

which are certified and rounded outward, so the interval's coverage is never below its level. Wilson's interval and the textbook Wald interval p^±zp^(1−p^)/n\hat p \pm z\sqrt{\hat p(1-\hat p)/n} are offered for comparison; the experiments page measures how often each contains the truth.

Fisher's exact test

import { fisherTest } from "@ratmath/stats";

const f = fisherTest([[3, 1], [1, 3]]);
f.pValue;               // 17/35
f.oddsRatio;            // 9
fisherTest([[3, 1], [1, 3]], { alternative: "greater" }).pValue;   // 17/70

With the row and column totals of a 2 by 2 table held fixed, the top-left count is hypergeometric when the rows and columns are unrelated. The p-value adds up the tables that are no more likely than the one observed.

Permutation tests

import { permutationTest } from "@ratmath/stats";

const p = permutationTest([12, 15, 11, 18, 14], [9, 10, 13, 8, 11]);
p.pValue;               // 1/21: of the 252 ways to deal ten values into two fives
p.kind;                 // "exact"
permutationTest([12, 15, 11, 18, 14], [9, 10, 13, 8, 11], { paired: true }).pValue;   // 1/8
permutationTest(x, y, { statistic: "median" });

If two groups do not differ, every way of dealing the pooled values into groups of these sizes is equally likely. The p-value is the fraction of deals whose difference in means (or medians, or any statistic you pass) is at least as extreme as the one observed. With { paired: true } the deals are the 2n2^n ways of swapping within pairs.

When there are more deals than limit (200,000 unless you say otherwise), the test draws resamples random deals from seed instead. Then the result's kind is "simulated", and pValueInterval is an exact interval for the p-value it estimates. A result never changes kind silently.

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