Expand description
Symbolic probability and statistics: random variables, exact moments, probabilities, densities.
Symbolic probability and statistics (SymPy’s stats): random variables
with named distributions, exact moments, probabilities of events,
densities and distribution functions — all as expressions — plus
conditioning, transformations and mixtures of distributions.
A RandomVariable is a symbol together with a Distribution. The
queries are exact where the distribution’s parameters are exact:
use symplex::prelude::*;
use symplex::stats::{Distribution, RandomVariable};
let ctx = Context::new();
let x = RandomVariable::new(&ctx, "X", Distribution::normal(ctx.int(0), ctx.int(1)));
assert_eq!(x.mean(), ctx.int(0));
assert_eq!(x.variance(), ctx.int(1));
// E[X² + 3X] for a standard normal.
assert_eq!(x.expectation(&(x.symbol().powi(2) + 3 * x.symbol())).simplify(), ctx.int(1));
let y = RandomVariable::new(&ctx, "Y", Distribution::binomial(ctx.int(5), ctx.rational(1, 3)));
assert_eq!(y.mean(), ctx.rational(5, 3));
assert_eq!(y.probability(&y.symbol().gt(&ctx.int(2)))?, ctx.rational(17, 81));
// Conditioning, and a transformed variable.
let half = x.given(&x.symbol().gt(&ctx.int(0)))?; // X | X > 0
assert_eq!(half.mean().equals(&(ctx.int(2) / ctx.pi()).sqrt()), Some(true));
let w = x.transform("W", &(2 * x.symbol() + 1))?; // 2X + 1 ~ Normal(1, 2)
assert_eq!(w.variance(), ctx.int(4));§Design
- A distribution is a
Family: its support (Support, a finite union of intervals and points, continuous or on the integer lattice), its density (or probability mass function) as an expression in a free variable, and the closed forms it happens to have (moments, CDF, MGF, quantile, entropy), each optional. The generic machinery onDistributionanswers every query from those: it clips an event’s region to the support and measures each piece through the CDF, or by exactintegrate_definite/summationof the density, and it takesE[g(X)]through the raw moments whengis a polynomial. ImplementFamilyto add a distribution of your own. - Families compose:
Truncated(conditioning),Affine(aX + b),Transformed(g(X)by the change-of-variables formula) andMixturewrap other distributions and transport their closed forms exactly where the transport is exact. - Parameters are expressions: rational parameters give exact rational
answers, symbolic parameters give symbolic answers (
E[X] = μ). Parameter validity (σ > 0,0 ≤ p ≤ 1) is the caller’s promise for symbolic parameters and is checked for numeric ones by thetry_constructors. - Nothing here is numerical by default:
Distribution::sampleis the only place a random number generator appears, and it is a plain deterministic-seed generator so tests are reproducible.
Distribution families: the structs re-exported below
(Normal, Binomial, …); each has a Distribution::name(…)
constructor and a try_name twin.
Modules§
- aggregation
- Turning many raters’ labels into one answer: majority, plurality and weighted votes (exact), the Dawid–Skene EM model of rater confusion, Bradley–Terry strengths from pairwise comparisons, and the worker quality helpers that go with them (accuracy against gold labels, per-category precision / recall / F₁, confidence intervals for a proportion, gold-question screening).
- agreement
- Inter-rater agreement, exactly: percent agreement, Cohen’s κ (plain and weighted), Scott’s π, Fleiss’ κ, Krippendorff’s α, Gwet’s AC₁, the intraclass correlations of Shrout & Fleiss and Kendall’s W.
- data
- Descriptive statistics on observed data, exactly.
- estimation
- Parameter estimation from observed data: maximum likelihood, the method
of moments, and Bayesian conjugate updating — every estimate handed back
as a
Distributionso it plugs into the rest ofstats. - hypothesis
- Hypothesis tests, effect sizes, multiple-comparison corrections, resampling and power / sample-size utilities.
- information
- Information theory on finite distributions, exactly: entropies, divergences and distances between probability vectors, and the information shared by the two margins of a joint table.
- markov
- Discrete-time Markov chains on a finite state space with an exact
rational transition matrix (SymPy’s
stats.DiscreteMarkovChain). - multivariate
- The multivariate normal distribution with symbolic parameters, and exact multivariate descriptive statistics on data (covariance and correlation matrices, principal components).
- order
- Order statistics: the distribution of the
k-th smallest ofnindependent draws from a distribution, as aFamilyof its own. - regression
- Regression: exact ordinary and weighted least squares over ℚ, and
logistic regression by Newton–Raphson in
f64(statsmodels’OLS,WLS,Logit; numpy’spolyfit; scipy’slinregress). - reliability
- Scale reliability, item analysis and further agreement / association statistics: Cronbach’s α and its relatives (standardized α, KR-20, split-half with the Spearman–Brown prophecy, α-if-deleted, Guttman’s λ₂), classical item analysis (difficulty, the D discrimination index, point-biserial and corrected item–total correlations), a confidence interval and a test for Cohen’s κ, κ_max, Cochran’s Q, the ordinal association measures (Goodman–Kruskal γ, Somers’ D, Stuart’s τ-c), contingency-table diagnostics (expected counts, χ² contributions, standardized and adjusted residuals) and inference on Pearson’s r (Fisher’s z, a confidence interval, the t-test, comparing two r’s).
- sequential
- Wald’s sequential probability ratio test (SPRT): decide between two simple hypotheses one observation at a time, stopping as soon as the evidence is strong enough — the tool for screening workers, raters or models on the fly rather than after a fixed batch.
- survival
- Time-to-event analysis with right censoring, exactly: how long until a respondent completes (or abandons) a task, how long a worker stays active, how long an item takes to reach agreement.
Structs§
- Affine
Y = aX + bfora ≠ 0of known sign. Every closed form ofXis transported exactly:E[Y] = aμ + b,Var[Y] = a²σ², raw moments by the binomial expansion,F_Y(y) = F_X((y−b)/a)(mirrored fora < 0),M_Y(t) = e^{bt} M_X(at),Q_Y(p) = aQ_X(p) + b(oraQ_X(1−p) + b),H[Y] = H[X] + ln|a|(continuous). SymPy:density(a*X + b).- Bernoulli
Bernoulli(p):P(X = 1) = p,P(X = 0) = 1 − pon{0, 1}.- Beta
Beta(α, β): densityx^{α−1} (1−x)^{β−1} / B(α, β)on[0, 1].- Binomial
Binomial(n, p):P(X = k) = C(n, k) pᵏ (1−p)ⁿ⁻ᵏon0..=n.- Cauchy
Cauchy(x₀, γ): density1 / (πγ (1 + ((x−x₀)/γ)²))on ℝ. No moments of any order exist.- ChiSquared
ChiSquared(k)=Gamma(k/2, 2): densityx^{k/2−1} e^{−x/2} / (2^{k/2} Γ(k/2))on[0, ∞).- Discrete
Uniform DiscreteUniform(a, b):P(X = k) = 1/(b−a+1)on the integersa..=b.Die(s)isDiscreteUniform(1, s).- Distribution
- A probability distribution: a shared handle to a
Familywith the generic exact machinery on top. Construct with the associated functions (Distribution::normal(…),binomial(…), …, each with atry_twin validating numeric parameters), with the wrapper constructors (truncated,affine,transformed,mixture), or from your own family withfrom_family. - Exponential
Exponential(λ): densityλ e^{−λx}on[0, ∞).- FDistribution
FDistribution(d₁, d₂): density√((d₁x)^{d₁} d₂^{d₂} / (d₁x + d₂)^{d₁+d₂}) / (x B(d₁/2, d₂/2))on(0, ∞)(SymPy’sFDistribution(d1, d2)).- Finite
- An explicit finite table
P(X = vᵢ) = pᵢ(SymPyFiniteRV); the values need not be integers. - Gamma
Gamma(k, θ): densityx^{k−1} e^{−x/θ} / (Γ(k) θᵏ)on[0, ∞)(shape–scale parametrisation, SymPy’sGamma(k, theta)).- Geometric
Geometric(p): the number of trials up to and including the first success,P(X = k) = (1−p)^{k−1} pon1..∞(SymPy’s convention).- Hypergeometric
Hypergeometric(N, m, n): the number of successes inndraws without replacement from a population ofNcontainingmsuccesses,P(X = k) = C(m, k) C(N−m, n−k) / C(N, n)onmax(0, n+m−N) ..= min(n, m).- Laplace
Laplace(μ, b): densitye^{−|x−μ|/b} / (2b)on ℝ.- LogNormal
LogNormal(μ, σ):ln X ~ Normal(μ, σ); densitye^{−(ln x − μ)²/(2σ²)} / (xσ√(2π))on(0, ∞).- Logistic
Logistic(μ, s): densitye^{−(x−μ)/s} / (s (1 + e^{−(x−μ)/s})²)on ℝ.- Mixture
- A finite mixture
Σ wᵢ Fᵢof distributions of one kind. Every query is the weighted sum of the components’ answers, so nothing is lost when their supports overlap. - Negative
Binomial NegativeBinomial(r, p): the number of failures before ther-th success,P(X = k) = C(k+r−1, k) pʳ (1−p)ᵏon0..∞(SymPy’s convention).- Normal
Normal(μ, σ): densitye^{−(x−μ)²/(2σ²)} / (σ√(2π))on ℝ.- Pareto
Pareto(x_m, α): densityα x_mᵅ / x^{α+1}on[x_m, ∞).- Poisson
Poisson(λ):P(X = k) = λᵏ e^{−λ} / k!on0..∞.- Random
Variable - A random variable: a symbol with a distribution. Expressions in the symbol are random quantities; the queries below evaluate them exactly.
- Rng
- A deterministic pseudo-random generator (SplitMix64). Not cryptographic; seeded, reproducible, and good enough for Monte-Carlo sanity checks of exact results.
- StudentT
StudentT(ν): densityΓ((ν+1)/2) / (√(νπ) Γ(ν/2)) · (1 + x²/ν)^{−(ν+1)/2}on ℝ.- Support
- A finite union of intervals and points on the line, with the
Kindthat says how a density over it is read. - Transformed
Y = g(X)for a continuousXand agwith explicit inverse branches:f_Y(y) = Σ_i f_X(h_i(y)) |h_i′(y)|(the change-of-variables formula), on the image of the support. Built byDistribution::transformed, which recognises a strictly monotonegon the support (one branch), andX²,|X|, and even powers on a support symmetric enough for the two-branch formula. SymPy:density(g(X)).- Triangular
Triangular(a, b, c): density rising linearly from0atato2/(b−a)at the modecand falling linearly to0atb, on[a, b]witha ≤ c ≤ b.- Truncated
- The inner distribution conditioned on a region: density
f(x)/P(R)onsupport ∩ R. Built byDistribution::truncated/RandomVariable::given. SymPy:given(X, cond). - Uniform
Uniform(a, b): density1/(b − a)on[a, b].- Weibull
Weibull(λ, k): density(k/λ) (x/λ)^{k−1} e^{−(x/λ)ᵏ}on[0, ∞). SymPy’sWeibull(alpha, beta)hasalpha = λ(scale) andbeta = k(shape).
Enums§
- Kind
- Whether a distribution puts its mass on intervals of ℝ (a density) or on isolated points (a probability mass function).
- Piece
- One connected part of a
Support.
Traits§
- Family
- A probability distribution family: its support and density, plus
whatever closed forms it has (each defaults to “none”, which makes the
generic machinery in
Distributionintegrate or sum instead).
Functions§
- conditional_
expectation E[g(X) | event] = E[g(X)·1_event] / P(event)for an event in the same variable:g · densityintegrated (summed) over the part of the support where the event holds, divided by its probability. SymPy:E(X, X > 0)(=√(2/π)for a standard normal),E(X**2, X > 0).- conditional_
probability P(event | given) = P(event ∧ given) / P(given)for two events in the same variable. SymPy:P(X > 1, X > 0)(=2·P(X > 1)for a standard normal).- correlation
- Pearson correlation
Cov[g, h] / √(Var[g]·Var[h]). SymPy:correlation(X, 2*X + 1)(= 1). - covariance
Cov[g, h] = E[gh] − E[g]E[h]for expressions in independent variables. SymPy:covariance(X, 2*X)(= 2 for a standard normalX).- entropy
- The (differential) entropy
H[X] = −E[ln f(X)]in nats — for a discrete variable the Shannon entropy−Σ p(k) ln p(k). SymPy:entropy(X)(log(2)/2 + 1/2 + log(pi)/2for a standard normal). - expectation
E[g(X₁, …, Xₖ)]for independent random variables. SymPy:E(X*Y),E(X + Y),E(expr)with several variables inexpr.- probability
P(event)for an event in several independent variables. SymPy:P(And(X > 0, Y > 0)),P(X < Y).- same_
family - The equality every family’s
Family::eq_familydelegates to: the other family is the same concrete type and compares equal. - sum_
distribution - The distribution of
A + Bfor independenta,bwhen it belongs to a closed family;Noneotherwise. The closure results implemented (parameters that must agree —p,λ— are compared structurally): - variance
Var[g] = E[g²] − E[g]²for an expressiongin independent variables. SymPy:variance(X + Y),variance(2*X + 1).
Type Aliases§
- Sampler
- A closure producing one
f64sample per call.