Math Problem with the sum of squares in a big loop

309 Views Asked by At

I have the following math problem (writen in code):

var sum: CGFloat = 0
var sum2: CGFloat = 0

for _ in 0 ..< 10000000 { // This is a loop and runs 10000000 times

  let x = CGFloat(Float(arc4random()) / Float(UINT32_MAX)) // Random between 0-1
  let y = CGFloat(Float(arc4random()) / Float(UINT32_MAX)) // Random between 0-1

  sum +=   abs(x-y) * abs(x-y) // abs() gives the absolute value
  sum2 +=  abs((x-y))

}

var averageSum1: CGFloat = sum/10000000 // to get the average value from sum
var averageSum2: CGFloat = sum2/10000000 // to get the average value from sum2

1) print("\(averageSum1)")  // = 0.166666.. = 1/6

2) print("\(averageSum2)") // = 0.333333.. = 1/3
3) print("\((averageSum2)*(averageSum2))") // = 0.11111.. = 1/9

For those who don't understand the code, we have \begin{align}\text{sum}&=\sum_{k=0}^{99999999}|x_k-y_k|^2\\ \text{sum2}&=\sum_{k=0}^{99999999}|x_k-y_k|\end{align}

Where $x_k$ and $y_k$ are random numbers in $[0,1]$

We calculate

\begin{align}\text{averageSum1}&=\frac{\text{sum}}{100000000}\\ \text{averageSum2}&=\frac{\text{sum2}}{100000000}\end{align}

We therefore have:

  1. $\text{averageSum1}=\frac 16$

  2. $\text{averageSum2}=\frac 13$

  3. $\text{averageSum2}^2=\frac 19$

My question is, why is the result of 1) $\frac 16$ and not $\frac 19$ like in 3). I would think that $\frac 13 \times \frac 13 = \frac 19$. Even in a long loop? Has anyone an explanation for it?

Thank you in advance!

3

There are 3 best solutions below

1
On BEST ANSWER

Consider if you just picked $0,1$ randomly, rather than a value in between. What would the average be of the difference? Half the time, the difference would be $1$ and half the time it would be zero, so the average would be $\frac{1}{2}$.

But the average of the square of the difference will be the same, $\frac{1}{2}$, because $0^2=0$ and $1^2=1$.

Your continuous random variables are attempting to estimate:

$$\int_{0}^1\int_{0}^1 |x-y|\,dx\,dy$$

and:

$$\int_{0}^1\int_{0}^1 |x-y|^2\,dx\,dy$$

You can actually compute these with some elementary calculus to get the values $\frac{1}{3}$ and $\frac{1}{6}$.

0
On

$$\begin{align}\text{averageSum1}&=\Bbb E[|(X-Y)(X-Y)|]\\&=\Bbb E[X^2-2XY+Y^2]\\&=\Bbb E[X^2]-2\Bbb E[X]\Bbb E[Y]+\Bbb E[Y^2] \\&=\frac13-2\cdot\frac12\cdot\frac12+\frac13\\&=\frac16\end{align}$$ $$\begin{align}\text{averageSum2}&=\Bbb E[|X-Y|]^2\\&=\left(\int_0^1\int_0^1|x-y|\,dx\,dy\right)^2\\ &=\left(\int_0^1\left(\int_0^yy-x\,dx+\int_y^1x-y\,dx\right)\,dy\right)^2\\ &=\left(\int_0^1y^2-y+\frac12\,dy\right)^2\\ &=\left(\frac13\right)^2\\&=\frac19 \end{align}$$

0
On

I think your question is: when $x$ and $y$ are chosen uniformly at random in $[0,1]$, why is the expected value of $|x-y|^2$ not equal to the square of the expected value of $|x-y|$? In fact it's very generally the case that $\text{Var}[X]=E[X^2]-E[X]^2\ge 0$, with equality only when $X$ is almost surely equal to a single value. In this case, you can directly calculate the two results: $$ E[(x-y)^2]=\int_{0}^{1}dx\int_{0}^{1}dy(x-y)^2=-\frac{1}{3}\int_{0}^{1}dx(x-y)^3\vert_{y=0}^{1}=\frac{1}{3}\int_{0}^{1}(x^3-(x-1)^3)dx\\=\int_{0}^{1}\left(x^2-x+\frac{1}{3}\right)dx=\left(\frac{1}{3}x^3-\frac{1}{2}x^2+\frac{1}{3}x\right)\vert_{x=0}^{1}=\frac{1}{6}, $$ while $$ E[|x-y|]=\int_{0}^{1}dx\int_{0}^{1}dy|x-y|=2\int_{0}^{1}dx\int_{0}^{x}dy(x-y)=-\int_{0}^{1}dx(x-y)^2\vert_{y=0}^{x}\\=\int_{0}^{1}x^2 dx=\frac{1}{3}x^3\vert_{x=0}^{1}=\frac{1}{3}. $$ As a side effect, you've also computed the standard deviation of $|x-y|$, since this is $$ \sigma[|x-y|]=\sqrt{\text{Var}[|x-y|]}=\sqrt{E[(x-y)^2]-E[|x-y|]^2}=\sqrt{\frac{1}{6}-\left(\frac{1}{3}\right)^2}=\sqrt{\frac{1}{18}}=\frac{\sqrt{2}}{6}. $$