Learn R Programming

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 + NormalNormal
  • Gamma + Gamma (same rate) → Gamma
  • exp(Normal)LogNormal
  • Normal(0,1)^2ChiSq(1)
  • min(Exp, ..., Exp)Exp
  • c * 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 Theorem
  • lln() — Law of Large Numbers
  • delta_clt() — delta method for transformed means
  • normal_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:

Copy Link

Version

Install

install.packages('algebraic.dist')

Monthly Downloads

280

Version

0.9.1

License

GPL (>= 3)

Issues

Pull Requests

Stars

Forks

Maintainer

Alexander Towell

Last Published

February 27th, 2026

Functions in algebraic.dist (0.9.1)

cdf.weibull_dist

Cumulative distribution function for a Weibull distribution.
conditional.edist

Conditional distribution for expression distributions.
conditional.empirical_dist

Method for obtaining the condition distribution, x | P(x), of empirical_dist object x.
density.chi_squared

Method for obtaining the density (pdf) of a chi_squared object.
density.mvn

Function generator for obtaining the pdf of an mvn object (multivariate normal).
density.beta_dist

Probability density function for a beta distribution.
density.normal

Method for obtaining the pdf of an normal object.
delta_clt

Delta Method CLT Limiting Distribution
countable_set

Countable Set
density.uniform_dist

Probability density function for a uniform distribution.
density.poisson_dist

Probability mass function for a Poisson distribution.
dim.chi_squared

Retrieve the dimension of a chi_squared object.
density.gamma_dist

Method for obtaining the density (pdf) of a gamma_dist object.
density.exponential

Method to obtain the pdf of an exponential object.
conditional.mixture

Conditional distribution of a mixture.
conditional.mvn

Conditional distribution for multivariate normal.
cdf.lognormal

Cumulative distribution function for a log-normal distribution.
dim.countable_set

Get the dimension of a countable set.
dim.interval

Return the dimension of the interval.
dim.weibull_dist

Dimension of a Weibull distribution (always 1).
dim.uniform_dist

Dimension of a uniform distribution (always 1).
dim.beta_dist

Dimension of a beta distribution (always 1).
density.weibull_dist

Probability density function for a Weibull distribution.
dim.mixture

Dimension of a mixture distribution.
expectation_data

Function used for computing expectations given data (e.g., from an MC simulation or bootstrap). it expects a matrix, or something that can be coerced to a matrix (e.g., a data frame). it also expects a function g to apply to each row of the data, and returns the expectation of g under the empirical distribution of the data. it also returns a confidence interval for the expectation, and the number of samples used to compute the expectation.
dim.mvn

Method for obtaining the dimension of an mvn object.
density.edist

Density for expression distributions.
dim.finite_set

Return the dimension of the finite set.
dim.gamma_dist

Retrieve the dimension of a gamma_dist object.
density.empirical_dist

Method for obtaining the pdf of a empirical_dist object.
expectation.poisson_dist

Exact expectation for a Poisson distribution.
edist

Takes an expression e and a list vars and returns a lazy edist (expression distribution object), that is a subclass of dist that can be used in place of a dist object.
format.empirical_dist

Format method for empirical_dist objects.
empirical_dist

Construct empirical distribution object.
expectation.empirical_dist

Method for obtaining the expectation of empirical_dist object x under function g.
expectation.dist

Expectation of a Function Applied to a dist Object
format.exponential

Format method for exponential objects.
dim.exponential

Method to obtain the dimension of an exponential object.
dim.empirical_dist

Method for obtaining the dimension of a empirical_dist object.
ensure_realized

Memoized MC fallback materialization.
format.weibull_dist

Format a weibull_dist object as a character string.
format.lognormal

Format a lognormal object as a character string.
expectation.univariate_dist

Method for obtaining the expectation of f with respect to a univariate_dist object x.
format.gamma_dist

Format a gamma_dist object as a character string.
expectation

Generic method for obtaining the expectation of f with respect to x.
conditional

Generic method for obtaining the conditional distribution of a distribution object x given condition P.
-.dist

Method for negation or subtraction of dist objects.
density.lognormal

Probability density function for a log-normal distribution.
Summary.dist

Summary group generic for distribution objects.
format.realized_dist

Format a realized_dist object as a character string.
density.mixture

Probability density function for a mixture distribution.
conditional.dist

Method for obtaining the condition distribution, x | P(x), of dist object x.
format.normal

Format method for normal objects.
has.interval

Determine if a value is contained in the interval.
has.finite_set

Determine if a value is contained in the finite set.
format.poisson_dist

Format a poisson_dist object as a character string.
interval

Interval
inv_cdf

Generic method for obtaining the quantile (inverse cdf) of an object.
gamma_dist

Construct a gamma distribution object.
inv_cdf.lognormal

Inverse CDF (quantile function) for a log-normal distribution.
dim.lognormal

Dimension of a log-normal distribution (always 1).
hazard.lognormal

Hazard function for a log-normal distribution.
dim.poisson_dist

Dimension of a Poisson distribution (always 1).
finite_set

Finite set
dim.normal

Method for obtaining the dimension of a normal object.
is_exponential

Function to determine whether an object x is an exponential object.
hazard.weibull_dist

Hazard function for a Weibull distribution.
format.mixture

Format a mixture object as a character string.
format.mvn

Format method for mvn objects.
infimum.interval

Return the (vector of) infimum of the interval.
exponential

Construct exponential distribution object.
infimum.finite_set

Return the infimum of the finite set.
hazard

Generic method for obtaining the hazard function of an object.
hazard.gamma_dist

Method for obtaining the hazard function of a gamma_dist object.
format.beta_dist

Format a beta_dist object as a character string.
format.edist

Format method for edist objects.
hazard.chi_squared

Method for obtaining the hazard function of a chi_squared object.
hazard.exponential

Method to obtain the hazard function of an exponential object.
format.chi_squared

Format a chi_squared object as a character string.
has

Support
inv_cdf.chi_squared

Method for obtaining the inverse cdf (quantile function) of a chi_squared object.
is_lognormal

Test whether an object is a lognormal.
inv_cdf.beta_dist

Inverse CDF (quantile function) for a beta distribution.
inv_cdf.normal

Method for obtaining the inverse cdf of an normal object.
infimum

Get the infimum of the support.
has.countable_set

Check membership in a countable set.
is_uniform_dist

Test whether an object is a uniform_dist.
is_gamma_dist

Test whether an object is a gamma_dist.
format.uniform_dist

Format a uniform_dist object as a character string.
inv_cdf.poisson_dist

Inverse CDF (quantile function) for a Poisson distribution.
is_beta_dist

Test whether an object is a beta_dist.
inv_cdf.uniform_dist

Inverse CDF (quantile function) for a uniform distribution.
is_poisson_dist

Test whether an object is a poisson_dist.
is_realized_dist

Test whether an object is a realized_dist.
inv_cdf.weibull_dist

Inverse CDF (quantile function) for a Weibull distribution.
mean.exponential

Method to obtain the mean of an exponential object.
mean.gamma_dist

Retrieve the mean of a gamma_dist object.
marginal.empirical_dist

Method for obtaining the marginal distribution of empirical_dist object x.
marginal

Generic method for obtaining the marginal distribution of a distribution object x over components indices.
mean.poisson_dist

Mean of a Poisson distribution.
is_weibull_dist

Test whether an object is a weibull_dist.
inv_cdf.edist

Inverse CDF (quantile function) for expression distributions.
inv_cdf.empirical_dist

Method for obtaining the inverse CDF (quantile function) of a univariate empirical_dist object.
is_chi_squared

Test whether an object is a chi_squared.
is_mixture

Test whether an object is a mixture distribution.
params.chi_squared

Method for obtaining the parameters of a chi_squared object.
mean.mvn

Retrieve the mean of a mvn object.
mean.normal

Retrieve the mean of a normal object.
mean.mixture

Mean of a mixture distribution.
mean.lognormal

Mean of a log-normal distribution.
is_dist

Function to determine whether an object x is a dist object.
marginal.mixture

Marginal distribution of a mixture.
is_mvn

Function to determine whether an object x is an mvn object.
mean.edist

Method for obtaining the mean of an edist object.
is_normal

Function to determine whether an object x is an normal object.
params.edist

Method for obtaining the parameters of an edist object.
params.mixture

Retrieve the parameters of a mixture object.
^.dist

Power operator for distribution objects.
print.beta_dist

Print a beta_dist object.
params.mvn

Method for obtaining the parameters of a mvn object.
marginal.mvn

Generic method for obtaining the marginal distribution of an mvn object x over components indices.
mean.weibull_dist

Mean of a Weibull distribution.
mean.univariate_dist

Method for obtaining the mean of univariate_dist object x.
sampler.mvn

Function generator for sampling from a mvn (multivariate normal) object.
sampler.mixture

Sampler for a mixture distribution.
print.realized_dist

Print a realized_dist object.
print.poisson_dist

Print a poisson_dist object.
nparams.empirical_dist

Method for obtaining the name of a empirical_dist object. Since the empirical distribution is parameter-free, this function returns 0.
mean.empirical_dist

Method for obtaining the mean of empirical_dist object x.
infimum.countable_set

Get the infimum of a countable set.
inv_cdf.gamma_dist

Method for obtaining the inverse cdf (quantile function) of a gamma_dist object.
inv_cdf.exponential

Method to obtain the inverse cdf of an exponential object.
is_edist

Function to determine whether an object x is an edist object.
mixture

Construct a mixture distribution.
mvn

Construct a multivariate or univariate normal distribution object.
params.gamma_dist

Method for obtaining the parameters of a gamma_dist object.
mean.beta_dist

Mean of a beta distribution.
mean.chi_squared

Retrieve the mean of a chi_squared object.
lognormal

Construct a log-normal distribution object.
lln

Law of Large Numbers Limiting Distribution
is_empirical_dist

Function to determine whether an object x is an empirical_dist object.
params

Generic method for obtaining the parameters of an object.
poisson_dist

Construct a Poisson distribution object.
+.dist

Method for adding dist objects, or shifting a distribution by a scalar.
print.mvn

Method for printing an mvn object.
sampler.uniform_dist

Sampler for a uniform distribution.
nobs.empirical_dist

Method for obtaining the number of observations used to construct a empirical_dist object.
params.normal

Method for obtaining the parameters of a normal object.
params.poisson_dist

Retrieve the parameters of a poisson_dist object.
params.beta_dist

Retrieve the parameters of a beta_dist object.
nparams.mixture

Number of parameters for a mixture distribution.
print.normal

Print method for normal objects.
mean.uniform_dist

Mean of a uniform distribution.
nparams

Generic method for obtaining the number of parameters of distribution-like object x.
normal

Construct univariate normal distribution object.
normal_approx

Moment-Matching Normal Approximation
rmap.empirical_dist

Method for obtaining the empirical distribution of a function of the observations of empirical_dist object x.
print.interval

Print the interval.
rmap.mvn

Computes the distribution of g(x) where x is an mvn object.
params.weibull_dist

Retrieve the parameters of a weibull_dist object.
params.uniform_dist

Retrieve the parameters of a uniform_dist object.
sampler.empirical_dist

Method for obtaining the sampler for a empirical_dist object.
print.gamma_dist

Print method for gamma_dist objects.
sampler.exponential

Method to sample from an exponential object.
sampler.weibull_dist

Sampler for a Weibull distribution.
sup.normal

Method for obtaining the support of a normal object, where the support is defined as values that have non-zero probability density.
params.empirical_dist

empirical_dist objects have no parameters, so this function returns NULL.
params.lognormal

Retrieve the parameters of a lognormal object.
print.lognormal

Print a lognormal object.
print.chi_squared

Print method for chi_squared objects.
print.mixture

Print a mixture object.
print.edist

Print method for edist objects.
sup.chi_squared

Support for chi-squared distribution, the positive real numbers (0, Inf).
sup.edist

Support for expression distributions.
supremum.finite_set

Return the supremum of the finite set.
supremum.interval

Return the (vector of) supremum of the interval.
*.dist

Multiplication of distribution objects.
sampler.default

Sampler for non-dist objects (degenerate distributions).
sampler.chi_squared

Method for sampling from a chi_squared object.
sampler.beta_dist

Sampler for a beta distribution.
rmap.edist

Map function over expression distribution.
simplify.edist

Method for simplifying an edist object.
rmap.dist

Method for obtaining g(x)) where x is a dist object.
/.dist

Division of distribution objects.
print.exponential

Print method for exponential objects.
print.empirical_dist

Print method for empirical_dist objects.
params.exponential

Method for obtaining the parameters of an exponential object.
print.uniform_dist

Print a uniform_dist object.
print.summary_dist

Print method for summary_dist objects.
sup.poisson_dist

Support of a Poisson distribution.
sup

Generic method for retrieving the support of a (dist) object x.
sup.beta_dist

Support of a beta distribution.
sampler.edist

Method for obtaining the sampler of an edist object.
sup.uniform_dist

Support of a uniform distribution.
obs

Retrieve the observations used to construct a distribution-like object. This is useful for obtaining the data used to construct an empirical distribution, but it is also useful for, say, retrieving the sample that was used by a fitted object, like an maximum likelihood estimate.
vcov.univariate_dist

Method for obtaining the variance of univariate_dist object.
supremum.countable_set

Get the supremum of a countable set.
supremum

Get the supremum of the support.
vcov.uniform_dist

Variance of a uniform distribution.
uniform_dist

Construct a uniform distribution object.
print.weibull_dist

Print a weibull_dist object.
obs.empirical_dist

Method for obtaining the observations used to construct a empirical_dist object.
sup.weibull_dist

Support of a Weibull distribution.
vcov.gamma_dist

Retrieve the variance of a gamma_dist object.
vcov.lognormal

Variance of a log-normal distribution.
vcov.beta_dist

Variance of a beta distribution.
sample_mvn_region

Function for obtaining sample points for an mvn object that is within the p-probability region. That is, it samples from the smallest region of the distribution that contains p probability mass. This is done by first sampling from the entire distribution, then rejecting samples that are not in the probability region (using the statistical distance mahalanobis from mu).
sup.empirical_dist

Method for obtaining the support of empirical_dist object x.
realize

Materialize any distribution to empirical_dist by sampling.
sup.exponential

Support for exponential distribution, the positive real numbers, (0, Inf).
sampler

Generic method for sampling from distribution-like objects.
sampler.gamma_dist

Method for sampling from a gamma_dist object.
rmap

Generic method for applying a map f to distribution object x.
realized_dist

Construct a realized distribution object.
sampler.lognormal

Sampler for a log-normal distribution.
vcov.weibull_dist

Variance of a Weibull distribution.
vcov.exponential

Retrieve the variance of a exponential object.
vcov.empirical_dist

Method for obtaining the variance of empirical_dist object x.
weibull_dist

Construct a Weibull distribution object.
vcov.normal

Retrieve the variance-covariance matrix (or scalar) of a normal object.
vcov.poisson_dist

Variance of a Poisson distribution.
vcov.chi_squared

Retrieve the variance of a chi_squared object.
sup.lognormal

Support of a log-normal distribution.
summary.dist

Method for obtaining a summary of a dist object.
sampler.normal

Method for sampling from a normal object.
sampler.poisson_dist

Sampler for a Poisson distribution.
summary_dist

Method for constructing a summary_dist object.
sup.gamma_dist

Support for gamma distribution, the positive real numbers (0, Inf).
surv.exponential

Method to obtain the cdf of an exponential object.
simplify

Generic method for simplifying distributions.
surv.gamma_dist

Method for obtaining the survival function of a gamma_dist object.
vcov.default

Variance-covariance for non-dist objects (degenerate distributions).
vcov.edist

Method for obtaining the variance-covariance matrix (or scalar)
surv

Generic method for obtaining the survival function of an object.
sup.mvn

Method for obtaining the support of a mvn object, where the support is defined as values that have non-zero probability density.
simplify.dist

Default Method for simplifying a dist object. Just returns the object.
sup.mixture

Support of a mixture distribution.
surv.weibull_dist

Survival function for a Weibull distribution.
surv.lognormal

Survival function for a log-normal distribution.
surv.chi_squared

Method for obtaining the survival function of a chi_squared object.
vcov.mixture

Variance of a mixture distribution.
vcov.mvn

Retrieve the variance-covariance matrix of an mvn object.
cdf.chi_squared

Method for obtaining the cdf of a chi_squared object.
Math.dist

Math group generic for distribution objects.
beta_dist

Construct a beta distribution object.
affine_transform

Affine transformation of a normal or multivariate normal distribution.
cdf.empirical_dist

Method for obtaining the cdf of empirical_dist object x.
cdf.beta_dist

Cumulative distribution function for a beta distribution.
cdf.edist

CDF for expression distributions.
cdf

Generic method for obtaining the cdf of an object.
as_dist

Convert an object to a probability distribution.
cdf.exponential

Method to obtain the cdf of an exponential object.
cdf.gamma_dist

Method for obtaining the cdf of a gamma_dist object.
cdf.poisson_dist

Cumulative distribution function for a Poisson distribution.
cdf.normal

Method for obtaining the cdf of an normal object.
cdf.mvn

Method for obtaining the CDF of a mvn object.
cdf.mixture

Cumulative distribution function for a mixture distribution.
clt

Central Limit Theorem Limiting Distribution
chi_squared

Construct a chi-squared distribution object.
cdf.uniform_dist

Cumulative distribution function for a uniform distribution.