Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Shrinking data with a random matrix
PCA finds the best low-dimensional view by studying the data. Random projection skips that study: it multiplies every data point by the same random matrix, squeezing, say, 5000 numbers down to 200. Surprisingly, distances between points survive: two points that were far apart stay far apart, and close points stay close, up to a small random distortion that shrinks as the target dimension grows.
Random projection costs one matrix multiply, needs no training and works on streaming data. It speeds up nearest-neighbour search, compresses telemetry and sensor logs before clustering, makes huge least-squares and SVD problems tractable (sketching), and underlies locality-sensitive hashing. Its guarantee does not depend on the original dimension.
- 1. A random projection matrix is chosen…
- 2. What does random projection approximately preserve?
- 3. As the target dimension k grows, distortion…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.The numbers by hand
Gaussian projection. Use a d × k matrix R with independent N(0, 1/k) entries, and map x to z = Rᵀx. For any fixed vector v (such as the difference of two points), the ratio ‖Rᵀv‖²/‖v‖² is distributed as χ²ₖ/k:
- mean 1: squared lengths are preserved on average;
- standard deviation √(2/k): 0.2 at k = 50, 0.1 at k = 200, 0.05 at k = 800.
Johnson–Lindenstrauss bound. To keep all pairwise distances among n points within 1 ± ε with high probability, the classic sufficient dimension is
k ≥ 4 ln n / (ε²/2 − ε³/3).
- n = 500, ε = 0.1: 5327.
- n = 1000, ε = 0.1: 5921. Doubling n adds only about 600.
- n = 500, ε = 0.5: 299.
The bound never mentions d. Projecting from a million dimensions costs the same k as from ten thousand.
Sparse matrices. Entries +√(s/k), 0, −√(s/k) with probabilities 1/(2s), 1 − 1/s, 1/(2s):
- Achlioptas, s = 3: a third of the entries are non-zero (0.333);
- very sparse (Li, Hastie and Church), s = √d: for d = 10 000, only 1% are non-zero.
The steps in plain English:
- Pick k from the distortion you can tolerate: the s.d. of the squared-distance ratio is about √(2/k).
- Draw R once (Gaussian, or sparse for speed) with a fixed seed so that new data can be projected identically.
- Compute Z = XR, without centring or scaling unless you want those distances.
- Run the downstream method (nearest neighbours, clustering, regression) on Z; check distortion on a sample of pairs.
- 1. S.d. of the squared-distance ratio at k = 100?
- 2. The JL bound depends on the original dimension d…
- 3. Fraction of non-zeros in an Achlioptas matrix?
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.Why it works, and what the exercise found
Expected length. Each projected coordinate is zⱼ = Σᵢ Rᵢⱼvᵢ, a sum of independent zero-mean terms with variance ‖v‖²/k. So E‖z‖² = k · ‖v‖²/k = ‖v‖². For Gaussian R each zⱼ is exactly normal, and ‖z‖²k/‖v‖² is χ²ₖ.
Concentration. A χ²ₖ/k variable stays within 1 ± ε except with probability at most 2 exp(−k(ε²/2 − ε³/3)/2). There are n(n − 1)/2 < n²/2 pairs; a union bound over them gives failure probability below n² exp(−k(ε²/2 − ε³/3)/2). That is less than 1 once k exceeds 4 ln n/(ε²/2 − ε³/3), which is the formula above.
Sparse matrices have the same mean and nearly the same variance: for unit v, the variance of the ratio is 2/k + (s − 3)Σvᵢ⁴/k. The extra term vanishes for s = 3 and is small for very sparse matrices unless v is concentrated on a few coordinates.
Exercise 1 (500 random points in 2000-D, about 2000 random pairs). Each cell gives the projected-to-original squared-distance ratio as mean ± s.d., then the worst |ratio − 1|.
| matrix | k = 50 | k = 200 | k = 800 |
|---|---|---|---|
| Gaussian | 1.003 ± 0.207, 0.786 | 0.996 ± 0.101, 0.353 | 0.996 ± 0.050, 0.182 |
| Achlioptas | 0.978 ± 0.200, 0.867 | 0.993 ± 0.099, 0.367 | 1.004 ± 0.050, 0.174 |
| very sparse | 0.989 ± 0.210, 0.969 | 0.978 ± 0.101, 0.318 | 1.000 ± 0.053, 0.182 |
- The spreads match √(2/k): 0.2, 0.1 and 0.05.
- The three kinds of matrix are indistinguishable, even though the very sparse one had density 0.0223 (1/√2000 = 0.0224): about 45 times fewer multiplications than Gaussian.
- At k = 800 the worst sampled distortion was about 0.2, but the JL bound demands k ≥ 5327 for ε = 0.1, more than twice the original 2000 dimensions. The bound is a worst case over all pairs with a loose union bound; it tells you the scaling, not the k to use.
from sklearn.random_projection import GaussianRandomProjection, SparseRandomProjection, johnson_lindenstrauss_min_dim
k = johnson_lindenstrauss_min_dim(n_samples=500, eps=0.1) # 5327 (pessimistic)
Z = SparseRandomProjection(n_components=200, random_state=0).fit_transform(X) # density 1/√d by default- 1. For Gaussian R, k‖z‖²/‖v‖² follows…
- 2. Where does the ln n in the JL bound come from?
- 3. In exercise 1, the very sparse matrix's distortion was…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Optimality. Larsen and Nelson (2017) proved that k = Ω(ε⁻² log n) is necessary for any linear map, and indeed any map, that preserves all distances among some sets of n points. The JL dimension cannot be improved in general, except in constants. For data on a low-dimensional subspace or manifold, far fewer dimensions suffice: a subspace embedding of an r-dimensional subspace needs only k = O(r/ε²).
Fast transforms. Multiplying by a dense d × k matrix costs O(dk) per point. The fast Johnson–Lindenstrauss transform (Ailon and Chazelle, 2006) randomises signs, applies a Hadamard or Fourier transform to spread mass evenly, then samples coordinates, costing about O(d log d). Sparse JL matrices (Kane and Nelson) need only O(ε⁻¹ log n) non-zeros per input coordinate.
Randomized linear algebra. Sketching a tall matrix A with a subspace embedding S lets you solve least squares on SA instead of A. The randomized SVD of Halko, Martinsson and Tropp projects A onto a random k-dimensional range, then does a small exact SVD. That is random projection used to find the top principal subspace quickly (see the randomized linear algebra prerequisite).
What random projection is not. It neither denoises nor separates. Because Rᵀ is close to an isometry on the data, every algorithm run on Z behaves roughly as it would on X: at best it matches the full-dimensional result. PCA can do better, by discarding noise directions, or much worse, by discarding the signal.
Interview questions.
- Random projection or PCA? Random projection when d is huge, data streams, you need distances rather than interpretation, or you cannot afford an SVD. PCA when the variance structure matters and the leading directions carry the signal.
- How do you choose k in practice? Not from the JL formula: project a sample, measure the distance distortion or downstream accuracy, and increase k until it plateaus.
- 1. Is the O(log n / ε²) dimension improvable in general?
- 2. The fast JL transform uses…
- 3. Can random projection make 1-NN more accurate than in the original space?
Level 4: Expert
★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.Random projection versus PCA when the signal hides
History.
- W. Johnson and J. Lindenstrauss (1984) proved the lemma, as a step in extending Lipschitz maps.
- P. Indyk and R. Motwani (1998) used it for approximate nearest neighbours.
- D. Achlioptas (2003) introduced database-friendly ±1 projections.
- P. Li, T. Hastie and K. Church (2006) introduced very sparse projections.
- N. Ailon and B. Chazelle (2006) gave the fast JL transform; N. Halko, P.-G. Martinsson and J. Tropp (2011) surveyed randomized matrix decompositions.
Exercise 2. Three classes in 200-D, randomly rotated, 600 training and 600 test points, 3 datasets. Ten axes have large noise (s.d. 6). Either the classes differ strongly on those noisy, high-variance axes, or they differ weakly across the other 190 quiet axes. The table gives 1-NN test accuracy (chance is 0.33).
| data | method | k = 2 | 5 | 10 | 25 | 50 | 100 | full 200-D |
|---|---|---|---|---|---|---|---|---|
| signal in high-variance directions | random projection | 0.407 | 0.529 | 0.563 | 0.660 | 0.675 | 0.680 | 0.704 |
| PCA | 0.746 | 0.747 | 0.711 | 0.709 | 0.709 | 0.702 | ||
| signal in low-variance directions | random projection | 0.387 | 0.372 | 0.416 | 0.448 | 0.544 | 0.552 | 0.613 |
| PCA | 0.342 | 0.338 | 0.344 | 0.632 | 0.624 | 0.608 |
- Random projection climbed steadily toward the full-dimensional accuracy and never exceeded it in either setting: it preserves geometry, good and bad.
- When the signal lived in the high-variance directions, PCA beat the full space (0.746 against 0.704) by throwing away 190 noise-only dimensions.
- When the signal lived in low-variance directions, PCA with k ≤ 10 sat at chance (0.34): its top components were exactly the ten noisy axes. Once k passed 10 it picked up the signal directions and slightly exceeded the full space (0.632 against 0.613).
- At small k, random projection was the safer choice on the second dataset (0.416 against 0.344 at k = 10), and PCA on the first. Neither is universally better; random projection’s virtue is that its behaviour does not depend on where the signal hides.
Random projection in AI, robotics and aerospace
- Nearest-neighbour search and LSH: sign random projections (SimHash) give compact binary codes for place recognition and duplicate detection.
- Compressing high-rate telemetry: sketch thousands of channels before clustering or anomaly detection on board, where memory and compute are tight.
- Compressed sensing: random measurement matrices satisfy the restricted isometry property, the sparse-signal cousin of JL, used in single-pixel and radar imaging.
- Large-scale learning: random features and sketched least squares make kernel and regression problems fit in memory.
References
- W. B. Johnson, J. Lindenstrauss (1984). “Extensions of Lipschitz mappings into a Hilbert space.” Contemporary Mathematics 26.
- D. Achlioptas (2003). “Database-friendly random projections: Johnson–Lindenstrauss with binary coins.” Journal of Computer and System Sciences 66(4).
- P. Li, T. J. Hastie, K. W. Church (2006). “Very sparse random projections.” Proc. KDD.
- N. Ailon, B. Chazelle (2006). “Approximate nearest neighbors and the fast Johnson–Lindenstrauss transform.” Proc. STOC.
- K. G. Larsen, J. Nelson (2017). “Optimality of the Johnson–Lindenstrauss lemma.” Proc. FOCS.
- S. Dasgupta, A. Gupta (2003). “An elementary proof of a theorem of Johnson and Lindenstrauss.” Random Structures and Algorithms 22(1).