Expected value of the absolute value of the difference between two independent uniform random variables? 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?
 A: For every independent random variables $X_1$ and $X_2$ with densities $f_1$ and $f_2$ and every measurable function $g$,
$$
\operatorname E[g(X_1,X_2)]=\int_{D_1}\int_{D_2} g(x_1,x_2) f_1(x_1) f_2(x_2) 
\, \mathrm{d}x_2 \, \mathrm{d}x_1.
$$
where $D_1$ and $D_2$ are the domains of $X_1$ and $X_2$. Since $f_1(x_1) = f_2(x_2) = 1/2$, and $D_1=D_2=[0,2]$ we have that
$$
\operatorname E[|X_1-X_2|]=\int_0^2\int_0^2 \frac{|x_1-x_2|}{4} \, \mathrm{d}x_2 \, \mathrm{d}x_1 =\frac{2}{3}.
$$
A: 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.
