PCA: Maximum Variance and Minimum Reconstruction Error

7 minute read

Published:

TL;DR: Principal component analysis finds an orthogonal set of directions ordered by how much of the data's variance each one carries. It answers two apparently different questions โ€” "where is the data most spread out?" and "which subspace can I project onto while losing the least?" โ€” with the same eigenvectors. Compute it with an SVD, never by forming \(X^\top X\), and remember that it is a linear method: no amount of it will unroll a spiral.

The same answer to two questions

Question one: maximum variance. Find the unit vector \(u\) along which the projected data \(Xu\) has the largest variance. That variance is \(u^\top \Sigma u\) where \(\Sigma\) is the covariance matrix, so you want

\[ \max_{\lVert u \rVert = 1} \ u^\top \Sigma u , \]

whose solution is the top eigenvector of \(\Sigma\), with the maximum value equal to the top eigenvalue.

Question two: minimum reconstruction error. Find the \(k\)-dimensional subspace such that projecting the data onto it loses as little as possible in squared distance.

These sound like different problems. They are the same problem, and the reason is one line of Pythagoras. For a centred point \(x_i\) and an orthogonal projection \(P\),

\[ \underbrace{\lVert x_i \rVert^2}_{\text{fixed}} \;=\; \underbrace{\lVert P x_i \rVert^2}_{\text{variance kept}} \;+\; \underbrace{\lVert x_i - P x_i \rVert^2}_{\text{error}} . \]

The left side does not depend on \(P\). So maximising the first term on the right and minimising the second are the same optimisation, and they must select the same subspace.

Why this matters practically: it tells you what PCA is optimising when someone asks. It is not "finding meaningful structure" or "removing noise" โ€” it is minimising squared reconstruction error over linear subspaces. Everything PCA is good at, and everything it is bad at, follows from that being the objective.

The mechanics, on numbers you can check

Take five points, already centred:

\[ X = \begin{pmatrix} -2 & -1 \\ -1 & -1 \\ 0 & 0 \\ 1 & 1 \\ 2 & 1 \end{pmatrix} . \]

The covariance is \(\Sigma = \tfrac{1}{n-1}X^\top X\) with \(n = 5\):

\[ \Sigma = \tfrac{1}{4}\begin{pmatrix} 10 & 6 \\ 6 & 4 \end{pmatrix} = \begin{pmatrix} 2.5 & 1.5 \\ 1.5 & 1.0 \end{pmatrix} . \]

For a \(2 \times 2\) matrix the eigenvalues follow from the trace and determinant: \(\operatorname{tr}\Sigma = 3.5\) and \(\det\Sigma = 2.5 - 2.25 = 0.25\), so

\[ \lambda = \frac{7 \pm \sqrt{45}}{4} \qquad\Longrightarrow\qquad \lambda_1 = 3.4271, \quad \lambda_2 = 0.0729 . \]

They sum to \(3.5\), matching the trace, and multiply to \(0.25\), matching the determinant. The first component carries \(3.4271/3.5 = 97.9\%\) of the variance, and its eigenvector is \(u_1 \approx (0.851,\ 0.526)\).

Five points with their two principal axes drawn to scaleThe first principal axis runs along the direction of greatest spread and is much longer than the second, whose length reflects that it carries only two per cent of the variance. Axis lengths are proportional to โˆšฮป (โˆ’2,โˆ’1) (โˆ’1,โˆ’1) (1,1) (2,1) PC1 โ€” 97.9% PC2 โ€” 2.1% The second axis is genuinely that short; the ratio is โˆš0.0729 to โˆš3.4271.
The two principal axes of the worked example, drawn with lengths proportional to the square roots of their eigenvalues. PC2 is short because it is short โ€” the drawing is not compressed for effect.

Now check the equivalence claim numerically. Projecting onto \(u_1\) alone leaves a total squared reconstruction error of \(0.2918\). And \((n-1)\lambda_2 = 4 \times 0.0729 = 0.2918\). The variance you discard is the error you incur โ€” exactly, not approximately.

