Skip to main content

Module stats

Module stats 

Source
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 on Distribution answers every query from those: it clips an event’s region to the support and measures each piece through the CDF, or by exact integrate_definite / summation of the density, and it takes E[g(X)] through the raw moments when g is a polynomial. Implement Family to add a distribution of your own.
  • Families compose: Truncated (conditioning), Affine (aX + b), Transformed (g(X) by the change-of-variables formula) and Mixture wrap 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 the try_ constructors.
  • Nothing here is numerical by default: Distribution::sample is 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 Distribution so it plugs into the rest of stats.
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 of n independent draws from a distribution, as a Family of its own.
regression
Regression: exact ordinary and weighted least squares over ℚ, and logistic regression by Newton–Raphson in f64 (statsmodels’ OLS, WLS, Logit; numpy’s polyfit; scipy’s linregress).
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 + b for a ≠ 0 of known sign. Every closed form of X is transported exactly: E[Y] = aμ + b, Var[Y] = a²σ², raw moments by the binomial expansion, F_Y(y) = F_X((y−b)/a) (mirrored for a < 0), M_Y(t) = e^{bt} M_X(at), Q_Y(p) = aQ_X(p) + b (or aQ_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 − p on {0, 1}.
Beta
Beta(α, β): density x^{α−1} (1−x)^{β−1} / B(α, β) on [0, 1].
Binomial
Binomial(n, p): P(X = k) = C(n, k) pᵏ (1−p)ⁿ⁻ᵏ on 0..=n.
Cauchy
Cauchy(x₀, γ): density 1 / (πγ (1 + ((x−x₀)/γ)²)) on ℝ. No moments of any order exist.
ChiSquared
ChiSquared(k) = Gamma(k/2, 2): density x^{k/2−1} e^{−x/2} / (2^{k/2} Γ(k/2)) on [0, ∞).
DiscreteUniform
DiscreteUniform(a, b): P(X = k) = 1/(b−a+1) on the integers a..=b. Die(s) is DiscreteUniform(1, s).
Distribution
A probability distribution: a shared handle to a Family with the generic exact machinery on top. Construct with the associated functions (Distribution::normal(…), binomial(…), …, each with a try_ twin validating numeric parameters), with the wrapper constructors (truncated, affine, transformed, mixture), or from your own family with from_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’s FDistribution(d1, d2)).
Finite
An explicit finite table P(X = vᵢ) = pᵢ (SymPy FiniteRV); the values need not be integers.
Gamma
Gamma(k, θ): density x^{k−1} e^{−x/θ} / (Γ(k) θᵏ) on [0, ∞) (shape–scale parametrisation, SymPy’s Gamma(k, theta)).
Geometric
Geometric(p): the number of trials up to and including the first success, P(X = k) = (1−p)^{k−1} p on 1..∞ (SymPy’s convention).
Hypergeometric
Hypergeometric(N, m, n): the number of successes in n draws without replacement from a population of N containing m successes, P(X = k) = C(m, k) C(N−m, n−k) / C(N, n) on max(0, n+m−N) ..= min(n, m).
Laplace
Laplace(μ, b): density e^{−|x−μ|/b} / (2b) on ℝ.
LogNormal
LogNormal(μ, σ): ln X ~ Normal(μ, σ); density e^{−(ln x − μ)²/(2σ²)} / (xσ√(2π)) on (0, ∞).
Logistic
Logistic(μ, s): density e^{−(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.
NegativeBinomial
NegativeBinomial(r, p): the number of failures before the r-th success, P(X = k) = C(k+r−1, k) pʳ (1−p)ᵏ on 0..∞ (SymPy’s convention).
Normal
Normal(μ, σ): density e^{−(x−μ)²/(2σ²)} / (σ√(2π)) on ℝ.
Pareto
Pareto(x_m, α): density α x_mᵅ / x^{α+1} on [x_m, ∞).
Poisson
Poisson(λ): P(X = k) = λᵏ e^{−λ} / k! on 0..∞.
RandomVariable
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 Kind that says how a density over it is read.
Transformed
Y = g(X) for a continuous X and a g with 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 by Distribution::transformed, which recognises a strictly monotone g on the support (one branch), and , |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 from 0 at a to 2/(b−a) at the mode c and falling linearly to 0 at b, on [a, b] with a ≤ c ≤ b.
Truncated
The inner distribution conditioned on a region: density f(x)/P(R) on support ∩ R. Built by Distribution::truncated / RandomVariable::given. SymPy: given(X, cond).
Uniform
Uniform(a, b): density 1/(b − a) on [a, b].
Weibull
Weibull(λ, k): density (k/λ) (x/λ)^{k−1} e^{−(x/λ)ᵏ} on [0, ∞). SymPy’s Weibull(alpha, beta) has alpha = λ (scale) and beta = 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 Distribution integrate 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 · density integrated (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 normal X).
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)/2 for a standard normal).
expectation
E[g(X₁, …, Xₖ)] for independent random variables. SymPy: E(X*Y), E(X + Y), E(expr) with several variables in expr.
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_family delegates to: the other family is the same concrete type and compares equal.
sum_distribution
The distribution of A + B for independent a, b when it belongs to a closed family; None otherwise. The closure results implemented (parameters that must agree — p, λ — are compared structurally):
variance
Var[g] = E[g²] − E[g]² for an expression g in independent variables. SymPy: variance(X + Y), variance(2*X + 1).

Type Aliases§

Sampler
A closure producing one f64 sample per call.