TITLE: Expected value of the absolute value of the difference between two independent uniform random variables?
QUESTION [6 upvotes]: I am trying to calculate the expected value of the absolute value of the difference between two independent uniform random variables.
Let $X_1\sim\operatorname{Uniform}(0, 2)$ and $X_2\sim\operatorname{Uniform}(0, 2)$ and $X_1$ and $X_2$ are independent. I want to calculate $\operatorname E \left[|X_1 - X_2|\right]$.
Can anyone please help?

REPLY [7 votes]: Alternatively, we can avoid integrating (explicitly) by using conditional expectation and mean/variance formulas:
$$
\begin{align}
\mathbb{E}[|X_1 - X_2|]
&= \mathbb{E}\big[\mathbb{E}[abs(X_1-X_2)|X_2]\big] \\
&= \mathbb{E}\Bigg[
\frac{X_2^2}{4} + \frac{(2-X_2)^2}{4}
\Bigg] \\
&= \frac{1}{4}\mathbb{E}[X_2^2 + (2-X_2)^2] \\
&= \frac{1}{4}\mathbb{E}[X_2^2 + 4 - 4X_2 + X_2^2] \\
&= \frac{1}{4}\mathbb{E}[X_2^2] + 1 - \mathbb{E}[X_2] + \frac{1}{4}\mathbb{E}[X_2^2] \\
&= \frac{1}{2}\mathbb{E}[X_2^2]^2 \\
&= \frac{1}{2}\mathbb{E}[X_2]^2 + \frac{1}{2}\text{Var}[X_2] \\
&= \frac{1}{2} + \frac{1}{6} = \frac{2}{3}
\end{align}
$$
The second line follows as the probability $\mathbb{P}[X_1 < X_2 | X_2] = \frac{X_2}{2}$, and in that case the expectation is $\mathbb{E}[abs(X_1-X_2)|X_2, X_1<X_2] = \mathbb{E}[X_2-X_1|X_2, X_1<X_2] = X_2 - \frac{X_2}{2} = \frac{X_2}{2}$. Similarly when $X_1>X_2$ we get the max.