Skip to content

Eigenvector Computation and Low-Rank Approximations

Pages 1002 and 1004 both ended at the same instruction: take the top MM eigenvectors of S\mathbf{S}. §10.4 is about carrying that out, and it opens two doors:

To get the eigenvalues (and the corresponding eigenvectors) of S\mathbf{S}, we can follow two approaches: we perform an eigendecomposition and compute the eigenvalues and eigenvectors of S\mathbf{S} directly. We use a singular value decomposition.

They give the same answer in exact arithmetic. In floating point they do not, and the difference is measurable.

  • Equations 10.47–10.49: the SVD of X\mathbf{X} and the eigendecomposition of S\mathbf{S}, and λd=σd2/N\lambda_d = \sigma_d^2/N — measured to 1.4×10131.4\times10^{-13}, with the eigenvectors agreeing to 2.1×1062.1\times10^{-6} degrees.
  • Why the SVD route is not merely an alternative. Forming XX\mathbf{X}\mathbf{X}^\top squares the condition number, measured exactly: κ(S)/κ(X)=246.8358=κ(X)\kappa(\mathbf{S})/\kappa(\mathbf{X}) = 246.8358 = \kappa(\mathbf{X}). At κ(X)=1010\kappa(\mathbf{X}) = 10^{10} the eigendecomposition returns 1818 negative eigenvalues of 200200 and a worst relative error of 5.3×1035.3\times10^{3}; the SVD holds 3.2×1073.2\times10^{-7}.
  • Equations 10.50–10.51, the Eckart-Young theorem: XX~M2=σM+1\lVert\mathbf{X}-\tilde{\mathbf{X}}_M\rVert_2 = \sigma_{M+1} exactly, measured to 7×10147\times10^{-14}.
  • §10.4.2’s Abel-Ruffini note, and what it actually rules out: not eigenvalues, only closed-form roots of the characteristic polynomial past degree 44.
  • Equation 10.52, power iteration, with its convergence rate measured against the prediction: λ2/λ1=0.578708\lambda_2/\lambda_1 = 0.578708 predicted, 0.5785410.578541 observed by step 2525.
  • And what happens when the top of the spectrum is nearly tied: 17691769 steps at a ratio of 0.990.99 against 2727 at 0.50.5.
  • When “wasteful to compute the full decomposition” becomes real: tens of times faster at D=2000D = 2000 for the top 55, with eigenvalues agreeing to better than 101010^{-10}.

Intuition: you built a matrix you did not need

Section titled “Intuition: you built a matrix you did not need”

S=1NXX\mathbf{S} = \frac{1}{N}\mathbf{X}\mathbf{X}^\top is a summary of X\mathbf{X}, and forming it is a lossy step. Not lossy in what it represents — the eigenvectors are intact — but in how precisely small quantities survive.

Squaring a number spreads its logarithm. If X\mathbf{X}‘s singular values span 10810^{8}, then S\mathbf{S}‘s eigenvalues span 101610^{16}, which is all the room a float64 has. Anything past that is gone, and what is past it is precisely the small end — the end Equation 10.44 sums to get JMJ_M.

The SVD reaches the same eigenvectors without ever forming the product. It is not a different method; it is the same method that skips a step which only ever loses information.

diagram Two routes to the same B, and what each costs mermaid
S=1Nn=1Nxnxn=1NXX,X=[x1,,xN]RD×N(10.45), (10.46)\mathbf{S} = \frac{1}{N}\sum_{n=1}^{N}\mathbf{x}_n\mathbf{x}_n^\top = \frac{1}{N}\mathbf{X}\mathbf{X}^\top, \qquad \mathbf{X} = [\mathbf{x}_1,\ldots,\mathbf{x}_N]\in\mathbb{R}^{D\times N} \qquad \text{(10.45), (10.46)}

