# Finding the PDF of the difference of two i.i.d exponential random variables.

I have two random variables $$X_1$$ and $$X_2$$ where:

$$f_{X_1}(x) = f_{X_2}(x) = \begin{cases} e^{-\frac x2}, \quad \text{if} \; 0 \leq x \leq \mathrm{ln}\: 4\\ 0, \quad \quad \text{otherwise} \end{cases}$$

and a third random variable $$Y = X_1 - X_2$$. Now I understand that the sum of two independent random variables can found using a convolution so it makes sense that the difference can also be found using a convolution but how?

My PDF looks like this:

and I know that graphically the convolution for $$X_1 +X_2$$ looks like this:

1. First integral from $$0$$ to the point where red is still inside blue (in this case $$t$$)

1. Then integral from $$t-\mathrm{ln}\:4$$ to $$\mathrm{ln}\:4$$

So how does this change once we take the difference and not the sum? Essentially, if flipping and shifting is the convolution sum then what it different when there is a difference that needs to be calculated?

Edit: Based on the answer by perepelart I solved the convolution sum as follows:

to find $$Y$$ we can start the convolution as follows:

Step 1: First, we flip $$f_Z$$ and slide it to the point that it starts entering $$f_X$$. Before this point $$f_Y$$ is zero.

Here-forth the convolution integral for $$-\mathrm{ln}\: 4 \leq t<0$$ becomes:

\begin{align*} f_X \ast f_Z &= \int_{-\infty}^{\infty} f_X(x) f_Z(t-x) dx\\ & = \int_{0}^{t+\mathrm{ln}\: 4} f_X(x) f_Z(t-x) dx\\ & = \int_{0}^{t+\mathrm{ln}\: 4} e^{-\frac x2} e^{-\frac{(t-x)}{2}} dx\\ & = (t+\mathrm{ln}\: 4)e^{-\frac t2} \qquad \text{for} \: -\mathrm{ln}\: 4 \leq t<0 \end{align*}

Step 2: In this step the sliding PDF $$f_Z$$ has began exiting the PDF $$f_X$$ which looks like this:

which will give us the values for $$f_Y$$ for $$0\leq t\leq\mathrm{ln}\: 4$$.

Here-forth the convolution integral for $$0 becomes:

\begin{align*} f_X \ast f_Z &= \int_{-\infty}^{\infty} f_X(x) f_Z(t-x) dx\\ & = \int_{t}^{\mathrm{ln}\: 4} f_X(x) f_Z(t-x) dx\\ & = \int_{t}^{\mathrm{ln}\: 4} e^{-\frac x2} e^{-\frac{(t-x)}{2}} dx\\ & = -(t-\mathrm{ln}\: 4)e^{-\frac t2} \qquad \text{for} \; 0 \leq t \leq\mathrm{ln}\: 4 \end{align*}

Hence, the PDF of $$Y$$ is:

$$f_Y(y)= \begin{cases} (y+\mathrm{ln}\: 4)e^{-\frac y2}, \qquad \text{for} \; -\mathrm{ln}\: 4 \leq y<0,\\ -(y-\mathrm{ln}\: 4)e^{-\frac y2}, \quad \; \text{for} \; 0 \leq y \leq \mathrm{ln}\: 4,\\ 0 \qquad \qquad \qquad\qquad \text{otherwise}. \end{cases}$$

The plot of $$f_Y(y)$$ looks as follows:

Although $$f_Y$$ is always non-negative it integrates to $$2$$, hence, it is an invalid PDF.

• +1 For the effort of plotting it all out! Commented Jul 17, 2023 at 16:09
• mirror the density function of the one with - sign before it in the y axis before doing convolution $x\to f(x) \to f(-x)$ Commented Jul 17, 2023 at 20:14
• Seems like you made an error from the beginning of your calculations, because for appropriate $t$, $f_Z(t-x) = e^{\frac{t-x}{2}}\ne e^{-\frac{t-x}{2}}$. Commented Jul 19, 2023 at 13:27
• @perepelart Thanks for adding your calculations and pointing out the error. Commented Jul 19, 2023 at 13:29

If we know the distribution of the $$X_2$$, we can find the distribution of the $$-X_2.$$ I tried to do it as straightforward as possible.

As $$F_\xi(y)$$ I denote the CDF of the random variable $$\xi$$.

$$F_{-X_2}(y)=\mathbb{P}(-X_2\leq y)=\mathbb{P}(X_2 \geq -y)=1-\mathbb{P}(X_2\le -y)=\\=1-F_{X_2}(-y).$$ Now, to find the density of $$-X_2$$, you can just differentiate $$F_{-X_2}(y)$$ : $$f_{-X_2}(y)=\frac{d}{dy}\left(F_{-X_2}(y)\right)=\frac{d}{dy}(1-F_{X_2}(-y))=\\=f_{X_2}(-y)=\mathbf{1}_{\{-\ln4\leq y\leq 0\}}e^{\frac{y}{2}}$$

By $$\mathbf{1}_{A}(y)$$ where $$A$$ is an event I denote indicator function. It equals $$1$$ whenever $$y \in A$$ and equals zero otherwise. It's just an elegant way to define a piecewise function.

Without indicator you can write it as follows :

$$f_{-X_2}(y)=\mathbf{1}_{\{-\ln4\leq y\leq 0\}}(y) \cdot e^{\frac{y}{2}} = \begin{cases}e^{\frac{y}{2}}, -\ln4\leq y\leq 0 \\ 0, \text{otherwise.} \end{cases}$$

Therefore you have two random variables, let's denote them $$X:=X_1$$ and $$Y := -X_2$$ and you need to find PDF of $$X+Y$$. And you know their densities so you can just use the convolution of the sum as you wanted.

UPDATE.

So you can check your work I added my calculations of the $$f_{X+Y}(y)$$. I will use indicator function during integrations to make derivations shorter.

For now we have : $$f_X(x) = \mathbf{1}_{\{0 \leq x \leq \ln 4\}}e^{-\frac{x}{2}}, f_Y(y) = \mathbf{1}_{\{-\ln4 \leq y \leq 0\}}e^{\frac{y}{2}}$$

Using convolution, we get :

$$f_{X+Y}(y)=\int\limits_{\mathbb{R}}f_X(x)f_Y(y-x)dx = \int\limits_{\mathbb{R}}\mathbf{1}_{\{0 \leq x \leq \ln 4\}}e^{-\frac{x}{2}}\mathbf{1}_{\{-\ln4 \leq y-x \leq 0\}}e^{\frac{y-x}{2}}dx = \\ =e^{\frac{y}{2}} \int\limits_{\mathbb{R}}\mathbf{1}_{\{0 \leq x \leq \ln 4\}}e^{-\frac{x}{2}}\mathbf{1}_{\{y \leq x \leq \ln4+y\}}e^{-\frac{x}{2}}dx = e^{\frac{y}{2}}\int\limits_{\max\{0,y\}}^{\min\{\ln4, \ln4+y\}}e^{-x}dx$$

To evaluate this integral I will consider different cases.

If $$0 \leq y \leq \ln4$$, we have :

$$f_{X+Y}(y)=e^{\frac{y}{2}}\int\limits_{y}^{\ln4}e^{-x}dx=-\frac{1}{4}e^{\frac{y}{2}} + e^{-\frac{y}{2}}$$

If $$-\ln4 \leq y < 0$$, we have :

$$f_{X+Y}(y)=e^{\frac{y}{2}}\int\limits_{0}^{\ln4+y}e^{-x}dx=e^{\frac{y}{2}}-\frac{1}{4}e^{-\frac{y}{2}}$$

If $$y > \ln 4$$ or $$y < - \ln 4$$, $$f_{X+Y}(y)$$ will be equal to $$0$$.

Finally,

$$f_{X+Y}(y) = \mathbf{1}_{\{0 \leq y \leq \ln 4 \}}(y) \cdot \left(-\frac{1}{4}e^{\frac{y}{2}} + e^{-\frac{y}{2}}\right) + \mathbf{1}_{\{-\ln 4 \leq y < 0 \}}(y) \cdot\left(e^{\frac{y}{2}}-\frac{1}{4}e^{-\frac{y}{2}}\right)$$

We can check that it's integrates to $$1$$ :

$$\int\limits_{\mathbb{R}}\mathbf{1}_{\{0 \leq y \leq \ln 4 \}}(y) \cdot \left(-\frac{1}{4}e^{\frac{y}{2}} + e^{-\frac{y}{2}}\right)dy = \int\limits_{0}^{\ln 4}(-\frac{1}{4}e^{\frac{y}{2}} + e^{-\frac{y}{2}})dy = \frac{1}{2},$$

$$\int\limits_{\mathbb{R}}\mathbf{1}_{\{-\ln 4 \leq y < 0 \}}(y) \cdot\left(e^{\frac{y}{2}}-\frac{1}{4}e^{-\frac{y}{2}}\right)dy = \int\limits_{-\ln 4}^{0}(e^{\frac{y}{2}}-\frac{1}{4}e^{-\frac{y}{2}})dy = \frac{1}{2}$$

Hence, by additivity of the integral,

$$\int\limits_{\mathbb{R}}f_{X+Y}(y)dy=\frac{1}{2}+\frac{1}{2}=1$$

Without indicator function, we can rewrite it as follows :

$$f_{X+Y}(y)=\begin{cases}e^{\frac{y}{2}}-\frac{1}{4}e^{-\frac{y}{2}} , -\ln4 \leq y < 0 \\ -\frac{1}{4}e^{\frac{y}{2}} + e^{-\frac{y}{2}}, 0 \leq y \leq \ln 4 \\0, \text{otherwise}\end{cases}$$

• it's an interesting idea but you can't just take the negative of $X_2$ because it's no longer necessarily a density when you make such a transformation. You can check whether it's still a density by integrating with the correct limits and seeing whether it integrates to 1. My guess is that it is not. Commented Jul 19, 2023 at 7:12
• @markleeds, it's a standard transformation of density function. If $Y=aX+b$ where $a,b$ are real numbers and $X$ has density $f_X$ then $f_Y(y)=\frac{1}{|a|}f_X\left(\frac{y-b}{a}\right)$. I just did full derivation of this fact in the specific case. And of course it will integrate to one. Commented Jul 19, 2023 at 7:17
• @markleeds And it's just the consequence of the general fact. Here we have $g(x)=-x$ and it's definitely monotonic function. But if I did something wrong, tell me. I tried to follow in my derivations the book "Introduction to Probability, Bertsekas and Tsitsiklis". They call such transformations "Derived Distributions". I use this many times in my work and maybe cause of it I just cannot see some problems here. Commented Jul 19, 2023 at 7:51
• So the PDF of $-X_2$ is $1$ for the range $\{-\ln4\leq y\leq 0\}$ and then it's a growing exponential after that until $ln4$? Commented Jul 19, 2023 at 8:34
• I see. So $-X_2$ is basically the red function in my second plot. So I can just flip it again and slide it to get my desired result. Thanks a lot! Commented Jul 19, 2023 at 8:43

You cannot do this unless $$X_1$$ and $$X_2$$ are independent, or you know their joint distribution. Let's assume you have independent and identically distributed random variables.

So how does this change once we take the difference and not the sum?

When $$Y=X_1-X_2$$, then $$X_1=Y+X_2$$. So the convolution we use is:

\begin{align}f_Y(y) &= \int_\Bbb R f_{X_1\!}(y+x)\,f_{X_2\!}(x)\,\mathrm d x\\[2ex]&=\int_{0\leq x\leq\ln 4, 0\leq y+x\leq\ln 4}\mathrm e^{-x/2}\mathrm e^{-(y+x)/2}\,\mathrm dx\\[2ex]&=\mathrm e^{-y/2}\mathbf 1_{-\!\ln 4\leq y\leq\ln 4}\int\limits_{\max(0,-y)}^{\ln(4)+\min(0, -y)}\mathrm e^{-x}\,\mathrm d x\\[2ex]&~~\vdots\end{align}

The rest is just choosing where to partition the support for the distribution of $$Y$$.

• Would it be possible for me to do the same thing by considering $Y = X_1 + (-X_2)$ which would involve finding the PDF of $-X_2$ and the doing the convolution as I described. I am asking because I have a Signal Processing background so the sort of convolution you explained does not make intuitive sense to me. Also is there a typo with the bold 1? Commented Jul 18, 2023 at 15:56
• It is the same thing. \begin{align}f_{-X_2}(z) &= \mathrm e^{z/2} \mathbf 1_{-\!\ln(4)\leq z\leq 0}\\ f_{Y}(y) &= \int_\Bbb R f_{X_1}(y-z)\,f_{-X_2}(z)\,\mathrm d z\\&=\int_{-\!\ln(4)\leq z\leq 0, 0\leq y-z\leq\ln(4)}\mathrm e^{-(y-z)/2}\,\mathrm e^{z/2}\,\mathrm d z \end{align} Commented Jul 18, 2023 at 23:15
• @CaporalFourrier you can take a look at my answer, where I did exactly that you wanted. Deleted it cause of silent downvote, but if it will help you, I can undelete. Commented Jul 19, 2023 at 6:43