ML Foundations Lecture 11 of 14
Part III — Linear Algebra · Lecture 11

Eigenvalues, Eigenvectors, and Quadratic Forms

Some directions survive a linear map unchanged except for scaling. Finding them turns a matrix into a list of numbers, turns curvature into geometry, and explains — precisely — why gradient descent zig-zags, why learning rates have a ceiling, and what PCA is doing.

By the end you can

  • Define eigenvalues and eigenvectors and compute them for \(2\times2\) matrices by hand
  • State the spectral theorem for symmetric matrices and use the diagonalisation \(\mA=\mQ\Lambda\mQ^\top\)
  • Classify a quadratic form as positive definite, semi-definite or indefinite
  • Derive the condition-number bound on gradient-descent convergence
  • Explain PCA as the eigendecomposition of a covariance matrix

Assumed

  • Lectures 8–10: vectors, matrices as maps, projection, least squares
  • Lecture 7: convexity and the second-derivative test

1Directions a matrix does not turn

A matrix rotates, stretches and shears the plane, and most vectors come out pointing somewhere new. But some directions are special: the map only lengthens or shortens them, leaving them on the same line.

Definition — eigenvalue, eigenvector

For a square \(\mA\in\RR^{d\times d}\), a nonzero vector \(\vv\) is an eigenvector with eigenvalue \(\lambda\in\RR\) if

\[ \mA\vv = \lambda\vv. \]

The requirement \(\vv\neq\vzero\) matters: \(\mA\vzero=\lambda\vzero\) holds for every \(\lambda\) and would make the definition vacuous. Note that if \(\vv\) is an eigenvector so is \(c\vv\) for any \(c\neq0\) — it is really the direction, not the vector, that is the object of interest.

Hunting for eigenvectors: rotate v and watch when Av stays on the same lineInteractive
vAveigen-directions
20.0
2.00
1.00
1.00
2.00
Sweep the angle. For most directions the orange Av points somewhere else entirely. At the eigen-directions (drawn faintly in green) the two arrows lie on one line and the readout reports the ratio — that ratio is the eigenvalue. With the default symmetric matrix the two eigen-directions are exactly 90° apart, which is the spectral theorem you will meet below. Make the matrix asymmetric (A₁₂ ≠ A₂₁) and that perpendicularity is lost.

2Computing them

The characteristic equation0 / 5
\[\mA\vv = \lambda\vv \quad\Longleftrightarrow\quad \mA\vv - \lambda\vv = \vzero\]
Move everything to one side. Careful: \(\mA-\lambda\) is not a legal expression — you cannot subtract a scalar from a matrix. The repair is the next step.
\[\mA\vv - \lambda\mI\vv = \big(\mA-\lambda\mI\big)\vv = \vzero\]
Insert the identity. \(\lambda\vv = \lambda\mI\vv\), which is legal and lets us factor. Now \(\mA-\lambda\mI\) is a genuine matrix: subtract \(\lambda\) from each diagonal entry.
\[\vv \neq \vzero \text{ and } (\mA-\lambda\mI)\vv=\vzero \quad\Longrightarrow\quad \mathcal{N}(\mA-\lambda\mI) \neq \{\vzero\}\]
Reformulate as a null-space condition. We need a nonzero vector killed by \(\mA-\lambda\mI\). By Lecture 9, that means the matrix is not injective, hence not invertible, hence singular.
\[\boxed{\;\det\big(\mA-\lambda\mI\big) = 0\;} \qquad\textbf{(characteristic equation)}\]
Singular means zero determinant. Expanding this determinant gives a degree-\(d\) polynomial in \(\lambda\), the characteristic polynomial, and its roots are exactly the eigenvalues. A degree-\(d\) polynomial has \(d\) roots over \(\mathbb{C}\), counted with multiplicity — so a \(d\times d\) matrix has \(d\) eigenvalues, though some may be complex or repeated.
\[d=2:\quad \det\begin{bmatrix}a-\lambda & b\\ c & d-\lambda\end{bmatrix} = \lambda^2 - \underbrace{(a+d)}_{\tr\mA}\lambda + \underbrace{(ad-bc)}_{\det\mA} = 0\]
The 2×2 case, worth memorising. Expanding: \((a-\lambda)(d-\lambda)-bc\). The two coefficients are the trace and the determinant, which gives the pair of identities \(\lambda_1+\lambda_2=\tr\mA\) and \(\lambda_1\lambda_2=\det\mA\). These hold in every dimension and are the fastest sanity check available on any eigenvalue computation.
Work one by hand

Find the eigenvalues and eigenvectors of \(\mA=\begin{bmatrix}2&1\\1&2\end{bmatrix}\).

Then check your answer against \(\tr\mA\) and \(\det\mA\), and note whether the eigenvectors are perpendicular.

Characteristic equation. \(\tr\mA=4\), \(\det\mA=4-1=3\), so

\[\lambda^2-4\lambda+3=0 \;\Longrightarrow\; (\lambda-3)(\lambda-1)=0 \;\Longrightarrow\; \lambda_1=3,\ \lambda_2=1.\]

Check: \(3+1=4=\tr\mA\) ✓, \(3\times1=3=\det\mA\) ✓.

Eigenvector for \(\lambda=3\). Solve \((\mA-3\mI)\vv=\vzero\):

\[\begin{bmatrix}-1&1\\1&-1\end{bmatrix}\begin{bmatrix}v_1\\v_2\end{bmatrix}=\vzero \;\Longrightarrow\; -v_1+v_2=0 \;\Longrightarrow\; v_1=v_2.\]

So \(\vv_1=(1,1)\), or normalised \(\frac{1}{\sqrt2}(1,1)\). Note the two rows gave the same equation — they must, because the matrix is singular by construction. If they had given independent equations you would have made an arithmetic error.

Eigenvector for \(\lambda=1\). \((\mA-\mI)\vv=\vzero\):

\[\begin{bmatrix}1&1\\1&1\end{bmatrix}\vv=\vzero \;\Longrightarrow\; v_1+v_2=0 \;\Longrightarrow\; \vv_2=(1,-1).\]

Perpendicular? \(\vv_1\cdot\vv_2 = 1(1)+1(-1)=0\) ✓. They are orthogonal, and this is not a coincidence — \(\mA\) is symmetric, and the spectral theorem below says symmetric matrices always have orthogonal eigenvectors.

Verify: \(\mA(1,1)^\top = (2+1, 1+2)^\top = (3,3)^\top = 3(1,1)^\top\) ✓, and \(\mA(1,-1)^\top = (2-1, 1-2)^\top = (1,-1)^\top = 1\cdot(1,-1)^\top\) ✓.

3The spectral theorem

Theorem — spectral theorem for symmetric matrices

Let \(\mA\in\RR^{d\times d}\) be symmetric (\(\mA^\top=\mA\)). Then:

  1. All \(d\) eigenvalues of \(\mA\) are real.
  2. Eigenvectors for distinct eigenvalues are orthogonal, and one can always choose a full orthonormal basis \(\vq_1,\dots,\vq_d\) of eigenvectors.
  3. Consequently \(\mA\) diagonalises orthogonally:
\[ \mA = \mQ\,\Lambda\,\mQ^\top, \qquad \mQ = [\vq_1|\cdots|\vq_d] \text{ with } \mQ^\top\mQ = \mI, \qquad \Lambda = \operatorname{diag}(\lambda_1,\dots,\lambda_d). \]

Let me prove the orthogonality part, because it is three lines and it shows exactly where symmetry is used.

Why symmetric matrices have orthogonal eigenvectors0 / 4
\[\mA\vu = \alpha\vu, \qquad \mA\vv = \beta\vv, \qquad \alpha \neq \beta\]
Two eigenpairs with different eigenvalues. We want to show \(\vu\cdot\vv=0\).
\[\vv^\top \mA\vu = \vv^\top(\alpha\vu) = \alpha\,(\vv^\top\vu)\]
Compute the scalar \(\vv^\top\mA\vu\) one way. Substitute the first eigen-equation and pull out the scalar.
\[\vv^\top\mA\vu = (\mA^\top\vv)^\top\vu = (\mA\vv)^\top\vu = (\beta\vv)^\top\vu = \beta\,(\vv^\top\vu)\]
Compute the same scalar the other way. The second equality is the only place symmetry is used: \(\mA^\top=\mA\). Without it the argument collapses, which is why non-symmetric matrices need not have orthogonal eigenvectors.
\[\alpha(\vv^\top\vu) = \beta(\vv^\top\vu) \;\Longrightarrow\; (\alpha-\beta)(\vv^\top\vu)=0 \;\Longrightarrow\; \vv^\top\vu = 0\]
Conclude. The two computations give the same number. Since \(\alpha\neq\beta\) the first factor is nonzero, so the second must vanish. ∎
Intuition

Read \(\mA=\mQ\Lambda\mQ^\top\) right to left as a three-step recipe for what a symmetric matrix does: \(\mQ^\top\) rotates you into the eigenbasis; \(\Lambda\) stretches each of those axes independently by \(\lambda_i\); \(\mQ\) rotates back. Every symmetric matrix is a pure stretch, seen in a tilted coordinate system. That single sentence is most of what you need from this lecture.

4Quadratic forms

Definition — quadratic form and definiteness

For symmetric \(\mA\), the quadratic form is the scalar function \(q(\vx)=\vx^\top\mA\vx\). We call \(\mA\):

The spectral theorem turns definiteness into a statement about eigenvalues, and the proof is a change of variables:

\[\vx^\top\mA\vx = \vx^\top\mQ\Lambda\mQ^\top\vx = (\mQ^\top\vx)^\top\Lambda(\mQ^\top\vx) = \vz^\top\Lambda\vz = \sum_{i=1}^{d}\lambda_i z_i^2, \qquad \vz := \mQ^\top\vx.\]

Since \(\mQ\) is invertible, \(\vz\) ranges over all of \(\RR^d\) as \(\vx\) does, and \(\vz=\vzero\) only when \(\vx=\vzero\). Each \(z_i^2\ge0\). So the sign of \(q\) is entirely controlled by the signs of the eigenvalues:

Level sets of q(x) = xᵀAx — ellipses aligned to the eigenvectorsInteractive
eigenvector q₁eigenvector q₂level sets of qgradient descent path
6.00
1.00
30
0.150
12
Level sets of a quadratic form are ellipses whose axes are the eigenvectors and whose half-lengths go like 1/√λ. A large λ ratio means a long thin valley, and gradient descent bounces across it instead of running along it — the classic zig-zag. Set λ₁ = λ₂ and the ellipses become circles and descent goes straight to the centre in one step. Make one λ negative and the level sets become hyperbolas: a saddle, where descent escapes along the negative direction and never converges.

5Condition number and the speed of gradient descent

Now we can finish the story begun in Lecture 6. Minimise \(f(\vx)=\tfrac12\vx^\top\mA\vx\) with \(\mA\succ0\). The gradient is \(\grad f = \mA\vx\), so

\[\vx_{t+1} = \vx_t - \eta\mA\vx_t = (\mI-\eta\mA)\,\vx_t.\]

Write \(\vx_t\) in the eigenbasis, \(\vx_t = \sum_i z_i^{(t)}\vq_i\). Because \((\mI-\eta\mA)\vq_i = (1-\eta\lambda_i)\vq_i\), the coordinates decouple completely:

\[z_i^{(t)} = (1-\eta\lambda_i)^{t}\,z_i^{(0)}.\]

So gradient descent on a quadratic is \(d\) independent one-dimensional problems, each exactly the scalar recursion from Lecture 6. Everything follows:

Where this shows up in ML

Condition number is the hidden variable behind a great deal of practical advice:

Check yourself
Why is \(\mX^\top\mX\) always positive semi-definite, and when is it positive definite?
Correct. Correct on both counts. The identity comes from \((\mX\vv)^\top(\mX\vv)=\|\mX\vv\|^2\), and a squared norm is never negative — so semi-definiteness holds for any \(\mX\) whatsoever.

For strict positivity we need \(\|\mX\vv\|^2>0\) whenever \(\vv\neq\vzero\), i.e. \(\mX\vv\neq\vzero\), i.e. \(\mathcal{N}(\mX)=\{\vzero\}\), i.e. the columns are independent, i.e. \(\rank(\mX)=d\).

Both facts have consequences you have already used: semi-definiteness makes the least-squares objective convex (Lecture 10), and definiteness makes it strictly convex with a unique minimiser. And it explains the ridge fix: \(\vv^\top(\mX^\top\mX+\lambda\mI)\vv = \|\mX\vv\|^2+\lambda\|\vv\|^2 \ge \lambda\|\vv\|^2>0\).
Not quite. Symmetry gives you real eigenvalues, not positive ones. A symmetric matrix can easily be indefinite: \(\begin{bmatrix}1&0\\0&-1\end{bmatrix}\) is symmetric with eigenvalues \(+1\) and \(-1\). Semi-definiteness of \(\mX^\top\mX\) comes from its Gram structure, not from symmetry alone.
Not quite. They need not be. Take \(\mX=\begin{bmatrix}1&-1\end{bmatrix}\); then \(\mX^\top\mX=\begin{bmatrix}1&-1\\-1&1\end{bmatrix}\), which has a negative entry and is nevertheless positive semi-definite (eigenvalues 2 and 0). Entrywise sign and definiteness are unrelated properties — a common confusion worth eliminating now.

6PCA in one paragraph

Centre your data so each feature has mean zero, and form the covariance matrix \(\mS = \frac1n\mX^\top\mX \in \RR^{d\times d}\). It is symmetric and positive semi-definite, so the spectral theorem applies: \(\mS = \mQ\Lambda\mQ^\top\). For a unit direction \(\vu\), the variance of the data projected onto \(\vu\) is

\[\Var\big(\mX\vu\big) = \frac1n\|\mX\vu\|^2 = \vu^\top\mS\vu = \sum_i \lambda_i (\vq_i^\top\vu)^2.\]

Maximising this over unit \(\vu\) puts all the weight on the largest \(\lambda\): the answer is \(\vu=\vq_1\), with value \(\lambda_1\). The top eigenvector of the covariance matrix is the direction of maximum variance, and its eigenvalue is that variance. Keeping the top \(k\) eigenvectors is PCA; the fraction of variance retained is \(\sum_{i\le k}\lambda_i \big/ \sum_i\lambda_i\). We do this properly in Phase B, but the mathematics is already complete.

Eigenvalues, diagonalisation, conditioning and PCA in NumPyPython · NumPy

7Exercises

★

Find eigenvalues and eigenvectors by hand, and classify each matrix as positive definite, negative definite, indefinite, or semi-definite:

(a) \(\begin{bmatrix}3&0\\0&5\end{bmatrix}\)    (b) \(\begin{bmatrix}4&2\\2&1\end{bmatrix}\)    (c) \(\begin{bmatrix}0&1\\1&0\end{bmatrix}\)

(a) Diagonal, so the eigenvalues are the diagonal entries: \(3,5\), with eigenvectors \(\ve_1,\ve_2\). Both positive → positive definite.
(For any diagonal matrix, \(\mD\ve_i = d_{ii}\ve_i\) directly from the definition — no characteristic polynomial needed.)

(b) \(\tr=5\), \(\det = 4-4=0\). So \(\lambda^2-5\lambda+0=0\), giving \(\lambda(\lambda-5)=0\): \(\lambda=5,0\).
For \(\lambda=5\): \(\begin{bmatrix}-1&2\\2&-4\end{bmatrix}\vv=\vzero \Rightarrow v_1=2v_2\), so \(\vv=(2,1)\).
For \(\lambda=0\): \(\begin{bmatrix}4&2\\2&1\end{bmatrix}\vv=\vzero \Rightarrow 2v_1+v_2=0\), so \(\vv=(1,-2)\).
Orthogonal ✓ (\(2-2=0\)). One eigenvalue is zero → positive semi-definite but not definite. The zero eigenvalue means \(\vv=(1,-2)\) is in the null space: the matrix is singular, and \(q(\vv)=0\) there even though \(\vv\neq\vzero\).

(c) \(\tr=0\), \(\det=-1\). \(\lambda^2-1=0\), so \(\lambda=\pm1\).
\(\lambda=1\): \(v_1=v_2\), giving \((1,1)\). \(\lambda=-1\): \(v_1=-v_2\), giving \((1,-1)\).
Mixed signs → indefinite. This is the swap matrix, and \(q(\vx)=2x_1x_2\), which is clearly positive in the first quadrant and negative in the second. A saddle.

★★

Prove that for symmetric \(\mA\) with eigenvalues \(\lambda_1\ge\cdots\ge\lambda_d\),

\[ \lambda_d \|\vx\|^2 \;\le\; \vx^\top\mA\vx \;\le\; \lambda_1\|\vx\|^2 \quad\text{for all }\vx, \]

with equality attained at the corresponding eigenvectors. (This is the Rayleigh quotient bound.)

Then use it to prove that \(\lambda_{\max}(\mA+\lambda\mI) = \lambda_{\max}(\mA)+\lambda\), justifying the ridge claim from Lecture 10.

Proof of the bound. Diagonalise \(\mA=\mQ\Lambda\mQ^\top\) and set \(\vz=\mQ^\top\vx\). Since \(\mQ\) is orthogonal, \(\|\vz\|=\|\vx\|\) (orthogonal matrices preserve length: \(\|\mQ^\top\vx\|^2 = \vx^\top\mQ\mQ^\top\vx = \vx^\top\vx\)). Then

\[\vx^\top\mA\vx = \sum_{i=1}^d \lambda_i z_i^2.\]

Each \(z_i^2\ge0\), so replacing every \(\lambda_i\) by the largest can only increase the sum:

\[\sum_i \lambda_i z_i^2 \le \lambda_1\sum_i z_i^2 = \lambda_1\|\vz\|^2 = \lambda_1\|\vx\|^2.\]

Replacing every \(\lambda_i\) by the smallest gives the lower bound identically. ∎

Equality. Take \(\vx=\vq_1\), so \(\vz=\ve_1\) and the sum is exactly \(\lambda_1\), while \(\|\vx\|^2=1\). Equality attained ✓. Similarly \(\vq_d\) attains the lower bound.

Equivalently: \(\lambda_1 = \max_{\vx\neq\vzero}\frac{\vx^\top\mA\vx}{\vx^\top\vx}\), the Rayleigh quotient. This variational characterisation is what makes PCA's "maximise the variance" and "take the top eigenvector" the same instruction.

The ridge claim. If \(\mA\vq=\lambda\vq\) then

\[(\mA+c\mI)\vq = \mA\vq + c\vq = (\lambda+c)\vq,\]

so \(\vq\) is still an eigenvector, with eigenvalue shifted by \(c\). Since the eigenvectors are unchanged and every eigenvalue shifts by the same \(c\), the ordering is preserved and \(\lambda_{\max}(\mA+c\mI)=\lambda_{\max}(\mA)+c\), \(\lambda_{\min}(\mA+c\mI)=\lambda_{\min}(\mA)+c\). ∎

Hence for \(\mA=\mX^\top\mX\succeq0\) and \(c=\lambda>0\), every eigenvalue is at least \(\lambda>0\), so none is zero, so the matrix is invertible — the promise from Lecture 10. And the condition number becomes \(\frac{\lambda_{\max}+\lambda}{\lambda_{\min}+\lambda}\), which is strictly smaller than \(\frac{\lambda_{\max}}{\lambda_{\min}}\) whenever \(\lambda_{\max}>\lambda_{\min}\). Regularisation and preconditioning are literally the same operation.

★★

Let \(\mA\) be symmetric positive definite with eigen-decomposition \(\mQ\Lambda\mQ^\top\). Show:

  1. \(\mA^{-1} = \mQ\Lambda^{-1}\mQ^\top\), and its eigenvalues are \(1/\lambda_i\).
  2. \(\mA^{k} = \mQ\Lambda^{k}\mQ^\top\) for any positive integer \(k\).
  3. There is a symmetric positive definite \(\mA^{1/2}\) with \((\mA^{1/2})^2=\mA\).
  4. Why does this make Newton's method \(\vx - \mH^{-1}\grad f\) a "rescaling by curvature"?

1. Check by multiplying: \(\mQ\Lambda^{-1}\mQ^\top\,\mQ\Lambda\mQ^\top = \mQ\Lambda^{-1}(\mQ^\top\mQ)\Lambda\mQ^\top = \mQ\Lambda^{-1}\Lambda\mQ^\top = \mQ\mQ^\top = \mI\) ✓. And \(\Lambda^{-1}\) is diagonal with entries \(1/\lambda_i\), which exist because positive definiteness gives \(\lambda_i>0\).

2. Induction. \(\mA^2 = \mQ\Lambda\mQ^\top\mQ\Lambda\mQ^\top = \mQ\Lambda^2\mQ^\top\), the middle \(\mQ^\top\mQ\) collapsing to \(\mI\). Repeating gives \(\mA^k=\mQ\Lambda^k\mQ^\top\). ∎
This is why powers of matrices are easy in the eigenbasis and horrible outside it, and why the eigen-decomposition is the right tool for anything involving repeated application — Markov chains, discrete dynamical systems, and the gradient-descent recursion above.

3. Define \(\mA^{1/2} = \mQ\Lambda^{1/2}\mQ^\top\) with \(\Lambda^{1/2}=\operatorname{diag}(\sqrt{\lambda_i})\), well defined since \(\lambda_i>0\). Then \((\mA^{1/2})^2 = \mQ\Lambda^{1/2}\Lambda^{1/2}\mQ^\top = \mQ\Lambda\mQ^\top=\mA\) ✓, and it is symmetric (\((\mQ\Lambda^{1/2}\mQ^\top)^\top\) is itself) with positive eigenvalues \(\sqrt{\lambda_i}\), hence positive definite. ∎
Used constantly: sampling from \(\mathcal{N}(\vzero,\mS)\) is done as \(\mS^{1/2}\vz\) with \(\vz\sim\mathcal{N}(\vzero,\mI)\), and whitening a dataset is multiplication by \(\mS^{-1/2}\).

4. Write the Newton step in the eigenbasis of the Hessian. With \(\vg=\grad f\) and \(\tilde{\vg}=\mQ^\top\vg\), the step is

\[\mH^{-1}\vg = \mQ\Lambda^{-1}\mQ^\top\vg = \mQ\Lambda^{-1}\tilde{\vg},\]

i.e. component \(i\) of the gradient is divided by \(\lambda_i\). Directions of high curvature get small steps, directions of low curvature get large ones. That is exactly what is needed to fix the zig-zag in the figure above: after the rescaling, every direction has effective curvature 1, the condition number becomes 1, and the ellipses become circles. Newton converges in a single step on a quadratic for precisely this reason.

And it identifies the cost: you need \(\mH^{-1}\), which is \(O(d^3)\) to compute and \(O(d^2)\) to store. Adam's diagonal rescaling is the cheap version — it fixes the conditioning that a diagonal Hessian would cause, and ignores the rest.

★★★

Coding. Implement PCA from scratch and verify it against three independent characterisations.

  1. Generate 500 points in \(\RR^{10}\) whose true structure is 3-dimensional plus noise.
  2. Centre, compute the covariance, and take its eigendecomposition with eigh. Plot (or print) the eigenvalue spectrum. Can you see the true rank?
  3. Verify that projecting onto the top \(k\) eigenvectors minimises reconstruction error, by comparing against 1000 random \(k\)-dimensional subspaces.
  4. Verify that the singular values of the centred data matrix satisfy \(\sigma_i^2/n = \lambda_i\).
import numpy as np
rng = np.random.default_rng(0)
n, d, k_true = 500, 10, 3

Z = rng.normal(size=(n, k_true))          # latent
W = rng.normal(size=(k_true, d))          # loading
X = Z @ W + 0.35*rng.normal(size=(n, d))  # observed
Xc = X - X.mean(0)

S = Xc.T @ Xc / n
vals, vecs = np.linalg.eigh(S)
o = np.argsort(vals)[::-1]; vals, vecs = vals[o], vecs[:, o]
print("eigenvalues:", np.round(vals, 4))
print("explained  :", np.round(np.cumsum(vals)/vals.sum(), 4))

def recon_err(Q):                          # Q: d x k with orthonormal columns
    P = Q @ Q.T
    return np.mean(np.sum((Xc - Xc @ P)**2, axis=1))

for k in (1, 2, 3, 4):
    Qk = vecs[:, :k]
    best = recon_err(Qk)
    rand = min(recon_err(np.linalg.qr(rng.normal(size=(d, k)))[0])
               for _ in range(1000))
    print(f"k={k}  PCA err {best:.5f}   best of 1000 random subspaces {rand:.5f}"
          f"   {'PCA wins' if best <= rand else 'PROBLEM'}")

U, sv, Vt = np.linalg.svd(Xc, full_matrices=False)
print("\\nsigma^2 / n :", np.round(sv**2/n, 6)[:5])
print("eigenvalues  :", np.round(vals, 6)[:5])
print("match?", np.allclose(sv**2/n, vals))
print("top singular vector vs top eigenvector (up to sign):",
      np.allclose(np.abs(Vt[0]), np.abs(vecs[:, 0])))

What you should see.

2. The first three eigenvalues are large and comparable; the remaining seven are small and roughly equal to the noise variance \(0.35^2\approx0.12\). That gap — the "elbow" — is the visible signature of the true rank. Cumulative explained variance jumps to about 97–98% at \(k=3\) and then creeps up slowly, which is the standard way of choosing \(k\).

3. PCA beats every random subspace at every \(k\), usually by a wide margin. That is the Eckart–Young theorem in empirical form: the top-\(k\) eigen-subspace is the optimal rank-\(k\) approximation, not merely a good one. Note that reconstruction error and retained variance are two sides of one identity — total variance is fixed, so minimising what you throw away is maximising what you keep.

4. \(\sigma_i^2/n = \lambda_i\) to machine precision, and the right singular vectors equal the eigenvectors up to sign. This is the practical route: computing the SVD of \(\mX_c\) directly is both faster and numerically better than forming \(\mX_c^\top\mX_c\), because forming that product squares the condition number — the same lesson as lstsq versus inv in Lecture 10. Every real PCA implementation, including scikit-learn's, uses the SVD.

Sign ambiguity is worth noting: if \(\vq\) is an eigenvector so is \(-\vq\), so the sign of each principal component is arbitrary and can flip between library versions or platforms. If you ever see a PCA plot mirror itself for no reason, that is why.


Next: Part IV. We have differentiated functions of one variable and manipulated vectors and matrices. Lecture 12 puts them together: gradients, Jacobians and Hessians, the multivariable chain rule, and the matrix-calculus identities we have been using on credit since Lecture 10.