The SVD is X=UΣV\mathbf{X} = \mathbf{U}\boldsymbol\Sigma\mathbf{V}^\top with URD×D\mathbf{U}\in\mathbb{R}^{D\times D} and VRN×N\mathbf{V}^\top\in\mathbb{R}^{N\times N} orthogonal, so

S=1NUΣVV=INΣU=1NUΣΣU(10.48)\mathbf{S} = \frac{1}{N}\mathbf{U}\boldsymbol\Sigma\underbrace{\mathbf{V}^\top\mathbf{V}}_{=\,\mathbf{I}_N}\boldsymbol\Sigma^\top\mathbf{U}^\top = \frac{1}{N}\mathbf{U}\boldsymbol\Sigma\boldsymbol\Sigma^\top\mathbf{U}^\top \qquad \text{(10.48)}

The columns of U\mathbf{U} are the eigenvectors of S\mathbf{S}, and

λd=σd2N(10.49)\lambda_d = \frac{\sigma_d^2}{N} \qquad \text{(10.49)}
ddσd\sigma_dσd2/N\sigma_d^2/Nλd\lambda_dgap
11162.397363162.397363151.56841162151.56841162151.56841162151.568411621.1×10131.1\times10^{-13}
22123.540283123.54028387.7138021487.7138021487.7138021487.713802141.4×10141.4\times10^{-14}
33114.453400114.45340075.2849472275.2849472275.2849472275.284947221.4×10131.4\times10^{-13}
4496.98243596.98243554.0551309054.0551309054.0551309054.055130905.0×10145.0\times10^{-14}
5595.49816195.49816152.4132110952.4132110952.4132110952.413211092.1×10142.1\times10^{-14}

Largest gap over all 6464: 1.4×10131.4\times10^{-13}. Worst angle between ud\mathbf{u}_d and bd\mathbf{b}_d over the top 2020: 2.1×1062.1\times10^{-6} degrees.

figure Equation 10.49 measured, and Eckart-Young measured matplotlib
Left, two curves on a log scale lying exactly on top of each other, descending from about 150 across fifty-two indices and then dropping sharply. Right, three curves of error against M: two coincident ones falling from about 124 to 32, and a third much higher red one falling only from 153 to 115. Left, two curves on a log scale lying exactly on top of each other, descending from about 150 across fifty-two indices and then dropping sharply. Right, three curves of error against M: two coincident ones falling from about 124 to 32, and a third much higher red one falling only from 153 to 115.
Left: the squared singular values divided by N and the eigenvalues of S are one curve. Right: the truncated SVD's spectral-norm error is exactly the next singular value, matching to 7e-14 — while the best of 400 random rank-M subspaces never gets close.

κ(XX)=κ(X)2\kappa(\mathbf{X}\mathbf{X}^\top) = \kappa(\mathbf{X})^2. Measured on the digit-”8” data:

value
κ(X)\kappa(\mathbf{X}), from the nonzero singular values2.468358×1022.468358\times10^{2}
κ(S)\kappa(\mathbf{S}), from the nonzero eigenvalues6.092792×1046.092792\times10^{4}
their ratio246.8358=κ(X)246.8358 = \kappa(\mathbf{X})

At κ=247\kappa = 247 that is harmless. Pushed on a synthetic 200×200200\times200 matrix whose singular values span a chosen range:

