Your own densities
Probability courses hand out densities like on and ask for , the mean and the variance; then they ask for the distribution of , 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 is a density on its support when there and . 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, 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: on has mass 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 , 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 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 increasing on the support, , and the density is . Decreasing flips the inequality. transform proves monotone from the sign of over the whole support (and refuses otherwise), finds exactly when it is a simple fraction and encloses it by bisection otherwise, and keeps the mean and variance exact when has polynomial pieces and 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 must have an enclosure on the whole support, so works on but is refused on , where its derivative is unbounded; on an unbounded support only affine is accepted, with mean and variance .
Order statistics
The -th smallest of independent draws, , has and density . For a uniform or polynomial density these are polynomials again, so the result has exact moments. The maximum of uniforms on has density , which is Beta(, 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)²
@ratmath/stats and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.