Tutorials › Exam SRM - Statistics for Risk Modeling › The Hat Matrix and Regression Leverage

Exam SRM · Regression Diagnostics · Tutorial 98 of 926

The Hat Matrix and Regression Leverage

Learn how the hat matrix maps responses to fitted values and why its diagonal measures predictor-space leverage.

Intermediate 12 min read

What You'll Learn

  • Derive the OLS hat matrix from the least-squares estimator
  • Calculate fitted values by multiplying the response vector by the hat matrix
  • Compute leverage from the diagonal of the hat matrix
  • Interpret leverage geometrically in simple and multiple regression
  • Distinguish predictor-space leverage from unusual response values
  • Check key properties of the hat matrix and its leverage values

Why the Hat Matrix Matters

In ordinary least squares (OLS), fitted values are calculated from the observed responses and the predictor values. The hat matrix makes that relationship explicit: it is the matrix that maps the response vector to the fitted-value vector. Its diagonal entries, called leverages, describe how unusual each observation’s predictor values are relative to the full design.

Leverage concerns the predictors, not whether an observed response is unusually high or low. That distinction matters in regression diagnostics: an observation can have unusual predictors and still lie close to the fitted regression surface. The hat matrix helps identify this predictor-space feature, but leverage alone does not establish that an observation is erroneous or influential.

Definition: For a linear regression with design matrix \(X\) of full column rank, the hat matrix is \(H=X(X^\mathsf{T}X)^{-1}X^\mathsf{T}\). It maps the response vector \(y\) to the OLS fitted-value vector \(\hat{y}\): \(\hat{y}=Hy\). The leverage of observation \(i\) is the diagonal entry \(h_{ii}\) of \(H\).

Deriving the Mapping from Responses to Fitted Values

Suppose the regression model is \(y=X\beta+\varepsilon\), where \(y\) is an \(n\)-vector of observed responses, \(X\) is the \(n\times p\) design matrix, and \(\beta\) is a \(p\)-vector of coefficients. The rows of \(X\) contain the predictor values; when the model includes an intercept, each row also contains a 1. Assume \(X\) has full column rank, so the OLS coefficient estimate is uniquely defined. The matrix identity below is an algebraic result of OLS; normality or constant error variance is not required to compute \(H\).

OLS chooses \(\hat{\beta}\) to minimize the residual sum of squares, \((y-X\beta)^\mathsf{T}(y-X\beta)\). Differentiating with respect to \(\beta\) and setting the derivative to zero gives the normal equations. Full column rank allows us to solve them:

$$ X^\mathsf{T}X\hat{\beta}=X^\mathsf{T}y \qquad\Longrightarrow\qquad \hat{\beta}=(X^\mathsf{T}X)^{-1}X^\mathsf{T}y. $$

The fitted values are \(X\hat{\beta}\). Substituting the expression for \(\hat{\beta}\) shows that fitted values are a matrix transformation of the observed responses:

$$ \hat{y}=X\hat{\beta} =X(X^\mathsf{T}X)^{-1}X^\mathsf{T}y =Hy. $$

Thus, the name “hat matrix” reflects its role in putting a hat on \(y\): \(H\) transforms \(y\) into \(\hat{y}\). This formula does not apply as written if \(X\) is rank deficient, because \((X^\mathsf{T}X)^{-1}\) then does not exist. In this tutorial, the full-rank OLS setting is assumed throughout.

Worked Example: Constructing the Hat Matrix and Fitted Values

Fit a simple linear regression with an intercept using predictor values \(x=(0,1,2)\) and responses \(y=(2,1,5)^\mathsf{T}\). Construct \(H\), calculate the fitted values, and identify each observation’s leverage.

Method and design matrix. The design matrix has a column of ones for the intercept and a column for \(x\):

$$ X= \begin{pmatrix} 1&0\\ 1&1\\ 1&2 \end{pmatrix}, \qquad X^\mathsf{T}X= \begin{pmatrix} 3&3\\ 3&5 \end{pmatrix}. $$

The determinant of \(X^\mathsf{T}X\) is \(3(5)-3(3)=6\), so the matrix is invertible. Its inverse is:

$$ (X^\mathsf{T}X)^{-1} =\frac{1}{6} \begin{pmatrix} 5&-3\\ -3&3 \end{pmatrix}. $$

Construct \(H\). Multiplying \(X(X^\mathsf{T}X)^{-1}X^\mathsf{T}\) gives:

$$ H= \begin{pmatrix} 5/6&1/3&-1/6\\ 1/3&1/3&1/3\\ -1/6&1/3&5/6 \end{pmatrix}. $$

Calculate fitted values. Multiply each row of \(H\) by \(y\):

$$ \hat{y}_1=(5/6)(2)+(1/3)(1)+(-1/6)(5)=7/6, $$ $$ \hat{y}_2=(1/3)(2)+(1/3)(1)+(1/3)(5)=8/3, $$ $$ \hat{y}_3=(-1/6)(2)+(1/3)(1)+(5/6)(5)=25/6. $$

