How to work with conditional expectations and variances? I have seen this statement in a lecture note :
$$E[(Y-f(X))^2\,|\,X]=Var[Y|X]+E^2[(Y-f(X)\,|\,X]]$$
If $Z,X$ are random variables, then shouldn't be 
$Var[Z|X]=E[Z^2|X]-E^2[Z|X]$?In which case shouldn't the term $Var[Y|X]$ be $Var[Y-f(X)\,|\,X]$?
 A: Start with the equation known to you, viz. 
$\operatorname{var}(Z\mid X) = E[Z^2\mid X] - \left(E[Z\mid X]\right)^2$
and re-write it as follows.
$$\begin{align}
E[Z^2\mid X] &= \operatorname{var}(Z\mid X) + \left(E[Z\mid X]\right)^2\\
E[(Y-f(X))^2\mid X] &= \operatorname{var}((Y-f(X))\mid X) + \left(E[(Y-f(X))\mid X]\right)^2 &\scriptstyle{\text{substitute}~ Y-f(X)~\text{for}~Z}\\
\end{align}$$ 
Now, notice that the first term on the right is a conditional variance
-- the (conditional) variance of $Y-f(X)$, in fact, --  given the value of $X$. But if $X$
has value $x$, say, then $f(X)$ is a constant (let's call it $a$), and
not a random variable at all. Thus, the conditional variance of $Y-f(X)$ 
given that $X = x$ is the same as the  conditional variance of 
$Y-a$ given that $X=x$.
But, variance is invariant to translation of the data set (that is, $Y$ and
$Y-a$ have the same variance), and thus we get
$$E[(Y-f(X))^2\mid X] = \operatorname{var}(Y\mid X) + \left(E[(Y-f(X))\mid X]\right)^2 $$
which is what your lecture note asserts.
