Skip to content

PCA in High Dimensions

§10.4 asked how to compute the eigenvectors. §10.5 asks a sharper question: is S\mathbf{S} even the right matrix to decompose?

For example, if our xn\mathbf{x}_n are images with 10,00010{,}000 pixels (e.g., 100×100100 \times 100 pixel images), we would need to compute the eigendecomposition of a 10,000×10,00010{,}000 \times 10{,}000 covariance matrix.

That matrix is 0.750.75 GiB and its decomposition is cubic in DD. If you have 5050 images, almost all of it is zeros.

  • Why NDN \ll D makes S\mathbf{S} enormously redundant — and exactly how redundant: measured, a 784×784784\times784 covariance built from 5050 points has 735735 eigenvalues at machine zero.
  • Equation 10.56’s substitution cm:=Xbm\mathbf{c}_m := \mathbf{X}^\top\mathbf{b}_m, turning a D×DD\times D eigenproblem into an N×NN\times N one with the same nonzero eigenvalues — measured agreement 3.4×10133.4\times10^{-13}.
  • Equation 10.57’s recovery of bm\mathbf{b}_m from cm\mathbf{c}_m, and the one-line remark after it that is not optional: measured, skipping the normalisation turns a reconstruction error of 1.160×1011.160\times10^{1} into 6.269×1046.269\times10^{4}.
  • What it actually saves: 47.9×47.9\times at D=500D = 500 rising to 4565×\mathbf{4565\times} at D=4000D = 4000, with the top five eigenvalues agreeing to 5.5×10125.5\times10^{-12}; and 0.750.75 GiB of memory against 19.519.5 KiB.
  • Two claims in §10.5 that centred data contradicts. The book says the rank of S\mathbf{S} is NN, and that 1NXX\frac{1}{N}\mathbf{X}^\top\mathbf{X} “has rank NN and is invertible”. Both are off by one: centring puts 1\mathbf{1} in the null space, so the rank is N1N-1 and the Gram matrix is singular — measured condition number 4.15×10174.15\times10^{17}.
  • Why §10.8 says this idea “can be pushed to the extreme”: the N×NN\times N matrix is built entirely from inner products, which is the door to kernel PCA.

Intuition: fifty points cannot span ten thousand directions

Section titled “Intuition: fifty points cannot span ten thousand directions”

NN points span at most an NN-dimensional subspace — N1N-1 once you have subtracted their mean. Every direction outside it has exactly zero variance in the data, so S\mathbf{S} has a huge null space that was never interesting.

Decomposing S\mathbf{S} spends O(D3)O(D^3) work to discover that DN+1D - N + 1 of its eigenvalues are zero. The Gram matrix 1NXX\frac{1}{N}\mathbf{X}^\top\mathbf{X} carries the same information in N×NN\times N numbers, because it never leaves the subspace the data occupies.

diagram Section 10.5's substitution, and why it is free mermaid

Starting from the eigenvector equation with S=1NXX\mathbf{S} = \frac{1}{N}\mathbf{X}\mathbf{X}^\top:

Sbm=1NXXbm=λmbm(10.55)\mathbf{S}\mathbf{b}_m = \frac{1}{N}\mathbf{X}\mathbf{X}^\top\mathbf{b}_m = \lambda_m\mathbf{b}_m \qquad \text{(10.55)}

Multiply both sides by XRN×D\mathbf{X}^\top \in \mathbb{R}^{N\times D} from the left:

1NXXN×NXbm=:cm=λmXbm    1NXXcm=λmcm(10.56)\underbrace{\frac{1}{N}\mathbf{X}^\top\mathbf{X}}_{N\times N}\underbrace{\mathbf{X}^\top\mathbf{b}_m}_{=:\,\mathbf{c}_m} = \lambda_m\mathbf{X}^\top\mathbf{b}_m \iff \frac{1}{N}\mathbf{X}^\top\mathbf{X}\,\mathbf{c}_m = \lambda_m\mathbf{c}_m \qquad \text{(10.56)}

