Type at least 2 letters. Enter opens the first result, Esc closes.

    Linear Algebra · Lesson 18 · Linear algebra for modern ML

    Sketching, Johnson–Lindenstrauss and random matrix theory

    Advanced 90 min 4 interactive figures 5 videos Colab notebook

    Before this lesson: lesson 09 (PCA and randomized SVD), lesson 12 (QR and least squares) and lesson 17 (perturbation theory: Weyl and Davis–Kahan). Time: about 90 minutes, plus the notebook.


    Why this matters #

    Some matrices are too big to factor, or even to read many times. A sketch multiplies the data by a short random matrix and works with the small result. The randomized SVD of lesson 09 was the first example. The same idea solves huge least-squares problems and compresses embeddings and gradients.

    Randomness also explains noise. With n=50n = 50 ensemble members and p=106p = 10^6 grid points, the sample covariance of an ensemble forecast is mostly sampling noise. Random matrix theory says exactly how pure noise looks, so you can tell which eigenvalues or singular values carry signal. This replaces "look for the elbow" in a PCA or EOF analysis by a rule with a reason.

    1. Predict first #

    2. The picture #

    One vector first. Fix x∈RDx \in \mathbb{R}^D and draw g∈RDg \in \mathbb{R}^D with independent N(0,1)N(0,1) entries. Then g⊤x∼N(0,∥x∥2)g^\top x \sim N(0, \|x\|^2), so (g⊤x)2(g^\top x)^2 is an unbiased estimate of ∥x∥2\|x\|^2. It is a bad estimate: its relative standard deviation is 2\sqrt 2. Now use kk independent rows, stacked as G∈Rk×DG \in \mathbb{R}^{k\times D}. The average ∥Gx∥2/k\|Gx\|^2/k is still unbiased, and its relative standard deviation is 2/k\sqrt{2/k}: 0.1 for k=200k = 200.

    So a random map x↦Gx/kx \mapsto Gx/\sqrt k keeps one length with error about 2/k\sqrt{2/k}, whatever DD is. The tails are close to Gaussian, so the probability of a large error falls exponentially in kk. This is why we can keep n2n^2 distances at once with only k∝log⁡nk \propto \log n: a union bound over n2n^2 events multiplies the failure probability by n2n^2, and in the exponent this is only an extra term 2ln⁡n2\ln n.

    Interactive figure: loading…

    Project 50 points from R1000\mathbb{R}^{1000} to Rk\mathbb{R}^k. The histogram shows, for all 1225 pairs, the squared distance after the projection divided by the squared distance before. The violet curve is its exact law χk2/k\chi^2_k/k: it does not depend on the points or on DD. Move kk: the spread shrinks like 2/k\sqrt{2/k}. Below k≈94k \approx 94, Theorem 3 (without the δ\delta term) gives no ε<1\varepsilon < 1; above it the aqua lines show 1±ε1 \pm \varepsilon, and the whole histogram almost always stays inside them (press New points a few times: the union bound is pessimistic).

    Quick check

    A random projection keeps all pairwise distances of n=104n = 10^4 points within 1±0.21 \pm 0.2 with k=2126k = 2126 rows when the points live in R106\mathbb{R}^{10^6}. The same points are now embedded in R109\mathbb{R}^{10^9} (extra zero coordinates, or a finer grid). Which kk does the JL bound ask for now?

    A whole subspace. A dd-dimensional subspace holds infinitely many vectors. But its unit sphere is covered by a finite net of eO(d)e^{O(d)} points (every unit vector is close to one of them), so it behaves like eO(d)e^{O(d)} vectors. Then "log⁡(number of points)\log(\text{number of points})" becomes dd, and a sketch needs k∝dk \propto d rows. Section 3 shows this with a figure.

    Noise has a shape. Estimate a covariance from nn samples in pp dimensions. If p/np/n is not small, the sample eigenvalues spread out, even when all true eigenvalues are equal. The spread is deterministic as n,p→∞n, p \to \infty: the Marchenko–Pastur law. A true signal direction is visible only if its eigenvalue sticks out of this "bulk".

    Interactive figure: loading…

    Pure noise is not flat. Each data set has nn samples of p=100p = 100 independent N(0,1)N(0,1) features, so every true eigenvalue is 1. The histogram pools the eigenvalues of X⊤X/nX^\top X/n from four data sets; the violet curve is the Marchenko–Pastur density for γ=p/n\gamma = p/n. Move γ\gamma: the bulk widens to [(1−γ)2,(1+γ)2][(1-\sqrt\gamma)^2, (1+\sqrt\gamma)^2], and for γ>1\gamma > 1 part of the spectrum is exactly 0. Then add a spike θ\theta (Theorem 5 below): its orange top eigenvalues leave the bulk only when θ>γ\theta > \sqrt\gamma.

    3. Definitions and main results #

    Subspace embeddings #

    Start with a picture. Take a plane (a 2-dimensional subspace) inside R5000\mathbb{R}^{5000} and its unit circle: all vectors of the plane with length 1. A sketch SS maps each of them to a vector SySy. If we draw each yy at the length ∥Sy∥\|Sy\|, the circle becomes an ellipse. A good sketch gives an ellipse that is almost round.

    Interactive figure: loading…

    Move kk and watch the ellipse. The dashed circle is the set of unit vectors yy of the plane. The orange ellipse shows the same vectors at the length ∥Sy∥\|Sy\|; its half-axes are the singular values σ1,σ2\sigma_1, \sigma_2 of SUSU. The aqua ring is the band 1−ε≤∥Sy∥≤1+ε\sqrt{1-\varepsilon} \le \|Sy\| \le \sqrt{1+\varepsilon}. Drag the blue vector: its image (violet) always lies on the ellipse. For ε=0.5\varepsilon = 0.5, the ellipse leaves the ring in about one draw out of three at k=20k = 20, and almost never at k=100k = 100. Press New S a few times, or Run 1000 draws to count.

    From the picture to the formula, step by step. Let U∈Rn×dU \in \mathbb{R}^{n\times d} have orthonormal columns that span the subspace U\mathcal U.

    1. Every y∈Uy \in \mathcal U is y=Uzy = Uz for one z∈Rdz \in \mathbb{R}^d, and ∥y∥=∥z∥\|y\| = \|z\| because U⊤U=IU^\top U = I.
    2. Then ∥Sy∥2=z⊤(U⊤S⊤SU)z=z⊤Mz\|Sy\|^2 = z^\top (U^\top S^\top S U) z = z^\top M z, with the small d×dd\times d matrix M=U⊤S⊤SUM = U^\top S^\top SU.
    3. "All lengths are kept within 1±ε1 \pm \varepsilon" means (1−ε)∥z∥2≤z⊤Mz≤(1+ε)∥z∥2(1-\varepsilon)\|z\|^2 \le z^\top Mz \le (1+\varepsilon)\|z\|^2 for every zz.
    4. By the Rayleigh quotient (lesson 06), this holds exactly when every eigenvalue of MM lies in [1−ε,1+ε][1-\varepsilon, 1+\varepsilon], that is, ∥Id−M∥2≤ε\|I_d - M\|_2 \le \varepsilon.
    5. The eigenvalues of M=(SU)⊤(SU)M = (SU)^\top(SU) are the squared singular values of SUSU. So every singular value of SUSU lies in [1−ε,1+ε][\sqrt{1-\varepsilon}, \sqrt{1+\varepsilon}]: the ellipse lies in the ring.

    Definition. Let U⊆Rn\mathcal{U} \subseteq \mathbb{R}^n be a subspace and 0<ε<10 < \varepsilon < 1. A matrix S∈Rk×nS \in \mathbb{R}^{k\times n} is an ε\varepsilon-subspace embedding for U\mathcal{U} if, for all y∈Uy \in \mathcal{U},

    (1−ε)∥y∥2≤∥Sy∥2≤(1+ε)∥y∥2.(1-\varepsilon)\|y\|^2 \le \|Sy\|^2 \le (1+\varepsilon)\|y\|^2 .

    By steps 1–5, this is the same as ∥Id−U⊤S⊤SU∥2≤ε\|I_d - U^\top S^\top S U\|_2 \le \varepsilon. A random SS is oblivious if it works with probability ≥1−δ\ge 1-\delta for any fixed U\mathcal U of dimension dd, without looking at the data.

    Three standard sketches for a dd-dimensional subspace (rows up to constant factors, for a fixed failure probability δ\delta; nnz⁡(A)\operatorname{nnz}(A) is the number of nonzero entries of AA):

    Sketch What SS is Rows kk (order) Cost of SASA, A∈Rn×dA \in \mathbb{R}^{n\times d}
    Gaussian G/kG/\sqrt k, Gij∼N(0,1)G_{ij} \sim N(0,1) d/ε2d/\varepsilon^2 O(k⋅nnz⁡(A))O(k\cdot\operatorname{nnz}(A)), dense: O(knd)O(knd)
    SRHT random signs, then a fast Hadamard transform, then kk random rows dlog⁡d/ε2d\log d/\varepsilon^2 O(ndlog⁡k)O(nd\log k)
    CountSketch each row of AA is added, with a random sign, to one random bucket d2/ε2d^2/\varepsilon^2 O(nnz⁡(A))O(\operatorname{nnz}(A))

    The Gaussian sketch needs the fewest rows but is the most expensive to apply. CountSketch is the cheapest to apply (one pass over the nonzeros) but needs more rows.

    Exact row counts and the SRHT

    Woodruff (2014), Theorems 6, 7 and 9, give these sizes for an ε\varepsilon-subspace embedding of a dd-dimensional subspace with probability at least 1−δ1-\delta:

    Sketch What SS is Rows kk
    Gaussian G/kG/\sqrt k, Gij∼N(0,1)G_{ij} \sim N(0,1) O((d+log⁡(1/δ))/ε2)O((d + \log(1/\delta))/\varepsilon^2)
    SRHT n/k RHD\sqrt{n/k}\,RHD: random signs DD, normalized Walsh–Hadamard HH (entries ±1/n\pm1/\sqrt n), kk sampled rows RR O(ε−2log⁡d (d+log⁡n)2)O(\varepsilon^{-2}\log d\,(\sqrt d + \sqrt{\log n})^2)
    CountSketch one ±1\pm1 per column, in a random row O(d2/(δε2))O(d^2/(\delta\varepsilon^2))

    Why the SRHT needs DD and HH: uniform row sampling alone fails when a few rows carry most of the energy (a vector like e1e_1). The random signs and the Hadamard transform spread the energy of every vector over all coordinates, so that sampling kk rows uniformly becomes safe.

    Two ways to use a sketch for least squares #

    We want x∗=arg⁡min⁡x∥Ax−b∥x^* = \arg\min_x \|Ax - b\| with A∈Rn×dA \in \mathbb{R}^{n\times d} and n≫dn \gg d. There are two ways to use a sketch:

    Sketch-and-solve Sketch-and-precondition
    What it does solves the small problem min⁡x∥S(Ax−b)∥\min_x \|S(Ax - b)\| uses SA=QRSA = QR only to build RR; then runs LSQR on the full problem with the preconditioner R−1R^{-1}
    Accuracy residual within a factor ≈1+ε\approx 1+\varepsilon of the optimum; xx itself can be far off full accuracy (machine precision), like a direct solver
    Cost one sketch + one small QR one sketch + one small QR + about 30 cheap iterations, each one product with AA and A⊤A^\top
    When to use you need a good fit (a small residual), not exact coefficients you need accurate coefficients xx, or AA is ill-conditioned

    The next two theorems explain both rows of "Accuracy".

    Theorem 1 (sketch-and-solve least squares) #

    Let A∈Rn×dA \in \mathbb{R}^{n\times d}, b∈Rnb \in \mathbb{R}^n, x∗=arg⁡min⁡x∥Ax−b∥x^* = \arg\min_x\|Ax - b\| and r∗=Ax∗−br^* = Ax^* - b. Let SS be an ε\varepsilon-subspace embedding for U=range⁡([A    b])\mathcal U = \operatorname{range}([A\;\; b]) (dimension at most d+1d+1), and x~=arg⁡min⁡x∥S(Ax−b)∥\tilde x = \arg\min_x \|S(Ax - b)\|. Then

    ∥Ax~−b∥≤1+ε1−ε  ∥Ax∗−b∥.\|A\tilde x - b\| \le \sqrt{\frac{1+\varepsilon}{1-\varepsilon}}\;\|Ax^* - b\|.

    Proof.

    1. For every xx, the vector Ax−bAx - b lies in U\mathcal U, so the embedding bounds apply to it.
    2. Lower bound at x~\tilde x: ∥Ax~−b∥2≤11−ε∥S(Ax~−b)∥2\|A\tilde x - b\|^2 \le \frac{1}{1-\varepsilon}\|S(A\tilde x - b)\|^2.
    3. x~\tilde x minimizes the sketched problem: ∥S(Ax~−b)∥2≤∥S(Ax∗−b)∥2\|S(A\tilde x - b)\|^2 \le \|S(Ax^* - b)\|^2.
    4. Upper bound at x∗x^*: ∥S(Ax∗−b)∥2≤(1+ε)∥Ax∗−b∥2\|S(Ax^* - b)\|^2 \le (1+\varepsilon)\|Ax^* - b\|^2.
    5. Chain 2–4 and take square roots. ■\blacksquare

    For small ε\varepsilon the factor is about 1+ε1+\varepsilon. The guarantee is about the residual, not about xx. Since r∗⊥range⁡(A)r^* \perp \operatorname{range}(A), Pythagoras gives ∥A(x~−x∗)∥2=∥Ax~−b∥2−∥r∗∥2≤2ε1−ε∥r∗∥2\|A(\tilde x - x^*)\|^2 = \|A\tilde x - b\|^2 - \|r^*\|^2 \le \frac{2\varepsilon}{1-\varepsilon}\|r^*\|^2, so

    ∥x~−x∗∥≤2ε1−ε  ∥r∗∥σmin⁡(A).\|\tilde x - x^*\| \le \sqrt{\tfrac{2\varepsilon}{1-\varepsilon}}\;\frac{\|r^*\|}{\sigma_{\min}(A)} .

    With a large residual and an ill-conditioned AA (small σmin⁡\sigma_{\min}), x~\tilde x can be far from x∗x^*: section 5 shows a 170% error. For a Gaussian sketch the average is known exactly: E∥Ax~−b∥2=(1+dk−d−1)∥r∗∥2\mathbb{E}\|A\tilde x - b\|^2 = (1 + \frac{d}{k-d-1})\|r^*\|^2.

    Quick check

    You fit a least-squares model with sketch-and-solve, ε=0.1\varepsilon = 0.1. The matrix AA has condition number 10610^6 and the optimal residual is not small. What does Theorem 1 promise?

    Theorem 2 (sketch-and-precondition) #

    Let SS be an ε\varepsilon-subspace embedding for range⁡(A)\operatorname{range}(A) and SA=QRSA = QR a thin QR (R∈Rd×dR \in \mathbb{R}^{d\times d}). Then κ(AR−1)≤(1+ε)/(1−ε)\kappa(AR^{-1}) \le \sqrt{(1+\varepsilon)/(1-\varepsilon)}, where κ\kappa is the 2-norm condition number (lesson 13).

    In words: RR undoes almost all of the bad scaling of AA. The matrix AR−1AR^{-1} has nearly orthonormal columns, whatever κ(A)\kappa(A) was.

    Proof of Theorem 2
    1. SAR−1=QSAR^{-1} = Q has orthonormal columns, so ∥SAR−1z∥=∥z∥\|SAR^{-1}z\| = \|z\| for every z∈Rdz \in \mathbb{R}^d.
    2. AR−1z∈range⁡(A)AR^{-1}z \in \operatorname{range}(A), so the embedding gives (1−ε)∥AR−1z∥2≤∥SAR−1z∥2=∥z∥2≤(1+ε)∥AR−1z∥2(1-\varepsilon)\|AR^{-1}z\|^2 \le \|SAR^{-1}z\|^2 = \|z\|^2 \le (1+\varepsilon)\|AR^{-1}z\|^2.
    3. Divide by ∥z∥2\|z\|^2: ∥AR−1z∥/∥z∥\|AR^{-1}z\|/\|z\| lies in [1/1+ε, 1/1−ε][1/\sqrt{1+\varepsilon},\, 1/\sqrt{1-\varepsilon}] for every z≠0z \ne 0. So every singular value of AR−1AR^{-1} lies in this interval, and their ratio is at most (1+ε)/(1−ε)\sqrt{(1+\varepsilon)/(1-\varepsilon)}. ■\blacksquare

    Now run LSQR (CG on the normal equations, done stably) on min⁡y∥AR−1y−b∥\min_y \|AR^{-1}y - b\| and set x=R−1yx = R^{-1}y. With κ=κ(AR−1)\kappa = \kappa(AR^{-1}), the error ∥A(x(m)−x∗)∥\|A(x^{(m)} - x^*)\| after mm iterations is at most 2((κ−1)/(κ+1))m2\big((\kappa - 1)/(\kappa + 1)\big)^m times its start value (Woodruff §2.6, Eq. (15)). Even a crude sketch with ε=1/2\varepsilon = 1/2 gives κ≤3\kappa \le \sqrt3 and a factor 0.270.27 per iteration: about 27 iterations to reach 10−1510^{-15}, whatever κ(A)\kappa(A) is. This reaches full accuracy because the sketch only builds the preconditioner; the iteration solves the original problem. Blendenpik and LSRN work this way.

    Theorem 3 (Johnson–Lindenstrauss) #

    Let x1,…,xn∈RDx_1, \dots, x_n \in \mathbb{R}^D, 0<ε<10 < \varepsilon < 1, 0<δ<10 < \delta < 1. Let G∈Rk×DG \in \mathbb{R}^{k\times D} have independent N(0,1)N(0,1) entries and f(x)=Gx/kf(x) = Gx/\sqrt k. If

    k≥4ln⁡n+2ln⁡(1/δ)ε2/2−ε3/3,k \ge \frac{4\ln n + 2\ln(1/\delta)}{\varepsilon^2/2 - \varepsilon^3/3},

    then with probability at least 1−δ1 - \delta, for all pairs i,ji, j:

    (1−ε)∥xi−xj∥2≤∥f(xi)−f(xj)∥2≤(1+ε)∥xi−xj∥2.(1-\varepsilon)\|x_i - x_j\|^2 \le \|f(x_i) - f(x_j)\|^2 \le (1+\varepsilon)\|x_i - x_j\|^2 .

    So k=O(ε−2log⁡(n/δ))k = O(\varepsilon^{-2}\log(n/\delta)), independent of DD.

    Proof, step by step. Each step has the numbers for n=104n = 10^4 points, ε=0.2\varepsilon = 0.2 and δ=0.01\delta = 0.01, where the bound asks for k=2657k = 2657.

    1. Reduce to one vector. ff is linear, so f(xi)−f(xj)=f(xi−xj)f(x_i) - f(x_j) = f(x_i - x_j). It suffices to show that for each fixed unit vector uu (the normalized difference), Q=∥Gu∥2Q = \|Gu\|^2 satisfies (1−ε)k≤Q≤(1+ε)k(1-\varepsilon)k \le Q \le (1+\varepsilon)k, except with a small probability. Numbers: there are n(n−1)/2=49,995,000≈5⋅107n(n-1)/2 = 49{,}995{,}000 \approx 5\cdot10^7 difference vectors.
    2. Exact distribution. Row ii of GG gives gi⊤u∼N(0,∥u∥2)=N(0,1)g_i^\top u \sim N(0, \|u\|^2) = N(0,1), and the rows are independent. So QQ is a sum of kk squared standard normals: Q∼χk2Q \sim \chi^2_k, EQ=k\mathbb{E}Q = k. Numbers: we want QQ in [0.8k,1.2k]=[2125.6,3188.4][0.8k, 1.2k] = [2125.6, 3188.4].
    3. Chernoff, upper tail. For Z∼N(0,1)Z \sim N(0,1) and λ<1/2\lambda < 1/2, EeλZ2=(1−2λ)−1/2\mathbb{E}e^{\lambda Z^2} = (1-2\lambda)^{-1/2}. By Markov's inequality applied to eλQe^{\lambda Q}, P(Q≥(1+ε)k)≤e−λ(1+ε)k(1−2λ)−k/2P(Q \ge (1+\varepsilon)k) \le e^{-\lambda(1+\varepsilon)k}(1-2\lambda)^{-k/2}. Choose λ=ε2(1+ε)\lambda = \frac{\varepsilon}{2(1+\varepsilon)}, so 1−2λ=11+ε1 - 2\lambda = \frac{1}{1+\varepsilon}. The bound becomes ((1+ε)e−ε)k/2=exp⁡ ⁣(−k2(ε−ln⁡(1+ε)))\big((1+\varepsilon)e^{-\varepsilon}\big)^{k/2} = \exp\!\big(-\tfrac k2(\varepsilon - \ln(1+\varepsilon))\big).
    4. Use ln⁡(1+ε)≤ε−ε2/2+ε3/3\ln(1+\varepsilon) \le \varepsilon - \varepsilon^2/2 + \varepsilon^3/3: P(Q≥(1+ε)k)≤exp⁡ ⁣(−k2(ε2/2−ε3/3))P(Q \ge (1+\varepsilon)k) \le \exp\!\big(-\tfrac k2(\varepsilon^2/2 - \varepsilon^3/3)\big). Numbers: ε2/2−ε3/3=0.02−0.00267=0.01733\varepsilon^2/2 - \varepsilon^3/3 = 0.02 - 0.00267 = 0.01733, and exp⁡(−26572⋅0.01733)=e−23.03≈1.0⋅10−10\exp(-\tfrac{2657}{2}\cdot 0.01733) = e^{-23.03} \approx 1.0\cdot10^{-10}.
    5. Lower tail, the same way with e−λQe^{-\lambda Q} and λ=ε2(1−ε)\lambda = \frac{\varepsilon}{2(1-\varepsilon)}: P(Q≤(1−ε)k)≤((1−ε)eε)k/2≤e−kε2/4P(Q \le (1-\varepsilon)k) \le \big((1-\varepsilon)e^{\varepsilon}\big)^{k/2} \le e^{-k\varepsilon^2/4}, using ln⁡(1−ε)≤−ε−ε2/2\ln(1-\varepsilon) \le -\varepsilon - \varepsilon^2/2. This is no larger than the bound of step 4. Numbers: one pair fails with probability at most 2⋅1.0⋅10−10=2.0⋅10−102 \cdot 1.0\cdot10^{-10} = 2.0\cdot10^{-10}.
    6. Union bound. There are fewer than n2/2n^2/2 pairs, each failing with probability at most 2exp⁡(−k2(ε2/2−ε3/3))2\exp(-\tfrac k2(\varepsilon^2/2 - \varepsilon^3/3)). The total is at most n2exp⁡(−k2(ε2/2−ε3/3))n^2\exp(-\tfrac k2(\varepsilon^2/2 - \varepsilon^3/3)), which is ≤δ\le \delta exactly when kk satisfies the bound above. Numbers: 108⋅e−23.03≈0.0100≤δ10^8 \cdot e^{-23.03} \approx 0.0100 \le \delta. ■\blacksquare

    The bound is pessimistic. For k=2657k = 2657, the exact χk2\chi^2_k probability of leaving [0.8k,1.2k][0.8k, 1.2k] is about 3.4⋅10−123.4\cdot10^{-12}, about 60 times smaller than the Chernoff bound of step 5. This is why the histogram in section 2 almost always stays inside the aqua lines.

    Remarks.

    • The map is oblivious: it does not look at the data, unlike PCA.
    • Inner products survive too. By 4⟨u,v⟩=∥u+v∥2−∥u−v∥24\langle u, v\rangle = \|u+v\|^2 - \|u-v\|^2, if ff keeps the squared norms of u±vu \pm v, then ∣⟨f(u),f(v)⟩−⟨u,v⟩∣≤ε|\langle f(u), f(v)\rangle - \langle u, v\rangle| \le \varepsilon for unit u,vu, v.
    • The log⁡n\log n cannot be improved, even by nonlinear maps (Vershynin, Remark 5.3.4), and Larsen and Nelson (2017) showed that ε−2log⁡n\varepsilon^{-2}\log n as a whole is optimal (for ε\varepsilon not extremely small).
    • Random signs (±1\pm1 entries), sparse JL matrices and the fast JL transform (the SRHT) give the same guarantee, up to constants, at lower cost.
    • With an eO(d)e^{O(d)}-point net on the unit sphere of a dd-dimensional subspace, the same proof gives the Gaussian row of the table: k=O((d+log⁡(1/δ))/ε2)k = O((d + \log(1/\delta))/\varepsilon^2).

    The Marchenko–Pastur law #

    Setup. X∈Rn×pX \in \mathbb{R}^{n\times p} has independent entries with mean 0 and variance σ2\sigma^2: nn samples (rows), pp features. The sample covariance is Σ^=X⊤X/n\hat\Sigma = X^\top X/n (p×pp\times p), and the aspect ratio is γ=p/n\gamma = p/n.

    Theorem 4 (Marchenko–Pastur 1967). As n,p→∞n, p \to \infty with p/n→γ∈(0,∞)p/n \to \gamma \in (0, \infty), the histogram of the pp eigenvalues of Σ^\hat\Sigma converges to the density

    fγ(λ)=(λ+−λ)(λ−λ−)2πσ2γλf_\gamma(\lambda) = \frac{\sqrt{(\lambda_+ - \lambda)(\lambda - \lambda_-)}}{2\pi\sigma^2\gamma\lambda}

    on [λ−,λ+][\lambda_-, \lambda_+], where λ±=σ2(1±γ)2\lambda_\pm = \sigma^2(1 \pm \sqrt\gamma)^2. For γ≤1\gamma \le 1 this is the whole law. For γ>1\gamma > 1 this part has mass 1/γ1/\gamma, and a point mass 1−1/γ1 - 1/\gamma sits at 0 (Σ^\hat\Sigma has rank at most n<pn < p).

    What to read from it:

    • The mean of the law is σ2\sigma^2: the trace is right, but the eigenvalues spread around it.
    • The spread is set by γ\gamma alone. For γ→0\gamma \to 0 (n≫pn \gg p) the support shrinks to {σ2}\{\sigma^2\}.
    • Singular values of XX itself fill about [σ(n−p), σ(n+p)][\sigma(\sqrt n - \sqrt p),\, \sigma(\sqrt n + \sqrt p)]. Non-asymptotic version for Gaussian entries: E∥X∥2≤σ(n+p)\mathbb{E}\|X\|_2 \le \sigma(\sqrt n + \sqrt p) (Vershynin, Theorem 7.3.1).
    • With a finite fourth moment, the largest eigenvalue converges to λ+\lambda_+. Its fluctuations are of relative order n−2/3n^{-2/3} (the Tracy–Widom law), so the edge is sharp.
    • Link to lesson 17: for Σ=σ2I\Sigma = \sigma^2 I, ∥Σ^−Σ∥2→σ2(2γ+γ)\|\hat\Sigma - \Sigma\|_2 \to \sigma^2(2\sqrt\gamma + \gamma). Weyl's inequality then lets each sample eigenvalue move by up to this amount.
    Quick check

    You have n=900n = 900 samples of p=100p = 100 independent features, all with variance 1. Where do the eigenvalues of X⊤X/nX^\top X/n lie (approximately)?

    Theorem 5 (spiked covariance, BBP phase transition). Let the rows have covariance σ2(I+θvv⊤)\sigma^2(I + \theta vv^\top), with ∥v∥=1\|v\| = 1 and spike strength θ>0\theta > 0. As above, with γ≤1\gamma \le 1:

    • If θ>γ\theta > \sqrt\gamma: the top sample eigenvalue converges to σ2(1+θ)(1+γ/θ)>λ+\sigma^2(1+\theta)(1+\gamma/\theta) > \lambda_+, and the top sample eigenvector v^\hat v satisfies ⟨v^,v⟩2→1−γ/θ21+γ/θ\langle\hat v, v\rangle^2 \to \dfrac{1 - \gamma/\theta^2}{1 + \gamma/\theta}.
    • If θ≤γ\theta \le \sqrt\gamma: the top eigenvalue sticks to λ+\lambda_+, and ⟨v^,v⟩→0\langle\hat v, v\rangle \to 0.

    Two lessons: a signal weaker than γ\sqrt\gamma is undetectable by PCA, and a detected signal is biased: the sample eigenvalue is too large and the eigenvector is tilted (Davis–Kahan in action). In the figure of section 2, set γ=0.25\gamma = 0.25 and move θ\theta across 0.50.5.

    More on the BBP transition
    • Where the threshold comes from. Write ℓ(θ)=(1+θ)(1+γ/θ)=1+γ+θ+γ/θ\ell(\theta) = (1+\theta)(1+\gamma/\theta) = 1 + \gamma + \theta + \gamma/\theta. Its derivative 1−γ/θ21 - \gamma/\theta^2 is zero at θ=γ\theta = \sqrt\gamma, where ℓ(γ)=1+2γ+γ=(1+γ)2=λ+/σ2\ell(\sqrt\gamma) = 1 + 2\sqrt\gamma + \gamma = (1+\sqrt\gamma)^2 = \lambda_+/\sigma^2. So the formula for the top eigenvalue touches the bulk edge exactly at the threshold. Below it the formula would rise again (for example ℓ(0.4)=2.275\ell(0.4) = 2.275 for γ=0.25\gamma = 0.25), but it no longer describes an eigenvalue: the top eigenvalue stays at the edge.
    • The overlap (1−γ/θ2)/(1+γ/θ)(1 - \gamma/\theta^2)/(1 + \gamma/\theta) is 0 at θ=γ\theta = \sqrt\gamma and tends to 1 as θ→∞\theta \to \infty. Near the threshold the eigenvalue is visible but the eigenvector is almost useless.
    • Sources. Baik, Ben Arous and Péché (2005) for the eigenvalue (complex Gaussian data), Baik and Silverstein (2006) and Paul (2007) for real data and the eigenvector.
    • Finite samples. At finite nn the top noise eigenvalue fluctuates around λ+\lambda_+ on the Tracy–Widom scale n−2/3n^{-2/3}, and the transition is smooth instead of sharp.

    How to decide if a top eigenvalue is signal. A step list for a sample covariance with nn samples and pp features:

    1. Compute γ=p/n\gamma = p/n.
    2. Estimate the noise variance σ2\sigma^2: from data with no signal, or from the bulk (for example the median eigenvalue compared with the median of the MP law).
    3. Compute the bulk edge λ+=σ2(1+γ)2\lambda_+ = \sigma^2(1+\sqrt\gamma)^2.
    4. An eigenvalue clearly above λ+\lambda_+ (with a margin for finite-nn fluctuations) is signal. Everything at or below λ+\lambda_+ is consistent with pure noise.
    5. For a detected eigenvalue λ^\hat\lambda, remove the bias. Set ℓ=λ^/σ2\ell = \hat\lambda/\sigma^2 and solve ℓ=(1+θ)(1+γ/θ)\ell = (1+\theta)(1+\gamma/\theta) for θ\theta: θ=12[(ℓ−1−γ)+(ℓ−1−γ)2−4γ ]\theta = \tfrac12\big[(\ell - 1 - \gamma) + \sqrt{(\ell - 1 - \gamma)^2 - 4\gamma}\,\big]. The true variance in that direction is σ2(1+θ)\sigma^2(1+\theta), not λ^\hat\lambda.
    6. A direction you cannot see may still exist: it is only known that θ≤γ\theta \le \sqrt\gamma. More samples (smaller γ\gamma) lower the threshold.

    Signal plus noise for one data matrix. Let Y=X0+σZY = X_0 + \sigma Z be m×nm\times n with m≤nm \le n, β=m/n\beta = m/n, X0X_0 of low rank and ZZ with independent N(0,1)N(0,1) entries. The noise singular values fill [n σ(1−β),  n σ(1+β)][\sqrt n\,\sigma(1-\sqrt\beta),\; \sqrt n\,\sigma(1+\sqrt\beta)]. A rule to separate:

    1. Estimate σ\sigma (from a noise-only region, or from the median singular value).
    2. Keep the singular values above the bulk edge (1+β)n σ(1+\sqrt\beta)\sqrt n\,\sigma (plus a margin): below it, signal and noise cannot be told apart.
    3. If the goal is the smallest mean squared error of X^0\hat X_0, use the slightly higher Gavish–Donoho threshold τ∗=λ∗(β)n σ\tau^* = \lambda^*(\beta)\sqrt n\,\sigma, with λ∗(1)=4/3≈2.309\lambda^*(1) = 4/\sqrt3 \approx 2.309 for square matrices (2.858 times the median singular value when σ\sigma is unknown). Components just above the edge are real but so noisy that keeping them increases the error.

    For a square matrix and a rank-one signal of size xn σx\sqrt n\,\sigma, the observed top singular value is about n σ (1+x2)/x\sqrt n\,\sigma\,(1+x^2)/x if x>1x > 1, and stays at the edge 2n σ2\sqrt n\,\sigma if x≤1x \le 1.

    High-dimensional geometry and superposition #

    • Norms concentrate. For g∼N(0,ID)g \sim N(0, I_D), ∥g∥=D+O(1)\|g\| = \sqrt D + O(1) (Vershynin §3.1): random vectors live near a sphere.
    • Random directions are almost orthogonal. For independent uniform unit vectors u,v∈Rdu, v \in \mathbb{R}^d, ⟨u,v⟩\langle u, v\rangle has mean 0 and variance 1/d1/d, and P(∣⟨u,v⟩∣≥t)≤2e−dt2/2P(|\langle u, v\rangle| \ge t) \le 2e^{-dt^2/2}. The union bound of the JL proof then gives N≈edε2/4N \approx e^{d\varepsilon^2/4} directions with all ∣cos⁡∣≤ε\lvert\cos\rvert \le \varepsilon; equivalently, NN random directions have max⁡∣cos⁡∣≲4ln⁡N/d\max\lvert\cos\rvert \lesssim \sqrt{4\ln N/d}. In d=1000d = 1000 you get about 20,000 directions with ∣cos⁡∣≤0.2\lvert\cos\rvert \le 0.2, but only 1000 exactly orthogonal ones.
    • Superposition (Elhage et al. 2022): a network can represent more features than it has dimensions by giving each feature an almost-orthogonal direction. The price is interference ⟨wi,wj⟩≠0\langle w_i, w_j\rangle \ne 0. It pays off when features are sparse (rarely active together), and a nonlinearity such as ReLU filters out the small interference. A consequence: features need not align with single neurons. This is why interpretability work uses sparse autoencoders to find them.
    Interactive figure: loading…

    Raise the dimension. Left: the norms of 300 random Gaussian vectors g∼N(0,Id)g \sim N(0, I_d), divided by d\sqrt d. Right: the angles between the 4950 pairs among 100 of them. The violet curves are the exact densities. As dd grows, ∥g∥\|g\| stays at d\sqrt d with a standard deviation of about 0.710.71, whatever dd is, so ∥g∥/d\|g\|/\sqrt d piles up at 1. The angles pile up at 90°90° with a spread of about 57.3°/d57.3°/\sqrt d. In d=2d = 2 every angle is equally likely; in d=2000d = 2000 no pair is more than a few degrees from orthogonal.

    Quick check

    You draw many pairs of random directions in R10 000\mathbb{R}^{10\,000} (for example two token embeddings at initialization). What do the angles between them look like?

    Answers to section 1. (1) No. kk depends only on nn and ε\varepsilon: with ε=0.2\varepsilon = 0.2, k≥4ln⁡(104)/(0.02−0.00267)≈2126k \ge 4\ln(10^4)/(0.02 - 0.00267) \approx 2126 (about 2,660 for a 99% guarantee), the same for D=106D = 10^6 or D=1012D = 10^{12}. (2) γ=0.25\gamma = 0.25, so about [(1−0.5)2,(1+0.5)2]=[0.25,2.25][(1-0.5)^2, (1+0.5)^2] = [0.25, 2.25]: a factor 9 between the smallest and the largest, although all true eigenvalues are 1.

    4. Worked example #

    (a) JL dimension. n=104n = 10^4 points, ε=0.2\varepsilon = 0.2. Then ε2/2−ε3/3=0.02−0.002667=0.017333\varepsilon^2/2 - \varepsilon^3/3 = 0.02 - 0.002667 = 0.017333 and 4ln⁡(104)=36.844\ln(10^4) = 36.84, so k≥36.84/0.017333=2125.5k \ge 36.84/0.017333 = 2125.5: k=2126k = 2126. This is the bound without the δ\delta term (the form scikit-learn uses). With the exact number n(n−1)/2n(n-1)/2 of pairs it still guarantees success with probability at least 1/n1/n, and in practice the union bound is very pessimistic. For a real guarantee, δ=0.01\delta = 0.01 adds 2ln⁡100=9.212\ln 100 = 9.21 to the numerator: k≥2657k \ge 2657. With ε=0.1\varepsilon = 0.1 and n=106n = 10^6: k≥11,842k \ge 11{,}842. The ε−2\varepsilon^{-2} is expensive; the log⁡n\log n is cheap.

    (b) A tiny CountSketch. Fit a constant: A=(1,1,1,1)⊤A = (1,1,1,1)^\top, b=(1,2,3,6)⊤b = (1,2,3,6)^\top. The exact answer is the mean, x∗=3x^* = 3, with ∥r∗∥2=4+1+0+9=14\|r^*\|^2 = 4 + 1 + 0 + 9 = 14. Hash rows 1, 3 to bucket 1 and rows 2, 4 to bucket 2, with signs (+,+,−,+)(+, +, -, +):

    S=(10−100101),S = \begin{pmatrix} 1 & 0 & -1 & 0 \\ 0 & 1 & 0 & 1\end{pmatrix},
    SA=(02),Sb=(−28).SA = \begin{pmatrix}0\\2\end{pmatrix},\quad Sb = \begin{pmatrix}-2\\8\end{pmatrix}.

    The sketched problem min⁡x (0⋅x+2)2+(2x−8)2\min_x\,(0\cdot x + 2)^2 + (2x - 8)^2 gives x~=4\tilde x = 4, and ∥Ax~−b∥2=9+4+1+4=18\|A\tilde x - b\|^2 = 9 + 4 + 1 + 4 = 18: the residual is 18/14=1.13\sqrt{18/14} = 1.13 times the optimum. This SS distorts range⁡([A  b])\operatorname{range}([A\;b]) by ε=0.79\varepsilon = 0.79, so Theorem 1 only promises 1.79/0.21=2.9\sqrt{1.79/0.21} = 2.9. Two buckets for a 2-dimensional subspace is far too few; real sketches use k≫dk \gg d.

    (c) Noise or signal? n=1000n = 1000 samples, p=250p = 250 features, σ2=1\sigma^2 = 1, so γ=0.25\gamma = 0.25 and γ=0.5\sqrt\gamma = 0.5.

    • Noise bulk: eigenvalues of Σ^\hat\Sigma in [0.25,2.25][0.25, 2.25]; singular values of XX in 1000 [0.5,1.5]=[15.8,47.4]\sqrt{1000}\,[0.5, 1.5] = [15.8, 47.4].
    • Spike θ=3\theta = 3 (true variance 4 along vv): 3>0.53 > 0.5, so it is detected. The top sample eigenvalue is about 4(1+0.25/3)=4.334(1 + 0.25/3) = 4.33, not 4, and ⟨v^,v⟩2≈(1−0.25/9)/(1+0.25/3)=0.897\langle\hat v, v\rangle^2 \approx (1 - 0.25/9)/(1 + 0.25/3) = 0.897, an angle of about 19°. Going back (step 5 of the step list): ℓ=4.33\ell = 4.33, ℓ−1−γ=3.083\ell - 1 - \gamma = 3.083, and θ=12(3.083+3.0832−1)=12(3.083+2.917)=3\theta = \tfrac12\big(3.083 + \sqrt{3.083^2 - 1}\big) = \tfrac12(3.083 + 2.917) = 3. ✓
    • Spike θ=0.4\theta = 0.4 (true variance 1.4): 0.4<0.50.4 < 0.5, so it is invisible. The top eigenvalue stays at about 2.25, and v^\hat v is unrelated to vv.
    • So with 1000 samples, a direction with 40% more variance than the others cannot be found by PCA at all. Four times more samples (γ=0.0625\gamma = 0.0625, threshold 0.25) would reveal it.
    Quick check

    A data set has n=800n = 800 samples of p=200p = 200 features with noise variance 1. One direction has extra variance θ\theta (covariance I+θvv⊤I + \theta vv^\top). For which θ\theta can PCA detect it, for large nn and pp?

    5. In code #

    NumPy, SciPy and scikit-learn check the claims of sections 3 and 4. The notebook for this lesson has these cells (at smaller sizes) and the exercises.

    import numpy as np
    from scipy.spatial.distance import pdist
    from scipy.sparse.linalg import LinearOperator, lsqr
    from scipy.linalg import solve_triangular
    from sklearn.random_projection import GaussianRandomProjection, johnson_lindenstrauss_min_dim
    
    rng = np.random.default_rng(0)
    
    # 1) JL: 500 points in D = 10_000 dimensions, eps = 0.25.
    n, D, eps = 500, 10_000, 0.25
    k = johnson_lindenstrauss_min_dim(n, eps=eps)     # the bound of Theorem 3 with delta = 1
    X = rng.standard_normal((n, D))
    Y = GaussianRandomProjection(n_components=k, random_state=0).fit_transform(X)
    ratio = pdist(Y, "sqeuclidean") / pdist(X, "sqeuclidean")
    print(k, ratio.min().round(3), ratio.max().round(3))   # 954 0.814 1.246: inside [0.75, 1.25]
    
    # 2) Sketch-and-solve vs sketch-and-precondition, kappa(A) = 1e6.
    m, d, ks = 20_000, 50, 500
    A = rng.standard_normal((m, d)) @ np.diag(np.logspace(0, -6, d))
    b = A @ rng.standard_normal(d) + 1e-3 * rng.standard_normal(m)
    x_opt = np.linalg.lstsq(A, b, rcond=None)[0]
    S = rng.standard_normal((ks, m)) / np.sqrt(ks)          # dense Gaussian sketch
    x_sk = np.linalg.lstsq(S @ A, S @ b, rcond=None)[0]
    res = lambda x: np.linalg.norm(A @ x - b)
    rel = lambda x: np.linalg.norm(x - x_opt) / np.linalg.norm(x_opt)
    print(res(x_sk) / res(x_opt), rel(x_sk))   # 1.044 (theory: E ratio^2 = 1 + d/(k-d-1) = 1.11), 1.73
    R = np.linalg.qr(S @ A, mode="r")
    AR = LinearOperator((m, d), matvec=lambda y: A @ solve_triangular(R, y),
                        rmatvec=lambda z: solve_triangular(R, A.T @ z, trans="T"))
    print(np.linalg.cond(A), np.linalg.cond(solve_triangular(R, A.T, trans="T").T))  # 1e6 -> 1.9
    y, istop, iters = lsqr(AR, b, atol=1e-14, btol=1e-14, iter_lim=500)[:3]
    print(iters, rel(solve_triangular(R, y)))                   # 28 iterations, error ~3e-12
    print(lsqr(A, b, atol=1e-14, btol=1e-14, iter_lim=500)[2])  # 500: no preconditioner, no convergence
    
    # 3) Marchenko-Pastur: pure noise, then one spike of strength theta.
    n, p = 1000, 250
    gamma = p / n
    Z = rng.standard_normal((n, p))
    ev = np.linalg.eigvalsh(Z.T @ Z / n)
    print(ev.min().round(3), ev.max().round(3), (1 - gamma**0.5)**2, (1 + gamma**0.5)**2)
    v = np.zeros(p); v[0] = 1.0
    for theta in [3.0, 0.4]:
        Xs = Z + (np.sqrt(1 + theta) - 1) * np.outer(Z @ v, v)    # covariance I + theta v v^T
        w, V = np.linalg.eigh(Xs.T @ Xs / n)
        print(theta, w[-1].round(3), (V[:, -1] @ v)**2)   # 3.0: ~4.4, ~0.91;  0.4: ~2.24, ~0
    
    # 4) Rank-3 signal + noise (500 x 500, sigma = 1): which singular values are signal?
    N = 500
    U0 = np.linalg.qr(rng.standard_normal((N, 3)))[0]
    V0 = np.linalg.qr(rng.standard_normal((N, 3)))[0]
    Yn = U0 @ np.diag([60.0, 30.0, 18.0]) @ V0.T + rng.standard_normal((N, N))
    s = np.linalg.svd(Yn, compute_uv=False)
    print(s[:5].round(1), round(2 * N**0.5, 1), round(4 / 3**0.5 * N**0.5, 1))
    # [67.1 47.2 44.  43.7 43.4]  edge 44.7  Gavish-Donoho 51.6
    
    # 5) Near-orthogonality: 4000 random unit vectors in R^1000.
    W = rng.standard_normal((4000, 1000))
    W /= np.linalg.norm(W, axis=1, keepdims=True)
    G = W @ W.T
    np.fill_diagonal(G, 0)
    print(np.abs(G).max().round(3), np.sqrt(4 * np.log(4000) / 1000).round(3))  # 0.157 <= 0.182
    

    Part 2: the residual is near-optimal but xx is off by 170%, because κ(A)=106\kappa(A) = 10^6; the same sketch as a preconditioner gives full accuracy. Part 4: the signal singular values 60,30,1860, 30, 18 are 2.68,1.34,0.802.68, 1.34, 0.80 in units of Nσ=22.4\sqrt N\sigma = 22.4, so theory predicts observed values 68.368.3, 46.746.7 and "at the edge" (0.80<10.80 < 1). The bulk-edge rule keeps two components, Gavish–Donoho keeps one, and the third is undetectable.

    6. Where it shows up #

    • Large least squares and low-rank approximation. Sketch-and-precondition solvers (Blendenpik, LSRN) for n≫dn \gg d; the randomized SVD (lesson 09) sketches the range with AΩA\Omega; streaming PCA keeps only a sketch.
    • Embeddings and gradients. Random projection before nearest-neighbor search; TRAK (Park et al. 2023) compresses per-example gradients with a JL projection for data attribution.
    • Ensemble data assimilation. With N=50N = 50 members for a state of size 10610^6, the sample covariance has rank at most 49 and spurious long-range correlations. Localization, inflation and shrinkage (Ledoit–Wolf) are the fixes.
    • Choosing the number of EOFs or POD modes. Compare the singular values with the noise bulk edge, or use the Gavish–Donoho threshold, instead of an "elbow" by eye.
    • Neural networks. For a random initial weight matrix WW, the eigenvalues of W⊤WW^\top W (the squared singular values) follow the MP law; trained layers show outliers above the bulk and heavy tails, which carry learned structure (Martin and Mahoney, JMLR 2021). Superposition motivates sparse autoencoders in interpretability.

    7. Common mistakes #

    • Using sketch-and-solve when you need xx. It controls the residual only. For an accurate xx, sketch-and-precondition.
    • A dense Gaussian sketch on dense data. Forming SASA costs O(knd)O(knd), more than the O(nd2)O(nd^2) of Householder QR when k>dk > d. Use SRHT or CountSketch for speed; Gaussian is for analysis and for small problems.
    • Forgetting the 1/k1/\sqrt k scaling, or applying the JL bound for nn points to a whole subspace (infinitely many points): a subspace needs k∝dk \propto d.
    • Believing kk is small for small ε\varepsilon. ε=0.01\varepsilon = 0.01 costs ≈8⋅104ln⁡n\approx 8\cdot10^4\ln n dimensions. JL helps when DD is huge; for 768-dimensional embeddings, PCA or quantization is often better.
    • Reading sample eigenvalues as population eigenvalues when p/np/n is not small, or mixing up γ=p/n\gamma = p/n with n/pn/p.
    • Treating a value just above λ+\lambda_+ as certain signal. Finite-nn (Tracy–Widom) fluctuations, a wrong σ\sigma, or correlated noise move the edge.

    8. Check yourself #

    1. Use the bound of Theorem 3 for n=106n = 10^6 points and ε=0.1\varepsilon = 0.1 (δ\delta not counted). Does the answer change for D=104D = 10^4?
      Answerk≥4ln⁡(106)/(0.005−0.000333)=55.26/0.004667≈11,842k \ge 4\ln(10^6)/(0.005 - 0.000333) = 55.26/0.004667 \approx 11{,}842. It does not depend on DD, so for D=104D = 10^4 JL gives no reduction at all: use a larger ε\varepsilon, or PCA.
    2. A sketch has ∥I−U⊤S⊤SU∥2=0.2\|I - U^\top S^\top SU\|_2 = 0.2 on range⁡([A  b])\operatorname{range}([A\;b]). Bound the sketch-and-solve residual, and κ(AR−1)\kappa(AR^{-1}) for SA=QRSA = QR.
      AnswerBoth by 1.2/0.8=1.5≈1.225\sqrt{1.2/0.8} = \sqrt{1.5} \approx 1.225: the residual is at most 22.5% above the optimum, and κ(AR−1)≤1.225\kappa(AR^{-1}) \le 1.225.
    3. Why does the JL dimension grow like log⁡n\log n and not like nn?
      AnswerOne distance fails with probability ≤2e−ckε2\le 2e^{-ck\varepsilon^2} (exponential tail of χk2\chi^2_k). A union bound over fewer than n2/2n^2/2 pairs multiplies this by n2n^2, which is absorbed when ckε2≥2ln⁡n+ln⁡(1/δ)ck\varepsilon^2 \ge 2\ln n + \ln(1/\delta).
    4. n=2000n = 2000 samples, p=500p = 500 features, noise variance 1. Where is the noise bulk of X⊤X/nX^\top X/n? Is a spike θ=1\theta = 1 visible? Give the predicted top eigenvalue and squared overlap.
      Answerγ=0.25\gamma = 0.25: bulk [0.25,2.25][0.25, 2.25]. θ=1>γ=0.5\theta = 1 > \sqrt\gamma = 0.5, so yes: top eigenvalue →2(1+0.25)=2.5\to 2(1 + 0.25) = 2.5, and ⟨v^,v⟩2→(1−0.25)/(1+0.25)=0.6\langle\hat v, v\rangle^2 \to (1 - 0.25)/(1 + 0.25) = 0.6.
    5. Y=X0+σZY = X_0 + \sigma Z is 1000×10001000\times1000 with σ=0.1\sigma = 0.1. Give the bulk edge and the Gavish–Donoho threshold. Do you keep an observed singular value of 7.0?
      AnswerEdge 21000⋅0.1=6.322\sqrt{1000}\cdot0.1 = 6.32; τ∗=2.309⋅1000⋅0.1=7.30\tau^* = 2.309\cdot\sqrt{1000}\cdot0.1 = 7.30. The value 7.0 is clearly above the edge, so it is almost surely real signal, but it is below τ∗\tau^*: for the smallest mean squared error, drop it (its singular vectors are too noisy to help).
    6. How can a 1000-dimensional residual stream hold 10510^5 features with little interference?
      AnswerRandom directions are almost orthogonal: 10510^5 random unit vectors in R1000\mathbb{R}^{1000} have max⁡∣cos⁡∣≲4ln⁡105/1000≈0.21\max\lvert\cos\rvert \lesssim \sqrt{4\ln 10^5/1000} \approx 0.21. If the features are sparse, few are active at once, so the interference stays small, and a nonlinearity can remove it.
    Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
    1. Define an ε\varepsilon-subspace embedding SS for the subspace with orthonormal basis UU.

    2. What does sketch-and-solve least squares guarantee, and what does it not guarantee?

    3. What is the idea of sketch-and-precondition?

    4. State the Johnson–Lindenstrauss lemma (the size of kk).

    5. Why does the JL dimension grow like log⁡n\log n?

    6. Pure noise, variance σ2\sigma^2, γ=p/n≤1\gamma = p/n \le 1: where are the eigenvalues of X⊤X/nX^\top X/n?

    7. When can PCA detect a spike θ\theta (covariance σ2(I+θvv⊤)\sigma^2(I + \theta vv^\top)), and where is the top eigenvalue then?

    8. How long is a random Gaussian vector g∼N(0,Id)g \sim N(0, I_d)?

    9. Compute the typical spread of the angle between two random directions in R10 000\mathbb{R}^{10\,000}.

    10. Rank the Gaussian, SRHT and CountSketch by the cost of computing SASA for A∈Rn×dA \in \mathbb{R}^{n\times d}.

    Open the review →

    9. Interview angle #

    • "State and prove JL." — χk2\chi^2_k concentration for one vector by Chernoff, then a union bound over pairs: k=O(ε−2log⁡n)k = O(\varepsilon^{-2}\log n), independent of DD.
    • "How do you solve least squares with 10910^9 rows and 100 columns?" — Sketch (SRHT or CountSketch) to a few times dd rows, QR of SASA as a preconditioner, then LSQR: about O(nnz⁡(A)log⁡(1/tol))O(\operatorname{nnz}(A)\log(1/\text{tol})) work plus the small QR, and full accuracy.
    • "Random projection or PCA?" — JL is oblivious, cheap and keeps every pairwise distance up to 1±ε1\pm\varepsilon, with kk set by nn. PCA adapts to the data and is optimal on average for rank kk, but gives no worst-case guarantee for single pairs.
    • "Is this eigenvalue of my covariance signal?" — Compare with the MP edge σ2(1+p/n)2\sigma^2(1+\sqrt{p/n})^2; spikes below p/n\sqrt{p/n} are undetectable, and detected ones are biased upward.

    10. Practice #

    The notebook for this lesson runs in Colab. It has five exercises, each with a check cell and a folded solution:

    1. Write jl_min_dim(n, eps, delta) from Theorem 3 and compare it with scikit-learn.
    2. Write a Gaussian random projection and measure the distortion of all pairwise distances against 2/k\sqrt{2/k} and the JL ε\varepsilon.
    3. Sketch-and-solve least squares: check the residual factor of Theorem 1 and see how far x~\tilde x is from x∗x^* for an ill-conditioned AA.
    4. Sketch-and-precondition: build RR from the QR of SASA, check κ(AR−1)\kappa(AR^{-1}), and solve to full accuracy with LSQR.
    5. Signal or noise: compute the Marchenko–Pastur edges and the BBP prediction, and count the singular values of a noisy low-rank matrix above the bulk edge.

    11. Watch and read #

    Follow the proof: one vector, the chi-squared tail, then the union bound over pairs (Theorem 3).
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    Watch the noise bulk appear and a spike leave it; compare with the figure in section 2.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    A full lecture on Theorem 4: where the Marchenko–Pastur density and its edges come from.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    Sketching for least squares and low rank, the topic of Theorems 1 and 2.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    From 17:04, almost-orthogonal directions in high dimension and superposition.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    • Roman Vershynin, High-Dimensional Probability, 2nd ed. (free PDF): §3.1 (concentration of the norm), §5.3 (Theorem 5.3.1, the JL lemma, and Remark 5.3.4 on optimality), §4.6 and §7.3 (Theorems 4.6.1 and 7.3.1, singular values of random matrices).
    • David Woodruff, Sketching as a Tool for Numerical Linear Algebra (2014): §2.1 (Definitions 1–2, Theorems 6, 7, 9), §2.5 (Theorem 21, regression) and §2.6 (sketch-and-precondition).
    • Gavish and Donoho, The Optimal Hard Threshold for Singular Values is 4/√3 (IEEE Trans. Inf. Theory 2014): §I (the bulk edge and the threshold), Eq. (11) and Table I for λ∗(β)\lambda^*(\beta).
    • Elhage et al., Toy Models of Superposition (Anthropic, 2022): "Definitions and Motivation" (the superposition hypothesis) and "Demonstrating Superposition".