Certified

Continuous distributions

The distributions behind the tables at the back of a statistics book, without the tables. A probability or a quantile is an enclosure: two rationals with the true value between them, as close together as you ask.

Using one

import { normal, studentT, chiSquare, show } from "@ratmath/stats";

const Z = normal();                       // mean 0, standard deviation 1
show(Z.cdf("1.96"), 20);                  // "0.97500210485177956586"   P(Z ≤ 1.96)
show(Z.sf(6), 15);                        // "0.000000000986588"        P(Z > 6)
show(Z.prob(-1, 1), 15);                  // "0.682689492137086"
show(Z.pdf(0), 15);                       // "0.398942280401433"        1/√(2π)
show(Z.quantile("0.975"), 18);            // "1.95996398454005424…"

show(studentT(9).quantile("0.975"), 15);  // "2.262157162798206"
show(chiSquare(4).sf("9.488"), 12);       // "0.049994405578"
show(normal(100, 15).sf(130), 12);        // "0.022750131948"

Each method takes { bits }: 96 for values and 64 for quantiles unless you say otherwise, and as many as you like.

show(Z.cdf("1.96", { bits: 160 }), 40);        // "0.9750021048517795658634157309591628099775"
show(Z.quantile("0.975", { bits: 200 }), 50);  // "1.95996398454005423552459443052055152795555007786955"

Where an exact answer exists it is returned as a fraction: uniform(0, 4).cdf(1) is 1/4, a mean such as gamma(3, 2).mean is 3/2, and studentT(9).variance is 9/7. A moment that does not exist is null: cauchy().mean.

The family

ConstructorDensity on its supportDistribution function, through
normal(μ, σ)1σ2πe−(x−μ)2/2σ2\frac{1}{\sigma\sqrt{2\pi}} e^{-(x-\mu)^2/2\sigma^2}the error function
lognormal(μ, σ)log⁡X\log X is normalthe error function of log⁡x\log x
gamma(shape, rate)βαΓ(α)xα−1e−βx\frac{\beta^\alpha}{\Gamma(\alpha)} x^{\alpha-1} e^{-\beta x}, x>0x \gt 0P(α,βx)P(\alpha, \beta x), the incomplete gamma function
chiSquare(k)gamma with shape k/2k/2, rate 1/21/2P(k/2,x/2)P(k/2, x/2)
exponential(rate)λe−λx\lambda e^{-\lambda x}, x>0x \gt 01−e−λx1 - e^{-\lambda x}
weibull(shape, scale)ks(x/s)k−1e−(x/s)k\frac{k}{s}(x/s)^{k-1} e^{-(x/s)^k}, x>0x \gt 01−e−(x/s)k1 - e^{-(x/s)^k}
beta(a, b)xa−1(1−x)b−1B(a,b)\frac{x^{a-1}(1-x)^{b-1}}{B(a,b)}, 0<x<10 \lt x \lt 1Ix(a,b)I_x(a, b), the incomplete beta function
studentT(df)∝(1+t2/ν)−(ν+1)/2\propto (1 + t^2/\nu)^{-(\nu+1)/2}Ix(ν/2,1/2)I_x(\nu/2, 1/2) with x=ν/(ν+t2)x = \nu/(\nu + t^2)
fDist(d1, d2)ratio of two scaled chi-squaresIx(d1/2,d2/2)I_x(d_1/2, d_2/2) with x=d1f/(d1f+d2)x = d_1 f/(d_1 f + d_2)
uniform(a, b)1/(b−a)1/(b-a), a<x<ba \lt x \lt bexact
cauchy(location, scale)1πs(1+((x−x0)/s)2)\frac{1}{\pi s (1 + ((x-x_0)/s)^2)}the arctangent

The incomplete gamma and beta functions are certified in the analytic library: a series of positive terms is added with every operation rounded outward, and the tail that is left off is bounded by a geometric series and added to the upper end.

Quantiles

A quantile is the xx where the distribution function crosses a level pp. The library starts from a floating-point guess, brackets it between two rationals a<x<ba \lt x \lt b with F(a)<p<F(b)F(a) \lt p \lt F(b) both certified, and narrows the bracket with secant steps. Because every sign is certified, the result is an enclosure of the true quantile, whatever the guess was. See Certified answers.

The fast side

Z.fast.cdf(1.96);          // 0.9750021048517794
Z.fast.quantile(0.975);    // 1.9599639845400538
Z.fast.pdf(0);             // 0.3989422804014327

Every distribution has floating-point versions under fast, for plotting and for simulations. They are accurate to roughly twelve digits or better and make no promise beyond that; the certified methods are the ones to quote.

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