Exercise: Regression as Projection — Computing the Hat Matrix
Problem
Consider the matrix X∈Rn×p with full column rank (p≤n) and the hat matrix H=X(X⊤X)−1X⊤.
-
Prove that H is symmetric: H⊤=H.
-
Prove that H is idempotent: H2=H.
-
Show tr(H)=p. (Hint: use the cyclic property of trace: tr(ABC)=tr(BCA)=tr(CAB).)
-
Interpret: the residual vector e=(I−H)y is orthogonal to the column space of X. Verify directly that X⊤e=0 for the fitted β^.
-
Numerical example. Construct
X=11110123 and
y=1223. Compute
H,
y^=Hy, and the residuals
e=y−y^. Verify that
tr(H)=2=p.
Hint
For trace: tr(H)=tr(X(X⊤X)−1X⊤)=tr((X⊤X)−1X⊤X)=tr(Ip)=p.
Jump to
the solution when you're ready.