Linear Algebra

Hat Matrix and Regression Leverage

Inspect how the hat matrix turns predictor geometry into leverage and leave-one-out predictions.

By Riano · Published · Updated · Report a correction

Hat Matrix and Regression Leverage

The hat matrix is the matrix that maps the vector of observed values to the vector of fitted values.

Worked Example

Consider an OLS regression with an intercept and one predictor for four observations. Suppose the predictor values are x = [0, 1, 2, 10] (the "Extreme" design) and responses are y = [2, 5, 8, 42] (the "Last outlier" responses).

  1. Calculate the mean of x: x = (0 + 1 + 2 + 10) / 4 = 3.25.
  2. Calculate the sum of squares Sxx: (0 - 3.25)2 + (1 - 3.25)2 + (2 - 3.25)2 + (10 - 3.25)2 = 62.75.
  3. The hat matrix entries are given by Hij = 1/4 + (xi - x)(xj - x) / Sxx.
  4. For the last observation (i=3), the leverage is H33 = 1/4 + (10 - 3.25)2 / 62.75 = 0.9760956175298805.
  5. The fitted value for the last observation is 41.7609561752988, with a residual of 0.23904382470119523.
  6. The leave-one-out prediction (LOO prediction) for the last observation is (41.7609561752988 - 0.9760956175298805 * 42) / (1 - 0.9760956175298805) = 32.0.

Experiment

In the interactive Hat Matrix and Regression Leverage module, you can explore the effects of different design and response configurations on the leverage.

  1. Set the Design control to Single distinct (where x = [0, 0, 0, 1]). Notice how the last observation has a leverage of exactly 1.
  2. Set the Responses control to Last outlier and the Observation control to 3.
  3. Observe that for Single distinct, the LOO prediction is Unavailable because removing the observation at x=1 leaves the model unidentifiable at that point.
  4. Switch back to the Extreme design and observe the Leverage and LOO prediction values. High leverage points disproportionately affect the fitted regression line.

Limitations

The hat matrix relies on the invertibility of the matrix XTX (or in the simple linear case, Sxx > 0). If all predictors are identical (e.g., the Repeated design where x = [1, 1, 1, 1]), then Sxx = 0. In this singular case, the fitted projection reduces to a simple mean prediction where every entry is 1/n and the rank is 1. We artificially compute a unique constant fitted projection H = 11T/4 despite nonunique coefficients without using an inverse singular XTX.

References

The following reference provides the basis for the calculations:

Experience it interactively

Adjust parameters, observe in real time, and build deep intuition with Riano’s interactive Hat Matrix and Regression Leverage module.

Try Hat Matrix and Regression Leverage on Riano →

More in Linear Algebra