Tutorials › Generalized Linear Models › Weighted Projection Geometry and the Hat Matrix

Gaussian GLMs and Weights · Tutorial 333 of 981

Weighted Projection Geometry and the Hat Matrix

See how weighted least squares becomes an ordinary projection after transforming the data, and use the resulting hat matrix to understand fitted values and leverage.

Beginner 9 min read

What You'll Learn

  • Derive the weighted hat matrix from the weighted least-squares objective
  • Distinguish the projection matrix in transformed coordinates from the raw-response fitted-value matrix
  • Prove symmetry, idempotence, rank, and weighted residual orthogonality
  • Calculate weighted fitted values and leverage in concrete models
  • Interpret diagonal leverage values and recognize common matrix-notation errors

Why Weighted Projection Geometry Matters

Weighted least squares chooses coefficients so that the fitted response is close to the observed response, while giving observations different weights in the squared-error criterion. The resulting geometry is not mysterious: after scaling each observation by the square root of its weight, the problem becomes ordinary least squares in transformed coordinates. In those coordinates, the fitted vector is an orthogonal projection.

This viewpoint gives a precise meaning to the weighted hat matrix. It also clarifies two related ideas that are easy to confuse: the matrix that projects the transformed response, and the matrix that maps the original response to its fitted values. Their diagonal entries agree, so they give the same observation-by-observation leverage.

Definition: Let \(y\in\mathbb{R}^n\) be a response vector, let \(X\in\mathbb{R}^{n\times p}\) be a design matrix with full column rank, and let \(W=\operatorname{diag}(w_1,\ldots,w_n)\), where every \(w_i>0\). The weighted least-squares estimate minimizes \((y-X\beta)^\mathsf{T}W(y-X\beta)\). The weighted hat matrix in transformed coordinates is \[ H=W^{1/2}X(X^\mathsf{T}WX)^{-1}X^\mathsf{T}W^{1/2}. \] Here \(W^{1/2}=\operatorname{diag}(\sqrt{w_1},\ldots,\sqrt{w_n})\). Full column rank and positive weights ensure that \(X^\mathsf{T}WX\) is invertible.

From Weighted Least Squares to a Projection

The objective can be rewritten as an ordinary sum of squares by defining the transformed response \(y^*=W^{1/2}y\) and transformed design matrix \(X^*=W^{1/2}X\). Indeed, \((y-X\beta)^\mathsf{T}W(y-X\beta)=\|y^*-X^*\beta\|^2\). Because the columns of \(X^*\) are linearly independent, its least-squares coefficient estimate is \(\hat{\beta}=(X^\mathsf{T}WX)^{-1}X^\mathsf{T}Wy\). Multiplying the fitted transformed response by the corresponding matrices gives the projection formula below.

$$ \hat{\beta}=(X^\mathsf{T}WX)^{-1}X^\mathsf{T}Wy, \qquad \hat{y}^*=W^{1/2}X\hat{\beta}=Hy^*. $$

Thus \(H\) acts on \(y^*\), not directly on \(y\). The fitted response on the original scale is \(\hat{y}=X\hat{\beta}\). Its mapping from the original response is given by a different matrix, often denoted \(S\):

$$ \hat{y}=Sy,\qquad S=X(X^\mathsf{T}WX)^{-1}X^\mathsf{T}W =W^{-1/2}HW^{1/2}. $$

The matrix \(S\) need not be symmetric in the ordinary Euclidean geometry. It is the matrix \(H\) that is symmetric and represents an ordinary orthogonal projection. Since \(S\) and \(H\) have the same diagonal entries, either matrix gives the same leverage for each observation.

Worked Example: A Weighted Intercept-Only Fit

Take \(X=(1,1)^\mathsf{T}\), \(W=\operatorname{diag}(1,4)\), and \(y=(2,5)^\mathsf{T}\). The model has an intercept only, so both observations share one fitted value.

First, \(X^\mathsf{T}WX=1+4=5\), and \(X^\mathsf{T}Wy=2+4(5)=22\). Therefore \(\hat{\beta}=22/5=4.4\), giving \(\hat{y}=(4.4,4.4)^\mathsf{T}\).

For the transformed coordinates, \(W^{1/2}X=(1,2)^\mathsf{T}\). Hence \(H=(1/5)(1,2)^\mathsf{T}(1,2)\), or explicitly

$$ H= \begin{pmatrix} 1/5&2/5\\ 2/5&4/5 \end{pmatrix}, \qquad y^*= \begin{pmatrix} 2\\10 \end{pmatrix}, \qquad Hy^*= \begin{pmatrix} 4.4\\8.8 \end{pmatrix}. $$

