Quiz: principal component analysis

Short questions to test your understanding. Try each on paper first, then open the answer.


Q1. Why do we subtract the mean image before looking for the direction of largest spread?

We want the directions in which the images differ from each other. Without centring, the best direction through (0, 0) mostly points at the mean image: it describes how bright the images are on average, which one number (the mean) already tells us.


Q2. Two-pixel images from neighbouring pixels of a smooth image: where do PC1 and PC2 point, and what do their scores measure?

PC1 \approx (1, 1)/\sqrt2: both pixels together, the score measures the brightness of the pair. PC2 \approx (1, -1)/\sqrt2: one up, the other down, the score measures the difference, a small edge. Neighbouring pixels are similar, so PC1 carries most of the variance.


Q3. A covariance matrix is S = \begin{pmatrix} 3 & 1 \\ 1 & 3 \end{pmatrix}. Give the principal components, their variances, and the share of PC1.

For \begin{pmatrix} a & b \\ b & a \end{pmatrix} the eigenvectors are (1, \pm 1)/\sqrt2.

PC1 = (1, 1)/\sqrt2 with variance 3 + 1 = 4; PC2 = (1, -1)/\sqrt2 with variance 3 - 1 = 2. Share of PC1: 4/6 = 67\,\%. The total, 6, is also the sum of the diagonal.


Q4. Mean image (0.6, 0.6), PC1 = (1, 1)/\sqrt2. Encode the image (0.9, 0.5) with one score, rebuild it, and give the squared error.

Centred: (0.3, -0.1). Score (0.3 - 0.1)/\sqrt2 = 0.141. Rebuilt: (0.6, 0.6) + 0.141\cdot(1, 1)/\sqrt2 = (0.7, 0.7). Error (0.2, -0.2), squared length 0.08: the difference between the two pixels is lost.


Q5. The eigenvalues of a PCA are 6, 2, 1, 0.5, 0.5. How many components do you need for 90 % of the variance? What is the mean squared reconstruction error with that many?

Cumulative shares: 60, 80, 90, 95, 100 %. Three components give exactly 90 %. The error is the sum of the dropped eigenvalues: 0.5 + 0.5 = 1.


Q6. Both the Fourier transform and PCA rewrite an image patch in a new orthonormal basis. What is the main difference?

The Fourier basis (waves) is fixed: the same for every image. The PCA basis is learned from a set of images: the eigenvectors of their covariance, ordered by how much those images vary along them. For a given number of kept coefficients PCA has the smallest squared error on its own data; Fourier is general and needs no fitting. On ordinary image patches the two look alike.


Q7. You run PCA on cell measurements: diameter in nm and circularity (between 0 and 1). PC1 explains 99.99 % of the variance. What has gone wrong?

Probably nothing about the cells: the diameter in nm has a numerically huge variance compared with a number between 0 and 1, so PC1 is just “diameter”. PCA depends on the units. Standardise each feature (divide by its standard deviation) when the features have different units.


Q8. (math) Why is the variance of the scores along a unit vector \mathbf u equal to \mathbf u^\top S\mathbf u, and why is it largest at an eigenvector of S?

The scores are \mathbf u^\top\mathbf y_i (centred, mean 0), so their variance is \frac1n\sum(\mathbf u^\top\mathbf y_i)^2 = \mathbf u^\top\big(\frac1n\sum\mathbf y_i\mathbf y_i^\top\big)\mathbf u = \mathbf u^\top S\mathbf u. Writing \mathbf u = \sum c_j\mathbf u_j in the orthonormal eigenbasis, \mathbf u^\top S\mathbf u = \sum\lambda_j c_j^2 with \sum c_j^2 = 1: a weighted average of the eigenvalues, largest when all the weight is on \lambda_1, that is \mathbf u = \mathbf u_1.