How $X^T(I_n-n^{-1}J_n)X=\sum_{i=1}^{n}(X_i-\bar X)^2$?

As pointed out in the comments, you've assumed $X^\top J_n X = X^\top X$ which is not true.

If you work out the matrix multiplication, you'll find that $X^\top J_n X = \sum_{i=1}^n \sum_{j=1}^n X_i X_j = \left(\sum_{i=1}^n X_i\right)^2 = (n\bar{X})^2$.

It then remains to show the equality $\sum_{i=1}^n (X_i - \bar{X})^2 = \sum_{i=1}^n X_i^2 - \frac{1}{n}(n\bar{X})^2$, e.g. by expanding the left-hand side.


The left hand side evaluates to

$$ \sum_iX_i^2 - \frac1n (\sum_jX_j)^2 = \sum_i X_i^2 - n\bar{X}^2 $$

while the right hand side gives

$$ \sum_i(X_i - \bar{X})^2 = \sum_i(X_i^2 - 2X_i\bar{X} + \bar{X}^2) = \sum_iX_i^2 - 2n\bar{X}^2 +\bar{X}^2 = \sum_iX_i^2 - n\bar{X}^2 $$


Let ${\bar x} := \dfrac1n {\bf 1}_n^\top {\bf x}$.

$$ {\bf x}^\top \left( {\bf I}_n - \frac1n {\bf 1}_n {\bf 1}_n^\top \right) {\bf x} = \| {\bf x} \|_2^2 - n {\bar x}^2 = \left\| {\bf x} - {\bar x} {\bf 1}_n \right\|_2^2 $$