# Problem with Density of $X-Y$

Assuming $f(x,y) = 1$, $0<x<1$, $0<y<1$, obtain the density of $Z = X-Y$.

-

One approach is to "guess" that the density function of $Z$ on $[-1,1]$ is a continuous piecewise linear "hat" function, with boundary density zero at $z = -1,1$ and a knot at $z = 0$. Then in order for the function to have required integral $1$ over $[-1,1]$ the density of $Z$ at $z = 0$ must be $1$.

Here's a geometric justification for the "guess". Consider the unit square in the $xy$-plane that supports the probability of $X$ and $Y$ independently. The random variable $Z = X-Y$ is constant along line segments of slope $1$ within this region, and the density of $Z$ is proportional to the lengths of such line segments, reaching a maximum length along the "main diagonal" where $x = y$.

-

A mechanical approach is to compute $$\mathrm E(u(Z))=\mathrm E(u(X-Y))=\iint_{[0,1]^2}u(x-y)\,\mathrm dx\mathrm dy,$$ for every measurable bounded function $u$. Use the change of variable $(z,t)=(x-y,y)$. Then $(x,y)=(z+t,t)$ hence the Jacobian is $1$ and $$\mathrm E(u(Z))=\iint u(z)\,[0\leqslant t,z+t\leqslant 1]\,\mathrm dz\mathrm dt=\int u(z)g(z)\,\mathrm dz,$$ with $$g(z)=\int[0\leqslant t,z+t\leqslant 1]\,\mathrm dt=\int[\max(0,-z)\leqslant t\leqslant\min(1,1-z)]\,\mathrm dt.$$ If $|z|\geqslant1$, $g(z)=0$. If $|z|\leqslant1$, $g(z)=1-|z|$. This is the density of $Z$.

-

$Z=X-Y$

Let $W=X\implies X=W$ and $Y=W-Z$

Jacobian ,$J=\frac{\partial(X,Y)}{\partial(Z,W)}=1$

Thus, joint distribution of $Z,W$ is $g(W,Z)=f(W,W-Z)=1$ for $0\lt W\lt 1$ and $0\lt W-Z\lt 1\implies Z\lt W\lt 1+Z$

Thus, Distribution function of $Z$ is $h(Z)=\int_{max(0,Z)}^{min(1,1+Z)}g(W,Z)dW=min(1,1+Z)-max(0,Z)$

Thus, for $-1\lt Z\lt 0$, $h(Z)=1+Z$

and for $0\lt Z\lt 1$, $h(Z)=1-Z$

Thus, $h(Z)=1-|Z|$ for $-1\lt Z\lt 1$ and $0$ otherwise

SEE http://en.wikipedia.org/wiki/Probability_density_function#Products_and_quotients_of_independent_random_variables for my method if you are not familiar with this.

-
This is way overkill. There is no need to find a joint distribution and then obtain the needed density as a marginal density. –  Dilip Sarwate Sep 27 '12 at 19:32

$Z = X-Y$ takes on values in $(-1,1)$.

• For $-1 \leq z < 0$, $P\{Z \leq z\} = P\{X-Y \leq z\}$ is the probability that the random point $(X,Y)$ is in the triangular region with vertices at $(0,1), (1+z,1), (0,-z)$ of area $\frac{1}{2}(1+z)^2$.

• For $0 \leq z < 1$, $P\{Z \geq z\} = P\{X-Y \geq z\}$ is the probability that the random point $(X,Y)$ is in the triangular region with vertices at $(1,0), (z,0), (1,z)$ of area $\frac{1}{2}(1-z)^2$.

Hence, $$F_Z(z)=\begin{cases} 0, & z < -1,\\ {\scriptstyle\frac{1}{2}}(1+z)^2, & -1 \leq z < 0,\\ {\scriptstyle\frac{1}{2}}(1-z)^2, & 0 \leq z < 1,\\ 1, & z \geq 1, \end{cases} ~~ \Rightarrow f_Z(z) = \begin{cases} 1 + z, &-1 \leq z < 0,\\ 1-z, & 0 \leq z < 1,\\ 0, &\text{otherwise.} \end{cases}$$

Alternatively, $X$ and $Y$ are independent random variables uniformly distributed on $(0,1)$ and so $X$ and $-Y$ are also independent random variables with $-Y$ being uniformly distributed on $(-1,0)$. Hence, the density of $Z = X + (-Y)$ is the convolution of the densities of $X$ and $-Y$. This readily gives the result referred to in the first paragraph of hardmath's answer.

-