3.2 Projection and Orthogonality
: 30 minutes
This is the chapter the rest of the course leans on hardest. When we fit a regression on Nov 3, the fitted values will turn out to be a projection, and the reason regularization behaves the way it does will turn out to be a statement about the geometry you meet here. It is worth slowing down.
Orthogonality
Two vectors are orthogonal when their dot product is zero. In two or three dimensions this is exactly perpendicularity; in higher dimensions we take it as the definition.
\mathbf{u} \perp \mathbf{v} \quad\Longleftrightarrow\quad \mathbf{u}^\top\mathbf{v} = 0
Projecting onto a line
Given a vector \mathbf{b} and a direction \mathbf{a}, which multiple of \mathbf{a} lies closest to \mathbf{b}?
Write the candidate as \hat{x}\mathbf{a}. The error \mathbf{b} - \hat{x}\mathbf{a} is smallest exactly when it is orthogonal to \mathbf{a}:
\mathbf{a}^\top(\mathbf{b} - \hat{x}\mathbf{a}) = 0 \quad\Longrightarrow\quad \hat{x} = \frac{\mathbf{a}^\top\mathbf{b}}{\mathbf{a}^\top\mathbf{a}}
so the projection of \mathbf{b} onto the line through \mathbf{a} is
\mathbf{p} = \frac{\mathbf{a}^\top\mathbf{b}}{\mathbf{a}^\top\mathbf{a}}\,\mathbf{a}.
The residual \mathbf{b} - \mathbf{p} should be orthogonal to \mathbf{a}. Check it rather than trust it:
Notice what \mathbf{p} came out to be here: \mathbf{a} was the vector of all ones, and the projection is the mean of \mathbf{b} repeated. Averaging is a projection. That is not a coincidence, and it is why an intercept-only regression predicts the mean.
Projecting onto a subspace
Now let A be an m \times n matrix whose columns span some subspace of \mathbb{R}^m—its column space, written \mathcal{C}(A). Given \mathbf{b} \in \mathbb{R}^m, which vector in \mathcal{C}(A) is closest to \mathbf{b}?
Every element of the column space is A\mathbf{x} for some \mathbf{x}. The same argument applies: the residual must be orthogonal to every column of A, which is to say
A^\top(\mathbf{b} - A\hat{\mathbf{x}}) = \mathbf{0} \quad\Longrightarrow\quad A^\top A\,\hat{\mathbf{x}} = A^\top\mathbf{b}.
These are the normal equations. When A^\top A is invertible,
\hat{\mathbf{x}} = (A^\top A)^{-1}A^\top\mathbf{b}, \qquad \mathbf{p} = A\hat{\mathbf{x}} = \underbrace{A(A^\top A)^{-1}A^\top}_{P}\,\mathbf{b}.
P is the projection matrix onto \mathcal{C}(A).
A projection matrix has two properties worth verifying, because together they are what it means to be a projection: it is symmetric, and applying it twice does nothing new.
Least squares is a projection
The matrix A above was not arbitrary. Its first column is all ones and its second counts 0,1,2,3—it is the design matrix for fitting a straight line to four points. Solving the normal equations is fitting that line.
So “fit a line by least squares” and “project the data onto the column space of the design matrix” are the same sentence in two languages. The fitted values are the projection; the residuals are what is left over, and they are orthogonal to every predictor:
That last check is worth remembering. Residuals orthogonal to the predictors is not a diagnostic you should hope for; it is a consequence of how the coefficients were chosen. If a residual plot shows structure against a predictor already in the model, something is wrong with your code, not with your data.
On Nov 3 we look at linear regression as prediction. Two things carry over directly:
- The fitted values \hat{\mathbf{y}} = P\mathbf{y} are a projection, and P is often called the hat matrix because it puts the hat on \mathbf{y}.
- When the columns of A are nearly parallel, A^\top A is close to singular, (A^\top A)^{-1} is enormous, and the coefficients become wild while the fit stays fine. That is multicollinearity, and it is what ridge regression exists to fix. The SVD is what makes the fix legible.
Orthogonal columns make everything easier
If the columns of A are mutually orthogonal, A^\top A is diagonal and the normal equations decouple: each coefficient can be computed independently of the others.
This is the entire motivation for the QR decomposition in 3.1: rewrite A = QR with Q having orthonormal columns, and the projection collapses to P = QQ^\top, with no inverse to compute.
Same projection, computed without ever forming A^\top A—which matters, because forming A^\top A squares the conditioning of the problem.