Read the leverage values. The diagonal entries are \(h_{11}=5/6\), \(h_{22}=1/3\), and \(h_{33}=5/6\). The two endpoint predictor values have greater leverage than the middle value because they lie farther from the predictor mean. As a check, the fitted values lie on the OLS line with intercept \(7/6\) and slope \(3/2\): at \(x=0,1,2\), this gives \(7/6,8/3,25/6\), respectively.

Common mistake avoided: leverage is read from the diagonal of \(H\), not from the fitted values or residuals. The fitted value \(25/6\), for example, is not the leverage of observation 3; its leverage is \(5/6\).

What the Diagonal Entries Measure

The \(i\)th fitted value is a weighted sum of all the observed responses:

$$ \hat{y}_i=\sum_{j=1}^{n}h_{ij}y_j. $$

The diagonal entry \(h_{ii}\) is the weight attached to observation \(i\)’s own response in its fitted value. It is also a measure of how far the observation’s predictor row extends in the geometry of the design. A larger \(h_{ii}\) indicates more unusual predictor coordinates relative to the other rows of \(X\), not necessarily an unusual response.

The geometry can be seen from the properties of \(H\). It is symmetric, \(H^\mathsf{T}=H\), and idempotent, \(H^2=H\). Symmetry follows directly from the formula for \(H\). For idempotence, multiply \(H\) by itself and use \((X^\mathsf{T}X)^{-1}X^\mathsf{T}X=I\):

$$ H^2 =X(X^\mathsf{T}X)^{-1}X^\mathsf{T}X(X^\mathsf{T}X)^{-1}X^\mathsf{T} =H. $$

These properties show that \(H\) is the matrix for projection onto the column space of \(X\), the space of response vectors that the regression model can fit. In particular, they explain why diagonal entries are between 0 and 1. Since \(H\) is symmetric and idempotent,

$$ h_{ii}=(H^2)_{ii} =\sum_{j=1}^{n}h_{ij}h_{ji} =\sum_{j=1}^{n}h_{ij}^2. $$

The right-hand side is nonnegative. Also, \(H\) is an orthogonal projection, which cannot increase vector length. Projecting the \(i\)th coordinate unit vector therefore gives a vector of squared length at most 1; that squared length is \(h_{ii}\). Hence \(0\le h_{ii}\le1\). This bound is an algebraic property, not a rule for classifying observations as high or low leverage.

Leverage in Regression with an Intercept

For a simple linear regression with an intercept, leverage has a convenient form in terms of the predictor values. Let \(\bar{x}\) be their mean and \(S_{xx}=\sum_{j=1}^{n}(x_j-\bar{x})^2\), with \(S_{xx}>0\). The diagonal entry for observation \(i\) is:

$$ h_{ii}=\frac{1}{n}+\frac{(x_i-\bar{x})^2}{S_{xx}}. $$

The first term is shared by all observations. The second increases as \(x_i\) moves farther from the predictor mean, measured relative to the overall spread of the predictor values. This is the one-predictor version of a broader idea: in multiple regression with an intercept, leverage reflects how far the observation’s centered predictor vector lies from the center, accounting for the scaling and relationships among predictors.

To see the multiple-predictor expression, let \(Z\) be the design matrix with the intercept column removed and each predictor column centered. If the centered predictor columns are linearly independent, then:

$$ h_{ii}=\frac{1}{n}+z_i^\mathsf{T}(Z^\mathsf{T}Z)^{-1}z_i, $$

where \(z_i\) is the centered predictor vector for observation \(i\). The quadratic term accounts for predictor scales and correlations together; looking at one predictor at a time can miss an unusual combination of otherwise moderate values.

Worked Example: Leverage from Two Centered Predictors

A hypothetical pricing study uses an intercept and two predictors. Across five observations, the centered predictor values are \(x_1=(-2,-1,0,1,2)\) and \(x_2=(1,-2,2,-2,1)\). Find the leverage of observations 1, 2, and 3, and check the sum of all five leverages.

Method. Both predictor columns have mean zero. Their cross-product is \((-2)(1)+(-1)(-2)+(0)(2)+(1)(-2)+(2)(1)=0\), so they are orthogonal. Their sums of squares are \(10\) and \(14\). Thus \(Z^\mathsf{T}Z\) is diagonal with entries 10 and 14, and the leverage formula becomes:

$$ h_{ii}=\frac{1}{5}+\frac{x_{1i}^2}{10}+\frac{x_{2i}^2}{14}. $$

For observation 1, \(x_{11}=-2\) and \(x_{21}=1\):

$$ h_{11}=\frac{1}{5}+\frac{4}{10}+\frac{1}{14} =\frac{47}{70}\approx0.6714. $$

For observation 2, \(x_{12}=-1\) and \(x_{22}=-2\):

$$ h_{22}=\frac{1}{5}+\frac{1}{10}+\frac{4}{14} =\frac{41}{70}\approx0.5857. $$

For observation 3, \(x_{13}=0\) and \(x_{23}=2\):

$$ h_{33}=\frac{1}{5}+0+\frac{4}{14} =\frac{17}{35}\approx0.4857. $$

