Random Projections and the Johnson–Lindenstrauss Lemma
Reducing embedding dimension with a matrix that never saw the data — and the dimension-independent price of preserving every distance
Overview & motivation
The previous topic reduced an embedding cloud’s dimension by reading the cloud’s own covariance: principal component analysis finds the directions the data actually varies along and projects onto the top of them. It is variance-optimal, but it pays a price that is easy to overlook — it must see the data first, eigendecompose a covariance, and recompute when the corpus changes. This topic asks a question that sounds too good to be true: can we shrink with a map that never looks at the data at all?
The answer is the Johnson–Lindenstrauss lemma, and it is yes. Draw a random matrix — Gaussian entries will do — and the map preserves every pairwise distance in a set of points to within a factor of , provided is on the order of . The headline is the dimension that does not appear: the target depends on and the tolerance , but not on the ambient dimension . A million points embed into the same target dimension whether they started in or .
Before the algebra, drag the target-dimension slider and watch the distortion concentrate:
The first panel is the histogram of pairwise distortions: at small the projected distances scatter wildly, and as grows they pull into the band around — the visual signature of the chi-squared law tightening. The second separates the typical pair from the worst pair, which is the difference between an average and a guarantee. The third shows that the dense Gaussian matrix is a luxury: Rademacher and sparse variants concentrate just as tightly. The fourth marks the edge of the job: a random projection is a distance sketch, so it preserves distances rather than exact rankings — which is why it belongs at the front of an approximate-search pipeline, not in place of the retriever.
What we cover
- The random map, and the unbiasedness that every Johnson–Lindenstrauss proof begins from.
- The chi-squared law of the squared norm, and the concentration that makes it useful.
- The lemma itself: a union bound over the difference vectors, and the dimension-independent target .
- Why the bound is worst-case, and how far the typical pair beats it.
- Database-friendly projections: binary and sparse matrices with the same guarantee.
- Optimality of the bound, and the division of labor with PCA: what a data-oblivious distance sketch is for.
- A finance case study and the tested code.
The random map, and why it preserves norm in expectation
Fix a target dimension and draw with independent standard-normal entries . The random projection of a vector is
The scaling is the only piece of design in the whole construction, and it is there to make the map preserve length on average. Write for the -th row of , so the -th coordinate of is the inner product .
Theorem 1 (The random projection is unbiased, and its squared norm is chi-squared).
For and a Gaussian random matrix ,
where is the chi-squared distribution with degrees of freedom. In particular the normalized squared norm has mean and variance .
Proof.
Each row is a standard Gaussian vector in , so the inner product is a linear combination of independent normals and is therefore itself normal, with mean and variance . Hence , and the standardized quantity . Because the rows of are independent, the are independent. Now
and a sum of independent squared standard normals is by definition the chi-squared distribution with degrees of freedom. So , which has mean and variance . Multiplying through by gives .
∎Two things are worth reading off this. The map is unbiased — on average it neither stretches nor shrinks — and its fluctuation, with variance , shrinks as the target dimension grows. The standard deviation is the single number the histogram panel is tracking: at it is about , at about . Unbiasedness alone is useless — a map can be right on average and wrong every time. What rescues the construction is that this fluctuation does not merely shrink, it concentrates exponentially.
Concentration: the chi-squared tail
The variance tells us the typical deviation, but the lemma needs a statement about rare deviations: how unlikely is it that a single vector’s squared norm strays from by more than ? The answer is the chi-squared tail, and it decays exponentially in .
Theorem 2 (Sub-exponential concentration of the squared norm).
For the Gaussian random projection and any ,
Proof.
Write with independent, so . We bound the upper tail with the Chernoff / moment-generating-function method developed in concentration inequalities. For any , the moment generating function of a single is , so by independence , and Markov’s inequality applied to gives
The exponent is minimized at , where . Substituting collapses the bracket to , so
using the elementary inequality for . The lower tail is bounded the same way with and yields the same right-hand side (in fact a slightly smaller one); the sharp two-sided constants are Laurent and Massart’s (2000). Adding the two tails gives the factor of .
∎This is the load-bearing estimate. The probability that a fixed vector’s length is mangled by more than decays like , so we can drive it as low as we like by spending dimensions: doubling squares the failure probability. The next step spends exactly enough of them to cover not one vector but every pairwise difference in a data set at once.
The Johnson–Lindenstrauss lemma
A distance is the norm of a difference: . Because is linear, , so preserving the distance between two points is exactly preserving the norm of their difference vector. Theorem 2 controls one such vector; to control all of them we apply it to every difference and add up the failure probabilities — the union bound.
Theorem 3 (Johnson–Lindenstrauss (1984)).
Let and . If
then a single Gaussian random projection satisfies, with positive probability,
In particular such an exists.
Proof.
Apply Theorem 2 to each of the difference vectors . The event that some pair is distorted by more than is the union of the per-pair failure events, so by the union bound its probability is at most the sum of the per-pair probabilities:
using . This is strictly below exactly when , that is when . When the failure probability is below , the success probability is positive, so at least one realization of embeds every pair within tolerance — and a random draw succeeds with probability at least , which we can push toward by taking a constant factor larger.
∎Look at what the target dimension depends on. The denominator is a function of alone; the numerator is . The ambient dimension has vanished. This is the sense in which random projection beats the curse of dimensionality at its own game: the cost of preserving the geometry of points is set by how many points there are, not by how many coordinates they arrived with. The companion harness confirms the lemma directly — at the guaranteed , a single Gaussian projection holds all pairs inside the band across every random seed it tries.
The bound is worst-case: typical versus worst
The lemma is a guarantee, and a guarantee must hold for the worst pair, not the average one — which is why its constant is pessimistic. Set and , the finance configuration: the formula demands
larger than many ambient dimensions one would actually project from. Yet project the same cloud to a practical and the typical pair distorts by only about . The gap is the difference between an average and a maximum: with pairs, the worst of them still distorts by about at , and pulling that worst pair inside is what the large guaranteed buys. The “Typical vs worst” panel makes this concrete — the – band slips inside while the worst-pair envelope is still well outside it.
Two honest consequences follow. The constant is loose, and in practice one uses a far smaller than the lemma demands, accepting that a handful of pairs are badly distorted — random projection is a tool for approximate geometry. And the dimension independence is a statement about the worst case over arbitrary point sets; a structured, near-low-rank cloud of real embeddings is far easier than the worst case, which is exactly the slack the next sections exploit and measure.
Database-friendly projections
The dense Gaussian matrix is expensive: projecting one vector costs multiplications, and storing costs floats. Achlioptas (2003) showed that neither the Gaussian entries nor the density is necessary. Replace with random signs, or with a matrix that is mostly zero, and the guarantee survives.
Proposition 1 (Rademacher and sparse projections are unbiased).
Let the entries of be drawn independently as either Rademacher signs, with equal probability, or the sparse Achlioptas values with probabilities . In both cases , and the map is unbiased: .
Proof.
For the Rademacher case deterministically, so . For the sparse case ; with the matrix is two-thirds zeros. In either case the entries are independent with mean and second moment , so
the cross terms vanishing because for . Dividing by gives .
∎Unbiasedness is the easy half; what makes these valid Johnson–Lindenstrauss maps is that their squared norm concentrates at the same rate as the Gaussian one. Both families are sub-Gaussian, and Achlioptas proves a tail of the same exponential form — we cite that result rather than re-prove it, and the harness verifies it numerically: at the Gaussian, Rademacher, and sparse distortion spreads are , , and , all hugging the predicted . The “Projection families” panel shows the three side by side. The sparse map is the practical winner: it preserves distances for a third of the arithmetic, the property that makes random projection a cheap front end for large indexes.
There is an honest footnote, which the code surfaces: on a low-rank cloud, where the signal lives in a few coordinates, the very sparse map has a heavier-tailed per-matrix fluctuation than the Gaussian one — zeroing two-thirds of the matrix occasionally under-samples the signal directions. The concentration rate is unchanged, but the constant is a little worse, which is the kind of trade the sparsity buys.
Optimality, and the division of labor with PCA
Could a cleverer map do better than ? For a data-oblivious map asked to preserve all pairwise distances, no.
Proposition 2 (The target dimension is optimal (Larsen–Nelson 2017)).
For every and in the relevant range there exist points in such that any embedding into preserving all pairwise distances to within must have . The Johnson–Lindenstrauss bound is therefore tight up to the constant.
We cite this rather than prove it; the construction takes a near-orthogonal point set on which no projection can do better. So within its remit — preserving all pairwise distances without ever looking at the data — Johnson–Lindenstrauss is not merely serviceable but optimal: no data-oblivious map reaches a smaller target dimension. That is the achievement to keep in view. What the bound does not claim is optimality on a particular, structured cloud, and that is where PCA divides the labor rather than competing. PCA is data-dependent: it reads the covariance and spends every kept dimension on a direction the cloud actually varies along, so on a structured corpus it captures, per dimension, geometry that a map asking nothing of the data does not. The two are built for different jobs — JL for a fixed, dimension-independent guarantee over any point set with no training; PCA for the most it can wring from this one.
That division of labor has one sharp edge worth stating plainly, because it decides where each tool belongs. Johnson–Lindenstrauss preserves distances — which is exactly the currency an approximate-nearest-neighbor index trades in — but preserving every distance to does not preserve the order of distances when neighbors sit closer together than . On a tightly clustered cloud the tenth and eleventh nearest neighbors are nearly equidistant, so a wobble can swap them, and exact recall@10 falls — about for the random projection at against about for the data-dependent PCA of the same width (the “Recall after projection” panel). That is not the sketch underperforming; it is the sketch being a distance sketch rather than a retriever. Its role, by design, is the front end of an approximate-search pipeline — the dimensionality-reduction step before locality-sensitive hashing, the forthcoming topic — where a cheap, distance-preserving sketch is precisely what the next stage consumes, and the exact ordering is restored downstream by a rerank.
Finance case study
Honest caveats
Implementation
The companion module johnson_lindenstrauss.py owns every number this page and the laboratory cite. It builds the three projection families through a single generator stream — a deliberate choice, since drawing each random matrix from a freshly seeded generator over consecutive integers leaves their opening values weakly correlated and inflates the sampled variance with , a real and easily-missed NumPy pitfall. Its eight assertions encode the claims in order: unbiasedness for all three families; the chi-squared mean, variance, and Laurent–Massart tail; the lemma holding at the guaranteed ; dimension independence (a fixed vector’s distortion spread is for every , while the worst pairwise distortion grows with ); the three families concentrating alike; the recall gap between oblivious and data-dependent projection; a cross-check of the target dimension and distortion against scikit-learn’s random_projection; and the finance headline. The narrative notebook walks the same path section by section. Both run in about seven seconds and must exit cleanly before the topic ships.
Connections
- PCA is the data-dependent counterpart to this topic's data-oblivious projection: it reads the covariance and spends each kept dimension where the cloud actually varies, so at the same width it preserves far more nearest-neighbor recall, while random projection asks nothing of the data and pays for that obliviousness in retrieval — the two are the variance-optimal and the distance-preserving answers to the same dimensionality-reduction question pca-dimensionality-reduction
- the concentration of measure proved there is exactly the engine here: Johnson-Lindenstrauss works because the squared norm of a random projection is a sum of many independent contributions that concentrates sharply about its mean, the same thin-shell phenomenon that topic develops, now turned from a curse into the guarantee that distances survive high-dimensional-geometry
References & Further Reading
- paper Extensions of Lipschitz Mappings into a Hilbert Space — Johnson & Lindenstrauss (1984) The original lemma: any n points embed into O(epsilon^-2 log n) dimensions with (1 +/- epsilon) distance distortion
- paper An Elementary Proof of a Theorem of Johnson and Lindenstrauss — Dasgupta & Gupta (2003) The clean Gaussian-projection proof this topic follows, including the exp(-(k/2)(epsilon^2/2 - epsilon^3/3)) tail
- paper Database-Friendly Random Projections: Johnson-Lindenstrauss with Binary Coins — Achlioptas (2003) The Rademacher and sparse (+1/0/-1) projection matrices — same guarantee, a fraction of the arithmetic
- paper Adaptive Estimation of a Quadratic Functional by Model Selection — Laurent & Massart (2000) The sharp two-sided chi-squared tail bounds the concentration step cites for its exact constants
- paper Optimality of the Johnson-Lindenstrauss Lemma — Larsen & Nelson (2017) The matching lower bound: k = Theta(epsilon^-2 log n) cannot be improved in general
- paper Approximate Nearest Neighbors: Towards Removing the Curse of Dimensionality — Indyk & Motwani (1998) Random projection as the dimension-reduction step feeding locality-sensitive hashing — the forward link to approximate nearest neighbor search
- book The Random Projection Method — Vempala (2004) The standard monograph on random projection and its algorithmic uses
- 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 Very Sparse Random Projections — Li, Hastie & Church (2006) Pushing the sparsity of the projection matrix far past Achlioptas while keeping the guarantee