Learn R Programming

R package: algebraic.dist

An algebra over distributions (random elements).

Tags: multivariate distributions, multivariate normal distribution, multivariate empirical distribution, data generating process, R, data-science, statistics, inference, likelihood-models, probability-theory

GitHub Pages Documentation

The GitHub documentation can be viewed here.

See the vignette algebraic.dist: Example for a quick introduction to the package.

Installation

You can install the development version of algebraic.dist from GitHub with:

# install.packages("devtools")
devtools::install_github("queelius/algebraic.dist")

About

The R package algebraic.dist provides an algebra over distributions. It’s not fully-formed yet, but I plan on using it for a lot of my future work. For instance, I’ll move a lot of the code in algebraic.mle and likelihood.model to this package.

After that, I want to experiment with using the algebraic.dist to do the following:

  • Compose distributions such that operations over distributions generate other known distributions.

    There are a lot of well-known compositions, such as the exponential distribution being the minimum of independent exponential distributions, or the sum of independent normal distributions being a normal distribution, but there is a very large space of possible compositions that are not as well-known or well-studied that I want to explore.

  • Let people use an R expression to lazily compose functions of distributions. Simplifying a distribution expression will generate a most simple R expression that represents the same distribution.

Sometimes, this may result in a simple close-form distribution, like a multivariate normal distribution, but in other cases it may result in a (hopefully simpler) expression that composes multiple distributions and operations over them.

  • With these R expressions that represent distributions, we can define more operations, like taking the limiting distribution of a sequence of distributions, say $\lim_{n \to \infty} \frac{1}{n} \sum_{i=1}^n X_i$, which is of normal by the central limit theorem.

  • Deduce various properties of these distributions, such as their moments, variances, etc. Sometimes, this may require numerical integration or Monte Carlo methods, but if the expression simplifies to a known distribution, then we can use the known properties of that distribution.

I have a lot of this code in place in C++, but I want to re-implement it in R so that it’s more accessible to others. I may also implement some of the more interesting compositions in C++ and expose them to R via Rcpp, but I’m not sure yet. I use a lot of templates and metaprogramming in C++, and I’m not sure how well that will translate to Rcpp.

Copy Link

Version

Install

install.packages('algebraic.dist')

Version

0.1.0

License

GPL (>= 3)

Issues

Pull Requests

Stars

Forks

Maintainer

Alexander Towell

Last Published

December 17th, 2025

Functions in algebraic.dist (0.1.0)

density.mvn

Function generator for obtaining the pdf of an mvn object (multivariate normal).
expectation.empirical_dist

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

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

Construct univariate normal distribution object.
is_normal

Function to determine whether an object x is an normal object.
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.
nparams.empirical_dist

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

Interval
mvn

Construct a multivariate or univariate normal distribution object.
marginal.empirical_dist

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

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

Function to determine whether an object x is a dist object.
empirical_dist

Construct empirical distribution object.
inv_cdf.normal

Method for obtaining the inverse cdf of an normal object.
nobs.empirical_dist

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

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

Function to determine whether an object x is an mvn object.
exponential

Construct exponential distribution object.
marginal.mvn

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

Method to obtain the inverse cdf of an exponential object.
rmap.mvn

Computes the distribution of g(x) where x is an mvn object.
density.normal

Method for obtaining the pdf of an normal object.
expectation.univariate_dist

Method for obtaining the expectation of f with respect to a univariate_dist object x.
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.
has

Support
is_empirical_dist

Function to determine whether an object x is an empirical_dist object.
infimum.interval

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

Return the infimum of the finite set.
params.exponential

Method for obtaining the parameters of an exponential object.
params.mvn

Method for obtaining the parameters of a mvn object.
is_edist

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

Get the infimum of the support.
mean.empirical_dist

Method for obtaining the mean of empirical_dist object x.
has.finite_set

Determine if a value is contained in the finite set.
hazard.exponential

Method to obtain the hazard function of an exponential object.
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).
vcov.edist

Method for obtaining the variance-covariance matrix (or scalar)
vcov.default

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

Generic method for sampling from distribution-like objects.
rmap

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

Print method for summary_dist objects.
is_exponential

Function to determine whether an object x is an exponential object.
rmap.dist

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

Finite set
obs.empirical_dist

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

Generic method for obtaining the parameters of an object.
print.interval

Print the interval.
mean.mvn

Retrieve the mean of a mvn object.
mean.exponential

Method to obtain the mean of an exponential object.
sup

Generic method for retrieving the support of a (dist) object x.
print.exponential

Print method for exponential objects.
sampler.exponential

Method to sample from an exponential object.
sup.empirical_dist

Method for obtaining the support of empirical_dist object x.
+.dist

Method for adding dist objects.
mean.edist

Method for obtaining the mean of an edist object.
has.interval

Determine if a value is contained in the interval.
params.edist

Method for obtaining the parameters of an edist object.
hazard

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

Generic method for simplifying distributions.
limit.edist

We have an edist object, which is a subclass of dist, and now we're placing it inside of a limit expression, where the limit is understood to be with respect to sample size. We need to define a method for this.
marginal

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

Method for obtaining the mean of univariate_dist object x.
mean.normal

Retrieve the mean of a normal object.
params.empirical_dist

empirical_dist objects have no parameters, so this function returns NULL.
sampler.edist

Method for obtaining the sampler of an edist object.
sampler.empirical_dist

Method for obtaining the sampler for a empirical_dist object.
rmap.empirical_dist

Method for obtaining the empirical distribution of a function of the observations of empirical_dist object x.
supremum.finite_set

Return the supremum of the finite set.
sampler.default

Sampler for non-dist objects (degenerate distributions).
supremum.interval

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

Default Method for simplifming a dist object. Just returns the object.
simplify.edist

Method for simplifying an edist object.
print.edist

Method for printing an edist object.
sampler.normal

Method for sampling from a normal object.
print.empirical_dist

Method for printing a dist object
summary.dist

Method for obtaining a summary of a dist 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.
vcov.mvn

Retrieve the variance-covariance matrix of an mvn object.
vcov.exponential

Retrieve the variance of a exponential object.
vcov.empirical_dist

Method for obtaining the variance of empirical_dist object x.
summary_dist

Method for constructing a summary_dist object.
vcov.univariate_dist

Method for obtaining the variance of univariate_dist object.
print.normal

Method for printing a normal object.
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.normal

Method for obtaining the parameters of a normal object.
print.mvn

Method for printing an mvn object.
surv

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

Function generator for sampling from a mvn (multivariate normal) object.
surv.exponential

Method to obtain the cdf of an exponential object.
supremum

Get the supremum of the support.
vcov.normal

Retrieve the variance-covariance matrix (or scalar) of a normal object.
sup.exponential

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

Method for obtaining the cdf of empirical_dist object x.
conditional

Generic method for obtaining the conditional distribution of a distribution object x given condition P.
cdf.exponential

Method to obtain the cdf of an exponential object.
cdf

Generic method for obtaining the cdf of an object.
conditional.empirical_dist

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

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

Method for obtaining the pdf of a empirical_dist object.
cdf.mvn

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

Method for obtaining the cdf of an normal object.
density.exponential

Method to obtain the pdf of an exponential object.
dim.interval

Return the dimension of the interval.
dim.normal

Method for obtaining the dimension of a normal object.
dim.empirical_dist

Method for obtaining the dimension of a empirical_dist object.
dim.exponential

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

Method for obtaining the dimension of an mvn object.
dim.finite_set

Return the dimension of the finite set.
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.
-.dist

Method for negation or subtraction of dist objects.
expectation.dist

Expectation of a Function Applied to a dist Object