@ratmath/stats

Statistics that says what kind of answer it is giving

Probability and statistics in one library, with three kinds of answer that are never mixed up. An exact answer is a fraction. A certified answer is a pair of fractions with the true value between them, as narrow as you ask. A simulated answer comes from a seeded random experiment, and carries its seed and its own sampling error.

What is here

A first program

import { dice, binomial, mean, variance, normal, show, binomialTest, model, estimatorStudy, fast } from "@ratmath/stats";

// exact: fractions
dice(2).prob(([a, b]) => a + b === 7);        // 1/6
binomial(10, "1/3").cdf(4);                   // 15488/19683
variance([2, 4, 4, 4, 5, 5, 7, 9]);           // 32/7
binomialTest(14, 20, "1/2").pValue;           // 15115/131072

// certified: enclosures, to any precision
show(normal().quantile("0.975"), 18);                 // "1.95996398454005424…"
show(normal().cdf("1.96", { bits: 160 }), 40);        // "0.9750021048517795658634157309591628099775"

// simulated: a known truth, disturbed, and a procedure judged against it
const line = model({ truth: [2, 0.5], x: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], noise: normal(0, 1) });
String(estimatorStudy({ model: line, n: 50, truth: 0.5, runs: 2000, seed: 1,
  estimator: (data) => fast.leastSquares(data.x, data.y).slope }));
// "mean 0.49993, sd 0.049765, bias -0.000066071 ± 0.0021811, rmse 0.049753 (simulated: 2000 runs, seed 1)"

Three kinds of answer

Exact. Finite probability, the discrete distributions with rational parameters, descriptive statistics of exact data, and the exact tests all work in fractions. P(two dice sum to 7)P(\text{two dice sum to } 7) is 1/61/6, and the variance of a list of decimals is the fraction it is. Nothing is rounded, so two exact answers can be compared with equals.

Certified. The normal distribution function has no closed form, so its values are enclosures: two rationals with the true value between them. Ask for more bits and the enclosure narrows. A comparison of a certified p-value with a level answers "below", "above" or "too close to call"; it never guesses. See Certified answers.

Simulated. Anything found by drawing random numbers says so: its kind is "simulated", it carries the seed that reproduces it, and a simulated rate comes with the exact binomial interval for the rate it estimates. See Experiments.

Data from a truth you chose

The part of this library that most packages lack: say what the truth is, say how observations depart from it (noise, unequal spread, contamination, error in xx, rounding, censoring, missing values), draw data, and see whether a test or a regression recovers what you put in. Changing one departure leaves the random numbers of the others alone, so two versions of a model can be compared on the same data. See Models with noise.

Inference and regression

The standard tests and intervals (z and t tests, chi-square and F tests, proportions, fitted distributions) keep their statistic exact where it can be: t2t^2, X2X^2 and FF are fractions, and the p-value is a certified enclosure computed at that exact argument. Linear regression and analysis of variance solve the normal equations over the rationals, so coefficients, residuals, leverages and R2R^2 are exact. The package's own tests then use seeded models to check that the intervals cover and the tests have their stated size; see Testing the tests.

Ranks, chains and beliefs

The rank tests (sign, Wilcoxon, Mann–Whitney, Kruskal–Wallis, Spearman, Kendall) count their null distributions, ties included, so their p-values are exact fractions; Kolmogorov–Smirnov p-values are exact too, by matrix powers and by counting lattice paths. The bootstrap is exact for samples small enough to weigh every resample, and seeded otherwise. Markov chains get their stationary distributions, absorption probabilities and hitting times exactly, and conjugate Bayesian updating keeps posterior parameters, means and predictive probabilities as fractions. See Rank tests, The bootstrap, Markov chains and Bayesian updating.

What is planned

From the library roadmap. Sizes are rough estimates in lines of source.

PhaseContentsEstimate
1Finite probability, discrete distributions, descriptive statistics, exact testsbuilt
2Continuous distributions, certifiedbuilt
3Seeded generation, models with noise and contamination, experimentsbuilt
4Estimation, confidence intervals, the standard testsbuilt
5Regression and analysis of variancebuilt
6Rank tests, permutation tests, the bootstrapbuilt
7Markov chains; conjugate Bayesian updatingbuilt
8Graphics hooks800 in graphics

The library stands on Core (fractions and enclosures), analytic (the certified special functions) and discrete (counting and the seeded generator, which is exported again here as rng).

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