Prove that $E((X-a)^2)$ is minimized when $a=E(X)$ $X$ is an arbitrary continuous random variable.
I tried to do this by saying since $X-a$ is squared that means its lowest possible value is 0 and then I tried to solve for $a$ when the expression is $0$.
$$
\begin{align}
E((X-a)^2)&=0\\
E(X^2)-2aE(X) +a^2&=0\\
a^2-2aE(X)+E(X)^2&=E(X)^2-E(X^2)\\
(a-E(X))^2&=E(X)^2-E(X^2)\\
a-E(X)&=\sqrt{(E(X))^2-E(X^2)}\\
a&=E(X) \pm\sqrt{(E(X))^2-E(X^2)}\\
\end{align}
$$
Now I don't know if there's some trick to say that $\sqrt{(E(X))^2-E(X^2)}=0$ or if I'm barking up the wrong tree with this approach.
 A: By linearity of expectation, we have 
$E((X-a)^2)=a^2-2E(X)a+E(X^2)=(a-E(X))^2+E(X^2)-E(X)^2$. Considering this as a quadratic function on $a$, it is clear that the minimum is acquired when $a=E(X)$.
A: For every $a\in\mathbb R$ we have:$$\begin{aligned}\mathsf{E}\left(X-a\right)^{2} & =\mathsf{E}\left(X-\mathsf{E}X+\mathsf{E}X-a\right)^{2}\\
 & =\mathsf{E}\left[\left(X-\mathsf{E}X\right)^{2}+2\left(X-\mathsf{E}X\right)\left(\mathsf{E}X-a\right)+\left(\mathsf{E}X-a\right)^{2}\right]\\
 & =\mathsf{E}\left(X-\mathsf{E}X\right)^{2}+\mathsf{E}2\left(X-\mathsf{E}X\right)\left(\mathsf{E}X-a\right)+\left(\mathsf{E}X-a\right)^{2}\\
 & =\mathsf{E}\left(X-\mathsf{E}X\right)^{2}+2\left(\mathsf{E}X-a\right)\mathsf{E}\left(X-\mathsf{E}X\right)+\left(\mathsf{E}X-a\right)^{2}\\
 & =\mathsf{E}\left(X-\mathsf{E}X\right)^{2}+2\left(\mathsf{E}X-a\right)\left(\mathsf{E}X-\mathsf{E}X\right)+\left(\mathsf{E}X-a\right)^{2}\\
 & =\mathsf{E}\left(X-\mathsf{E}X\right)^{2}+\left(\mathsf{E}X-a\right)^{2}\\
 & \geq\mathsf{E}\left(X-\mathsf{E}X\right)^{2}
\end{aligned}
$$
LHS takes minimum value iff $a=\mathsf EX$
A: Let's define a function which depends on $ a $:
$$ f \left( a \right) = \mathbb{E} \left[ \left( X - a \right)^{2} \right] $$
Now, the optimization problem is:
$$ \hat{a} = \arg \min_{a} f \left( a \right) $$
The requirement for optimal point - $ f' \left( \hat{a} \right) = 0 $.
Working on the function as in the answer by @shrimpabcdefg:
$$\begin{align*}
\frac{d}{d a} f \left( a \right) & = \frac{d}{d a} \mathbb{E} \left[ \left( X - a \right)^{2} \right]  \\
& = \frac{d}{d a} {a}^{2} - 2 a \mathbb{E} \left[ X \right] + \mathbb{E} \left[ X \right]^{2} \\
& = 2 a - 2 \mathbb{E} \left[ X \right] \\
& \Rightarrow 2 \hat{a} - 2 \mathbb{E} \left[ X \right] = 0 \Rightarrow \hat{a} = \mathbb{E} \left[ X \right]
\end{align*}$$
A: Your error is in the very first line of your displayed equations where you assume that the minimum value that $E[(X-a)^2]$ is $0$ and attempt to solve for $a$.  The minimum value of $E[(X-a)^2]$ (regarded as a function of $a$) is not $0$; it is $\sigma^2$ where $\sigma$ is the standard deviation of $X$.  Keep in mind that $(E[X])^2 - E[X^2]$ is generally a negative number (it equals $-\sigma^2$) and so the value of $a$ that you compute as the minimizer of $E[(X-a)^2]$ is actually a complex number.
