sdr.subtract_rvs(X: rv_continuous | rv_histogram, Y: rv_continuous | rv_histogram, p: float = 1e-16) rv_histogram

Numerically calculates the distribution of the difference 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 difference \(Z = X - Y\).

Notes

Given two independent random variables \(X\) and \(Y\) with PDFs \(f_X(x)\) and \(f_Y(y)\), we compute the PDF of \(Z = X - Y\) as follows.

The PDF of \(Z\), denoted \(f_Z(z)\), can be obtained using the convolution formula for independent random variables. For the difference \(Z = X - Y\), the PDF of \(Y\) is flipped.

\[f_Z(z) = \int_{-\infty}^\infty f_X(x) f_Y(x - z) \, dx\]

Examples

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

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

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

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.subtract_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("Difference of normal and Rayleigh random variables");
   ...: 
../../_images/sdr_subtract_rvs_1.png

Compute the distribution of the difference 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(-10, 10, 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.subtract_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("Difference of Rayleigh and Rician random variables");
   ...: 
../../_images/sdr_subtract_rvs_2.png

Compute the distribution of the difference 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(-10, 10, 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.subtract_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.xlim(-10, 10); \
   ....: plt.xlabel("Random variable"); \
   ....: plt.ylabel("Probability density"); \
   ....: plt.title("Difference of Rician and Chi-squared random variables");
   ....: 
../../_images/sdr_subtract_rvs_3.png