Simple Algorithm
Machine Learning · Convergence · 26 min

Random Projections & Johnson–Lindenstrauss

Why random directions preserve distances.

Multiply your data by a matrix of random numbers and every pairwise distance survives, nearly unchanged. The Johnson–Lindenstrauss lemma says how few dimensions this needs, about log⁡n/ε2\log n/\varepsilon^2, whatever the original dimension. This lesson proves it, makes the projection sparse and fast, shows that the bound cannot be beaten, and puts random sketches to work in least squares, low-rank approximation, clustering and sparse recovery.

What you’ll learn

  • State the Johnson–Lindenstrauss lemma in its distributional and nn-point forms, and prove it for Gaussian matrices with explicit constants from a Chernoff bound for χ2\chi^2.
  • Explain why sparse sign matrices and the fast Johnson–Lindenstrauss transform work, and what each costs per vector.
  • State the lower bound that makes k=Θ(ε−2log⁡n)k=\Theta(\varepsilon^{-2}\log n) optimal, and connect it to families of nearly orthogonal vectors.
  • Use subspace embeddings to sketch least squares, and relate random projections to randomized SVD, kk-means and compressed sensing through Gordon's set-dependent bound.

The lemma: dimension without d

Take nn points in Rd\R^d, with dd perhaps in the millions. A random projection maps them to Rk\R^k by a matrix drawn without looking at the data. The Johnson–Lindenstrauss lemma says that every one of the (n2)\binom n2 distances survives within a factor 1±ε1\pm\varepsilon once kk is of order log⁡n/ε2\log n/\varepsilon^2. The ambient dimension dd does not appear at all. Johnson & Lindenstrauss (1984) proved it with a random orthogonal projection, on the way to a theorem about extending Lipschitz maps. Indyk & Motwani (1998) showed that a matrix of independent Gaussians works too, and Dasgupta & Gupta (2003) gave an elementary proof with explicit constants; the Gaussian version below is the cleanest.

Predict before you look

Using the bound above, how many dimensions guarantee that all pairwise squared distances among n=106n=10^6 points stay within ±10%\pm10\% (ε=0.1\varepsilon=0.1) with probability at least 99% (δ=0.01\delta=0.01)?

Show the answer

13816 dimensions

4ln⁡106≈55.264\ln10^6\approx55.26 and 2ln⁡100≈9.212\ln100\approx9.21, while ε2/2−ε3/3≈0.004667\varepsilon^2/2-\varepsilon^3/3\approx0.004667, so k≥64.47/0.004667≈13,816k\ge64.47/0.004667\approx13{,}816, whether dd is 10510^5 or 10910^9. Without the δ\delta term, the bound 4ln⁡n/(ε2/2−ε3/3)≈11,8424\ln n/(\varepsilon^2/2-\varepsilon^3/3)\approx11{,}842 is what scikit-learn's johnson_lindenstrauss_min_dim uses; there the union bound only guarantees success with probability at least 1/n1/n, which is still enough to show that a good map exists.

ε\varepsilon0.50.20.10.050.01
kk for n=106n=10^6, δ=0.01\delta=0.017743,72013,81653,357about 1.3 million
The 1/ε21/\varepsilon^2 is the expensive part; nn enters only through log⁡n\log n. Random projection pays off when dd is far larger than ε−2log⁡n\varepsilon^{-2}\log n: bag-of-words or hashed features, random-feature expansions, or data that must be compressed before anyone can look at them. For a 2-D picture it is useless; that is a job for the methods of Manifold Learning: t-SNE & UMAP.

The proof: concentration of a chi-squared variable

