S14 · Lesson 40 · Principal component analysis
Finding the directions of greatest variance as eigenvectors of the covariance, the variance each component explains, reconstruction error from discarded eigenvalues, the 2 × 2 case in closed form, whitening, choosing how many components to keep, and compression
Sensor arrays, images and spectra often have hundreds of numbers per sample that move together: neighboring pixels, adjacent wavelengths, strain gauges on the same beam. Principal component analysis finds the few directions along which the data actually vary and describes each sample by its coordinates along them. It compresses, denoises, makes data visible in two dimensions, and decorrelates inputs before other methods see them. Underneath it's an eigenvalue problem on the covariance matrix, the same mathematics as the normal modes of A7 and the singular value decomposition.
- The 2σ contour of a two-dimensional Gaussian with variances a and b and covariance c, with its two principal axes drawn at ±2 standard deviations.
- The long axis is the first principal component: the direction of greatest variance. Its eigenvalue is the variance along it.
- With c = 0 the axes line up with the coordinates; as c grows they rotate toward 45°, and when a = b any c puts them exactly at 45°.
Predict
Two measurements have variances 3 and 2 and covariance 1. What fraction of the total variance does the first principal component capture?
The covariance matrix [[3, 1], [1, 2]] has eigenvalues = 3.618 and 1.382, and the total variance is its trace, 5. The first component captures 3.618/5 = 72.4%, more than the larger single coordinate's 60% because it leans into the correlation, at 31.7° from the first axis.
Name it
The principal components
For centered data with covariance S, the variance along a unit vector u is . It's largest along the eigenvector of S with the largest eigenvalue, and the variance there equals that eigenvalue. The next component is the best direction perpendicular to the first, and so on: the principal components are the eigenvectors of S in order of eigenvalue , and each sample's coordinates are its projections .
Explained variance and reconstruction
The total variance is the trace, . Keeping M components captures the fraction , and reconstructing from them leaves a mean squared error equal to the sum of the discarded eigenvalues. Equivalently, PCA is the best rank-M approximation, which is why it's computed from the singular value decomposition of the centered data matrix.
The 2 × 2 case
For S = [[a, c], [c, b]]:
Whitening and choosing M
Dividing each projection by gives uncorrelated coordinates of unit variance (whitening). M is chosen from a target fraction of explained variance or from the "elbow" where eigenvalues level off into noise. Storing N samples of dimension D with M components takes NM coordinates plus MD for the components and D for the mean.
Worked example
Work through the predict question's covariance: eigenvalues, the first component's direction, and the error of a one-component reconstruction.
Show the solution
- Eigenvalues.
- First component. so = (0.851, 0.526).
- Reconstruction error. A one-component reconstruction leaves a mean squared error of 1.382, the discarded eigenvalue, against 2 or 3 for dropping a raw coordinate.
Answer the eigenvalues are 3.618 and 1.382, the first component is (0.851, 0.526), at 31.7°, and a one-component reconstruction has a mean squared error of 1.382.
Worked example
A data set's eigenvalues are 5, 2, 1, 0.5, 0.3 and 0.2. How much variance do two and three components explain? And how much do 1000 images of 784 pixels shrink with 50 components?
Show the solution
- Explained variance. The total is 9: leaving reconstruction errors of 2.0 and 1.0.
- Compressed images. Raw, they take 784,000 numbers; compressed,
- Ratio.
Answer two components explain 77.8% of the variance and three 88.9%; the images shrink by a factor of 8.7.
Worked example
Data have covariance . Find the whitening scales, and the whitened length of the point (2, 1) measured from the mean.
Show the solution
- Eigenvalues.
- Whitening scales. Whitening rotates onto the principal axes and scales them by , giving unit variance in every direction:
- Whitened length. It's the Mahalanobis distance , with
- For the point (2, 1). One standard deviation, though its plain length is 2.24.
- Why. The point lies along the direction where the data spread most.
Answer the whitening scales are 0.424 and 0.834, and the point (2, 1) has a whitened length of 1.
Check
A covariance matrix has variances 2 and 1 and covariance 1.5. What's the variance along the first principal component?
Check
For variances 3 and 1 with covariance 1.5, at what angle from the first axis does the first principal component point?
Check
A data set's covariance has eigenvalues 4, 2, 1.5 and 0.5. What fraction of the variance do the first 1 components explain?
Check
With eigenvalues 4, 2, 1.5 and 0.5, what mean squared reconstruction error does keeping 1 components leave?
Check
Eigenvalues fall geometrically, with r = 0.8, over a long series. How many components capture a fraction 0.9 of the variance?
Check
500 samples of dimension 100 are stored as 40 principal-component coordinates each, plus the components and the mean. By what factor does storage shrink?
Check
Two features have variances 2 and 1 and covariance 1.2.
(a)
What is the variance along the second principal component?
(b)
What is the covariance's condition number?
Check
A two-feature covariance has variances a and b and covariance c.
Step 1
What is its first eigenvalue?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
Its second?
Step 3
What fraction of the variance does the first component explain?
Explain why
Why are the directions of greatest variance the eigenvectors of the covariance matrix?
The variance of the data projected onto a unit vector u is , a quadratic form in u. Maximizing it subject to with a Lagrange multiplier gives , so every stationary direction is an eigenvector, and the variance along it is its eigenvalue. The largest eigenvalue gives the direction of greatest spread. Because S is symmetric its eigenvectors are orthogonal, so the components form a rotated coordinate system in which the data are uncorrelated, and the variances in the new coordinates, the eigenvalues, add up to the total.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.