I am trying to calculate the expected value of the following variable Z:
$$ Z = (X - Y)^2 $$
Where $X, Y$ are uniform distributions over the interval $[0, 1]$. To do this, I'm using python to evaluate a definite integral that I have computed to be:
$$ E[Z] = \int_0^1 \int_0^1 (x - y)^2 * (p(x) * p(y)) dxdy$$
This gives me the value $ 0.018561461590966083 $. In order to double check I also used the following formula:
$$ E[(X- Y)^2] = E[x^2] - 2E[x]E[y] + E[y^2] $$ After computing this, I get the value $ 0.14946790438658583 $
I'm less familiar with integration, so I would believe the double integral I'm doing is incorrect, but I'm not sure how so. I've done some searching as how to do this integral but all the examples I can find ask for $ E[|X - Y|]$, and the part that has me stuck largely is the exponentiation. How can I properly compute the integral for this?
We have that:
$$E(X-Y)^2=EX^2-2E(XY)+EY^2.$$
(You have the signal wrong on the expresssion in your post.)
Since $X$ and $Y$ are independent and indentically distributed, $E(XY)=EXEY=1/4$, and
$$EX^2=\int_0^1 x^2 dx=\frac{1}{3}=EY^2.$$
Therefore: $$E(X-Y)^2=\frac{1}{3}-2\frac{1}{4}+\frac{1}{3}=\frac{1}{6}\approx 0.167.$$