λm\lambda_m remains eigenvalue, which confirms our results from Section 4.5.3 that the nonzero eigenvalues of XX\mathbf{X}\mathbf{X}^\top equal the nonzero eigenvalues of XX\mathbf{X}^\top\mathbf{X}.”

Measured on N=50N = 50 synthetic images in D=784D = 784 dimensions:

mmfrom the 784×784784\times784from the 50×5050\times50gap
111010.577112721010.577112721010.577112721010.577112720.00.0
22968.72514472968.72514472968.72514472968.725144723.4×10133.4\times10^{-13}
33663.10638499663.10638499663.10638499663.106384990.00.0
44642.21899801642.21899801642.21899801642.218998010.00.0
55505.05599122505.05599122505.05599122505.055991222.3×10132.3\times10^{-13}

Nonzero eigenvalues in the 50×5050\times50 matrix: 49\mathbf{49}. Largest gap across all of them: 3.4×10133.4\times10^{-13}. Largest of eigenvalues 5050 through 784784 of the big matrix: 2.0×10132.0\times10^{-13}.

And to get back:

1NXXSXcm=λmXcm(10.57)\underbrace{\frac{1}{N}\mathbf{X}\mathbf{X}^\top}_{\mathbf{S}}\mathbf{X}\mathbf{c}_m = \lambda_m\mathbf{X}\mathbf{c}_m \qquad \text{(10.57)}

Remark. If we want to apply the PCA algorithm that we discussed in Section 10.6, we need to normalize the eigenvectors Xcm\mathbf{X}\mathbf{c}_m of S\mathbf{S} so that they have norm 11.

That remark is the whole recipe, not a footnote

Section titled “That remark is the whole recipe, not a footnote”

Xcm\mathbf{X}\mathbf{c}_m points the right way and is the wrong length. Measured:

mmXcm\lVert\mathbf{X}\mathbf{c}_m\rVertangle to bm\mathbf{b}_m
1154.37077054.3707701.7×1061.7\times10^{-6} deg
2241.71054641.7105462.4×1062.4\times10^{-6} deg
3336.19110936.1911090.00.0 deg
4432.71104132.7110411.5×1061.5\times10^{-6} deg
5530.08607830.0860780.00.0 deg

(Measured on the 2020 digit-”8” images used in the exercises below. The synthetic 784784-dimensional set gives norms in the hundreds instead, and the same angles.)

Every downstream formula — Equation 10.32’s z=Bx\mathbf{z} = \mathbf{B}^\top\mathbf{x}, Equation 10.34’s BB\mathbf{B}\mathbf{B}^\top, page 1004’s tr(BB)=M\mathrm{tr}(\mathbf{B}\mathbf{B}^\top) = M — assumed BB=I\mathbf{B}^\top\mathbf{B} = \mathbf{I}. Page 1003 already measured what happens when it isn’t. Here, on the digit-”8” images with N=20N = 20 and M=5M = 5:

B\mathbf{B} built fromreconstruction error
Xcm\mathbf{X}\mathbf{c}_m, used as is6.269×104\mathbf{6.269\times10^{4}}
the same, normalised1.160×1011.160\times10^{1}
the direct eigenvectors of S\mathbf{S}1.160×1011.160\times10^{1}
figure Equation 10.57 gives the right directions and the wrong lengths matplotlib
Top row, four eight-by-eight images: a handwritten eight, a saturated black-and-white blob that looks nothing like it, and two identical faithful reconstructions. Bottom, a bar chart of eight column norms between about 18 and 54, with a dashed line near zero marking the value 1. Top row, four eight-by-eight images: a handwritten eight, a saturated black-and-white blob that looks nothing like it, and two identical faithful reconstructions. Bottom, a bar chart of eight column norms between about 18 and 54, with a dashed line near zero marking the value 1.
The second panel is the same subspace and the wrong coordinates, which page 1003 measured as the cost of assuming orthonormality you do not have. The bar chart is why: these columns have norms between 18.52 and 54.37, and every formula from Equation 10.32 onwards assumed 1.

