On what value of $f(X)$ minimizes $E[(Y-f(X))^2|X]$

854 Views Asked by At

$X$ and $Y$ are random variables.

The question is: what value of $f(X)$ minimizes $E[(Y-f(X))^2|X]$.

I am pretty sure I have found the solution to this problem by writing: $$E[(Y-f(X)-E[X|Y] +E[X|Y] )^2|X]$$ and using the properties of the expected value.

A friend of mine instead proceeded this way:

$$E[(Y-f(X))^2|X]=E[Y^2+f(X)^2-2Yf(X)|X]=E[Y^2|X]+f(X)^2-2f(X)E[Y|X]$$

then he took the derivative of the last expression with respect to $f(X)$ and imposed the equality with 0 obtaining: $$2f(X)-2E[Y|X] = 0$$ so $f(X) = E[Y|X]$.

My question is: can I take the derivative with respect to a function of a random variable? I have only ever taken derivatives w.r.t. a variable of a function. Could I do this if I had $f(x)$( a ordinary real function)?