DerivationProof for Gaussian matrices (constants as in Dasgupta & Gupta, 2003)
  1. Q:=∥Au∥2∥u∥2=∑i=1kgi2∼χk2,EQ=k,EesQ=(1−2s)−k/2  (s<12)Q:=\frac{\norm{A\mathbf u}^2}{\norm{\mathbf u}^2}=\sum_{i=1}^kg_i^2\sim\chi^2_k,\qquad\E Q=k,\qquad\E e^{sQ}=(1-2s)^{-k/2}\ \ (s<\tfrac12)

    Fix u≠0\mathbf u\ne\mathbf 0. Each coordinate of AuA\mathbf u is N(0,∥u∥2)\mathcal N(0,\norm{\mathbf u}^2), independently, so the normalized squared length is a sum of kk independent squared standard Gaussians: a χk2\chi^2_k variable with mean kk. The proof is a concentration inequality for it.

  2. Pr⁡[Q≥(1+ε)k]≤min⁡0<s<1/2(1−2s)−k/2es(1+ε)k=((1+ε)e−ε)k/2\Pr[Q\ge(1+\varepsilon)k]\le\min_{0<s<1/2}\frac{(1-2s)^{-k/2}}{e^{s(1+\varepsilon)k}}=\big((1+\varepsilon)e^{-\varepsilon}\big)^{k/2}

    Upper tail. Markov's inequality applied to esQe^{sQ}, minimized at s=ε/(2(1+ε))s=\varepsilon/(2(1+\varepsilon)), gives

  3. Pr⁡[Q≥(1+ε)k]≤exp⁡(−k2(ε22−ε33))\Pr[Q\ge(1+\varepsilon)k]\le\exp\Big(-\frac k2\Big(\frac{\varepsilon^2}2-\frac{\varepsilon^3}3\Big)\Big)

    Use ln⁡(1+ε)≤ε−ε2/2+ε3/3\ln(1+\varepsilon)\le\varepsilon-\varepsilon^2/2+\varepsilon^3/3 for ε≥0\varepsilon\ge0 (the difference vanishes at 0 and has derivative ε3/(1+ε)≥0\varepsilon^3/(1+\varepsilon)\ge0):

  4. Pr⁡[Q≤(1−ε)k]≤((1−ε)eε)k/2≤e−kε2/4≤exp⁡(−k2(ε22−ε33))\Pr[Q\le(1-\varepsilon)k]\le\big((1-\varepsilon)e^{\varepsilon}\big)^{k/2}\le e^{-k\varepsilon^2/4}\le\exp\Big(-\frac k2\Big(\frac{\varepsilon^2}2-\frac{\varepsilon^3}3\Big)\Big)

    Lower tail. The same argument with e−sQe^{-sQ}, minimized at s=ε/(2(1−ε))s=\varepsilon/(2(1-\varepsilon)), and ln⁡(1−ε)≤−ε−ε2/2\ln(1-\varepsilon)\le-\varepsilon-\varepsilon^2/2 give a bound at least as strong:

  5. Pr⁡[some pair fails] ≤ n22⋅2exp⁡(−k2(ε22−ε33)) ≤ δ  ⟺  k≥4ln⁡n+2ln⁡(1/δ)ε2/2−ε3/3\Pr[\text{some pair fails}]\ \le\ \frac{n^2}2\cdot2\exp\Big(-\frac k2\Big(\frac{\varepsilon^2}2-\frac{\varepsilon^3}3\Big)\Big)\ \le\ \delta\iff k\ge\frac{4\ln n+2\ln(1/\delta)}{\varepsilon^2/2-\varepsilon^3/3}

    All pairs. The map is linear, f(xi)−f(xj)=f(xi−xj)f(\mathbf x_i)-f(\mathbf x_j)=f(\mathbf x_i-\mathbf x_j), so apply the distributional bound to the (n2)<n2/2\binom n2<n^2/2 difference vectors and take a union bound. The failure probability is below δ\delta under the stated condition on kk:

Nothing in the proof needs Gaussians beyond the tail bound. Any matrix with independent, mean-zero, unit-variance sub-Gaussian entries (tails at least as light as a Gaussian's) satisfies the distributional form with different constants (Matoušek, 2008), and so does a uniformly random orthogonal projection, scaled by d/k\sqrt{d/k}, the original construction. Achlioptas (2003) proved exactly the bound above, with δ=n−β\delta=n^{-\beta}, for two "database-friendly" choices: random signs ±1\pm1, and the sparse distribution 3⋅{+1,0,−1}\sqrt3\cdot\{+1,0,-1\} with probabilities 16,23,16\frac16,\frac23,\frac16, which skips two thirds of the work and needs only additions. Indyk & Motwani (1998) used random projections to make approximate nearest-neighbor search fast in high dimension.

Faster, sparser, and no better possible

A dense k×dk\times d projection costs kdkd operations per vector, about 104d10^4d in the example above. Making the matrix sparse helps, but taken too far it fails on sparse inputs: if x=e1\mathbf x=\mathbf e_1, only the few nonzeros in the first column of the matrix see it, and their number does not concentrate. The fix is to precondition. The fast Johnson–Lindenstrauss transform of Ailon & Chazelle (2009) is Φ=PHD\Phi=PHD: DD flips the sign of each coordinate at random, HH is the normalized d×dd\times d Walsh–Hadamard matrix, applied in O(dlog⁡d)\order{d\log d} time like an FFT, and PP is a sparse random projection. HDHD is orthogonal, so it preserves lengths, and it spreads every vector's mass over all coordinates: each coordinate of HDxHD\mathbf x is a random-sign sum, and Hoeffding's inequality with a union bound gives ∥HDx∥∞≤∥x∥2ln⁡(2d/δ)/d\norm{HD\mathbf x}_\infty\le\norm{\mathbf x}\sqrt{2\ln(2d/\delta)/d} with probability 1−δ1-\delta. On such flat vectors the sparse PP concentrates well, and the whole map takes O(dlog⁡d+min⁡{dε−2log⁡n, ε−2log⁡3n})\order{d\log d+\min\{d\varepsilon^{-2}\log n,\ \varepsilon^{-2}\log^3n\}} time per vector, against O(dε−2log⁡n)\order{d\varepsilon^{-2}\log n} for a dense projection.

Sparsity can also be designed in. Kane & Nelson (2014), improving on Dasgupta, Kumar & Sarlós (2010), built matrices with the optimal O(ε−2log⁡(1/δ))\order{\varepsilon^{-2}\log(1/\delta)} rows in which only an O(ε)\order{\varepsilon} fraction of each column is nonzero. At the extreme, CountSketch puts a single random sign in each column: too weak for the nn-point lemma with logarithmically many rows, but strong enough for the subspace embeddings of the next step.

Check your understanding

A k×dk\times d random matrix has independent entries that are nonzero with a small probability qq, scaled to have unit variance, and no preconditioning. For which inputs is ∥x∥\norm{\mathbf x} least reliably preserved?

  1. Dense vectors with all coordinates of similar size
  2. Vectors with a very large norm
  3. Vectors orthogonal to the all-ones vector
  4. Sparse vectors such as e1\mathbf e_1, which only the few nonzeros of one column ever see
Show the answer

D. Sparse vectors such as e1\mathbf e_1, which only the few nonzeros of one column ever see

For x=e1\mathbf x=\mathbf e_1, the output is the first column, whose squared norm is a scaled count of about qkqk nonzeros: unless qkqk is large, that count fluctuates wildly. Dense vectors average over many independent entries and concentrate. The randomized Hadamard step of the fast transform turns every input into a dense one before the sparse projection.

Where does log⁡n/ε2\log n/\varepsilon^2 come from? Apply a JL map to the n+1n+1 points 0,e1,…,en\mathbf 0,\mathbf e_1,\dots,\mathbf e_n. The images ui=f(ei)\mathbf u_i=f(\mathbf e_i) satisfy ∥ui∥2∈[1−ε,1+ε]\norm{\mathbf u_i}^2\in[1-\varepsilon,1+\varepsilon] and ∥ui−uj∥2∈[2−2ε, 2+2ε]\norm{\mathbf u_i-\mathbf u_j}^2\in[2-2\varepsilon,\,2+2\varepsilon], so by polarization ∣⟨ui,uj⟩∣≤2ε\abs{\langle\mathbf u_i,\mathbf u_j\rangle}\le2\varepsilon: nn nearly orthogonal vectors in k=O(ε−2log⁡n)k=\order{\varepsilon^{-2}\log n} dimensions. High-dimensional space holds exponentially many nearly orthogonal directions, about ecε2ke^{c\varepsilon^2k}, and Alon's lower bound is essentially the converse: up to a factor log⁡(1/ε)\log(1/\varepsilon) in the exponent, it holds no more.

The phenomenon is special to Euclidean geometry. In ℓ1\ell_1 there are nn-point sets for which any embedding with distortion DD needs nΩ(1/D2)n^{\Omega(1/D^2)} dimensions (Brinkman & Charikar, 2005).

Sketching: least squares, low rank, clustering, sparse recovery

DerivationSketch-and-solve least squares
  1. x~=arg min⁡x∥SAx−Sb∥,x⋆=arg min⁡x∥Ax−b∥\tilde{\mathbf x}=\argmin_{\mathbf x}\norm{SA\mathbf x-S\mathbf b},\qquad\mathbf x^\star=\argmin_{\mathbf x}\norm{A\mathbf x-\mathbf b}

    Solve the least-squares problem min⁡x∥Ax−b∥\min_{\mathbf x}\norm{A\mathbf x-\mathbf b} with A∈Rn×dA\in\R^{n\times d}, n≫dn\gg d. Every residual Ax−bA\mathbf x-\mathbf b lies in V=col⁡([A b])\mathcal V=\operatorname{col}([A\ \mathbf b]), of dimension at most d+1d+1. Let SS be an ε\varepsilon-subspace embedding for V\mathcal V, and compare the small sketched problem with the original:

  2. (1−ε)∥Ax~−b∥2≤∥S(Ax~−b)∥2≤∥S(Ax⋆−b)∥2≤(1+ε)∥Ax⋆−b∥2(1-\varepsilon)\norm{A\tilde{\mathbf x}-\mathbf b}^2\le\norm{S(A\tilde{\mathbf x}-\mathbf b)}^2\le\norm{S(A\mathbf x^\star-\mathbf b)}^2\le(1+\varepsilon)\norm{A\mathbf x^\star-\mathbf b}^2

    Chain the embedding inequalities through the optimality of x~\tilde{\mathbf x} for the sketched problem:

  3. ∥Ax~−b∥ ≤ 1+ε1−ε ∥Ax⋆−b∥\norm{A\tilde{\mathbf x}-\mathbf b}\ \le\ \sqrt{\frac{1+\varepsilon}{1-\varepsilon}}\ \norm{A\mathbf x^\star-\mathbf b}

    So the sketched solution's residual is within a factor about 1+ε1+\varepsilon of optimal, from a problem with O(d/ε2)\order{d/\varepsilon^2} rows instead of nn. The guarantee is on the residual: by Pythagoras ∥A(x~−x⋆)∥2≲2ε∥Ax⋆−b∥2\norm{A(\tilde{\mathbf x}-\mathbf x^\star)}^2\lesssim2\varepsilon\norm{A\mathbf x^\star-\mathbf b}^2, which bounds x~−x⋆\tilde{\mathbf x}-\mathbf x^\star only through the conditioning of AA.

A Gaussian sketch costs O(knd)\order{knd} to apply, more than the O(nd2)\order{nd^2} of solving directly, so sketching pays with fast sketches: Hadamard-based transforms (Sarlós, 2006) or CountSketch, applied in time proportional to the number of nonzeros of AA (Clarkson & Woodruff, 2013). Woodruff (2014) surveys the field. The same idea runs through other algorithms:

  • Randomized SVD. The Gaussian test matrix of Halko, Martinsson & Tropp (2011) is a sketch: AΩA\Omega captures the dominant column space of AA (its error bound is in Linear Algebra for ML: Eigenvectors & the SVD).
  • Clustering. Projecting to O(log⁡(k/ε)/ε2)\order{\log(k/\varepsilon)/\varepsilon^2} dimensions preserves the cost of every k-means clustering within 1+ε1+\varepsilon (Makarychev, Makarychev & Razenshteyn, 2019): the dimension depends on the number of clusters, not on nn.
  • Compressed sensing. The distributional lemma plus a union bound over all ss-sparse supports shows that a Gaussian m×dm\times d matrix with mm of order slog⁡(d/s)s\log(d/s) has the restricted isometry property of order ss (Baraniuk, Davenport, DeVore & Wakin, 2008), which compressed sensing needs (Sparse Recovery Guarantees).

Key takeaways

  • Johnson–Lindenstrauss: a Gaussian matrix with k≥(4ln⁡n+2ln⁡(1/δ))/(ε2/2−ε3/3)k\ge(4\ln n+2\ln(1/\delta))/(\varepsilon^2/2-\varepsilon^3/3) rows, scaled by 1/k1/\sqrt k, keeps all (n2)\binom n2 squared distances within 1±ε1\pm\varepsilon with probability 1−δ1-\delta, independently of dd; for n=106n=10^6, ε=0.1\varepsilon=0.1, δ=0.01\delta=0.01 that is about 13,800.
  • The proof is a Chernoff bound for χk2\chi^2_k, Pr⁡[∣Q/k−1∣>ε]≤2e−k(ε2/2−ε3/3)/2\Pr[\abs{Q/k-1}>\varepsilon]\le2e^{-k(\varepsilon^2/2-\varepsilon^3/3)/2}, plus a union bound over the difference vectors; any sub-Gaussian entries work, including Achlioptas's sparse {+1,0,−1}\{+1,0,-1\} matrices.
  • The fast JL transform PHDPHD flattens every vector with a randomized Hadamard transform so that a sparse projection works, in about dlog⁡dd\log d time per vector; sparse JL matrices need only an O(ε)\order{\varepsilon} fraction of nonzeros per column.
  • k=Θ(ε−2log⁡n)k=\Theta(\varepsilon^{-2}\log n) is optimal for any map (Larsen & Nelson), mirroring the roughly eε2ke^{\varepsilon^2k} nearly orthogonal directions that fit in Rk\R^k; nothing similar holds in ℓ1\ell_1.
  • Subspace embeddings with O(d/ε2)\order{d/\varepsilon^2} rows give near-optimal least squares from a small problem; random projections also drive randomized SVD, kk-means reduction to O(log⁡(k/ε)/ε2)\order{\log(k/\varepsilon)/\varepsilon^2} dimensions and the restricted isometry property, all instances of Gordon's bound m≳γ(T)2/ε2m\gtrsim\gamma(T)^2/\varepsilon^2.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

With δ=1/n\delta=1/n, the JL bound is k=6ln⁡n/(ε2/2−ε3/3)k=6\ln n/(\varepsilon^2/2-\varepsilon^3/3). If n=104n=10^4 points need k=2,000k=2{,}000 at some ε\varepsilon, about how many dimensions do n=108n=10^8 points need at the same ε\varepsilon?

  1. About 2,000: kk does not depend on nn
  2. About 4,000
  3. About 20,000
  4. About 20 million
Show the answer

B. About 4,000

kk is proportional to ln⁡n\ln n, and ln⁡108=2ln⁡104\ln10^8=2\ln10^4, so kk doubles. Ten thousand times more points cost only twice the dimension, and the original dimension dd never enters.

Question 2

For fixed nn and δ\delta, you tighten the distortion from ε=0.1\varepsilon=0.1 to ε=0.05\varepsilon=0.05. By about what factor does the bound on kk grow?

  1. About 2
  2. About 8
  3. It does not change
  4. About 4
Show the answer

D. About 4

The denominator ε2/2−ε3/3\varepsilon^2/2-\varepsilon^3/3 behaves like ε2/2\varepsilon^2/2 for small ε\varepsilon, so halving ε\varepsilon multiplies kk by about 4; for n=106n=10^6 and δ=0.01\delta=0.01 the exact values are 13,816 and 53,357, a factor 3.86. Precision, not the number of points, is what makes random projections expensive.

Question 3

You solve least squares on the sketch (SA, Sb)(SA,\,S\mathbf b), where SS is an ε\varepsilon-subspace embedding for the column space of [A b][A\ \mathbf b]. What is guaranteed about the solution x~\tilde{\mathbf x}?

  1. x~=x⋆\tilde{\mathbf x}=\mathbf x^\star exactly
  2. ∥x~−x⋆∥≤ε∥x⋆∥\norm{\tilde{\mathbf x}-\mathbf x^\star}\le\varepsilon\norm{\mathbf x^\star}, whatever AA is
  3. Its residual is within a factor (1+ε)/(1−ε)\sqrt{(1+\varepsilon)/(1-\varepsilon)} of the optimal residual
  4. Nothing, unless SS has Gaussian entries
Show the answer

C. Its residual is within a factor (1+ε)/(1−ε)\sqrt{(1+\varepsilon)/(1-\varepsilon)} of the optimal residual

Every residual lies in the embedded subspace, so (1−ε)∥Ax~−b∥2≤∥S(Ax~−b)∥2≤∥S(Ax⋆−b)∥2≤(1+ε)∥Ax⋆−b∥2(1-\varepsilon)\norm{A\tilde{\mathbf x}-\mathbf b}^2\le\norm{S(A\tilde{\mathbf x}-\mathbf b)}^2\le\norm{S(A\mathbf x^\star-\mathbf b)}^2\le(1+\varepsilon)\norm{A\mathbf x^\star-\mathbf b}^2. The argument only uses the embedding property, which fast and sparse sketches also provide.

Question 4

Could a cleverly designed nonlinear map embed every set of nn points from a high-dimensional space into far fewer than log⁡n/ε2\log n/\varepsilon^2 dimensions, keeping squared distances within 1±ε1\pm\varepsilon?

  1. Yes: nonlinear maps are strictly more powerful than linear ones
  2. No: some point sets need Ω(ε−2log⁡n)\Omega(\varepsilon^{-2}\log n) dimensions for any map, linear or not
  3. Yes, provided the map is chosen after seeing the points
  4. No, but only because random matrices cannot be derandomized
Show the answer

B. No: some point sets need Ω(ε−2log⁡n)\Omega(\varepsilon^{-2}\log n) dimensions for any map, linear or not

Larsen & Nelson (2017) exhibit nn-point sets for which every (1±ε)(1\pm\varepsilon) embedding into Rk\R^k has k=Ω(ε−2log⁡n)k=\Omega(\varepsilon^{-2}\log n), for ε\varepsilon above min⁡{n,d}−0.4999\min\{n,d\}^{-0.4999}. The oblivious random linear map is optimal up to a constant.

Further reading

  • W. B. Johnson & J. Lindenstrauss, "Extensions of Lipschitz mappings into a Hilbert space," Contemporary Mathematics 26, 189–206 (1984).
  • S. Dasgupta & A. Gupta, "An elementary proof of a theorem of Johnson and Lindenstrauss," Random Structures & Algorithms 22(1), 60–65 (2003).
  • N. Ailon & B. Chazelle, "The fast Johnson–Lindenstrauss transform and approximate nearest neighbors," SIAM Journal on Computing 39(1), 302–322 (2009).
  • K. G. Larsen & J. Nelson, "Optimality of the Johnson–Lindenstrauss lemma," Proceedings of the 58th IEEE Symposium on Foundations of Computer Science (FOCS), 633–638 (2017).
  • D. P. Woodruff, "Sketching as a tool for numerical linear algebra," Foundations and Trends in Theoretical Computer Science 10(1–2), 1–157 (2014).