κ(X)\kappa(\mathbf{X})κ(XX)\kappa(\mathbf{X}\mathbf{X}^\top)worst relative error, eighworst relative error, SVDnegative eigenvalues from eigh
10210^{2}10410^{4}4.3×10134.3\times10^{-13}8.2×10158.2\times10^{-15}00
10410^{4}10810^{8}9.5×10109.5\times10^{-10}7.6×10137.6\times10^{-13}00
10610^{6}101210^{12}1.1×1051.1\times10^{-5}2.7×10112.7\times10^{-11}00
10810^{8}101610^{16}6.9×1026.9\times10^{-2}2.8×1092.8\times10^{-9}00
101010^{10}102010^{20}5.3×103\mathbf{5.3\times10^{3}}3.2×107\mathbf{3.2\times10^{-7}}18\mathbf{18} of 200200
figure The red line has twice the slope, which is the whole story matplotlib
Left, a log-log plot with a red line rising steeply from 1e-15 to above 1e3 and a blue line rising more gently to about 1e-7, each tracking a dotted reference line; a dashed horizontal line marks 100 percent relative error. Right, a bar chart that is empty until the last two columns, which reach 8 and 18. Left, a log-log plot with a red line rising steeply from 1e-15 to above 1e3 and a blue line rising more gently to about 1e-7, each tracking a dotted reference line; a dashed horizontal line marks 100 percent relative error. Right, a bar chart that is empty until the last two columns, which reach 8 and 18.
The relative error of the eigendecomposition route grows like epsilon times kappa squared; the SVD route like epsilon times kappa. Both reference lines are drawn and both are followed. Past kappa = 1e8 the red line crosses 100 percent relative error, and by 1e10 eighteen of two hundred eigenvalues of a positive semi-definite matrix come back negative.

The best rank-MM approximation in the spectral norm,

X~M:=arg minrk(A)MXA2(10.50)\tilde{\mathbf{X}}_M := \operatorname*{arg\,min}_{\mathrm{rk}(\mathbf{A})\leq M}\lVert\mathbf{X}-\mathbf{A}\rVert_2 \qquad \text{(10.50)}

is obtained “by truncating the SVD at the top-MM singular value”:

X~M=UMΣMVMRD×N(10.51)\tilde{\mathbf{X}}_M = \mathbf{U}_M\boldsymbol\Sigma_M\mathbf{V}_M^\top \in \mathbb{R}^{D\times N} \qquad \text{(10.51)}
MMXX~M2\lVert\mathbf{X}-\tilde{\mathbf{X}}_M\rVert_2σM+1\sigma_{M+1}gapbest of 30003000 random rank-MM
11123.540283123.540283123.540283123.5402837.1×10147.1\times10^{-14}148.274427148.274427
22114.453400114.453400114.453400114.4534007.1×10147.1\times10^{-14}145.287326145.287326
5586.07248186.07248186.07248186.0724815.7×10145.7\times10^{-14}136.205379136.205379
101054.73207954.73207954.73207954.7320793.6×10143.6\times10^{-14}121.669733121.669733

The Frobenius-norm version, which is the one PCA actually optimises:

MMXX~MF\lVert\mathbf{X}-\tilde{\mathbf{X}}_M\rVert_Fj>Mσj2\sqrt{\sum_{j>M}\sigma_j^2}
11320.294771320.294771320.294771320.294771
55236.011581236.011581236.011581236.011581
2020101.622901101.622901101.622901101.622901

Squaring the last column and dividing by NN recovers page 1004’s JMJ_M320.2947712/174=589.590460320.294771^2/174 = 589.590460 — because XX~MF2=j>Mσj2=Nj>Mλj\lVert\mathbf{X}-\tilde{\mathbf{X}}_M\rVert_F^2 = \sum_{j>M}\sigma_j^2 = N\sum_{j>M}\lambda_j. Eckart-Young and Equation 10.44 are the same theorem in two notations.

Two claims here, and they are about different things.

Abel-Ruffini. Eigenvalues are roots of the characteristic polynomial, and for degree 55 or more there is “no algebraic solution” — no formula in radicals. “Therefore, in practice, we solve for eigenvalues or singular values using iterative methods.”

Only MM vectors are wanted. “It would be wasteful to compute the full decomposition, and then discard all eigenvectors with eigenvalues that are beyond the first few.” Measured, full eigh against a Lanczos solve for the top 55:

size of S\mathbf{S}full eigheigsh, k=5k = 5speed-uptop-55 eigenvalues agree to
500×500500\times50055.5355.53 ms2.642.64 ms21.1×21.1\times4.5×10134.5\times10^{-13}
2000×20002000\times20001588.521588.52 ms37.4337.43 ms42.4×\mathbf{42.4\times}4.5×10134.5\times10^{-13}

One run on one machine — the timings move between runs (a repeat gave 31×31\times and 30×30\times), but the shape does not: a full decomposition is cubic in DD, and five vectors were asked for.

At the chapter’s own D=784D = 784 this is already worth having; at the D=10,000D = 10{,}000 of §10.5’s 100×100100\times100 images it is the difference between feasible and not.

Equation 10.52, and how fast it actually is

Section titled “Equation 10.52, and how fast it actually is”
xk+1=SxkSxk,k=0,1,(10.52)\mathbf{x}_{k+1} = \frac{\mathbf{S}\mathbf{x}_k}{\lVert\mathbf{S}\mathbf{x}_k\rVert}, \qquad k = 0, 1, \ldots \qquad \text{(10.52)}

“This sequence of vectors converges to the eigenvector associated with the largest eigenvalue” — and the rate is not stated, but it is λ2/λ1\lambda_2/\lambda_1 per step. On the digit-”8” covariance, where that ratio is 0.5787080.578708:

kkangle to b1\mathbf{b}_1, degreesratio to the previous step
114.93×1014.93\times10^{1}
222.62×1012.62\times10^{1}0.5321160.532116
553.40×1003.40\times10^{0}0.5164790.516479
10101.60×1011.60\times10^{-1}0.5559460.555946
15159.40×1039.40\times10^{-3}0.5724670.572467
20205.95×1045.95\times10^{-4}0.5772690.577269
25253.84×1053.84\times10^{-5}0.578541\mathbf{0.578541}

The observed ratio climbs to the predicted 0.5787080.578708 as the contributions from λ3\lambda_3 onwards die out faster and only the λ2\lambda_2 component is left. The angle reaches exactly 00 at step 4040.

figure The rate is the eigenvalue gap, and nothing else matplotlib
Left, a log-scale plot of angle against iteration falling in a straight line from about 50 degrees to below 1e-6 by step 35, with a dashed reference line of the same slope; a dotted vertical line marks step 40. Right, a log-scale bar chart of iteration counts: 1769, 176, 63, 27 and 24. Left, a log-scale plot of angle against iteration falling in a straight line from about 50 degrees to below 1e-6 by step 35, with a dashed reference line of the same slope; a dotted vertical line marks step 40. Right, a log-scale bar chart of iteration counts: 1769, 176, 63, 27 and 24.
A straight line on a log scale is geometric convergence. The dashed line is the predicted ratio 0.578708 raised to the k, drawn with no fitting. The right panel is the same fact as a cost: when the top two eigenvalues are nearly tied, the iteration has almost nothing to separate them with.
λ2/λ1\lambda_2/\lambda_1steps to reach 10610^{-6} degrees
0.990.991769\mathbf{1769}
0.900.90176176
0.700.706363
0.500.502727
0.300.302424

“The original Google PageRank algorithm uses such an algorithm for ranking web pages based on their hyperlinks.” The same table explains why PageRank’s damping factor exists: it is chosen partly to keep the second eigenvalue away from the first.