Observations 4 and 2 have the same squared predictor coordinates, as do observations 5 and 1. Therefore \(h_{44}=41/70\) and \(h_{55}=47/70\). The sum is \(47/70+41/70+17/35+41/70+47/70=3\), equal to the number of fitted coefficients, including the intercept. This verifies the calculation.

Common mistake avoided: calculating leverage from just one predictor would ignore the contribution of the other. In this example both coordinates contribute to each observation’s leverage, with each contribution scaled by that predictor’s sum of squares.

Leverage Depends on the Design, Not the Responses

The formula \(H=X(X^\mathsf{T}X)^{-1}X^\mathsf{T}\) contains \(X\), but not \(y\). Consequently, changing the responses while keeping the predictor values and model specification fixed leaves every leverage unchanged. Conversely, adding or changing predictor values can alter the leverage of existing observations as well as that of the new observation.

Worked Example: How an Added Predictor Value Changes Leverage

A simple regression with an intercept initially uses predictor values \(0,1,2\). Add a fourth observation with predictor value \(5\). Calculate the leverage of the observation at \(x=2\) before and after adding the new design point, and calculate the new observation’s leverage.

Before adding the observation. There are \(n=3\) values, \(\bar{x}=1\), and \(S_{xx}=(0-1)^2+(1-1)^2+(2-1)^2=2\). Thus:

$$ h_{x=2}=\frac{1}{3}+\frac{(2-1)^2}{2} =\frac{5}{6}\approx0.8333. $$

After adding \(x=5\). The four predictor values have mean \(\bar{x}=(0+1+2+5)/4=2\). Their centered sum of squares is \(4+1+0+9=14\). The leverage for the existing observation at \(x=2\) is now \(1/4+0/14=1/4\). The new observation has leverage:

$$ h_{x=5}=\frac{1}{4}+\frac{(5-2)^2}{14} =\frac{1}{4}+\frac{9}{14} =\frac{25}{28}\approx0.8929. $$

The added design point changes the center and spread used to measure the other observations’ positions. Thus the old observation’s leverage changes too. No response values are needed for these calculations, and changing those responses would not change either set of leverage values.

Common mistake avoided: leverage is not a fixed label attached to a predictor value in isolation. It is calculated relative to the complete design matrix and the specified model.

Interpretation and a Common Exam Trap

A common mistake is to use response values in a leverage calculation, perhaps by treating a response as though it were a predictor or by assuming that a large residual implies high leverage. For example, in simple regression with an intercept, the correct formula uses \(x_i\), \(\bar{x}\), and \(S_{xx}\); replacing \(x_i\) with \(y_i\) does not calculate leverage. The response values enter the fitted values and residuals, but they do not enter \(H\).

The converse is important too: high leverage does not automatically mean an observation has a large residual. Leverage describes the position of the predictor row in the design. Whether the observed response agrees with the fitted relationship is a separate question. A leverage value by itself does not prove a response anomaly, indicate an error, or justify deleting an observation.

For the full-rank model, another useful check is that the sum of the diagonal entries equals the rank of \(X\), which is \(p\):

$$ \sum_{i=1}^{n}h_{ii}=\operatorname{tr}(H)=p. $$

This follows from the trace of \(X(X^\mathsf{T}X)^{-1}X^\mathsf{T}\), which equals the trace of the \(p\times p\) identity matrix. It is a check on a computed set of leverages, not a method for deciding whether any individual value is concerning. The practical interpretation always depends on the predictor design and the purpose of the model.

Key takeaway: OLS fitted values satisfy \(\hat{y}=Hy\), where \(H=X(X^\mathsf{T}X)^{-1}X^\mathsf{T}\). Each diagonal entry \(h_{ii}\) measures the predictor-space leverage of observation \(i\). It depends on the design matrix, not on the observed responses.

Check Your Understanding

Work each problem using the stated model and design, then compare with the answer.

  1. A simple regression with an intercept uses \(x=(1,2,3,4)\). Calculate the leverage for the observation at \(x=4\). (Answer: \(0.7000\))
  2. For a simple regression with an intercept and \(x=(-1,0,1)\), calculate the leverage of the middle observation. Would changing only the response values change it? (Answer: \(1/3\); no)
  3. A regression with an intercept uses two centered, orthogonal predictors across four observations: \(x_1=(-1,-1,1,1)\) and \(x_2=(-1,1,-1,1)\). Calculate the leverage of observation 1. (Answer: \(0.7500\))
  4. A simple regression with an intercept uses \(x=(0,1,2,3,4)\). Calculate the leverage at \(x=0\), showing the predictor mean and centered sum of squares. (Answer: \(\bar{x}=2\), \(S_{xx}=10\), \(h=0.6000\))
  5. For a specified observation, the row of the design matrix is \(x_i^\mathsf{T}=(1,2,1)\), and \((X^\mathsf{T}X)^{-1}\) is diagonal with entries \(0.20,0.05,0.04\). Using \(h_{ii}=x_i^\mathsf{T}(X^\mathsf{T}X)^{-1}x_i\), calculate its leverage. (Answer: \(0.4400\))
Clinical Biostats