R package: algebraic.dist
An 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
Installation
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")Overview
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.
Distribution types
Normal, exponential, gamma, Weibull, chi-squared, uniform, beta, log-normal, Poisson, multivariate normal (MVN), mixture, and empirical distributions.
Automatic simplification
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)→Uniform
When no rule matches, the result is a lazy edist that samples from its
components and evaluates the expression on demand.
Multivariate operations
- MVN conditioning: closed-form Schur complement via
conditional() - Affine transforms:
affine_transform(x, A, b)for exact linear maps - Mixture conditioning: Bayesian weight updates via Bayes’ rule
- Marginals: exact for MVN and mixture distributions
Limiting distributions
clt()— Central Limit Theoremlln()— Law of Large Numbersdelta_clt()— delta method for transformed meansnormal_approx()— moment-matching normal approximation
Quick example
library(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)Documentation
The full documentation is available at https://queelius.github.io/algebraic.dist/.
Vignettes:
- Getting started — core distribution objects, sampling, and basic operations
- Distribution algebra — simplification rules, limiting distributions, and the CLT/LLN/delta method
- Multivariate operations — MVN conditioning, affine transforms, and Gaussian mixture models