Algorithm Search⌘K

Definitioncore

Hat matrix

Also called influence matrix, projection matrix of least squares.

The matrix H = X (X^T X)^(-1) X^T that maps the responses to the fitted values. It is a projection; its diagonal holds the leverages.

The hat matrix of a linear regression is the n×nn \times n matrix HH that turns the observed responses yy into the fitted values: y^=Hy\hat y = Hy. It "puts the hat on yy". For a design matrix XX of full column rank it is H=X(X⊤X)−1X⊤H = X(X^\top X)^{-1}X^\top, the orthogonal projection onto the column space of XX. It depends only on XX, not on yy. Its diagonal entries are the leverages, and most single-point regression diagnostics (studentized residuals, Cook's distance, leave-one-out residuals) are simple functions of HH and the residuals. The fit itself is on the ordinary least squares page.

Definition

Let XX be a real n×pn \times p matrix of rank pp (so p≤np \le n). Its pp columns include the intercept column of ones when the model has one; some texts write kk for pp. The hat matrix is

H=X(X⊤X)−1X⊤,hij=xi⊤(X⊤X)−1xj,H = X(X^\top X)^{-1}X^\top, \qquad h_{ij} = x_i^\top (X^\top X)^{-1} x_j,

where xi⊤x_i^\top is row ii of XX. If b^=(X⊤X)−1X⊤y\hat b = (X^\top X)^{-1}X^\top y is the least squares coefficient vector, then y^=Xb^=Hy\hat y = X\hat b = Hy and the residual vector is e=y−y^=(I−H)ye = y - \hat y = (I - H)y. The matrix I−HI - H is sometimes called the residual maker.

Computation. With a thin QR decomposition X=Q1RX = Q_1R, where Q1Q_1 is n×pn \times p with orthonormal columns and RR is nonsingular, H=Q1RR−1R−⊤R⊤Q1⊤=Q1Q1⊤H = Q_1R R^{-1}R^{-\top}R^\top Q_1^\top = Q_1Q_1^\top. With a thin singular value decomposition X=UΣV⊤X = U\Sigma V^\top, H=UU⊤H = UU^\top. So hiih_{ii} is the squared length of row ii of Q1Q_1, which costs O(np2)O(np^2) for the factorization and O(np)O(np) for all diagonal entries, without forming X⊤XX^\top X or the n×nn \times n matrix HH. If XX has rank r<pr < p, the projection onto its column space is still unique and equals XX+XX^+, with X+X^+ the pseudoinverse, but the formula with (X⊤X)−1(X^\top X)^{-1} does not apply.

Properties. Each follows from the definition in one or two lines.

  1. Symmetric: H⊤=HH^\top = H, since (X⊤X)−1(X^\top X)^{-1} is symmetric.
  2. Idempotent: H2=X(X⊤X)−1X⊤X(X⊤X)−1X⊤=HH^2 = X(X^\top X)^{-1}X^\top X(X^\top X)^{-1}X^\top = H. So HH is the orthogonal projection onto col⁡(X)\operatorname{col}(X), and I−HI - H is the orthogonal projection onto its orthogonal complement, the residual space. In particular HX=XHX = X, (I−H)X=0(I - H)X = 0 and X⊤e=0X^\top e = 0.
  3. Trace and rank pp: tr⁡H=tr⁡((X⊤X)−1X⊤X)=tr⁡Ip=p\operatorname{tr} H = \operatorname{tr}((X^\top X)^{-1}X^\top X) = \operatorname{tr} I_p = p. A symmetric idempotent matrix has eigenvalues λ\lambda with λ2=λ\lambda^2 = \lambda, so each is 0 or 1; HH has pp eigenvalues 1 and n−pn - p eigenvalues 0, and tr⁡(I−H)=n−p\operatorname{tr}(I - H) = n - p, the residual degrees of freedom.
  4. Only the column space matters: if AA is a nonsingular p×pp \times p matrix, XAXA has the same hat matrix. Rescaling, centering or reordering the predictors does not change HH.
  5. Sensitivities: hij=∂y^i/∂yjh_{ij} = \partial \hat y_i/\partial y_j. Row ii gives the weights with which the fitted value y^i\hat y_i averages the responses.
  6. Covariances: if Var⁡(y)=σ2I\operatorname{Var}(y) = \sigma^2 I, then Var⁡(y^)=σ2H\operatorname{Var}(\hat y) = \sigma^2 H and Var⁡(e)=σ2(I−H)\operatorname{Var}(e) = \sigma^2(I - H), so Var⁡(ei)=σ2(1−hii)\operatorname{Var}(e_i) = \sigma^2(1 - h_{ii}): residuals are correlated and have unequal variances even when the errors do not.
  7. Diagonal bounds: hii=∑jhij2≥hii2h_{ii} = \sum_j h_{ij}^2 \ge h_{ii}^2 by properties 1 and 2, so 0≤hii≤10 \le h_{ii} \le 1. If the model has an intercept, H1=1H\mathbf 1 = \mathbf 1 (rows sum to 1) and H−1n11⊤H - \frac1n\mathbf 1\mathbf 1^\top is again a projection, so hii≥1/nh_{ii} \ge 1/n. The average of the hiih_{ii} is p/np/n.
  8. Leave-one-out: the prediction error of yiy_i from the fit without observation ii is yi−y^(i)=ei/(1−hii)y_i - \hat y_{(i)} = e_i/(1 - h_{ii}) when hii<1h_{ii} < 1. This follows from the Sherman-Morrison formula applied to X⊤X−xixi⊤X^\top X - x_ix_i^\top.

Examples and non-examples

Intercept only. X=1X = \mathbf 1 gives H=1n11⊤H = \frac1n\mathbf 1\mathbf 1^\top: every fitted value is yˉ\bar y, and every hii=1/nh_{ii} = 1/n.

Simple linear regression. For X=(1,x)X = (\mathbf 1, x), with xˉ\bar x the mean and Sxx=∑(xk−xˉ)2S_{xx} = \sum (x_k - \bar x)^2,

hij=1n+(xi−xˉ)(xj−xˉ)Sxx.h_{ij} = \frac1n + \frac{(x_i - \bar x)(x_j - \bar x)}{S_{xx}}.

A numerical case. Take x=(1,2,3,4,5,10)x = (1, 2, 3, 4, 5, 10), so n=6n = 6, p=2p = 2, xˉ=25/6\bar x = 25/6 and Sxx=305/6S_{xx} = 305/6. Exact arithmetic gives the diagonal

hii=111305, 79305, 59305, 51305, 1161, 5161≈0.364, 0.259, 0.193, 0.167, 0.180, 0.836,h_{ii} = \tfrac{111}{305},\ \tfrac{79}{305},\ \tfrac{59}{305},\ \tfrac{51}{305},\ \tfrac{11}{61},\ \tfrac{51}{61} \approx 0.364,\ 0.259,\ 0.193,\ 0.167,\ 0.180,\ 0.836,

whose sum is 2=p2 = p. The last row is 161(−12,−5,2,9,16,51)\frac{1}{61}(-12, -5, 2, 9, 16, 51); it sums to 1, and the sum of its squares is 51/61=h6651/61 = h_{66}, as property 7 says. So the fitted value at x=10x = 10 puts weight 0.836 on its own response, and the five other points share the rest, with negative weights on the far ones. With the responses y=(2,3,5,4,6,4)y = (2, 3, 5, 4, 6, 4) the residual at x=10x = 10 is e6=−63/61≈−1.033e_6 = -63/61 \approx -1.033, but the line fitted without that point predicts 10.3 there, a prediction error of −6.3=e6/(1−h66)-6.3 = e_6/(1 - h_{66}). A small residual at a high-leverage point says little about the fit there.

One-way layout. For group indicator columns, HH is block diagonal, and each block is 1ng11⊤\frac{1}{n_g}\mathbf 1\mathbf 1^\top: the fitted value is the group mean, and hii=1/ngh_{ii} = 1/n_g.

Non-example: the ridge smoother. Ridge regression gives y^=Sλy\hat y = S_\lambda y with Sλ=X(X⊤X+λI)−1X⊤S_\lambda = X(X^\top X + \lambda I)^{-1}X^\top, λ>0\lambda > 0. It is symmetric but not idempotent: its eigenvalues are dj2/(dj2+λ)<1d_j^2/(d_j^2 + \lambda) < 1, with djd_j the singular values of XX. Its trace, the effective degrees of freedom, is less than pp. Diagnostics that use H2=HH^2 = H do not carry over unchanged.

Non-example: weighted least squares. With weights WW, the fitted values are X(X⊤WX)−1X⊤WyX(X^\top WX)^{-1}X^\top Wy. This matrix is idempotent but not symmetric, an oblique projection. The symmetric version is W1/2X(X⊤WX)−1X⊤W1/2W^{1/2}X(X^\top WX)^{-1}X^\top W^{1/2}, the hat matrix of the scaled problem; weighted least squares uses that one.

Non-example: XX without full column rank. If two columns are equal, X⊤XX^\top X is singular and the formula fails. The projection XX+XX^+ still exists and has trace equal to the rank.

  • Leverage is the diagonal entry hiih_{ii}, the self-influence of observation ii.
  • Studentized residuals divide eie_i by an estimate of σ1−hii\sigma\sqrt{1 - h_{ii}}, using property 6.
  • Cook's distance measures the shift of all fitted values when observation ii is deleted, through hiih_{ii} and eie_i.
  • Smoother matrix. For a linear smoother y^=Sy\hat y = Sy (kernel, spline, ridge), SS plays the role of HH, and tr⁡S\operatorname{tr} S is called the effective number of parameters. The leave-one-out shortcut of property 8 holds for ridge regression and gives the closed form of leave-one-out cross-validation.
  • Projection matrices in general. Any symmetric idempotent matrix is the orthogonal projection onto its column space. The least squares problem page derives least squares as this projection.

References

  1. David C. Hoaglin and Roy E. Welsch, The hat matrix in regression and ANOVA, The American Statistician 32(1), 1978, pp. 17 to 22, The whole paper (the properties of H, the bounds on its diagonal, the average p/n and the rule 2p/n)
  2. George A. F. Seber and Alan J. Lee, Linear Regression Analysis, second edition, Wiley, 2003, Chapter 3 (least squares as an orthogonal projection, idempotent matrices and their trace) and Section 10.2 (residuals and hat matrix diagonals)
  3. David A. Belsley, Edwin Kuh and Roy E. Welsch, Regression Diagnostics, Wiley, 1980, Chapter 2 (the hat matrix and the diagnostics built on its diagonal)
  4. Gene H. Golub and Charles F. Van Loan, Matrix Computations, fourth edition, Johns Hopkins University Press, 2013, Section 5.2 (the thin QR factorization, whose Q_1 Q_1^T is the orthogonal projection on the range of X)

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/7G73ZX/text.yaml