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

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?

Name it

The principal components

For centered data with covariance S, the variance along a unit vector u is 𝐮TS𝐮. 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 𝐮i of S in order of eigenvalue λ1≥λ2≥…, and each sample's coordinates are its projections 𝐮iT(𝐱−𝐱¯).

Explained variance and reconstruction

The total variance is the trace, ∑iλi. Keeping M components captures the fraction ∑i≤Mλi/∑iλi, 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]]:

λ±=a+b2±(a−b2)2+c2,tan⁡2θ=2ca−b

Whitening and choosing M

Dividing each projection by λi 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
  1. Eigenvalues. λ=2.5±0.25+1=3.618,1.382
  2. First component. θ=12tan−1⁡(2/1)=31.7∘ so 𝐮1 = (0.851, 0.526).
  3. 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
  1. Explained variance. The total is 9: (5+2)/9=77.8%(5+2+1)/9=88.9% leaving reconstruction errors of 2.0 and 1.0.
  2. Compressed images. Raw, they take 784,000 numbers; compressed, 1000×50+50×784+784=89,984
  3. Ratio. 784,000/89,984=8.7

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 [4223]. Find the whitening scales, and the whitened length of the point (2, 1) measured from the mean.

Show the solution
  1. Eigenvalues. λ=3.5±0.25+4=5.562,1.438
  2. Whitening scales. Whitening rotates onto the principal axes and scales them by 1/λ, giving unit variance in every direction: 1/5.562=0.4241/1.438=0.834
  3. Whitened length. It's the Mahalanobis distance 𝐱TΣ−1𝐱, with Σ−1=18[3−2−24]
  4. For the point (2, 1). 𝐱TΣ−1𝐱=(12−8+4)/8=1 One standard deviation, though its plain length is 2.24.
  5. 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, λk∝rk 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°.

Explain why

Why are the directions of greatest variance the eigenvectors of the covariance matrix?

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.