The transformed fitted vector is \(W^{1/2}\hat{y}=(4.4,8.8)^\mathsf{T}\), as required. The diagonal entries are \(h_{11}=0.2\) and \(h_{22}=0.8\): the observation with weight 4 has more leverage in this intercept-only fit.

Projection Properties and Their Proof

The geometric claims follow directly from writing the weighted hat matrix in terms of the transformed design \(X^*=W^{1/2}X\). The columns of \(X^*\) span the model’s fitted subspace in transformed coordinates.

Theorem: Under the conditions in the definition, \(H\) is symmetric and idempotent, and its column space is the column space of \(W^{1/2}X\). Therefore \(H\) is the Euclidean orthogonal projection onto that \(p\)-dimensional subspace. In addition, the weighted residual \(r=y-\hat{y}\) satisfies \(X^\mathsf{T}Wr=0\).
1
Rewrite the matrix.
Set \(X^*=W^{1/2}X\). Then \(X^{*\mathsf{T}}X^*=X^\mathsf{T}WX\), so \(H=X^*(X^{*\mathsf{T}}X^*)^{-1}X^{*\mathsf{T}}\).
2
Check symmetry.
The matrix \(X^{*\mathsf{T}}X^*\) is symmetric positive definite, so its inverse is symmetric. Transposing the expression for \(H\) therefore gives \(H^\mathsf{T}=H\).
3
Check idempotence.
Multiplying \(H\) by itself and cancelling the middle factor gives \(H^2=X^*(X^{*\mathsf{T}}X^*)^{-1}(X^{*\mathsf{T}}X^*)(X^{*\mathsf{T}}X^*)^{-1}X^{*\mathsf{T}}=H\).
4
Identify the projected subspace.
Every column of \(H\) is in the column space of \(X^*\). Also, \(HX^*=X^*\), so \(H\) leaves every vector in that column space unchanged. These facts identify \(H\) as the orthogonal projection onto that space.
5
Verify weighted residual orthogonality.
The weighted least-squares normal equations are \(X^\mathsf{T}W(y-X\hat{\beta})=0\). Since \(r=y-X\hat{\beta}\), this is \(X^\mathsf{T}Wr=0\). Equivalently, \(W^{1/2}r\) is orthogonal to every column of \(W^{1/2}X\). Thus the residual is orthogonal to the fitted subspace in transformed coordinates. \(\square\)

The normal equations also show why \(\hat{y}\) is the weighted projection of \(y\): among fitted vectors \(X\beta\), the chosen one leaves a residual orthogonal to the model space under the weighted inner product \(\langle u,v\rangle_W=u^\mathsf{T}Wv\). The theorem expresses the same fact as an ordinary orthogonal projection after transformation.

Worked Example: Weighted Line and Leverage

Fit a line with an intercept to three observations at \(x=0,1,2\), using weights \(1,2,1\) and responses \(1,2,4\). The design rows are \((1,0),(1,1),(1,2)\). Direct calculation gives

$$ X^\mathsf{T}WX= \begin{pmatrix}4&4\\4&6\end{pmatrix}, \qquad (X^\mathsf{T}WX)^{-1}= \begin{pmatrix}3/4&-1/2\\-1/2&1/2\end{pmatrix}, \qquad X^\mathsf{T}Wy= \begin{pmatrix}9\\12\end{pmatrix}. $$

Multiplying the last two matrices gives \(\hat{\beta}=(3/4,3/2)^\mathsf{T}\). The fitted values are therefore \((3/4,9/4,15/4)^\mathsf{T}\), and the residuals are \((1/4,-1/4,1/4)^\mathsf{T}\). Check weighted orthogonality: the intercept component is \(1(1/4)+2(-1/4)+1(1/4)=0\), and the slope component is \(0+2(1)(-1/4)+2(1/4)=0\).

For a design row \(x_i^\mathsf{T}\), the \(i\)th diagonal entry of \(H\) is \(h_{ii}=w_i x_i^\mathsf{T}(X^\mathsf{T}WX)^{-1}x_i\). At \(x=0\), this is \(1(3/4)=3/4\). At \(x=1\), it is \(2[(1,1)(1/4,0)^\mathsf{T}]=1/2\). At \(x=2\), it is \(1[(1,2)(-1/4,1/2)^\mathsf{T}]=3/4\). The leverages sum to \(3/4+1/2+3/4=2\), the number of fitted coefficients.

