In All of Statistics by Larry Wasserman, page 100, Example 7.11, the example is asking about the plug in estimator of the variance for a linear functional (i.e. the plug in estimator for $\sigma^2=T(F)=\mathbb{V}(X)=\int x^2 dF(x)-(\int xdF(x))^2$).
It says the plug in estimator is
\begin{align} \hat{\sigma}^2 &= \int x^2 d\hat{F}(x)-\left(\int xd\hat{F}(x)\right)^2\\ &=\frac{1}{n}\sum_{i=1}^n X_i^2 - \left(\frac{1}{n}\sum_{i=1}^n X_i\right)^2\\ &=\frac{1}{n}\sum_{i=1}^n (X_i-\bar{X}_n)^2 \end{align}
Here, $F$ is the distribution that we pick samples $X_1,\ldots,X_n$ from and $\hat{F}$ is the empirical distribution function generated from these sample.
I am confused about the algebra going from 2nd to 3rd line above. I feel like it should be \begin{align} \hat{\sigma}^2 &= \int x^2 d\hat{F}(x)-\left(\int xd\hat{F}(x)\right)^2\\ &=\frac{1}{n}\sum_{i=1}^n X_i^2 - \left(\frac{1}{n}\sum_{i=1}^n X_i\right)^2\\ &\color{red}{=\frac{1}{n}\sum_{i=1}^n X_i^2-(\bar{X}_n)^2} \end{align}
Can someone help me figure out what I am seeing wrong? Thanks a lot.
Both are correct.
\begin{align} \frac1n \sum_{i=1}^n X_i^2 - \left( \frac1n \sum_{i=1}^n X_i\right)^2 &= \frac1n \left( \sum_{i=1}^n X_i^2 - \frac1n\left(\sum_{i=1}^n X_i \right)^2\right)\\ &=\frac1n \left( \sum_{i=1}^n X_i^2 - \frac1n\left( n\bar{X}\right)^2\right)\\ &=\frac1n \left( \sum_{i=1}^n X_i^2 - n\left( \bar{X}\right)^2\right)\\ &=\frac1n \left( \sum_{i=1}^n X_i^2 - 2n\left( \bar{X}\right)^2+n\bar{X}^2\right)\\ &=\frac1n \left( \sum_{i=1}^n X_i^2 - 2\bar{X}\sum_{i=1}^n X_i+\sum_{i=1}^n\bar{X}^2\right)\\ &= \frac1n \sum_{i=1}^n (X_i - \bar{X})^2 \end{align}