In the context of regression for machine learning, suppose I have a function from an instance space $I$ to $\mathbb{R}$, say $f:I \rightarrow R$, and that I have an estimator $\hat{f}:I \rightarrow R$.
The textbook I am reading states without proof that the expectation $\mathbb{E}[(f(x)-\hat{f}(x))^2] = (f(x)-\mathbb{E}[\hat{f}(x)])^2 + \mathbb{E}[(\hat{f}(x)-\mathbb{E}[\hat{f}(x)])^2]$.
Expanding the left side using linearity and the fact that expectation of a constant is the constant I get:
$\mathbb{E}[f(x)^2-2f(x)\hat{f}(x)+\hat{f}(x)^2] = f(x)^2-2f(x)\mathbb{E}[\hat{f}(x)]+\mathbb{E}[f(x)^2]$, and I am not sure how this equals the right side. Any insights to easily see this appreciated.
Note that $\bigl(f(x)-\hat f(x)\bigr)^2=f(x)^2-2f(x)\hat f(x)+\hat f(x)^2$, which is different than what you wrote due to the sign of the last term. (EDIT: This was corrected in the question shortly after I pointed it out)
Thus, $$ \mathbb E\bigl(f(x)-\hat f(x)\bigr)^2=\bigl(f(x)-\mathbb E\hat f(x)\bigr)^2+\mathbb E\bigl(\hat f(x)^2\bigr)-\bigl(\mathbb E\hat f(x)\bigr)^2. $$ Finally, observe that $$ \mathbb E\bigl(\hat f(x)^2\bigr)-\bigl(\mathbb E\hat f(x)\bigr)^2=\mathbb E\bigl(\hat f(x)-\mathbb E\hat f(x)\bigr)^2, $$ by expanding out the right side and cancelling. Note that both expressions appearing in the last step equal the variance $\textrm{Var}\hat f(x)$.
Thus, I believe the intended identity is the following: $$ \mathbb E\bigl(f(x)-\hat f(x)\bigr)^2=\bigl(f(x)-\mathbb E\hat f(x)\bigr)^2+\mathbb E\bigl(\hat f(x)-\mathbb E\hat f(x)\bigr)^2. $$