Leverage: Bounds, Total, and Interpretation

The diagonal entries \(h_{ii}\) are called leverages. Since \(H\) is an orthogonal projection, each \(h_{ii}\) is the squared length of the projection of the \(i\)th coordinate unit vector onto the fitted subspace. This provides a direct proof of their bounds. It also shows that leverage depends on the design and weights, not on the observed response values.

Result: For every observation, \(0\leq h_{ii}\leq 1\), and \(\sum_{i=1}^n h_{ii}=p\). Moreover, \(h_{ii}=w_i x_i^\mathsf{T}(X^\mathsf{T}WX)^{-1}x_i\), where \(x_i^\mathsf{T}\) is row \(i\) of \(X\).

To prove the bounds, let \(e_i\) be the \(i\)th coordinate unit vector. Symmetry and idempotence give \(h_{ii}=e_i^\mathsf{T}He_i=(He_i)^\mathsf{T}(He_i)=\|He_i\|^2\geq0\). An orthogonal projection cannot increase length, so \(\|He_i\|\leq\|e_i\|=1\), giving \(h_{ii}\leq1\). For the total, the trace of a projection equals its rank: its eigenvalues are 1 on its \(p\)-dimensional range and 0 on the orthogonal complement. Thus \(\sum_i h_{ii}=\operatorname{tr}(H)=p\). Finally, taking the \(i\)th diagonal element of the matrix formula for \(H\) gives the stated row-wise expression.

A higher leverage means that the observation’s response has more opportunity to affect the fitted value at that location, conditional on the design and weights. It does not by itself establish that the observation is an outlier or that it has a large residual. Those are separate properties of the observed response relative to the fitted model.

Worked Example: Leverage in a Weighted Intercept-Only Model

Suppose three observations have weights \(1,1,2\), and the model contains only an intercept. Then \(X=(1,1,1)^\mathsf{T}\), \(X^\mathsf{T}WX=4\), and the weighted mean is \(\hat{\beta}=(y_1+y_2+2y_3)/4\).

The transformed design is \((1,1,\sqrt{2})^\mathsf{T}\). Therefore the diagonal leverages are \(1/4,1/4,2/4\), or \(0.25,0.25,0.50\). They are nonnegative, each is at most 1, and they sum to 1, the rank of the intercept-only design.

For example, if \(y=(2,4,10)^\mathsf{T}\), then \(\hat{\beta}=(2+4+2(10))/4=6.5\). The fitted vector is \((6.5,6.5,6.5)^\mathsf{T}\). The largest leverage belongs to the observation with weight 2, but its observed response of 10 does not enter the leverage calculation; leverage is determined by \(X\) and \(W\).

Common Mistake: Applying the Wrong Hat Matrix

A common error is to call \(S=X(X^\mathsf{T}WX)^{-1}X^\mathsf{T}W\) the symmetric weighted projection matrix. It maps \(y\) to the original-scale fit, but in general \(S^\mathsf{T}\neq S\). The symmetric projection matrix is \(H\), which maps \(W^{1/2}y\) to \(W^{1/2}\hat{y}\). The correction is not to discard \(S\): use \(S\) when mapping the original response to original-scale fitted values, and use \(H\) when invoking ordinary Euclidean projection properties. Their diagonal entries coincide, which is why either gives the same leverage values.

A second tempting shortcut is to interpret a large diagonal entry as proof of an unusual response. The calculation \(h_{ii}=w_i x_i^\mathsf{T}(X^\mathsf{T}WX)^{-1}x_i\) shows why that conclusion is unsupported: no \(y_i\) appears. Leverage describes the observation’s position in the weighted design, not whether its response is surprising.

Key takeaway: Weighted least squares is ordinary least squares on \(W^{1/2}y\) and \(W^{1/2}X\). The resulting matrix \(H\) is a symmetric, idempotent projection; its diagonal entries are bounded leverage values that sum to the number of fitted coefficients.

Check Your Understanding

Answer each question using the weighted projection definitions and results above.

  1. Why must \(X\) have full column rank and all weights be positive for the displayed formula for \(H\) to apply?
  2. Which matrix maps the original response \(y\) to the original-scale fitted response \(\hat{y}\), and which matrix projects the transformed response?
  3. For a weighted least-squares residual \(r\), what equation expresses its orthogonality to the design?
  4. Why do the leverages sum to \(p\), and what does this imply for their average when \(n\) observations are fitted?
  5. Can a high-leverage observation be identified as an outlier from its leverage alone? Explain which quantities determine leverage.
Clinical Biostats