The previous chapter ended with a precise goal: on centred data, find the unit vector whose line loses the least when every point is replaced by its projection. This chapter solves it, then follows the "repeat on the residuals" procedure to its end. The result is principal component analysis (PCA), one of the most widely used algorithms in data science.
From least error to a matrix problem
For a unit vector , the squared error of point is the squared length of its residual. Averaging over the data set,
Expand one term, using :
The first term does not depend on , so minimising is the same as maximising the other term:
Now rewrite as . Since does not depend on , it moves outside the sum:
The matrix
is the covariance matrix of the centred data. Entry is the average of feature times feature : the variances of the features on the diagonal and their covariances off it. So the best line is the solution of
A standard result of linear algebra (the Rayleigh quotient, a case of the Courant–Fischer min-max theorem) answers this immediately: the maximum is the largest eigenvalue of , reached when is the corresponding eigenvector. To see why, note that is symmetric, so it has an orthonormal basis of eigenvectors with eigenvalues . Write with ; then , a weighted average of the eigenvalues that is largest when all the weight sits on .
The same line maximises variance
The quantity we maximise has a plain meaning. Project every centred point onto and collect the numbers . Their average is , because the data is centred. Their variance is therefore just the average square:
So on centred data,
minimising reconstruction error = maximising the variance of the projections.
The two always add up to the same total, the average squared length of the points, , which is fixed by the data. Whatever variance the line keeps, it does not lose as error, and vice versa.
Why should we want the projections spread out? Because a compressed representation is only useful if it still tells points apart. Project onto a direction across a long thin cloud and every point lands near the origin: the coefficients crowd together and the points become indistinguishable. Project along the cloud and the coefficients stay spread out, preserving the differences between points. Variance is our measure of how much distinguishing information survives.
Repeating on the residuals
Take the residuals after the first line: . Every residual is perpendicular to (we checked this in the last chapter), so all of them live in the subspace orthogonal to . The best line for the residuals therefore also lies in that subspace: any component along would only add error. Hence .
The same argument repeats. After rounds we have unit vectors that are mutually perpendicular, an orthonormal set. Working through the residuals shows that after round each point has had all its components along removed:
In dimensions there is room for only perpendicular directions, so after rounds every residual is zero, and each point is rebuilt exactly as
That is just a change of basis: from the standard axes to the new axes . Linear algebra identifies these axes for us. The direction found in round is the eigenvector of with the -th largest eigenvalue; the eigenvectors of a symmetric matrix are exactly an orthonormal basis. The are called the principal components.
Where the compression happens
A change of basis on its own saves nothing. The saving comes from stopping early. If the residuals become zero after rounds, the data lies exactly in a -dimensional subspace, and each point needs only coefficients,
plus the shared directions. With points in dimensions that lie in a 3-dimensional subspace, we store numbers instead of , and reconstruct everything exactly.
Real data never lies exactly in a subspace, so we also need to know when the remaining directions carry so little that we can drop them. The eigenvalues answer that. Since and ,
the variance captured along the -th direction. Each eigenvalue is an average of squares, so it is never negative (the covariance matrix is positive semi-definite), and they decrease in order. A common rule of thumb keeps the smallest for which
that is, the top directions explain 95% of the variance. Plotting the eigenvalues in order (a scree plot) often shows an elbow: a few large values (signal) followed by a long tail of small ones (mostly noise).
The algorithm
- Centre the data: subtract the mean from every point.
- Form the covariance matrix .
- Compute its eigenvalues and eigenvectors, sorted from largest eigenvalue down.
- Choose , for example the smallest explaining 95% of the variance.
- Represent each point by its coefficients . To reconstruct, compute .
A worked picture: height and weight
Centre a set of heights and weights (in standard units) and suppose they rise together along the diagonal. The first principal component is then close to , so each person's first coefficient is : a measure of overall size. The second is perpendicular, , giving : whether someone is heavy for their height.
In the original axes, knowing a person's height tells you a lot about their weight. In the new axes, knowing someone's overall size tells you nothing about whether they are heavy for their height. The projections onto principal components are uncorrelated: in the new basis the covariance matrix is diagonal, with the eigenvalues on the diagonal. PCA has found new features, combinations of the old ones, that each carry information the others do not.