Use the SVD, not the covariance matrix

Everything above is easier and better conditioned through the singular value decomposition \(X = U S V^\top\). The right singular vectors are the principal directions, and the eigenvalues follow from the singular values:

\[ \lambda_i = \frac{s_i^2}{n-1} . \]

On the example, \(s = (3.7025,\ 0.5402)\), and \(s^2/4 = (3.4271,\ 0.0729)\) โ€” the eigenvalues again.

Forming \(X^\top X\) squares the conditioning. The condition number of \(X^\top X\) is the square of that of \(X\), so building the covariance matrix explicitly throws away roughly half your available precision before you start. The linear regression chapter makes the same argument about the normal equations, and the fix is the same: factorise \(X\) directly.

Two preprocessing steps that are not optional

Centring. PCA on uncentred data does not find the direction of greatest spread โ€” it finds the direction of the mean. Shift the example by \((10,10)\) and the leading direction becomes \(\approx(0.709, 0.705)\), which points at the centroid, not along the data. The variance structure is unchanged; only the centring was dropped. Most libraries centre for you. Verify that yours does.

Scaling. Covariance is not unit-free. Measure one feature in metres and another in millimetres and the millimetre featureโ€™s variance is a million times larger, so PC1 will align with it regardless of whether it carries any real information. If your features have incomparable units, standardise first โ€” which amounts to running PCA on the correlation matrix instead.

How many components

The honest answer is that this is a judgement call dressed up in several ways: keep enough components to reach a chosen cumulative explained-variance ratio (90%, 95%); look for a bend in the scree plot; or, if you are feeding a downstream model, cross-validate the number as a hyperparameter. The last is the only one that optimises something you actually care about. The first two are conventions, and the โ€œelbowโ€ is frequently not there.

What PCA cannot do

It is linear. The components are linear combinations of the original features, so PCA can rotate and project but never bend. Data on a spiral, a Swiss roll, or a circle has no low-dimensional linear description, and PCA will report that honestly by spreading variance across many components. That is not a failure of the algorithm; it is the algorithm telling you its model does not fit.

Variance is not importance. PCA is unsupervised and has never seen your labels. A direction with tiny variance can be the one that separates your classes, and PCA will discard it first. If you have labels and want a discriminative projection, that is a different method.

Components are rarely interpretable. PC1 is a weighted mixture of every original feature. Occasionally that mixture means something; usually it does not, and reading a story into the loadings is a common way to fool yourself.

Non-linear alternatives, and a warning. Kernel PCA applies the kernel trick to do PCA in a feature space. Autoencoders learn a non-linear encoder and decoder, and reduce to PCAโ€™s subspace when made linear with a squared-error loss. t-SNE and UMAP are for visualisation.

Do not read t-SNE or UMAP plots quantitatively. Both optimise the preservation of local neighbourhoods and explicitly do not preserve global geometry. Cluster sizes on the plot do not indicate cluster variance, distances between clusters do not indicate dissimilarity, and both change with the perplexity or neighbour count. They are excellent for noticing that groups exist, and unreliable for anything you would put a number on.

โœ… Key Takeaways

  • Maximising retained variance and minimising squared reconstruction error are the same optimisation โ€” Pythagoras splits a fixed total between them.
  • Components are eigenvectors of the covariance; explained variance is its eigenvalues. On the worked example \(\lambda = (3.4271, 0.0729)\), so PC1 carries 97.9%.
  • The discarded variance equals the reconstruction error: \((n-1)\lambda_2 = 0.2918\) is exactly the SSE from projecting onto PC1.
  • Compute via SVD, using \(\lambda_i = s_i^2/(n-1)\). Forming \(X^\top X\) squares the condition number for no benefit.
  • Centre always; standardise whenever features have different units. Skipping the centring makes PC1 point at the mean.
  • PCA is linear, unsupervised, and optimises reconstruction โ€” so it cannot unroll curved structure, and it has no reason to keep the direction your classifier needs.