A7 · Lesson 46 · Principal component analysis
Finding the directions where data varies
Ten sensors on a machine produce ten numbers per sample, but they rarely vary independently: a temperature rise moves all of them together, and a vibration moves a few in a fixed pattern. Principal component analysis finds those patterns. It rotates the data onto new axes ordered by how much the data spreads along each, so that the first few axes carry nearly all the variation and the rest are mostly noise. Underneath it's the spectral theorem applied to a covariance matrix, or equivalently, the SVD of the data.
- The ellipse is the one-standard-deviation contour of two correlated readings, with spreads , and correlation .
- The arrows are the principal components: the ellipse's axes, each as long as the standard deviation along it.
- Raise . The ellipse narrows along a diagonal and the first component's share of the variance grows: two correlated readings carry little more than one number's worth of information.
Predict
Two sensors read the same temperature, each with its own independent noise. Along which direction does their data spread the most?
Along . The shared temperature moves both readings together, so the data stretches along the diagonal; the independent noise adds a little spread in every direction. The first principal component is the average of the two sensors, scaled by , and the second is their difference, which contains only noise.
Name it
Covariance
Collect samples of readings as the rows of a matrix, and subtract each column's mean to get the centered data (). The covariance matrix
is , symmetric and positive semidefinite. Its diagonal holds the variances, and measures how readings and vary together.
Principal components
By the spectral theorem, with orthonormal eigenvectors and eigenvalues . Then:
- is the direction of greatest variance, and the variance along it is ;
- each later is the direction of greatest variance perpendicular to the earlier ones;
- the total variance is unchanged by the rotation, so is the fraction of variance component explains.
Through the SVD
If , then , so the principal components are the right singular vectors and . Libraries compute PCA this way, which avoids forming and squaring the condition number.
Using it
The scores of a sample are its coordinates along the components, . Keeping the first scores compresses each sample from numbers to , and reconstructing from them removes the variance in the discarded directions, which is often mostly noise.
Worked example
Find the principal components of the centered data , , , .
Show the solution
- Covariance matrix. has entries , and ; dividing by :
- Eigenvalues. The trace is 4.667 and the determinant 0.444, so
- First component. It's , about 32° above the axis.
- Variance explained. The data is nearly one-dimensional.
Answer the first principal component is , with variance 4.569 (97.9% of the total); the second has variance 0.097.
Worked example
Two sensors read a temperature that varies with standard deviation 2 °C, each with independent noise of 0.5 °C. Find the principal components.
Show the solution
- Covariance matrix. Each reading has variance , and they share the temperature's variance, 4:
- Eigenvalues. They are : 8.25 along and 0.25 along .
- Variance explained.
- Scores. The first score is , proportional to the average, which halves the noise variance; the second score is pure noise.
Answer the first component is , with variance 8.25 (97.1%), and the second is , with variance 0.25, pure noise.
Worked example
A 10-channel recording has covariance eigenvalues 5.2, 3.1, 0.9, 0.3, 0.2, 0.1, 0.08, 0.06, 0.04 and 0.02. How many components keep 95% of the variance?
Show the solution
- Total variance. The eigenvalues add to 10.0.
- Running sums. They are 5.2, 8.3, 9.2 and 9.5, so the first four components reach 95%.
- Storage. Each sample can then be stored as 4 scores instead of 10 readings, plus the four 10-entry components once for the whole recording.
- What's dropped. The other six directions together hold 0.5 of the variance.
Answer four components keep 95% of the variance.
Check
A covariance matrix has eigenvalues 5 and 0.5. What fraction of the variance does the first principal component explain?
Check
Two readings each have variance 2 and covariance 0.5. What is the variance along the first principal component?
Check
Two sensors share a signal of standard deviation 2 and each add independent noise of standard deviation 0.2.
(a)
What is the variance along the first component?
(b)
And along the second?
Check
The centered data matrix of 11 samples has a largest singular value of 10. What is the variance along the first principal component?
Check
The first principal component is . What is the score of the centered sample ?
Check
Find the principal variances of .
Step 1
What is the total variance?
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
What is the larger eigenvalue?
Check
Two sensors record the same signal with different noise. Center the data in the starter, take its SVD, and set lam to the principal variances , frac to the fraction the first explains and pc1 to the first component.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why are the principal components perpendicular, and why does that matter for compression?
They're the eigenvectors of the covariance matrix, which is symmetric, and the spectral theorem says a symmetric matrix has perpendicular eigenvectors. In those coordinates the covariance matrix is diagonal, so the scores along different components are uncorrelated: each component carries information the others don't. That's what makes it safe to drop the last few. Since the rotation keeps lengths, the total variance is split exactly among the components, and dropping the small ones removes exactly their share, no more. Correlated readings, by contrast, can't be dropped one at a time without losing part of what the others share.
Practice
Keep practicing
7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.