Singular value decomposition

Every matrix, of any shape, is a rotation, a stretch along the axes, and another rotation.

What you'll learn
  • Read A = UΣVᵀ as rotate, scale, rotate
  • Use singular values to compress a matrix with a low-rank approximation
  • Judge how trustworthy a matrix is from its condition number

Eigendecomposition only works for some square matrices. The singular value decomposition works for every matrix there is:

\[A = U\,\Sigma\,V^{\mathsf{T}}\]

\(U\) and \(V\) are orthogonal — rotations, possibly with a reflection — and \(\Sigma\) is diagonal, with the singular values \(\sigma_1 \ge \sigma_2 \ge \dots \ge 0\) on it. Read right to left, that is a recipe for what \(A\) does to a vector: rotate by \(V^{\mathsf{T}}\), stretch each axis by its \(\sigma_i\), rotate by \(U\).

The unit circle goes through the three stages. V aligns the right singular vectors with the axes, Σ stretches the circle into an axis-aligned ellipse, and U turns it into place. The ellipse's semi-axes are σ₁ and σ₂.

This is the deepest picture in the course: every linear map takes the unit circle to an ellipse, and the SVD tells you its axes. \(\sigma_1\) is the most any unit vector can be stretched, and \(\sigma_1\sigma_2 = \vert \det A\vert \) is the area factor from the first lesson.

A = [[3, 0, 5],
     [8, 1, 3]]
U, S, Vt = np.linalg.svd(A)     # S comes back already sorted, largest first

SVD and eigendecomposition

The two are close relatives. The singular values of \(A\) are the square roots of the eigenvalues of \(A^{\mathsf{T}}A\), the columns of \(V\) are its eigenvectors, and the columns of \(U\) are the eigenvectors of \(AA^{\mathsf{T}}\). The notebook confirms each of these numerically.

Low-rank approximation

Like the symmetric eigendecomposition, the SVD writes a matrix as a sum of rank-one layers, now ordered by importance:

\[A = \sum_i \sigma_i\,\mathbf{u}_i\mathbf{v}_i^{\mathsf{T}}\]

Keep only the first \(k\) layers and you get the best possible rank-\(k\) approximation to \(A\). The notebook reconstructs an image from five components:

ncomps = 5
Ur = U[:, 0:ncomps]
Sr = S[0:ncomps]
Vr = V[0:ncomps, :]
reconImage = Ur @ np.diag(Sr) @ Vr      # rank 5

How many layers are enough? Square the singular values and turn them into percentages of the total — the percent variance each one explains.

A matrix that is secretly rank 3, plus a little noise. Three singular values explain 99.1% of the variance; the fourth explains 0.1%. Keeping three layers throws away almost nothing.

That elbow is how you decide where to cut. It is also, almost word for word, principal component analysis.

Condition number

The ratio \(\kappa = \sigma_{\max}/\sigma_{\min}\) is the condition number. It measures how lopsided the ellipse is: near 1, the matrix treats every direction alike; very large, it nearly crushes some direction, and solving \(A\mathbf{x} = \mathbf{b}\) amplifies errors by up to \(\kappa\). When \(\sigma_{\min} = 0\) the matrix is singular and \(\kappa\) is infinite — the determinant lesson’s “can’t undo” case, seen through the SVD.

The pseudoinverse \(A^{+} = V\Sigma^{+}U^{\mathsf{T}}\) inverts the non-zero singular values and leaves the zeros alone. It is the least-squares solver for any matrix, square or not, singular or not.

Try it yourself. In the animation, find a matrix whose ellipse is a circle. What are \(\sigma_1\) and \(\sigma_2\), and what kind of matrix is it? Then follow the notebook’s challenge: build a matrix with condition number exactly 42 by choosing \(\Sigma\) yourself and multiplying by random orthogonal \(U\) and \(V\) (from np.linalg.qr).

View source on GitHub →