Guides / Linear algebra
Principal component analysis: center the data, take the eigenvectors of the covariance, and keep the top ones
Short answer
Center: subtract the mean x̄ from every point. Covariance: S = Σ(xi − x̄)(xi − x̄)T/(N − 1), a symmetric matrix. Eigen-decompose: Sqj = λjqj with unit eigenvectors, sorted so λ1 ≥ λ2 ≥ …. The principal directions are the qj, and λj is the variance along qj. Score of a point: zj = qjT(x − x̄). Reconstruction from k components: x̂ = x̄ + z1q1 + … + zkqk. Explained variance of the first k: (λ1 + … + λk)/(λ1 + … + λd), and the mean squared reconstruction error is the sum of the discarded eigenvalues. From an SVD of the centered data, the variances are σj2/(N − 1).
Why it works
For a unit vector u, the numbers uT(xi − x̄) are the positions of the points along the line through x̄ in the direction u, and their variance is uTSu. Writing u in the orthonormal eigenvector basis of S turns this into a weighted average of the eigenvalues, λ1c12 + λ2c22 + … with c12 + c22 + … = 1. A weighted average is largest when all the weight sits on the biggest value, so the direction of greatest spread is q1, and the spread there is λ1. Among directions perpendicular to q1, the best is q2, and so on.
The same picture explains the error. Each centered point splits into its components along the eigenvectors, and those pieces are perpendicular, so squared lengths add (Pythagoras). Keeping the first k components and dropping the rest leaves an error made of exactly the dropped pieces, whose average squared size is the sum of the dropped eigenvalues. The total variance, the trace of S, is the sum of all the eigenvalues, which is why explained-variance ratios add to 1. Dividing by N instead of N − 1 scales every eigenvalue by the same factor and changes none of the directions or ratios.
Example 1: center the data and build the covariance matrix
Four points are (0, 0), (3, 3), (6, 3) and (7, 2). Find the mean and the sample covariance matrix.
- x̄ = (16/4, 8/4) = (4, 2)Average each coordinate.
- (−4, −2), (−1, 1)Subtract x̄ from every point: the first two …
- (2, 1), (3, 0)… and the last two. Each column of deviations adds to 0, which checks the mean.
- Σ dx2 = 30The first of the three sums a 2 × 2 covariance matrix needs: 16 + 1 + 4 + 9.
- Σ dy2 = 64 + 1 + 1 + 0.
- Σ dx · dy = 98 − 1 + 2 + 0.
- S = 10332Divide each sum by N − 1 = 3. The variances 10 and 2 sit on the diagonal, the covariance 3 off it, and S is symmetric.
Example 2: principal directions and explained variance
Find the principal directions of the data in Example 1, the variance along each, and the fraction of the total variance the first component explains.
- λ2 − 12λ + 11 = 0det(S − λI) = (10 − λ)(2 − λ) − 9, with trace 12 and determinant 20 − 9 = 11.
- λ1 = 11, λ2 = 1The roots add to the trace and multiply to the determinant.
- λ = 11: −x + 3y = 0The first row of S − 11I is (−1, 3).
- q1 = (3, 1)/√10Normalize, since scores need unit vectors.
- q2 = (1, −3)/√10A symmetric matrix has perpendicular eigenvectors, so the second direction is q1 turned by 90°. Check: S(1, −3) = (1, −3).
- 11/(11 + 1) = 11/12 ≈ 91.7%The first component explains most of the spread. Its direction makes an angle of about 18.4° with the x-axis, and the variance along it, 11, beats the 10 along the x-axis itself.
Example 3: scores, reconstruction and the error
Using one component, find the scores of the four points of Example 1, reconstruct (0, 0), and find the mean squared reconstruction error.
- z = (3 dx + dy)/√10Dot each centered point with q1.
- = (−14, −2, 7, 9)/√10The scores add to 0, as the centered data do.
- (196 + 4 + 49 + 81)/10 = 33The sum of the squared scores.
- 33/3 = 11Divide by N − 1. The variance of the scores is λ1, a check on Example 2.
- x̂ = (4, 2) + (−14/√10)q1Start at the mean and walk along q1 by the score.
- = (4, 2) − 1.4(3, 1)q1 = (3, 1)/√10, and the two √10's multiply to 10.
- x̂ = (−0.2, 0.6)The reconstruction is the foot of the perpendicular from (0, 0) to the line through the mean along q1.
- error (0.2, −0.6)The point (0, 0) minus x̂.
- squared length 0.40.04 + 0.36.
- (0.4 + 1.6 + 0.1 + 0.9)/3The squared errors of all four points, divided by N − 1 like the variances …
- = 1 = λ2… add up to the discarded eigenvalue, exactly as the short answer says.
Example 4: how many components, from an SVD
The centered data matrix of N = 11 points in four dimensions has singular values 20, 10, 5 and 2. How many components are needed to explain at least 95% of the variance?
- λj = σj2/(N − 1)If Xc = UΣVT, then XcTXc = VΣ2VT, so the right singular vectors are the principal directions and the squared singular values, over N − 1, are the variances.
- λ = 40, 10, 2.5, 0.4The four variances.
- total = 52.9The sum of the variances, the trace of S.
- 40/52.9 ≈ 75.6%Cumulative explained variance after one component …
- 50/52.9 ≈ 94.5%… after two …
- 52.5/52.9 ≈ 99.2%… and after three.
- k = 3Two components fall just short of 95%; three clear it. The N − 1 cancels from every ratio, so 400/529, 500/529 and 525/529 give the same percentages.
Common mistakes, and the exact line they happen on
1. Skipping the centering
The data of Example 1, with the mean left in.
- XTX = 94414122The sums of x2, xy and y2 over the raw points (0, 0), (3, 3), (6, 3), (7, 2).
- q1 ≈ (0.911, 0.412)This is the mistake. This top eigenvector points at about 24.4°. Without centering, the matrix measures spread around the origin, so the top direction is pulled toward the mean (4, 2), which sits at 26.6°. The true principal direction is at 18.4°, and the variance of the centered data along this wrong direction is about 10.89, less than λ1 = 11. The uncentered “explained variance” of about 97% is just as misleading. Subtract x̄ first.
2. Reconstructing without adding the mean back
The reconstruction of (0, 0) in Example 3.
- z1 = −14/√10Right so far: the score was computed from the centered point.
- x̂ = z1q1 = (−4.2, −1.4)This is the mistake. z1q1 is the reconstruction of the centered point. Every reconstruction made this way is off by exactly the mean (4, 2). Add x̄ back: x̂ = (−0.2, 0.6).
3. Explained variance from unsquared singular values
The singular values 20, 10, 5, 2 of Example 4.
- σ = 20, 10, 5, 2Right so far: these are what an SVD routine returns.
- 20/37 ≈ 54.1%This is the mistake. Singular values are standard deviations (times √(N − 1)), not variances. Read this way, three components fall short of 95% and all four seem needed. Square first: 400, 100, 25 and 4 give 75.6%, 94.5% and 99.2%, so three components are enough.
- 30/37 ≈ 81.1%The same misreading after two components …
- 35/37 ≈ 94.6%… and after three, just short of 95%.
Practice
A covariance matrix is diagonal with entries 6 and 2. What are the principal directions, and what fraction of the variance does the first component explain?
Show answer
The coordinate axes, (1, 0) and then (0, 1), because a diagonal matrix's eigenvectors are the axes. The first explains 6/8 = 75%.
Find the eigenvalues and the first principal direction of S = 3113.
Show answer
λ = 4 and 2; q1 = (1, 1)/√2. The variance along the diagonal, 4, is more than the 3 along either axis.
The mean is (1, 2) and the first principal direction is (0.6, 0.8). Find the score of the point (4, 5), its one-component reconstruction, and the reconstruction error.
Show answer
Centered point (3, 3), score 0.6 · 3 + 0.8 · 3 = 4.2, reconstruction (1, 2) + 4.2(0.6, 0.8) = (3.52, 5.36), error vector (0.48, −0.36) of length 0.6.
A covariance matrix has eigenvalues 9, 4, 1 and 1. Keeping two components, what fraction of the variance is kept, and what is the mean squared reconstruction error?
Show answer
13/15 ≈ 86.7% kept; the error is the discarded 1 + 1 = 2.
The centered data matrix of N = 5 points has singular values 6 and 2. Find the variances along the principal directions and the fraction explained by the first.
Show answer
36/4 = 9 and 4/4 = 1, so the first explains 9/10 = 90%. Using 6/8 = 75% forgets to square.