# Absolute difference of two Uniform random variables.

Let $X$ and $Y$ be two independent random variables following a uniform distribution $[0,5]$ Determine $P(|X-Y| \leq 1).$

What i tried:

$f_{x}(x) = \begin{cases} \frac{1}{5}, & \text{0}\leq x \leq\text{5} \\ 0, & \text{otherwise} \end{cases}$

$f_{y}(y) = \begin{cases} \frac{1}{5}, & \text{0}\leq y \leq\text{5} \\ 0, & \text{otherwise} \end{cases}$

Sine $X$ and $Y$ are independent , we can get the joint distribution $f_{xy}(x,y)=f_{x}(x)f_{y}(y)$

Hence :

$f_{xy}(x.y) = \begin{cases} \frac{1}{25}, & \text{0}\leq x \leq\text{5} & \text{0}\leq y \leq\text{5} \\ 0, & \text{otherwise} \end{cases}$

$P(|X−Y|≤1)=P(-1<X−Y≤1)=P(X-Y \leq1)-P(X-Y\leq-1)$.

$P(X-Y\leq1)=P(X\leq1+Y)=1-P(X\geq1+Y=1-\int_{1}^{4}\int_{0}^{x-1}\frac{1}{25}dydx =\frac{17}{25}$

Similarly :

$P(X-Y\leq-1)=P(X\leq Y-1)=\int_{1}^{4}\int_{x+1}^{5}\frac{1}{25}dydx=\frac{9}{50}.$

Then $P(|X−Y|≤1)=\frac{17}{25}-\frac{9}{50}=\frac{1}{2}$

However the right answer seems to be $\frac{1}{5}$

What am i doing wrong?

• Easier: What proportion of the area inside the square $0\le x \le 5, 0\le y \le 5$ satisfies $|x-y|\le 1$? (Draw a picture.) – Steve Kass Jul 1 '18 at 15:36
• $P(X-Y \leq -1) = \int\limits_0^4\int\limits_{x+1}^5 \frac{1}{25} dxdy$ – dEmigOd Jul 1 '18 at 15:59
• @dEmigOd That's correct , I don't know where i got the 1 from , however this evaluates to $\frac{9}{25}$ as well, I guess there must have been a typo in the solution manual. – Raku Jul 1 '18 at 16:03
• it evaluates to $\frac{8}{25}$ and is consistent with drawings – dEmigOd Jul 1 '18 at 16:04
• Many times to get one's bearings, one should try some simulations (especially when it is simple to do so. Using R: n = 100000; x = 5*runif(n); y = 5*runif(n); z = abs(x-y); length(z[z < 1])/n. – JimB Jul 1 '18 at 19:56

There are two errors in your calculations. One is that you incorrectly evaluated the first integral, which comes out as $1-\frac9{50}$, since by symmetry it must be the complement of the other integral. The other one is that the integrals over $x$ should be over $[1,5]$, not $[1,4]$. If you fix these mistakes, you arrive at $\frac9{25}$, the solution that was already discussed in the comments.

(1) A simulation in R consistent with $P(|X - Y| \le 1) = 9/25:$ With a million iterations, the simulated result should be accurate to two places. (Similar to the one in @JimB's Comment.)

set.seed(701);  m = 10^6
x = runif(m, 0, 5);  y = runif(m, 0, 5)
w = abs(x - y)
mean(w);  sd(w)
## 1.665504    # aprx E(W)
## 1.178755    # aprx SD(W)
mean(w <= 1); 9/25
## 0.360622    # aprx P(W < 1)
## 0.36


(2) Because $(X,Y)$ is jointly uniform over the square with vertices $(0,0)$ and $(5,5),$ it is possible to find the desired probability using geometry. With a little algebra, one can find the region of the square that results in $|X-Y| \le 1.$ This is the green region in the plot below, made using the first 50,000 points simulated above. (See Comments by @SteveKass and @dEmigOd.)

mm = 50000; X = x[1:mm]; Y = y[1:mm];  cond = abs(X - Y) <= 1
plot(X,Y, pch=".");  points(X[cond], Y[cond], pch=".", col="green2")


• Cool pictures... – StubbornAtom Jul 2 '18 at 4:45