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

I think it is cleaner to expand the right-hand side. We have $$(X_i-\bar{X})^2=X_i^2-2X_i\bar{X}+(\bar{X})^2.$$ Sum over all $i$, noting that $2\sum X_i\bar{X}=2n\bar{X}\bar{X}=2n(\bar{X})^2$ and $\sum (\bar{X})^2=n(\bar{X})^2$.

There is some cancellation. Now divide by $n$ and we get the left-hand side.


Expand the square and use that $\sum\limits_{k=1}^nX_k=n\bar{X}$: $$ \begin{align} \frac1n\sum_{k=1}^n(X_k-\bar{X})^2 &=\frac1n\sum_{k=1}^n(X_k^2-2X_k\bar{X}+\bar{X}^2)\\ &=\frac1n\left(\sum_{k=1}^nX_k^2-2n\bar{X}\bar{X}+n\bar{X}^2\right)\\ &=\frac1n\sum_{k=1}^nX_k^2-\bar{X}^2\\ \end{align} $$


Since you tagged it under statistics, note that the LHS is simply $E[X^2] - E[X]^2$, and the RHS is $E[(X-\bar{X})^2]$. Both of these are ways to write Variance of $X$, hence they are equal.


\begin{align} \sum_{i=1}^n (X_i-\bar X)^2 & = \sum_{i=1}^n (X_i^2-2X_i\bar X + (\bar X)^2) \\[10pt] & = \left(\sum_{i=1}^n (X_i^2)\right) - \left( \sum_{i=1}^n 2X_i \bar X \right) + \left(\sum_{i=1}^n ((\bar X)^2) \right). \end{align}

In the second term, the factor $2\bar X$ does not change as $i$ goes from $1$ to $n$, so it can be pulled out, getting $$ 2\bar X\sum_{i=1}^n X_i. $$ Then we can say that $\displaystyle\sum_{i=1}^n X_i = n\bar X$. The second term is then $2n(\bar X)^2$.

In the third term, the sum is $(\bar X)^2 +\cdots+(\bar X)^2$. This is just $n(\bar X)^2$.

Now we have $$ \left(\sum_{i=1}^n (X_i^2)\right) - 2n(\bar X)^2 + n(\bar X)^2. $$ The last two terms can be collected into one term.