Learn R Programming

distributional (version 0.9.0)

dist_convolved: A convolved distribution

Description

[Experimental]

Creates the distribution of the sum of two or more independent random variables using numerical convolution of their distributions with a Fast Fourier Transform (FFT).

The convolution used depends on the support of the distributions being summed. A sum of distributions with lattice (integer) support is convolved exactly on the integer lattice, giving the probability mass function of the result. Any other sum is convolved on a common grid of cell probabilities, from which the density is obtained and interpolated with stats::approxfun(), and the cumulative distribution and quantile functions are derived by accumulation and inversion respectively.

This is primarily intended to be used via arithmetic on distributions: dist1 + dist2 or dist1 + dist2 + dist3 + .... Distributions with known closed-form sums (e.g. two dist_normal()) will use the exact result rather than this approximation. Chaining + automatically performs a single k-way FFT convolution rather than nested binary convolutions, avoiding compounding approximation errors.

Usage

dist_convolved(...)

Arguments

...

Two or more distribution vectors to sum. All will be recycled to a common length.

Details

Let \(Z = X_1 + X_2 + \cdots + X_k\) where the \(X_i\) are independent random variables.

Mean: $$E(Z) = \sum_{i=1}^{k} E(X_i)$$

Variance: $$\mathrm{Var}(Z) = \sum_{i=1}^{k} \mathrm{Var}(X_i)$$

Probability density function (p.d.f): $$f_Z(z) = (f_{X_1} * f_{X_2} * \cdots * f_{X_k})(z)$$

Support: \(\sum_i \inf S_{X_i}\) to \(\sum_i \sup S_{X_i}\), which is discrete if (and only if) all of the \(X_i\) are discrete.

The k-way convolution is computed in a single FFT pass: each component is discretised onto a common grid, transformed via FFT, all transforms are multiplied element-wise, and a single inverse FFT yields the result. This avoids compounding approximation errors from nested binary convolutions.

Components are discretised into probabilities rather than densities, which preserves their mass regardless of how the scales of the distributions being summed compare. When all of the distributions are supported on the integers this is done exactly on the integer lattice, and the result is a discrete distribution with an exact probability mass function. Otherwise the distributions are discretised into the probabilities of the cells of a common grid (which is aligned with the integers if any of the distributions are discrete, so that their atoms are also exact).

The accuracy of the FFT approximation can be controlled by passing n (number of grid cells, default 2^12) and tail_p (tail probability used to find finite grid bounds for distributions with infinite support, default 1e-6) to density(), cdf(), quantile(), or generate(). A warning is given if the grid is too coarse to resolve any of the distributions being summed.

See Also

Examples

Run this code
# Sum of a lognormal and an exponential (no closed-form result)
d <- dist_convolved(dist_lognormal(0, 1), dist_exponential(1))
d

density(d, 2)
cdf(d, 2)
quantile(d, 0.5)
generate(d, 5)

# Three distributions from different families
d3 <- dist_convolved(dist_lognormal(0, 0.5), dist_gamma(2, 1), dist_exponential(2))
d3

# Via arithmetic
d2 <- dist_lognormal(0, 1) + dist_exponential(1)
density(d2, 2)

# Sums of discrete distributions are computed exactly on the integer lattice
d_pois <- dist_poisson(2) + dist_poisson(3)
density(d_pois, 5) # dpois(5, 5)
support(d_pois)

# Mean and variance are computed exactly from components
mean(d)
variance(d)

Run the code above in your browser using DataLab