pch.quizTag Is the computation clear?
  1. Why is the SVD of X preferable to the eigendecomposition of S when the small eigenvalues matter?

    pch.quizShowAnswer

    B — Forming X X-transpose squares the condition number, so the relative error on small eigenvalues grows like kappa squared instead of kappa — Measured on a 200-by-200 matrix: at condition number 1e8 the eigendecomposition route already has 6.9 percent worst relative error, and at 1e10 it has 5.3e+03 and returns 18 negative eigenvalues — an impossible answer for a covariance matrix. The SVD route holds 3.2e-07. J_M sums exactly the small end.

  2. What does the Eckart-Young theorem say the spectral-norm error of the truncated SVD is?

    pch.quizShowAnswer

    B — Exactly sigma_(M+1), the largest singular value you dropped — Measured to 7e-14 at M = 1, 2, 5 and 10. In the Frobenius norm the error is the square root of the sum of the squared discarded singular values, which divided by N is exactly page 1004's J_M — 320.294771 squared over 174 is 589.590460. Eckart-Young and Equation 10.44 are one theorem in two notations.

  3. What does the Abel-Ruffini theorem actually rule out?

    pch.quizShowAnswer

    B — A closed-form expression in radicals for the roots of a general polynomial of degree five or more — nothing about computing them numerically — It explains why eigenvalue algorithms are iterative rather than a formula. It says nothing about accuracy or difficulty — np.linalg.eigh handles a 64-by-64 matrix in under a millisecond. And nobody computes eigenvalues by rooting the characteristic polynomial anyway; that route is catastrophically ill-conditioned.

  4. Power iteration converges at what rate?

    pch.quizShowAnswer

    B — Geometrically, at the ratio of the second eigenvalue to the first — On the digit-8 covariance the ratio is 0.578708 and the measured step-to-step ratio reaches 0.578541 by iteration 25, with no fitting. The cost of a near-tie is steep: 1,769 steps at a ratio of 0.99 against 27 at 0.5. That is also why PageRank's damping factor is chosen as it is.

Exercise 1 – One decomposition, two names

Section titled “Exercise 1 – One decomposition, two names”

Exercise 2 – What squaring the condition number costs

Section titled “Exercise 2 – What squaring the condition number costs”

Exercise 3 – Eckart-Young, and its Frobenius twin

Section titled “Exercise 3 – Eckart-Young, and its Frobenius twin”

Exercise 4 – Power iteration, and the rate it converges at

Section titled “Exercise 4 – Power iteration, and the rate it converges at”

Exercise 5 – The cost of computing what you throw away

Section titled “Exercise 5 – The cost of computing what you throw away”
  • Section 10.4 offers two routes to the same basis: the eigendecomposition of S, or the singular value decomposition of X.
  • Equation 10.49 connects them: each eigenvalue is the corresponding squared singular value divided by N, measured to 1.4e-13, with eigenvectors agreeing to two millionths of a degree.
  • Forming X X-transpose squares the condition number — measured exactly on the digit data, where the ratio of the two condition numbers is itself the condition number.
  • That costs relative accuracy at the bottom of the spectrum, which is exactly what Equation 10.44 sums. At condition number 1e8 the eigendecomposition route has 6.9 percent worst relative error; at 1e10 it has 5.3e+03.
  • And it returns 18 negative eigenvalues out of 200 for a positive semi-definite matrix, an impossible answer that flows straight into any variance-explained figure.
  • The large eigenvalues are fine either way, so if you only want the top few components, either route is safe.
  • Eckart-Young says the truncated SVD is the best rank-M approximation, and its spectral-norm error is exactly the next singular value — measured to 7e-14.
  • Its Frobenius error squared, divided by N, is page 1004’s reconstruction error. Eckart-Young and Equation 10.44 are one theorem in two notations.
  • Abel-Ruffini rules out a closed-form formula in radicals past degree four, and nothing else. Eigenvalues are still computed to full precision, iteratively, and never by rooting the characteristic polynomial.
  • Power iteration converges geometrically at the ratio of the second eigenvalue to the first — predicted 0.578708, measured 0.578541 by step 25, with nothing fitted.
  • A near-tie at the top is what makes it slow: 1,769 steps at a ratio of 0.99 against 27 at 0.5. That is also why PageRank has a damping factor.
  • Asking for only the top five eigenpairs is tens of times faster at D equal to 2000, and agrees to better than 1e-10.

Next: PCA in High Dimensions — §10.5, where NDN \ll D and the D×DD\times D covariance matrix is the wrong object to decompose entirely.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading