A746 of 56
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 s1, s2 and correlation r.
  • The arrows are the principal components: the ellipse's axes, each as long as the standard deviation along it.
  • Raise r. 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?

Name it

Covariance

Collect N samples of m readings as the rows of a matrix, and subtract each column's mean to get the centered data X (N×m). The covariance matrix

S=1N−1XTX

is m×m, symmetric and positive semidefinite. Its diagonal holds the variances, and Sij measures how readings i and j vary together.

Principal components

By the spectral theorem, S=QΛQT with orthonormal eigenvectors 𝐪i and eigenvalues λ1≥λ2≥⋯≥0. Then:

  • 𝐪1 is the direction of greatest variance, and the variance along it is λ1;
  • each later 𝐪i is the direction of greatest variance perpendicular to the earlier ones;
  • the total variance trace(S)=∑λi is unchanged by the rotation, so λi∑λj is the fraction of variance component i explains.

Through the SVD

If X=UΣVT, then XTX=VΣ2VT, so the principal components are the right singular vectors 𝐯i and λi=σi2N−1. Libraries compute PCA this way, which avoids forming XTX and squaring the condition number.

Using it

The scores of a sample are its coordinates along the components, 𝐱T𝐪i. Keeping the first k scores compresses each sample from m numbers to k, 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 (−2,−1), (−1,−1), (1,1), (2,1).

Show the solution
  1. Covariance matrix. XTX has entries ∑x2=10, ∑xy=6 and ∑y2=4; dividing by N−1=3: S=[3.333221.333]
  2. Eigenvalues. The trace is 4.667 and the determinant 0.444, so λ=4.667±4.6672−4(0.444)2=4.569 and 0.097
  3. First component. It's (0.851,0.526), about 32° above the x axis.
  4. Variance explained. 4.5694.667=97.9% The data is nearly one-dimensional.

Answer the first principal component is (0.851,0.526), 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
  1. Covariance matrix. Each reading has variance 22+0.52=4.25, and they share the temperature's variance, 4: S=[4.25444.25]
  2. Eigenvalues. They are 4.25±4: 8.25 along 12(1,1) and 0.25 along 12(1,−1).
  3. Variance explained. 8.258.5=97.1%
  4. Scores. The first score is x1+x22, proportional to the average, which halves the noise variance; the second score is pure noise.

Answer the first component is 12(1,1), with variance 8.25 (97.1%), and the second is 12(1,−1), 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
  1. Total variance. The eigenvalues add to 10.0.
  2. Running sums. They are 5.2, 8.3, 9.2 and 9.5, so the first four components reach 95%.
  3. 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.
  4. 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 12(1,1). What is the score of the centered sample (1,2)?

Check

Find the principal variances of S=[accb].

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°.

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 σi2/(N−1), 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?

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.