# Why can covariance be written both of these ways?

I read from a text book that covariance is $E((X - EX)(Y - EY))$. The book then states that this expression is equivalent to $E(XY) - E(X)E(Y)$. Where did the latter expression come from?

-

$(X-EX)(Y-EY)=XY-(EY)X-(EX)Y+(EX)(EY)$, and expectation is linear, so

\begin{align*} E\big((X-EX)(Y-EY)\big)&=E(XY)-(EY)EX-(EX)EY+(EX)(EY)\\ &=E(XY)-(EX)(EY)\;. \end{align*}

If you get lost in all the expectations, let $\alpha=E(X)$ and $\beta=E(Y)$; then

$$(X-\alpha)(Y-\beta)=XY-\beta X-\alpha Y+\alpha\beta\;,$$ and

\begin{align*} E\big((X-\alpha)(Y-\beta)\big)&=E(XY)-\beta E(X)-\alpha E(Y)+\alpha\beta\\ &=E(XY)-\beta\alpha-\alpha\beta+\alpha\beta\\ &=E(XY)-\alpha\beta\\ &=E(XY)-E(X)E(Y)\;. \end{align*}

-
Thanks, that makes sense overall. I also expanded out the factors, but I'm wondering why we can say that $E(E(Y)X) = EYEX$? – David Faux Nov 7 '12 at 23:46
@David: Because $E(Y)$ is a constant, the one that I called $\beta$, and $E$ is linear: $E(\beta X)=\beta E(X)$, just as with any linear function. – Brian M. Scott Nov 7 '12 at 23:48
Ah thank you! That makes sense! – David Faux Nov 7 '12 at 23:50
@David: You’re welcome. – Brian M. Scott Nov 7 '12 at 23:52