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 , 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
| Constructor | Counts | Mean, variance | |
|---|---|---|---|
| bernoulli(p) | one trial | ||
| binomial(n, p) | successes in trials | ||
| geometric(p) | trials up to the first success | ||
| negativeBinomial(r, p) | failures before the th success | ||
| hypergeometric(N, K, n) | marked items among drawn from with marked | , and the finite-population variance | |
| discreteUniform(a, b) | a whole number from to | ||
| poisson(λ) | events at rate |
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
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.
@ratmath/stats and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.