-
sdr.sum_distributions(X: rv_continuous | rv_histogram, Y: rv_continuous | rv_histogram, p: float =
1e-16
) rv_histogram Numerically calculates the distribution of the sum of two independent random variables \(X\) and \(Y\).
- Parameters:¶
- X: rv_continuous | rv_histogram¶
The distribution of the first random variable \(X\).
- Y: rv_continuous | rv_histogram¶
The distribution of the second random variable \(Y\).
- 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 + Y\).
Notes
The PDF of the sum of two independent random variables is the convolution of the PDF of the two distributions.
\[f_{X+Y}(t) = (f_X * f_Y)(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]: Y = scipy.stats.norm(loc=2, scale=1.5) In [3]: x = np.linspace(-5, 10, 1_001) In [4]: plt.figure(); \ ...: plt.plot(x, X.pdf(x), label="X"); \ ...: plt.plot(x, Y.pdf(x), label="Y"); \ ...: plt.plot(x, sdr.sum_distributions(X, Y).pdf(x), label="X + Y"); \ ...: plt.hist(X.rvs(100_000) + Y.rvs(100_000), bins=101, density=True, histtype="step", label="X + Y empirical"); \ ...: plt.legend(); \ ...: plt.xlabel("Random variable"); \ ...: plt.ylabel("Probability density"); \ ...: plt.title("Sum of two Normal distributions"); ...:
Compute the distribution of the sum of two Rayleigh distributions.
In [5]: X = scipy.stats.rayleigh(scale=1) In [6]: Y = scipy.stats.rayleigh(loc=1, scale=2) In [7]: x = np.linspace(0, 12, 1_001) In [8]: plt.figure(); \ ...: plt.plot(x, X.pdf(x), label="X"); \ ...: plt.plot(x, Y.pdf(x), label="Y"); \ ...: plt.plot(x, sdr.sum_distributions(X, Y).pdf(x), label="X + Y"); \ ...: plt.hist(X.rvs(100_000) + Y.rvs(100_000), bins=101, density=True, histtype="step", label="X + Y empirical"); \ ...: plt.legend(); \ ...: plt.xlabel("Random variable"); \ ...: plt.ylabel("Probability density"); \ ...: plt.title("Sum of two Rayleigh distributions"); ...:
Compute the distribution of the sum of two Rician distributions.
In [9]: X = scipy.stats.rice(2) In [10]: Y = scipy.stats.rice(3) In [11]: x = np.linspace(0, 12, 1_001) In [12]: plt.figure(); \ ....: plt.plot(x, X.pdf(x), label="X"); \ ....: plt.plot(x, Y.pdf(x), label="Y"); \ ....: plt.plot(x, sdr.sum_distributions(X, Y).pdf(x), label="X + Y"); \ ....: plt.hist(X.rvs(100_000) + Y.rvs(100_000), bins=101, density=True, histtype="step", label="X + Y empirical"); \ ....: plt.legend(); \ ....: plt.xlabel("Random variable"); \ ....: plt.ylabel("Probability density"); \ ....: plt.title("Sum of two Rician distributions"); ....: