S14 · Lesson 41 · Random projections
Shrinking high-dimensional data with a random matrix while keeping every distance nearly intact
Lesson 40's PCA reduces dimension by finding the directions of largest variance, which takes an eigen-decomposition of the data's covariance. A surprising alternative needs no look at the data at all: multiply every point by the same random matrix. The Johnson–Lindenstrauss lemma says that points in any number of dimensions can be mapped to about dimensions so that every pairwise distance changes by at most a factor . The target dimension depends on how many points there are and the accuracy wanted, not on the original dimension. Random projections speed up nearest-neighbor search, sketch data streams, and are the measurement matrices of lesson S7's compressed sensing.
- The target dimension that the Johnson–Lindenstrauss bound guarantees for points, , against the allowed distortion ε.
- Halving ε roughly quadruples ; multiplying the number of points by a thousand only adds a constant to it. The original dimension appears nowhere.
Predict
10 000 points live in a million dimensions. To keep every pairwise distance within ±10%, about how many dimensions does the Johnson–Lindenstrauss bound need?
= 7895. The million original dimensions don't enter. The bound is conservative: in practice a few thousand random dimensions usually do, and a looser ε = 0.2 needs only 2125.
Name it
The lemma
For any points in and , there is a linear map to with
such that every squared distance is preserved within a factor . A random matrix works with high probability: entries drawn independently from , or even with equal probability.
Why it works
For a fixed vector , each of the projected coordinates is a Gaussian with variance , so the projected squared length is times an average of independent variables: its mean is exactly and its relative spread shrinks as . A Chernoff bound (lesson A11's tail bounds) makes the probability of distortion beyond ε exponentially small in , and a union bound over all pairs gives the .
Against PCA
PCA finds the best -dimensional subspace for squared error on average, but needs the data and an eigen-decomposition, and can distort individual distances badly. Random projections are data-oblivious, cost one matrix multiply, work on streams, and guarantee every pairwise distance, but use more dimensions than PCA when the data really is near a low-dimensional subspace.
Uses
Approximate nearest-neighbor search in high dimensions, sketching streams of data into small summaries, speeding up least squares and SVD with random sketches, and compressed sensing's measurement matrices, which need the related restricted isometry property for sparse vectors.
Worked example
How many dimensions does the bound require for 10 000 points at ε = 0.1 and 0.2, and for a million points at ε = 0.1?
Show the solution
- The bound. With = 9.21:
- 10 000 points, ε = 0.1.
- 10 000 points, ε = 0.2.
- A million points, ε = 0.1.
- Compare. A hundred times more points costs only 50% more dimensions.
Answer 7895 dimensions at ε = 0.1 and 2125 at ε = 0.2 for 10 000 points, and 11 842 for a million points at ε = 0.1.
Worked example
Projecting a unit vector with a matrix of entries, what are the mean and standard deviation of its projected squared length, for = 100 and = 10 000?
Show the solution
- Projected squared length. It is with mean 1 and variance (each has variance 2).
- k = 100. so lengths typically change by about 14%.
- k = 10 000. It's 1.4%.
- The trend. The spread falls as , as for any average.
Answer the mean is 1 in both cases; the standard deviation is 0.141 for k = 100 and 0.014 for k = 10 000.
Worked example
A search engine stores 300-dimensional embeddings for 50 million items. Projecting to 64 dimensions with a random matrix: how much memory does it save, and what typical distortion results?
Show the solution
- Memory. times less.
- Distortion. The typical relative change in squared distance is about so distances shift by roughly 9%.
- Using it. That's too much to guarantee exact nearest neighbors, but enough to shortlist candidates, which are then re-ranked with the full vectors. That two-stage search is how large-scale similarity search works.
Answer memory falls by a factor of 4.7, and distances shift by roughly 9%.
Check
How many dimensions does the Johnson–Lindenstrauss bound require for 100000 points at distortion ε = 0.1?
Check
A unit vector is projected to 64 dimensions with entries. What is the standard deviation of its projected squared length?
Check
At ε = 0.1, how many more dimensions does the bound require for 10 times as many points (starting from 10000)?
Check
1000 million vectors of 1024 dimensions (4-byte floats) are randomly projected to 32 dimensions.
(a)
How much memory do the original vectors take?
(b)
By what factor does the projection shrink them?
Check
For one fixed vector projected to 200 dimensions, the chance its squared length is off by more than ε = 0.1 is at most . What is that bound?
Check
Why does the Johnson–Lindenstrauss dimension not depend on the data's original dimension ?
Check
Test the lemma. With rng = numpy.random.default_rng(seed), draw n = 100 points in 5000 dimensions from a standard Gaussian, then a projection matrix R = rng.standard_normal((k, 5000))/sqrt(k) with k = 200. Compute all pairwise squared distances before and after projecting, and set max_dist, the largest |ratio − 1| over all pairs, and rms_dist, the RMS of (ratio − 1). Compare rms_dist with .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
A team compresses sensor fingerprints from 4096 to 256 dimensions for a nearest-neighbor lookup, choosing between PCA and a random projection. Explain the trade-offs.
PCA finds the 256 directions that keep the most variance across the training data, so if the fingerprints really vary mainly along a few directions, it preserves distances better per dimension than a random projection. But it needs the training data and an eigen-decomposition, its quality depends on new fingerprints resembling the training set, and it guarantees only average error: some pairs can be badly distorted. A random projection needs no data, costs one matrix multiply, works the same for any future fingerprints, and by Johnson–Lindenstrauss distorts every pair by about ≈ 9% in squared distance, typically 4 to 5% in distance, with exponentially rare worse cases. For a shortlist followed by exact re-ranking, the random projection's simplicity and guarantees often win; for the most compact faithful representation of data that really is low-dimensional, PCA does.
Practice
Keep practicing
8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.