The book states, of the NDN \ll D case:

If there are no duplicate data points, the rank of the covariance matrix S\mathbf{S} is NN, so it has DN+1D - N + 1 many eigenvalues that are 00.

Those two halves are inconsistent: rank NN implies DND - N zeros, not DN+1D - N + 1. And later:

Assuming we have no duplicate data points, this matrix has rank NN and is invertible.

Both are true for uncentred data and false for the centred data §10.5 assumes in its first sentence (“Assume we have a centered dataset”). Centring makes the rows of X\mathbf{X} sum to zero, so X1=0\mathbf{X}\mathbf{1} = \mathbf{0} and 1\mathbf{1} is in the null space of XX\mathbf{X}^\top\mathbf{X}:

X\mathbf{X}rank(X)\mathrm{rank}(\mathbf{X})rank(1NXX)\mathrm{rank}(\frac{1}{N}\mathbf{X}^\top\mathbf{X})smallest eigenvalueκ\kappa
uncentred505050502.4077002.4077004.45×1024.45\times10^{2}
centred, as §10.5 assumes49\mathbf{49}49\mathbf{49}2.5×10142.5\times10^{-14}4.15×1017\mathbf{4.15\times10^{17}}
figure N points, N minus one directions matplotlib
Left, two overlaid eigenvalue curves on a log scale, falling from about a thousand and then dropping off a cliff at index forty-nine, marked by a dotted vertical line. Right, two bars of height 49 and 50 against a dotted line at fifty, labelled with condition numbers 4.15e+17 and 1.59e+04. Left, two overlaid eigenvalue curves on a log scale, falling from about a thousand and then dropping off a cliff at index forty-nine, marked by a dotted vertical line. Right, two bars of height 49 and 50 against a dotted line at fifty, labelled with condition numbers 4.15e+17 and 1.59e+04.
The D-by-D and N-by-N spectra are the same curve until the cliff. The cliff is at 49, not 50 — subtracting the mean uses up one degree of freedom, so the all-ones vector lands in the null space. The book's D minus N plus one figure for the number of zeros is the correct one; its statement that the rank is N, and that the Gram matrix is invertible, is not.

The book’s own example, 100×100100\times100 pixel images:

objectsize in memory, float64
S\mathbf{S}, 10,000×10,00010{,}000\times10{,}0000.745\mathbf{0.745} GiB
the Gram matrix, 50×5050\times5019.519.5 KiB

Measured, at fixed N=50N = 50, forming the matrix and decomposing it:

