# Sample variance derivation

I have quite a simple question but I can't for the life of me figure it out.

For a set of iid samples $\,\,X_1, X_2, .., X_n\,\,$ from distribution with mean $\,\mu$.

If you are given the sample variance as

$$S^2 = \frac{1}{n-1}\sum\limits_{i=1}^{n}\left(X_i - \bar{X}\right)^2$$

How can you write the following?

$$S^2 = \frac{1}{n-1}\left[\sum\limits_{i=1}^{n}\left(X_i - \mu\right)^2 - n\left(\mu - \bar{X}\right)^2\right]$$

All texts that cover this just skip the details but I can't work it out myself. I get stuck after expanding like so

$$S^2 = \frac{1}{n-1}\sum\limits_{i=1}^{n}\left(X_i^2 -2X_i\bar{X} + \bar{X}^2\right)$$

What am I missing?

Edit: A similarly equivalent expression is often given that I also can't derive but which may be more obvious is

$$S^2 = \frac{1}{n-1}\left[\sum\limits_{i=1}^{n}X_i^2 - n\bar{X}^2\right]$$

-

## 2 Answers

\begin{align*} \frac{1}{n-1}\left[\sum\limits_{i=1}^{n}\left(X_i - \mu\right)^2 - n\left(\mu - \bar{X}\right)^2\right] &= \frac{1}{n-1}\sum\limits_{i=1}^{n}\left[\left(X_i - \mu\right)^2 - \left(\mu - \bar{X}\right)^2\right]\\ &= \frac{1}{n-1}\sum\limits_{i=1}^{n}\left(X_i - \bar{X}\right) \left(X_i + \bar{X} - 2\mu\right).\end{align*} Now, $$\sum_{i=1}^n (X_i - \bar{X}) = \sum_{i=1}^n X_i - \sum_{i=1}^n \bar{X} = n\bar{X} - n\bar{X}= 0$$ and so \begin{align*} \frac{1}{n-1}\sum\limits_{i=1}^{n}\left(X_i - \bar{X}\right) \left(X_i + \bar{X} - 2\mu\right) &= \frac{1}{n-1}\left[\sum\limits_{i=1}^{n}\left(X_i - \bar{X}\right)X_i + \sum\limits_{i=1}^{n}\left(X_i - \bar{X}\right)\left(\bar{X} - 2\mu\right)\right]\\ &= \frac{1}{n-1}\left[\sum\limits_{i=1}^{n}\left(X_i - \bar{X}\right)X_i + \left(\bar{X} - 2\mu\right)\sum\limits_{i=1}^{n}\left(X_i - \bar{X}\right) \right]\\ &= \frac{1}{n-1}\left[\sum\limits_{i=1}^{n}\left(X_i - \bar{X}\right)X_i\right]\\ &= \frac{1}{n-1}\left[\sum\limits_{i=1}^{n}\left(X_i - \bar{X}\right)X_i - \bar{X}\sum\limits_{i=1}^{n}\left(X_i - \bar{X}\right)\right]\\ &= \frac{1}{n-1}\sum\limits_{i=1}^{n}\left(X_i - \bar{X}\right)^2\\ &= S^2 \end{align*}

-
Crystal clear, thank you. (I'm a little bit surprised that this is often assumed obvious because it isn't to me) –  Chris Jun 2 '12 at 2:41
@Chris If you liked Dilip's answer (I know I did) then it'd be nice if you upvote it and, perhaps, accept it as best answer, though you can wait for this last some time in case other answers pop up –  DonAntonio Jun 2 '12 at 3:05
@Don: Note that the OP cannot yet upvote until someone else upvotes his question so that he has at least 15 rep. –  cardinal Jun 2 '12 at 18:05

$$S^2=\frac{1}{n-1}\sum_{i=1}^n(X_i-\bar X)^2$$ $$=\frac{1}{n-1}\sum_{i=1}^n(X_i-\mu +\mu-\bar X)^2$$

$$=\frac{1}{n-1}\sum_{i=1}^n[(X_i-\mu)^2 +2(X_i-\mu)(\mu-\bar X)+(\mu-\bar X)^2]$$ $$=\frac{1}{n-1}[\sum_{i=1}^n(X_i-\mu)^2 +2\sum_{i=1}^n(X_i-\mu)(\mu-\bar X)+\sum_{i=1}^n(\mu-\bar X)^2]$$ $$=\frac{1}{n-1}[\sum_{i=1}^n(X_i-\mu)^2 +2n\sum_{i=1}^n(\frac{X_i}{n}-\frac{\mu}{n})(\mu-\bar X)+\sum_{i=1}^n(\mu-\bar X)^2]$$ $$=\frac{1}{n-1}[\sum_{i=1}^n(X_i-\mu)^2 -2n(\mu-\bar X)(\mu-\bar X)+n(\mu-\bar X)^2]$$

Let me add that replace $\frac{\sum_{i=1}^n X_i}{n}$ by $\bar X$ in the last but one line. and flip it around to get the negative sign.

-
The middle term gives zero. Hence the result. Hmm... –  cardinal Jun 2 '12 at 2:01
I guess then, my trouble is understanding why the middle term is zero. Could you elaborate please? –  Chris Jun 2 '12 at 2:03
@Chris: It's not zero, hence, my comment. –  cardinal Jun 2 '12 at 2:03
I have seen my mistake. I will fix it. –  smanoos Jun 2 '12 at 2:10
Thank you! I definitely need to get better at manipulating expressions inside summations. –  Chris Jun 2 '12 at 2:19