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
| Constructor | Density on its support | Distribution function, through |
|---|---|---|
| normal(μ, σ) | the error function | |
| lognormal(μ, σ) | is normal | the error function of |
| gamma(shape, rate) | , | , the incomplete gamma function |
| chiSquare(k) | gamma with shape , rate | |
| exponential(rate) | , | |
| weibull(shape, scale) | , | |
| beta(a, b) | , | , the incomplete beta function |
| studentT(df) | with | |
| fDist(d1, d2) | ratio of two scaled chi-squares | with |
| uniform(a, b) | , | exact |
| cauchy(location, scale) | 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 where the distribution function crosses a level . The library starts from a floating-point guess, brackets it between two rationals with 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.
@ratmath/stats and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.