# Equivalence of two formulas for variance and covariance

I know two formulas for variance:

$$\operatorname{variance}(f) = \operatorname{expectation}((f(x) - \operatorname{expectation}(f^2(x)) \\ = \operatorname{expectation}(f(x)^2) - \operatorname{expectation}(f(x))^2$$

How are these two the same?

Also how is $$\operatorname{cov}[x, y] = E((x − E[x])(y − E[y])) = E[xy] − E[x]E[y]$$

## 1 Answer

Let $X, Y$ be random variables with means $\mu_X = \mathrm{E}[X]$ and $\mu_Y = \mathrm{E}[Y]$. Then \begin{align*} \mathrm{Cov}[X,Y] &\equiv \mathrm{E}[(X - \mu_X)(Y - \mu_Y)] \\ &= \mathrm{E}[XY - \mu_Y X - \mu_X Y + \mu_X \mu_Y] \\ &= \mathrm{E}[XY] - \mathrm{E}[\mu_Y X] - \mathrm{E}[\mu_X Y] + \mathrm{E}[\mu_X \mu_Y] \\ &= \mathrm{E}[XY] - \mu_Y \mathrm{E}[X] - \mu_X \mathrm{E}[Y] + \mu_X \mu_Y \\ &= \mathrm{E}[XY] - \mu_X \mu_Y - \mu_X \mu_Y + \mu_X \mu_Y \\ &= \mathrm{E}[XY] - \mu_X \mu_Y \\ &= \mathrm{E}[XY] - \mathrm{E}[X]\mathrm{E}[Y]. \end{align*}

If $X = Y$, then $\mathrm{Cov}[X,Y] = \mathrm{Var}[X]$, $XY = X^2$, and the above becomes $$\mathrm{Var}[X] = \mathrm{E}[X^2] - \mathrm{E}[X]^2.$$ Now suppose $W = f(X)$; i.e., $W$ is a random variable that is some function $f$ of the random variable $X$. Then $$\mathrm{Var}[W] = \mathrm{E}[W^2] - \mathrm{E}[W]^2$$ is equivalent to $$\mathrm{Var}[f(X)] = \mathrm{E}[f^2(X)] - \mathrm{E}[f(X)]^2.$$

• Thanks .. But how is E[(f(x) - E[f(x)])$^2$] = E[f$^2$(x)] - E[f(x)]$^2$ – user2340452 Aug 25 '14 at 0:04
• OP: let $\mu=E(X)$, then $(X-\mu)^2=X^2-2\mu X+\mu^2$. Take expectations of both sides, you will get: $Var(X)=E[(X-\mu)^2]=E(X^2)-2\mu E(X)+\mu^2=E(X^2)-2\mu^2+\mu^2=E(X^2)-\mu^2$. – Kim Jong Un Aug 25 '14 at 0:08
• @user2340452 As I said in my answer, the formula for the variance is a special case of the formula for the covariance, where $X = Y$. Specifically, $$\mathrm{Cov}[X,X] = \mathrm{E}[(X - \mu_X)(X - \mu_X)] = \mathrm{E}[(X - \mu_X)^2] = \mathrm{Var}[X].$$ Therefore, from the proof of the covariance formula, we immediately get $$\mathrm{Var}[X] = \mathrm{E}[X^2] - \mathrm{E}[X]^2.$$ That is why I proved the covariance formula first. – heropup Aug 25 '14 at 5:42