PCA as Optimal Linear Dimensionality Reduction for Embeddings
The variance-optimal projection of an embedding cloud — what truncating 1536 dimensions to k keeps, and the honest reason it can still hurt retrieval
Overview & motivation
A vector retriever pays for every dimension twice. A single -dimensional float32 embedding is about six kilobytes, so an index of ten million of them is sixty gigabytes before any structure is added, and the cost of scanning or traversing that index — whether exact or approximate — scales with the dimension as well. The natural response is to shrink . The previous topic told us this should be possible without much loss: real embeddings do not fill , they lie near a low-dimensional surface, so most of the ambient coordinates carry little of the variation that distinguishes one document from another. The question this topic answers is how to shrink optimally, and how much retrieval quality survives when we do.
Principal component analysis is the variance-optimal linear answer. Given a cloud of embeddings, PCA finds the orthogonal directions along which the cloud actually spreads — the first captures the most variance, the second the most of what remains, and so on — and projects onto the top of them. We will see that these directions are the top eigenvectors of the centered covariance matrix, that this single construction simultaneously maximizes retained variance, minimizes reconstruction error, and decorrelates the coordinates, and that the resulting truncation is the best possible rank- approximation in a precise sense (the Eckart–Young–Mirsky theorem). We will also prove the fact that makes PCA matter for retrieval rather than merely for compression: the fraction of squared distance a projection preserves is exactly the fraction of variance it keeps.
Before the algebra, drag the rank slider and watch the spectrum decide what survives:
The first panel is the scree: a few directions hold most of the variance and the rest taper into a noise floor, so the cumulative explained-variance curve rises steeply then flattens. The second shows reconstruction error collapsing as grows — and a faint reference line for a random projection of the same width, which PCA always beats. The third projects three topical clusters onto the top two components, already enough to separate them. The fourth is the one that earns the word “retrieval”: nearest-neighbor recall retained after projection, PCA against random, as a function of how many dimensions we keep.
What we cover
- PCA’s three equivalent formulations, and why they all land on the covariance eigenvectors.
- The first principal direction as the maximizer of the Rayleigh quotient.
- Reconstruction error, and the identity that makes variance and error two views of one optimum.
- Eckart–Young–Mirsky, the SVD–PCA equivalence, explained variance, and effective rank.
- What survives projection: distortion equals explained variance, and PCA versus random.
- A finance case study, the honest caveat that variance-optimal is not retrieval-optimal, and the tested code.
PCA three equivalent ways
Fix a cloud of embeddings with mean , and let be the centered data matrix whose -th row is . The object PCA studies is the centered covariance
a symmetric positive-semidefinite matrix whose entry is the empirical covariance of coordinates and . A rank- linear compression keeps orthonormal directions, the columns of a matrix with ; the associated projector is , which sends a centered vector to its best approximation inside the chosen subspace, with coordinates (the scores) .
There are three natural ways to ask for the best such , and the content of PCA is that they have the same answer:
- Maximize retained variance. Keep the subspace along which the projected scores vary the most, i.e. maximize .
- Minimize reconstruction error. Keep the subspace whose projection loses the least, i.e. minimize the expected squared residual .
- Decorrelate. Keep directions along which the scores are uncorrelated, so the compressed coordinates carry independent information.
All three are solved by taking the columns of to be the top eigenvectors of . The next two sections prove the first two equivalences; the third is immediate once we have the eigenvectors, because in the eigenbasis the scores along distinct eigenvectors have covariance , which is diagonal — exactly the decorrelation statement, and the same diagonalization of a covariance matrix developed in multivariate distributions.
The principal directions are the top covariance eigenvectors
Start with the very first direction. Which unit vector captures the most variance of the projected data? The variance of the scalar scores is , the Rayleigh quotient of at , so the question is to maximize it over the unit sphere.
Theorem 1 (The principal directions maximize the Rayleigh quotient).
Let be symmetric positive-semidefinite with eigenvalues and orthonormal eigenvectors . Then
and, maximizing over unit vectors orthogonal to the first eigenvectors, the -th principal direction is with value . The top- subspace maximizing the total projected variance is therefore spanned by .
Proof.
By the spectral theorem a symmetric matrix factors as , where is orthogonal and . Any unit vector can be written in this eigenbasis as , and because is orthogonal . Substituting,
since is the largest eigenvalue and . Equality holds exactly when all the weight sits on the largest eigenvalue, , that is (uniquely so when ). For the -th direction, restricting to forces , so the same bound gives , attained at . Summing the per-direction maxima, no orthonormal can exceed , with equality at . The general statement that this greedy, one-direction-at-a-time argument is globally optimal over all -subspaces is the Courant–Fischer min-max theorem, which we cite (Horn and Johnson, 2013); the companion code confirms no random unit vector exceeds the Rayleigh maximum .
∎The geometric reading is the one to keep: stretches space most along , next-most along , and so on, and PCA keeps the directions of greatest stretch. The eigenvalue is the variance the cloud has along .
Reconstruction error, and why variance and error are one optimum
The variance formulation and the reconstruction formulation look different — one maximizes what we keep, the other minimizes what we lose — but they are the same optimization, joined by the Pythagorean theorem.
Theorem 2 (Variance maximization equals reconstruction-error minimization).
For any orthonormal and centered ,
Hence minimizing the expected squared reconstruction error over rank- orthonormal projectors is identical to maximizing the retained variance , and both are solved by , leaving the minimum residual
Proof.
The projector onto the column space of is orthogonal (, using ), so for every the decomposition is orthogonal and the Pythagorean theorem gives pointwise. Taking expectations, . Now and, since ,
which proves the identity. Because is fixed, minimizing the left side is maximizing — the variance problem of Theorem 1, whose optimum is at the top- eigenvectors. The residual is then .
∎So the best -dimensional summary of an embedding cloud, whether you measure “best” by variance kept or by reconstruction lost, is the same top- eigenspace, and the price of the compression is the sum of the eigenvalues you discard. The next theorem turns this from a statement about the covariance into the classical statement about the data matrix itself.
Eckart–Young–Mirsky and the SVD–PCA equivalence
PCA is usually computed not from the covariance but from the singular value decomposition of the centered data matrix, , with singular values on the diagonal of . The two pictures are the same object.
Proposition 1 (SVD–PCA equivalence).
If then , so the principal directions are the right singular vectors (the columns of ) and the eigenvalues are .
This is the result developed in pca-low-rank and svd; the one-line derivation substitutes the SVD into , and the companion harness verifies eigenvalue-by-eigenvalue that the covariance eigendecomposition and the SVD agree. With this identification, “best rank- approximation of the data” and “top- PCA projection” become the same truncation, and its optimality is the Eckart–Young–Mirsky theorem.
Theorem 3 (Eckart–Young–Mirsky (best low-rank approximation)).
Let and let be the truncated SVD keeping the top singular triples. Then over all matrices of rank at most ,
Proof.
The value first. The Frobenius norm is unitarily invariant — equals the sum of squared singular values of — and has singular values (the top canceled), so , which is by Proposition 1. For optimality, let have rank at most , so its null space has dimension at least . The span of has dimension , and two subspaces of whose dimensions sum to more than must intersect, so there is a unit vector . For that , , so , and writing gives because each there. Hence , the spectral-norm optimality; the Frobenius statement follows from the same interlacing applied to all the tail singular values, which we present as the truncated-SVD value above and cite in full to Eckart and Young (1936). The extension of this optimality to every unitarily-invariant norm is Mirsky’s theorem (Horn and Johnson, 2013), which we cite and the harness verifies numerically by checking that the truncated SVD beats every random rank- projection.
∎Two summaries of the spectrum do the practical work. The explained-variance ratio is the fraction of total variance the top directions hold; by Theorem 2 it is also one minus the relative reconstruction error. The effective rank (participation ratio) is a soft count of how many directions carry appreciable variance — it is small when a few eigenvalues dominate and approaches the ambient when the spectrum is flat. For the synthetic -dimensional financial cloud in the companion code, the effective rank is about and the top components already hold of the variance: the cloud is, for the purposes of distance, far lower-dimensional than its ambient size.
What survives projection: distortion equals explained variance
We have controlled reconstruction error. Retrieval, though, cares about distances — whether the nearest neighbor stays the nearest neighbor after we project. The bridge is a short identity that ties the geometry directly to the spectrum.
Theorem 4 (Projection distortion equals the explained-variance ratio).
Let be the top- PCA projector and a centered embedding. The expected fraction of squared norm retained by the projection is
and the fraction of the data’s mean pairwise squared distance that survives the projection is the same .
Proof.
Since projects onto the top- eigenvectors, and , whose ratio is by definition. For pairwise distances, expand over the centered cloud: , and the last term vanishes because the data are centered, . So the total pairwise squared distance is exactly times the total squared norm, and the identical computation for the projected cloud (which is also centered) replaces by . The retained fraction is therefore the same norm ratio, .
∎This is the sentence that makes PCA a retrieval tool rather than a compression trick: keep of the variance and you keep of the squared-distance energy, so the metric the retriever scores on is preserved in exactly the proportion the scree plot reports. It also sharpens the contrast with the alternative. A random projection — the data-oblivious method of the Johnson–Lindenstrauss lemma, the next topic in this track — preserves all pairwise distances of any point set with high probability, but only by keeping enough dimensions, , regardless of where the data actually live. PCA is data-dependent: it spends its dimensions on the directions this particular cloud occupies, so for a cloud of low effective rank it preserves far more distance per dimension. The companion code makes the gap concrete — at retained dimensions, PCA keeps of the top- nearest-neighbor recall where a random projection of the same width keeps only .
Finance case study
A candid note on the companion code, in the spirit of the rest of the series: the cloud is a synthetic low-rank-plus-noise construction, sampled deterministically so the notebook is CPU-only and reproducible to the decimal with no model download, and the recall numbers are measured on it. It is not the output of a trained encoder, and — as the caveats spell out next — a real encoder’s top components carry nuisance structure that this clean synthetic spectrum does not, which is exactly where variance-optimal and retrieval-optimal part ways.
The honest catch: variance-optimal is not retrieval-optimal
Everything above optimizes one objective — reconstruction error, equivalently retained variance — and proves PCA optimal for it. Retrieval has a different objective: preserve the ranking of similarity scores, so that the documents a query would have retrieved in full dimension are the ones it retrieves after projection. The two objectives usually agree, which is why PCA is a workhorse, but they can disagree, and the disagreement is not a footnote.
The reason is that the top principal components — the directions of greatest variance — are not always the directions of greatest relevance. In real embedding sets the largest-variance directions frequently encode nuisance structure: a dominant “common” direction shared by almost every embedding, or variation that tracks token frequency and document length rather than topic. Projecting onto the top components faithfully keeps that nuisance and can wash out the finer, lower-variance distinctions a retriever actually ranks on. The striking empirical counterpoint is that removing the top few components — the “All-but-the-Top” postprocessing of Mu and Viswanath (2018) — often improves similarity and retrieval, the opposite of what a pure variance objective would recommend. That is a measured heuristic, not a theorem, and we flag it as such; it is the cleanest evidence that variance-optimal and retrieval-optimal are genuinely different targets. These nuisance directions are themselves a property of learned representations, and they connect to the spherical geometry of the previous topic: PCA’s mean-centering is in tension with the uncentered, normalized vectors cosine retrieval actually compares.
Honest caveats
Implementation
The companion notebook (notebookPath) is self-contained, CPU-only, and runs in a few seconds on numpy, scipy, and scikit-learn. It samples the synthetic embedding cloud, computes PCA both by eigendecomposition of the covariance and by SVD of the centered data matrix, measures reconstruction error and the random-projection baseline, the explained-variance ratio and effective rank, the projection-distortion fractions, and the nearest-neighbor recall after projection, and prints the grid table that the SpectrumLaboratory mirrors to the decimal. Its verification harness makes each claim executable: the eigendecomposition and the SVD agree on eigenvalues to machine precision and on components up to sign (Proposition 1); no random unit vector beats the Rayleigh maximum (Theorem 1); the reconstruction identity holds and the residual equals the tail sum (Theorem 2); the truncated-SVD Frobenius error equals and beats every random rank- projection (Theorem 3); the retained squared-norm and squared-distance fractions both equal exactly (Theorem 4); recall@10 is monotone in the kept dimension and PCA beats random everywhere; and the whole pipeline agrees with scikit-learn’s reference PCA. The three pillars — the proofs above, the laboratory, and the tested code — agree by construction.
Connections
- latent semantic analysis is exactly the truncated SVD of the TF-IDF term-document matrix, so the optimal-low-rank theory proved here is the linear-algebra engine under that classical sparse-retrieval model, and the Eckart-Young guarantee is what makes LSA's reduced space the best rank-k summary of the term space vector-space-model-tfidf
- PCA centers and projects in R^d while that topic normalizes embeddings onto the sphere, and the tension between PCA's mean subtraction and cosine's uncentered angles is precisely the centering-versus-cosine caveat this topic raises about applying linear dimensionality reduction before spherical retrieval hypersphere-vmf-geometry
References & Further Reading
- paper On Lines and Planes of Closest Fit to Systems of Points in Space — Pearson (1901) The origin of PCA as the best-fitting line and plane minimizing perpendicular reconstruction distance
- paper Analysis of a Complex of Statistical Variables into Principal Components — Hotelling (1933) The variance-maximization formulation and the name 'principal components'
- paper The Approximation of One Matrix by Another of Lower Rank — Eckart & Young (1936) The original optimal low-rank approximation theorem — the truncated-SVD reconstruction value proved here
- book Principal Component Analysis — Jolliffe (2002) The definitive reference for PCA's formulations, explained variance, component retention, and pitfalls
- paper All-but-the-Top: Simple and Effective Postprocessing for Word Representations — Mu & Viswanath (2018) The empirical finding that removing the top principal components of an embedding set improves similarity and retrieval — the rigorFlag citation that variance-optimal is not retrieval-optimal
- paper Indexing by Latent Semantic Analysis — Deerwester, Dumais, Furnas, Landauer & Harshman (1990) LSA as the truncated SVD of the term-document matrix; the classical bridge to the vector space model
- paper Random Projection in Dimensionality Reduction: Applications to Image and Text Data — Bingham & Mannila (2001) Data-oblivious random projection in practice — the contrast partner to PCA's data dependence
- paper Finding Structure with Randomness: Probabilistic Algorithms for Constructing Approximate Matrix Decompositions — Halko, Martinsson & Tropp (2011) Randomized SVD — how the top-k spectrum is computed at embedding scale without a full decomposition
- book Matrix Analysis — Horn & Johnson (2013) Courant-Fischer min-max and Mirsky's extension of Eckart-Young to all unitarily-invariant norms — the cited halves of Theorems 1 and 3