# Deriving the in-sample error for linear model from the elements of statistical learning

From the elements of statistical learning, it was claimed that $$\frac{1}{N}\sum_{i=1}^N ||h(x_i) ||^2 \sigma^2_\varepsilon= \frac{p}{N}\sigma^2_\varepsilon$$

where $h(x_i) = X(X^TX)^{-1}x_i$. Can someone show me how to prove this ? Thanks

This came from the image below

$$\sum_{i=1}^{N}\|\textbf{h}(x_i)\|^2 =\sum_{i=1}^{N}\|\textbf{X}(\textbf{X}^T\textbf{X})^{-1}x_i\|^2 =tr\{(\textbf{X}(\textbf{X}^T\textbf{X})^{-1}\textbf{X}^T)^T(\textbf{X}(\textbf{X}^T\textbf{X})^{-1}\textbf{X}^T)\} =tr\{(\textbf{X}(\textbf{X}^T\textbf{X})^{-1}\textbf{X}^T)(\textbf{X}(\textbf{X}^T\textbf{X})^{-1}\textbf{X}^T)\} =tr\{\textbf{X}(\textbf{X}^T\textbf{X})^{-1}(\textbf{X}^T\textbf{X})(\textbf{X}^T\textbf{X})^{-1}\textbf{X}^T\} =tr\{\textbf{X}(\textbf{X}^T\textbf{X})^{-1}\textbf{X}^T\} =tr\{\textbf{X}^T\textbf{X}(\textbf{X}^T\textbf{X})^{-1}\} =tr\{\textbf{I}_p\}=p$$ Q.E.D.