# Pdf of $|X-Y|$ when $X,Y$ are independent Uniform $[0,a]$ variables

Need to find pdf of $$|X-Y|$$ .I little confused and not getting answer after taking below limits. As it is symmetrical I have taken one part of triangle. Considering lower triangle limits I have taken is x from $$y+z$$ to $$a$$ and outer limit : y from $$0$$ to $$a-z$$ Not getting expected answer. Could any help.

Answer: $$f_{z}(z)=\frac{2}{a}(1-\frac{z}{a})$$

Since $$X$$ and $$Y$$ are identically distributed, $$P(X-Y\le z,X\ge Y)=P(Y-X\le z,Y\ge X)$$

So for $$0< z,

\begin{align} P(|X-Y|\le z)&=2\times P(X-Y\le z,X\ge Y) \\&=2 \int P(X-y\le z,X\ge y\mid Y=y)f_Y(y)\,dy \\&=2\int P(y\le X\le z+y)\frac{\mathbf1_{0

Hence the pdf of $$Z=|X-Y|$$ is

$$f_Z(z)=\frac{2}{a^2}(a-z)\mathbf1_{0

Needless to say, the above algebra for the CDF is not as simple as drawing a picture of the region $$\{(x,y)\in[0,a]^2:|x-y|\le z\}$$ and finding its area.

Here is a picture for $$z=0.6$$ and $$a=3$$: (Also see the case $$a=1$$ discussed in this post)

|X-Y| can be written as below ,

$$P(|X-Y| \leq z )= P(X-Y \leq z ,X \ge Y )+P(Y-X \leq z ,Y >X ) \\$$

The limits can be visualized by drawing rectangle and |x-y| and take area of the other sides,which would be symmetry hence multiple 2.

$$F_Z(z)=1-2\int_{y=0}^{y=a-z} \int_{y+z}^{a}f(x,y) dxdy \\$$

After differentiating w.r.t z ,

$$f_Z(z)=0+2 \int_{0}^{a-z} f(y+z,y)dy \\$$

$$=\frac{2}{a^{2}}(a-z)$$