DDD×DD\times D routeN×NN\times N routespeed-uptop-55 eigenvalues agree to
50050030.5630.56 ms0.640.64 ms47.9×47.9\times6.8×10136.8\times10^{-13}
10001000112.57112.57 ms0.690.69 ms162.2×162.2\times6.8×10136.8\times10^{-13}
20002000631.67631.67 ms1.351.35 ms468.8×468.8\times6.8×10136.8\times10^{-13}
400040006188.996188.99 ms1.361.36 ms4565.2×\mathbf{4565.2\times}5.5×10125.5\times10^{-12}
figure One route grows with D cubed; the other barely moves matplotlib
Left, a log-log plot with a red line climbing steeply from about 30 to 6000 milliseconds alongside a dotted cubic reference, and a blue line staying near one millisecond throughout. Right, a log-scale bar chart in which the red bars grow to about 760 MiB while the blue bars stay flat at a fraction of a MiB. Left, a log-log plot with a red line climbing steeply from about 30 to 6000 milliseconds alongside a dotted cubic reference, and a blue line staying near one millisecond throughout. Right, a log-scale bar chart in which the red bars grow to about 760 MiB while the blue bars stay flat at a fraction of a MiB.
At fixed N the Gram-matrix route's only D-dependent cost is forming X-transpose X and the final X c_m, both linear in D. The red curve tracks the cubic reference. The gap is 47.9 times at D = 500 and 4565 times at D = 4000, for eigenvalues agreeing to 5.5e-12.
pch.quizTag Is the high-dimensional trick clear?
  1. With N = 50 centred points in D = 784 dimensions, how many nonzero eigenvalues does S have?

    pch.quizShowAnswer

    B — 49 — N points give N minus one directions once the mean is subtracted — Measured: 49 nonzero, 735 at machine zero. The book's count of D minus N plus one zeros is right; its statement that the rank is N is off by one for the centred data it assumes two paragraphs earlier. Centring puts the all-ones vector in the null space of X-transpose X.

  2. What does Equation 10.56 achieve?

    pch.quizShowAnswer

    B — It turns the D-by-D eigenproblem into an N-by-N one with exactly the same nonzero eigenvalues — Multiply S b = lambda b by X-transpose from the left, and X-transpose b becomes the eigenvector of the N-by-N Gram matrix with the same eigenvalue. Measured agreement on the top five: 3.4e-13. The eigenvectors come back via Equation 10.57, X c_m — after normalising.

  3. Why is the remark about normalising X c_m not optional?

    pch.quizShowAnswer

    B — Because every formula from Equation 10.32 onwards assumes B-transpose B equals the identity, and these columns have norms in the hundreds — The directions are exact — measured to under two millionths of a degree. The lengths are not: on the digit data the norms run from 18.52 to 54.37. Using them as is gives a reconstruction error of 6.269e+04 against 1.160e+01, which is page 1003's non-orthonormal-basis measurement all over again.

  4. What is the entry (i, j) of the matrix Equation 10.56 decomposes?

    pch.quizShowAnswer

    B — An inner product between data points i and j, divided by N — which is why the kernel trick applies — The dimension D appears nowhere in the matrix, only in the cost of forming each entry. Section 10.8 pushes that to infinite-dimensional features: replace the inner product with a kernel and the whole of this page still works. That is kernel PCA, and Chapter 12 uses the same substitution on the support vector machine.

Exercise 2 – Equation 10.56, and the eigenvalues that survive

Section titled “Exercise 2 – Equation 10.56, and the eigenvalues that survive”

Exercise 3 – The book says invertible; check it

Section titled “Exercise 3 – The book says invertible; check it”

Exercise 4 – Equation 10.57, with and without the remark

Section titled “Exercise 4 – Equation 10.57, with and without the remark”

Exercise 5 – The whole pipeline, both ways

Section titled “Exercise 5 – The whole pipeline, both ways”
  • Section 10.5 is for the case where you have far fewer data points than dimensions, which is the normal situation with images.
  • N centred points span N minus one directions, so the D-by-D covariance matrix has D minus N plus one eigenvalues at machine zero. Measured: 49 nonzero out of 784 for fifty points.
  • Equation 10.56 multiplies the eigenvector equation by X-transpose from the left, turning the D-by-D problem into an N-by-N one with exactly the same nonzero eigenvalues — measured agreement 3.4e-13.
  • Equation 10.57 recovers the eigenvectors as X times c, and the remark after it says to normalise them.
  • That remark is the whole recipe, not a footnote. The columns have norms in the tens or hundreds, and using them unnormalised turns a reconstruction error of 11.6 into 62,687.
  • The book says the Gram matrix has rank N and is invertible. For the centred data it assumes, the rank is N minus one and the condition number is 4.15e+17 — the all-ones vector is in the null space.
  • Nothing in Section 10.5 needs that inverse. Take at most N minus one components and the singularity never bites.
  • The saving is large and grows: 47.9 times faster at D equal to 500, and 4565 times at D equal to 4000, for eigenvalues agreeing to 5.5e-12.
  • And in memory, 0.75 GiB against 19.5 KiB for the book’s own hundred-by-hundred pixel example.
  • Every entry of the N-by-N matrix is an inner product between two data points, and the dimension appears nowhere in the matrix itself.
  • That is the door to kernel PCA, which Section 10.8 names, and to the same substitution Chapter 12 makes in the support vector machine.

Next: Key Steps of PCA in Practice — §10.6 assembles everything into a six-step recipe, including the standardisation step that changes the answer.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading