R package: algebraic.dist

GPL-3 License R-CMD-check CRAN status

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:

When no rule matches, the result is a lazy edist that samples from its components and evaluates the expression on demand.

Multivariate operations

Limiting distributions

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: