Exact

Discrete distributions

The named distributions on whole numbers. With rational parameters every probability of the binomial, geometric, negative binomial, hypergeometric and uniform distributions is a fraction. The Poisson distribution involves e−λe^{-\lambda}, so its probabilities are certified enclosures.

Using one

import { binomial, hypergeometric, geometric, negativeBinomial, poisson, show } from "@ratmath/stats";

const X = binomial(10, "1/3");
X.pmf(4);              // 4480/19683     P(X = 4)
X.cdf(4);              // 15488/19683    P(X ≤ 4)
X.sf(4);               // 4195/19683     P(X > 4)
X.prob(2, 5);          // 1792/2187      P(2 ≤ X ≤ 5)
X.quantile("0.9");     // 5              the least k with P(X ≤ k) ≥ 0.9
X.mean;                // 10/3
X.variance;            // 20/9
show(X.sd(), 12);      // "1.490711985000"
X.kind;                // "exact"

Parameters are read exactly: "1/3" is a third, and the JS number 0.1 is read by its decimal form as one tenth. toFinite() turns a distribution with finitely many values into a finite distribution, and sample(rng) draws from a seeded generator.

The family

ConstructorCountsP(X=k)P(X = k)Mean, variance
bernoulli(p)one trialpk(1−p)1−kp^k (1-p)^{1-k}p, p(1−p)p,\ p(1-p)
binomial(n, p)successes in nn trials(nk)pk(1−p)n−k\binom{n}{k} p^k (1-p)^{n-k}np, np(1−p)np,\ np(1-p)
geometric(p)trials up to the first success(1−p)k−1p(1-p)^{k-1} p1/p, (1−p)/p21/p,\ (1-p)/p^2
negativeBinomial(r, p)failures before the rrth success(k+r−1k)pr(1−p)k\binom{k+r-1}{k} p^r (1-p)^kr(1−p)/p, r(1−p)/p2r(1-p)/p,\ r(1-p)/p^2
hypergeometric(N, K, n)marked items among nn drawn from NN with KK marked(Kk)(N−Kn−k)/(Nn)\binom{K}{k}\binom{N-K}{n-k} \big/ \binom{N}{n}nK/NnK/N, and the finite-population variance
discreteUniform(a, b)a whole number from aa to bb1/(b−a+1)1/(b-a+1)(a+b)/2, ((b−a+1)2−1)/12(a+b)/2,\ ((b-a+1)^2-1)/12
poisson(λ)events at rate λ\lambdae−λλk/k!e^{-\lambda} \lambda^k / k!λ, λ\lambda,\ \lambda

geometric(p, { failures: true }) counts failures before the first success instead, starting at 0.

Poisson is certified

const N = poisson(3);
N.kind;                          // "certified"
show(N.pmf(2), 20);              // "0.22404180765538774341"
show(N.cdf(5), 15);              // "0.916082057968697"
N.quantile("0.99");              // 8: a whole number, decided for certain
N.pmf(2, { bits: 200 });         // the same enclosure, about 60 digits wide

e−3e^{-3} is irrational, so pmf returns a RationalInterval that contains the true value; { bits } sets how narrow. A quantile is still an exact whole number: the library raises the precision until the cumulative sums are separated from the level.

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