Certified

Your own densities

Probability courses hand out densities like f(x)=cx2f(x) = cx^2 on [0,2][0, 2] and ask for cc, the mean and the variance; then they ask for the distribution of X2X^2, or of the largest of three draws. fromDensity, transform and orderStatistic answer those questions, exactly whenever the density is a polynomial on each piece.

A density from a formula

A function ff is a density on its support when f≥0f \ge 0 there and ∫f=1\int f = 1. fromDensity checks both before it builds anything. For a polynomial piece the sign is decided between its isolated real roots and the mass comes from the antiderivative, so both facts are exact and proven. For any other formula, f≥0f \ge 0 is proven by interval evaluation on ever smaller pieces (on an unbounded support only over a window, which checks.nonnegative names), and the mass is enclosed by analytic's certified integral. An enclosure that misses 1 proves the mass is wrong and the density is refused. An enclosure that contains 1 proves only that the mass is within its width of 1: (1+10−50)cos⁡x/sin⁡1(1 + 10^{-50})\cos x/\sin 1 on [0,1][0, 1] has mass 1+10−501 + 10^{-50} and passes. Such a density is accepted with checks.massProven false and the interval in checks.massEnclosure; pass { requireExactMass: true } to refuse it instead. With { normalize: true } the formula is divided by its mass, reported as constant.

import { fromDensity, show } from "@ratmath/stats";

const X = fromDensity("x^2", [0, 2], { normalize: true });
X.constant;   // 3/8: ∫₀² x² dx = 8/3
X.mean;       // 3/2 = ∫ x · (3/8)x² dx
X.variance;   // 3/20
X.cdf(1);     // 1/8 = (1/2)³
fromDensity("x", [0, 2]);   // refused: the total mass is 2, not 1

A list of pieces gives a piecewise density. Each piece is [formula, [a, b]], in order and meeting end to end. The triangle on [0,2][0, 2], the sum of two uniforms, is

const T = fromDensity([["x", [0, 1]], ["2 - x", [1, 2]]]);
T.mean;        // 1
T.variance;    // 1/6 = 1/12 + 1/12
fromDensity("exp(-x)", [0, "inf"]).cdf(1);   // ≈ 0.632121 = 1 − 1/e

checks records how f≥0f \ge 0 and the mass were established, so a reader can see which facts are exact and which rest on an enclosure: massProven is true only for an exact mass.

A function of a random variable

For gg increasing on the support, P(g(X)≤y)=P(X≤g−1(y))P(g(X) \le y) = P(X \le g^{-1}(y)), and the density is f(g−1(y))/∣g′(g−1(y))∣f(g^{-1}(y)) / |g'(g^{-1}(y))|. Decreasing gg flips the inequality. transform proves gg monotone from the sign of g′g' over the whole support (and refuses otherwise), finds g−1(y)g^{-1}(y) exactly when it is a simple fraction and encloses it by bisection otherwise, and keeps the mean and variance exact when XX has polynomial pieces and gg is a polynomial.

import { uniform } from "@ratmath/stats";

const Y = uniform(0, 1).transform("x^2");
Y.cdf("1/4");   // 1/2: P(X² ≤ 1/4) = P(X ≤ 1/2), with g⁻¹(1/4) = 1/2 found exactly
Y.pdf("1/4");   // 1 = 1/(2·√(1/4))
Y.mean;         // 1/3 = E[X²]
uniform(-1, 1).transform("x^2");   // refused: not monotone on [−1, 1]

In this version g′g' must have an enclosure on the whole support, so x\sqrt x works on [1,4][1, 4] but is refused on [0,4][0, 4], where its derivative is unbounded; on an unbounded support only affine gg is accepted, with mean aμ+ba\mu + b and variance a2σ2a^2\sigma^2.

Order statistics

The kk-th smallest of nn independent draws, X(k)X_{(k)}, has P(X(k)≤x)=∑j≥k(nj)F(x)j(1−F(x))n−jP(X_{(k)} \le x) = \sum_{j \ge k} \binom n j F(x)^j (1 - F(x))^{n-j} and density n!(k−1)! (n−k)!Fk−1(1−F)n−kf\frac{n!}{(k-1)!\,(n-k)!} F^{k-1} (1-F)^{n-k} f. For a uniform or polynomial density these are polynomials again, so the result has exact moments. The maximum of nn uniforms on [0,1][0, 1] has density nxn−1n x^{n-1}, which is Beta(nn, 1).

const M = uniform(0, 1).orderStatistic(3, 3);   // the largest of three
M.mean;         // 3/4 = n/(n+1)
M.variance;     // 3/80
M.pdf("1/2");   // 3/4 = 3 · (1/2)²
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