sdr.sum_distribution(X: rv_continuous | rv_histogram, n_terms: int, p: float = 1e-16) rv_histogram

Numerically calculates the distribution of the sum of \(n\) i.i.d. random variables \(X_i\).

Parameters:
X: rv_continuous | rv_histogram

The distribution of the i.i.d. random variables \(X_i\).

n_terms: int

The number \(n\) of random variables to sum.

p: float = 1e-16

The probability of exceeding the x axis, on either side, for each distribution. This is used to determine the bounds on the x axis for the numerical convolution. Smaller values of \(p\) will result in more accurate analysis, but will require more computation.

Returns:

The distribution of the sum \(Z = X_1 + X_2 + \ldots + X_n\).

Notes

The PDF of the sum of \(n\) independent random variables is the convolution of the PDF of the base distribution.

\[f_{X_1 + X_2 + \ldots + X_n}(t) = (f_X * f_X * \ldots * f_X)(t)\]

Examples

Compute the distribution of the sum of two normal distributions.

In [1]: X = scipy.stats.norm(loc=-1, scale=0.5)

In [2]: n_terms = 2

In [3]: x = np.linspace(-6, 2, 1_001)

In [4]: plt.figure(); \
   ...: plt.plot(x, X.pdf(x), label="X"); \
   ...: plt.plot(x, sdr.sum_distribution(X, n_terms).pdf(x), label="X + X"); \
   ...: plt.hist(X.rvs((100_000, n_terms)).sum(axis=1), bins=101, density=True, histtype="step", label="X + X empirical"); \
   ...: plt.legend(); \
   ...: plt.xlabel("Random variable"); \
   ...: plt.ylabel("Probability density"); \
   ...: plt.title("Sum of two Normal distributions");
   ...: 
../../_images/sdr_sum_distribution_1.png

Compute the distribution of the sum of three Rayleigh distributions.

In [5]: X = scipy.stats.rayleigh(scale=1)

In [6]: n_terms = 3

In [7]: x = np.linspace(0, 10, 1_001)

In [8]: plt.figure(); \
   ...: plt.plot(x, X.pdf(x), label="X"); \
   ...: plt.plot(x, sdr.sum_distribution(X, n_terms).pdf(x), label="X + X + X"); \
   ...: plt.hist(X.rvs((100_000, n_terms)).sum(axis=1), bins=101, density=True, histtype="step", label="X + X + X empirical"); \
   ...: plt.legend(); \
   ...: plt.xlabel("Random variable"); \
   ...: plt.ylabel("Probability density"); \
   ...: plt.title("Sum of three Rayleigh distributions");
   ...: 
../../_images/sdr_sum_distribution_2.png

Compute the distribution of the sum of four Rician distributions.

In [9]: X = scipy.stats.rice(2)

In [10]: n_terms = 4

In [11]: x = np.linspace(0, 18, 1_001)

In [12]: plt.figure(); \
   ....: plt.plot(x, X.pdf(x), label="X"); \
   ....: plt.plot(x, sdr.sum_distribution(X, n_terms).pdf(x), label="X + X + X + X"); \
   ....: plt.hist(X.rvs((100_000, n_terms)).sum(axis=1), bins=101, density=True, histtype="step", label="X + X + X + X empirical"); \
   ....: plt.legend(); \
   ....: plt.xlabel("Random variable"); \
   ....: plt.ylabel("Probability density"); \
   ....: plt.title("Sum of four Rician distributions");
   ....: 
../../_images/sdr_sum_distribution_3.png