sdr.add_rvs(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 random variable X.

Y: rv_continuous | rv_histogram

The distribution of the 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

Given two independent random variables X and Y with PDFs fX(x) and fY(y), we compute the PDF of Z=X+Y as follows.

The PDF of Z, denoted fZ(z), can be obtained using the convolution formula for independent random variables.

fZ(z)=fX(x)fY(zx)dx

Examples

Compute the distribution of the sum of normal and Rayleigh random variables.

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

In [2]: Y = scipy.stats.rayleigh(loc=1, scale=2)

In [3]: x = np.linspace(-4, 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.add_rvs(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 normal and Rayleigh random variables");
   ...: 
../../_images/sdr_add_rvs_1.png

Compute the distribution of the sum of Rayleigh and Rician random variables.

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

In [6]: Y = scipy.stats.rice(3)

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.add_rvs(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 Rayleigh and Rician random variables");
   ...: 
../../_images/sdr_add_rvs_2.png

Compute the distribution of the sum of Rician and Chi-squared random variables.

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

In [10]: Y = scipy.stats.chi2(3)

In [11]: x = np.linspace(0, 20, 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.add_rvs(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 Rician and Chi-squared random variables");
   ....: 
../../_images/sdr_add_rvs_3.png