Related to Derivation of EPE in “The elements of statistical learning” There is a question about EPE at StackExchange where next expresion is indicated:
$$\mathbb{E}[(Y-f(X))^2]=\mathbb{E}[(Y-\mathbb{E}[Y|X])^2]+\mathbb{E}[(\mathbb{E}[Y|X]-f(X))^2]$$ 
I don't understand this. Anybody could explain me why $$\mathbb{E}[(Y-f(X))^2]$$ 
is equal to
$$\mathbb{E}[(Y-\mathbb{E}[Y|X])^2]+\mathbb{E}[(\mathbb{E}[Y|X]-f(X))^2]$$
?
Thanks.
 A: First observe
$$\mathbb{E}[(Y-f(X))^2]= \mathbb{E}[( (Y-\mathbb{E}[Y|X]) + (\mathbb{E}[Y|X]-f(X)) )^2].$$
so we only need to show
$$\mathbb{E}[(Y-\mathbb{E}[Y|X]) (\mathbb{E}[Y|X]-f(X))] = 0$$ (i.e. the "orthogonal" part).
Expand out the brackets to see the LHS is
$$
\mathbb{E}[Y\mathbb{E}[Y|X]-\mathbb{E}[Y|X]^2 -  Yf(X) + \mathbb{E}[Y|X]f(X)].
$$
Now $f(X)$ is measurable wrt to $X$ and so can be brought inside the final conditional expectation and the tower property kills the conditioning, i.e.
$$\mathbb{E}[\mathbb{E}[Y|X]f(X)]]$ = \mathbb{E}\mathbb{E}[Yf(X)|X]]= \mathbb{E}[Yf(X)]$$, whence the two terms involving $f(X)$ cancel.
Moreover, by conditioning on $X$ for the first term and then taking out the inner $\mathbb{E}[Y|X]$ (using the fact it is $X$-measurable) we get
$$
\mathbb{E}[Y\mathbb{E}[Y|X]] = \mathbb{E}\mathbb{E}[Y\mathbb{E}[Y|X]| X] = 
\mathbb{E}[\mathbb{E}[Y|X]^2]
$$
and we're done.
A: You basically want to show that the conditional expectation is the best predictor of $Y$, in the sense that minimizes the mean squared error.
Starting from the fact that you can always write $ Y = E[Y|X] + u,\ \text{with} \ \ E[u|X] = 0,$
to prove your equation, subtract and add $E[Y|X]$ inside the square parenthesis:
\begin{align}
E[(Y-f(X))^2] &= 
E [(Y- E[Y|X] + E[Y|X] - f(X))^2]\\ 
&= E[(Y  - E[Y|X])^2] + E[E[Y|X] - f(X))^2] \\
&+ 2E[(Y  - E[Y|X])\underbrace{(E[Y|X] - f(X))^2]}_{=m(X)}\end{align}
and the last term disappears since:
$$ E[(Y - E[Y|X])m(X)] = E[u \cdot m(X)] =0 \ \text{by the law of iterated expecation.} $$
