CONTENTS

Exercise: Regression as Projection — Computing the Hat Matrix

Problem

Consider the matrix XRn×pX \in \mathbb{R}^{n \times p} with full column rank (pnp \le n) and the hat matrix H=X(XX)1XH = X(X^\top X)^{-1}X^\top.

  1. Prove that HH is symmetric: H=HH^\top = H.

  2. Prove that HH is idempotent: H2=HH^2 = H.

  3. Show tr(H)=p\text{tr}(H) = p. (Hint: use the cyclic property of trace: tr(ABC)=tr(BCA)=tr(CAB)\text{tr}(ABC) = \text{tr}(BCA) = \text{tr}(CAB).)

  4. Interpret: the residual vector e=(IH)ye = (I - H)y is orthogonal to the column space of XX. Verify directly that Xe=0X^\top e = 0 for the fitted β^\hat\beta.

  5. Numerical example. Construct X=(10111213)X = \begin{pmatrix}1 & 0 \\ 1 & 1 \\ 1 & 2 \\ 1 & 3\end{pmatrix} and y=(1223)y = \begin{pmatrix}1 \\ 2 \\ 2 \\ 3\end{pmatrix}. Compute HH, y^=Hy\hat y = Hy, and the residuals e=yy^e = y - \hat y. Verify that tr(H)=2=p\text{tr}(H) = 2 = p.

Hint

For trace: tr(H)=tr(X(XX)1X)=tr((XX)1XX)=tr(Ip)=p\text{tr}(H) = \text{tr}(X(X^\top X)^{-1}X^\top) = \text{tr}((X^\top X)^{-1}X^\top X) = \text{tr}(I_p) = p.

Jump to the solution when you're ready.