algebraic.distAn algebra over probability distributions: compose, transform, and automatically simplify distribution expressions in R.
Tags: probability distributions, distribution algebra, automatic simplification, multivariate normal, mixture models, CLT, delta method, Monte Carlo, R, statistics
Install the stable release from CRAN:
install.packages("algebraic.dist")Or install the development version from GitHub:
# install.packages("devtools")
devtools::install_github("queelius/algebraic.dist")algebraic.dist lets you build and manipulate probability
distributions as first-class R objects. Algebraic operations
(+, -, *, /,
^, exp, log, min,
max, …) on distribution objects automatically simplify to
closed-form distributions when mathematical identities apply, and fall
back to lazy Monte Carlo expressions (edist) otherwise.
Normal, exponential, gamma, Weibull, chi-squared, uniform, beta, log-normal, Poisson, multivariate normal (MVN), mixture, and empirical distributions.
Over 20 built-in rules, including:
Normal + Normal → NormalGamma + Gamma (same rate) → Gammaexp(Normal) → LogNormalNormal(0,1)^2 → ChiSq(1)min(Exp, ..., Exp) → Expc * Uniform(a,b) → UniformWhen no rule matches, the result is a lazy edist that
samples from its components and evaluates the expression on demand.
conditional()affine_transform(x, A, b) for exact linear mapsclt() — Central Limit Theoremlln() — Law of Large Numbersdelta_clt() — delta method for transformed meansnormal_approx() — moment-matching normal
approximationlibrary(algebraic.dist)
# Sum of normals simplifies to a normal
x <- normal(1, 4)
y <- normal(2, 5)
z <- x + y
z
#> Normal distribution (mu = 3, var = 9)# exp of a normal simplifies to log-normal
w <- exp(normal(0, 1))
w
#> Log-normal distribution (meanlog = 0, sdlog = 1)# Gamma addition with matching rates
g <- gamma_dist(3, 2) + gamma_dist(4, 2)
g
#> Gamma distribution (shape = 7, rate = 2)# CLT: the standardized sample mean converges to N(0, 1)
clt(normal(5, 4))
#> Normal distribution (mu = 0, var = 4)The full documentation is available at https://queelius.github.io/algebraic.dist/.
Vignettes: