S1441 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 n points in any number of dimensions can be mapped to about ln⁡n/ε2 dimensions so that every pairwise distance changes by at most a factor 1±ε. 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 k that the Johnson–Lindenstrauss bound guarantees for n points, k≥4ln⁡n/(ε2/2−ε3/3), against the allowed distortion ε.
  • Halving ε roughly quadruples k; 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?

Name it

The lemma

For any n points in ℝd and 0<ε<1, there is a linear map to ℝk with

k≥4ln⁡nε2/2−ε3/3

such that every squared distance is preserved within a factor 1±ε. A random matrix works with high probability: entries drawn independently from N(0,1/k), or even ±1/k with equal probability.

Why it works

For a fixed vector 𝐯, each of the k projected coordinates is a Gaussian with variance ‖𝐯‖2/k, so the projected squared length is ‖𝐯‖2 times an average of k independent χ12 variables: its mean is exactly ‖𝐯‖2 and its relative spread shrinks as 1/k. A Chernoff bound (lesson A11's tail bounds) makes the probability of distortion beyond ε exponentially small in kε2, and a union bound over all (n2) pairs gives the ln⁡n.

Against PCA

PCA finds the best k-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
  1. The bound. With ln⁡(104) = 9.21: k≥4ln⁡nε2/2−ε3/3
  2. 10 000 points, ε = 0.1. k≥4ln⁡1040.12/2−0.13/3=7895
  3. 10 000 points, ε = 0.2. k≥4ln⁡1040.22/2−0.23/3=2125
  4. A million points, ε = 0.1. k≥4ln⁡1060.12/2−0.13/3=11842
  5. 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 k×d matrix of N(0,1/k) entries, what are the mean and standard deviation of its projected squared length, for k = 100 and k = 10 000?

Show the solution
  1. Projected squared length. It is 1k∑i=1kZi2 with mean 1 and variance 2/k (each Zi2 has variance 2).
  2. k = 100. σ=0.02=0.141 so lengths typically change by about 14%.
  3. k = 10 000. It's 1.4%.
  4. The trend. The spread falls as 1/k, 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
  1. Memory. 300/64=4.7 times less.
  2. Distortion. The typical relative change in squared distance is about 2/64=0.18 so distances shift by roughly 9%.
  3. 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 N(0,1/k) 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 2e−k(ε2/2−ε3/3)/2. What is that bound?

Check

Why does the Johnson–Lindenstrauss dimension not depend on the data's original dimension d?

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 2/k.

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.

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.