Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 1: Math FoundationsLinear Algebra

Singular Value Decomposition

P1.B1350 min●●○ IntermediateReviewed Nov 30, 2026

Why this matters for machine learning

The SVD shows what any matrix really does (rotate, stretch, rotate), ranks its directions from most to least important, and so tells you which parts of a matrix you can drop, which parts you must not divide by, and what the best simple summary of a data set is.

  • PCA is the SVD of the centred data matrix: the right singular vectors are the principal directions, and the squared singular values (divided by N−1N-1) are the variances along them. (P3.C3: Principal Component Analysis, coming soon)
  • LoRA fine-tunes a big weight matrix by learning a low-rank update BA\mathbf{B}\mathbf{A}, betting that a useful update is close to low rank; the SVD is the tool that measures how low. (P10.E3: Parameter-efficient fine-tuning, coming soon)
  • Recommender systems factor a big users-by-items table into two thin matrices of user and item features, the same low-rank idea as a truncated SVD. (P11.3: Recommender systems, coming soon)
  • A weight matrix can be compressed by replacing it with two thin factors, which is one of several model-compression ideas. (P11.8: Pruning and model compression, coming soon)
  • Linear regression with dependent or nearly dependent features is solved safely by an SVD-based least-squares routine, which uses the pseudo-inverse and the condition number. (P3.A2: Linear regression, coming soon)
  • The largest singular value of a weight matrix is the most that layer can stretch any vector, which is one reason very large ones can make signals and gradients explode through a deep network. (P4.16: Vanishing and exploding gradients, coming soon)

Before this: Matrices as data and as transformations, Span, independence, basis and rank, Transpose, symmetric and orthogonal matrices, Eigenvalues and eigenvectors

After this lesson you can

  • Describe how a 2×22\times2 matrix turns the unit circle into an ellipse, name its semi-axes σ1u1\sigma_1\mathbf{u}_1 and σ2u2\sigma_2\mathbf{u}_2, and read A=UΣV⊤\mathbf{A}=\mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^\top as rotate or reflect, stretch, rotate or reflect
  • State the shapes of the full and thin SVD, the properties of singular values (non-negative, sorted, rank == number of non-zero ones), and how they relate to the eigenvalues of A⊤A\mathbf{A}^\top\mathbf{A}
  • Build a rank-kk approximation from the top kk layers, state its error (Eckart–Young), and count the storage k(m+n+1)k(m+n+1) against mnmn
  • Form the pseudo-inverse A+=VΣ+U⊤\mathbf{A}^+=\mathbf{V}\boldsymbol{\Sigma}^+\mathbf{U}^\top, and use singular values for the condition number σ1/σn\sigma_1/\sigma_n, numerical rank and PCA, with np.linalg.svd, pinv, cond

In P1.B12 you looked for directions that a matrix only stretches. That works well for symmetric matrices, but a general matrix can turn every direction, and its eigenvalues can even be complex. This lesson gives the answer that works for every real matrix, of any shape: there is always a pair of perpendicular input directions that the matrix sends to a pair of perpendicular output directions, stretching each by some amount. That is the singular value decomposition, or SVD.

It is one of the most useful facts in applied linear algebra. It shows what a matrix does, it measures how many directions really matter, and it hands you the best simple approximation of a matrix, the pseudo-inverse, the condition number and PCA, all from one factorisation.

The picture: a circle becomes an ellipse

Start with the shear of P1.B12:

A=(1101)\mathbf{A}=\begin{pmatrix}1&1\\0&1\end{pmatrix}

Take all vectors of length 11: they form the unit circle. Send each one through A\mathbf{A}. The circle comes out as an ellipse (a stretched circle). This is true for every 2×22\times2 matrix that is not squashed flat: a linear map turns a circle into an ellipse.

An ellipse has a longest axis and a shortest axis, and they are perpendicular. Call the half-lengths σ1≥σ2\sigma_1\ge\sigma_2 (sigma) and the unit directions of the axes u1\mathbf{u}_1 and u2\mathbf{u}_2. So the axes are the arrows σ1u1\sigma_1\mathbf{u}_1 and σ2u2\sigma_2\mathbf{u}_2. Now ask which inputs land on the ends of these axes. Those inputs are two perpendicular unit vectors, v1\mathbf{v}_1 and v2\mathbf{v}_2:

Av1=σ1u1,Av2=σ2u2\mathbf{A}\mathbf{v}_1=\sigma_1\mathbf{u}_1,\qquad\mathbf{A}\mathbf{v}_2=\sigma_2\mathbf{u}_2

The numbers σ1,σ2\sigma_1,\sigma_2 are the singular values of A\mathbf{A}. For the shear they are σ1=12(1+5)≈1.618\sigma_1=\tfrac12(1+\sqrt5)\approx1.618 and σ2=12(5−1)≈0.618\sigma_2=\tfrac12(\sqrt5-1)\approx0.618, the numbers promised in P1.B12. The widget shows all of this and lets you apply the three steps one at a time.

Rotate, stretch, rotate: the factorisation of a 2×2 matrix

−2−112−2−112v₁v₂σ₁u₁σ₂u₂

Stage 3, after U (this is A): U is a rotation by 31.7° counterclockwise. The ellipse ends up where A puts it.

A = 11 01
σ₁ = 1.618, σ₂ = 0.618 (rank 2; σ₁σ₂ = 1 = |det A| = 1)
v₁ = (0.526, 0.851), v₂ = (−0.851, 0.526) (perpendicular; the signs of a pair can be flipped together with its u)
u₁ = (0.851, 0.526), u₂ = (−0.526, 0.851)
Check: Av₁ = (1.376, 0.851) = σ₁u₁ = 1.618 × (0.851, 0.526)
Check: Av₂ = (−0.325, 0.526) = σ₂u₂ = 0.618 × (−0.526, 0.851)

Rank 2: the circle becomes an ellipse with semi-axes σ₁ = 1.618 along u₁ and σ₂ = 0.618 along u₂. The perpendicular inputs v₁, v₂ land on the ends of the axes: Av₁ = σ₁u₁ and Av₂ = σ₂u₂.

Dashed grey circle: the unit circle (the input). Thick blue curve: its image, an ellipse. The blue arrows are v₁ (thin, on the circle) and its image; the pink arrows are v₂ and its image. Thin blue lines: the grid after the current stage; grey lines: the original grid. Use the stage buttons or the slider to apply Vᵀ, then Σ, then U. Entries are shown rounded to 3 decimals; the signs of v and u pairs are a choice.

Rotate, stretch, rotate. The dashed grey circle is the unit circle (the input); the thick blue curve is its image, an ellipse. The blue arrows are v₁ and its image, the pink arrows are v₂ and its image. At stage 3 the image arrows are the semi-axes σ₁u₁ and σ₂u₂. The Start, 1, 2 and 3 buttons, the Stage slider and Play all apply Vᵀ, then Σ, then U to the plane; the thin blue lines are the grid at the current stage. The sliders and presets change the matrix. The readout gives the singular values, v₁, v₂, u₁, u₂ and a check that Av₁ = σ₁u₁ and Av₂ = σ₂u₂, and the status says whether the matrix has rank 2, rank 1 or rank 0, equal singular values, or a reflection.

Things to try:

  1. At the start (the shear, stage 3): the ellipse is long and thin, tilted up to the right. The pink arrow σ2u2\sigma_2\mathbf{u}_2 is the short axis and the blue σ1u1\sigma_1\mathbf{u}_1 the long one. On the dashed circle, the thin arrows v1\mathbf{v}_1 and v2\mathbf{v}_2 are perpendicular. The check lines show Av1=σ1u1\mathbf{A}\mathbf{v}_1=\sigma_1\mathbf{u}_1 and Av2=σ2u2\mathbf{A}\mathbf{v}_2=\sigma_2\mathbf{u}_2.
  2. Press Start, then 1: Vᵀ, 2: Σ, 3: U. Step 1 turns the plane (the circle looks the same, but v1,v2\mathbf{v}_1,\mathbf{v}_2 now lie on the axes). Step 2 stretches the horizontal axis by σ1\sigma_1 and the vertical one by σ2\sigma_2. Step 3 turns the ellipse into its final place. Or drag the Stage slider, or press Play all.
  3. Press Diagonal: the matrix already stretches along the axes, so the turns are 0∘0^\circ (or a flip of an axis). Press Symmetric (σ=3\sigma=3 and 11, the eigenvalues of P1.B12): the ellipse axes lie along (1,1)(1,1) and (1,−1)(1,-1).
  4. Press Rotate 30°: σ1=σ2=1\sigma_1=\sigma_2=1 and the ellipse is a circle. A circle has no longest direction, so the status says that any perpendicular pair would work for v1,v2\mathbf{v}_1,\mathbf{v}_2.
  5. Press Rank 1: the ellipse collapses to a line segment, σ2=0\sigma_2=0, and the status says that v2\mathbf{v}_2 is the direction that is squashed to the origin. Press Zero: everything collapses to a point.
  6. Press Reflect: σ1=σ2=1\sigma_1=\sigma_2=1, and the status mentions a reflection (det⁡<0\det<0). Use the sliders to make det⁡\det negative in other ways, and watch the Stage-3 step flip the plane over.

Intuition

Every matrix, however messy, does three simple things in a row: it turns the plane (or flips it), it stretches along the two axes by different amounts, and it turns again. The first turn lines up the two special input directions with the axes. The stretch is the only part that changes lengths. The last turn puts the result where it belongs. The singular values are the stretch factors, so they are the “size” of each of the two directions.

The picture of a rubber sheet from P1.B12 breaks in two places. A flip is not something a sheet can do by pulling (it is a reflection, when det⁡<0\det<0). And for a matrix that is not square, the “plane” you start in and the space you end in have different numbers of dimensions, so the turns happen in different spaces.

Math

Every real m×nm\times n matrix can be written

A=UΣV⊤\mathbf{A}=\mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^\top

with U\mathbf{U} an orthogonal m×mm\times m matrix, V\mathbf{V} an orthogonal n×nn\times n matrix (P1.B10), and Σ\boldsymbol{\Sigma} an m×nm\times n matrix that is zero except for the numbers σ1≥σ2≥⋯≥0\sigma_1\ge\sigma_2\ge\dots\ge0 on its main diagonal. The proof that this always exists is not needed here. The columns vi\mathbf{v}_i of V\mathbf{V} are the right singular vectors, the columns ui\mathbf{u}_i of U\mathbf{U} the left singular vectors, and Avi=σiui\mathbf{A}\mathbf{v}_i=\sigma_i\mathbf{u}_i.

Code

import numpy as np

A = np.array([[1.0, 1.0], [0.0, 1.0]])
U, S, Vh = np.linalg.svd(A)    # S: the singular values (a 1-D array); Vh: V transposed
print(S)                        # about [1.618 0.618]
print(U @ np.diag(S) @ Vh)      # gives A back

The NumPy section below runs this with all the checks.

Rotate, stretch, rotate

Read A=UΣV⊤\mathbf{A}=\mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^\top from right to left, because Ax=U(Σ(V⊤x))\mathbf{A}\mathbf{x}=\mathbf{U}(\boldsymbol{\Sigma}(\mathbf{V}^\top\mathbf{x})) applies V⊤\mathbf{V}^\top first (P1.B6):

  1. V⊤\mathbf{V}^\top rotates or reflects. It is orthogonal, so it keeps lengths and angles (P1.B10). The dot products vi⊤x\mathbf{v}_i^\top\mathbf{x} are the coordinates of x\mathbf{x} along the new axes v1,v2,…\mathbf{v}_1,\mathbf{v}_2,\dots, and V⊤vi\mathbf{V}^\top\mathbf{v}_i is the ii-th axis arrow ei\mathbf{e}_i.
  2. Σ\boldsymbol{\Sigma} stretches along the axes: coordinate ii is multiplied by σi\sigma_i. This is the only step that changes lengths.
  3. U\mathbf{U} rotates or reflects again, so the ii-th axis ends up along ui\mathbf{u}_i.

Putting the middle step into words for the unit circle: step 1 maps it to itself, step 2 makes an axis-aligned ellipse with semi-axes σ1,σ2\sigma_1,\sigma_2, step 3 moves that ellipse. That is exactly what the widget stages show.

For the shear, with numbers rounded to three decimals:

v1=(0.5260.851),v2=(−0.8510.526)\mathbf{v}_1=\begin{pmatrix}0.526\\0.851\end{pmatrix},\quad \mathbf{v}_2=\begin{pmatrix}-0.851\\0.526\end{pmatrix} u1=(0.8510.526),u2=(−0.5260.851)\mathbf{u}_1=\begin{pmatrix}0.851\\0.526\end{pmatrix},\quad \mathbf{u}_2=\begin{pmatrix}-0.526\\0.851\end{pmatrix}

V⊤\mathbf{V}^\top is a rotation by 58.3∘58.3^\circ clockwise and U\mathbf{U} is a rotation by 31.7∘31.7^\circ counterclockwise. Check: Av1=(0.526+0.851, 0.851)=(1.376, 0.851)=1.618 u1\mathbf{A}\mathbf{v}_1=(0.526+0.851,\ 0.851)=(1.376,\ 0.851)=1.618\,\mathbf{u}_1.

The properties to remember

  • Singular values are non-negative and sorted: σ1≥σ2≥⋯≥0\sigma_1\ge\sigma_2\ge\dots\ge0. A negative sign, if there is one, is moved into U\mathbf{U} or V\mathbf{V}. That is why a reflection shows up as an orthogonal matrix with det⁡=−1\det=-1: when det⁡A<0\det\mathbf{A}<0, one of the two turns is really a flip. For a square matrix, ∣det⁡A∣=σ1σ2⋯σn|\det\mathbf{A}|=\sigma_1\sigma_2\cdots\sigma_n, since turns and flips do not change areas or volumes (P1.B8); the shear has σ1σ2=1\sigma_1\sigma_2=1.
  • Rank = number of non-zero singular values (P1.B7). A zero σi\sigma_i means the direction vi\mathbf{v}_i is squashed to 0\mathbf{0}: the vectors vi\mathbf{v}_i with σi=0\sigma_i=0 (together with any extra vi\mathbf{v}_i when n>mn>m) span the null space, and the vectors ui\mathbf{u}_i with σi>0\sigma_i>0 span the column space.
  • The factorisation exists for every real matrix. Not only square, not only symmetric, not only invertible.
  • Not unique in the details. The singular values are unique. If you flip the signs of vi\mathbf{v}_i and ui\mathbf{u}_i together, you get another valid SVD, and software may return either. If two singular values are equal (a rotation has 1,11,1), then any perpendicular pair in that plane works: the circle stays a circle.
  • Norms. The largest stretch max⁡∥Ax∥/∥x∥\max\lVert\mathbf{A}\mathbf{x}\rVert/\lVert\mathbf{x}\rVert over all x≠0\mathbf{x}\ne\mathbf{0} is σ1\sigma_1, the 2-norm ∥A∥2\lVert\mathbf{A}\rVert_2 (it is the longest semi-axis of the ellipse). The Frobenius norm ∥A∥F\lVert\mathbf{A}\rVert_F, the square root of the sum of the squares of all entries, equals σ12+σ22+⋯\sqrt{\sigma_1^2+\sigma_2^2+\cdots}. For the shear, ∥A∥F=3\lVert\mathbf{A}\rVert_F=\sqrt3 and 1.6182+0.6182=31.618^2+0.618^2=3.

Full and thin SVD

If A\mathbf{A} is m×nm\times n then the full SVD has U\mathbf{U} of shape m×mm\times m, Σ\boldsymbol{\Sigma} of shape m×nm\times n and V\mathbf{V} of shape n×nn\times n. There are only min⁡(m,n)\min(m,n) singular values, so for a tall matrix (m>nm>n) the extra m−nm-n columns of U\mathbf{U} multiply rows of zeros in Σ\boldsymbol{\Sigma} and do nothing. The thin (or economy) SVD drops them: U\mathbf{U} is m×r0m\times r_0, Σ\boldsymbol{\Sigma} is r0×r0r_0\times r_0 and V\mathbf{V} is n×r0n\times r_0, where r0=min⁡(m,n)r_0=\min(m,n). It rebuilds the same A\mathbf{A}, and it is what you want for tall data matrices. For the 3×23\times2 matrix A3\mathbf{A}_3 of P1.B9 the full U\mathbf{U} is 3×33\times3 and the thin one 3×23\times2.

Finding an SVD by hand

The singular values and vectors hide in a matrix you already know how to handle. Multiply A⊤A\mathbf{A}^\top\mathbf{A} out using A=UΣV⊤\mathbf{A}=\mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^\top and U⊤U=I\mathbf{U}^\top\mathbf{U}=\mathbf{I}:

A⊤A=VΣ⊤U⊤UΣV⊤=V (Σ⊤Σ) V⊤\begin{aligned} \mathbf{A}^\top\mathbf{A}&=\mathbf{V}\boldsymbol{\Sigma}^\top\mathbf{U}^\top\mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^\top\\ &=\mathbf{V}\,(\boldsymbol{\Sigma}^\top\boldsymbol{\Sigma})\,\mathbf{V}^\top \end{aligned}

This is an eigen-decomposition of the symmetric matrix A⊤A\mathbf{A}^\top\mathbf{A} (P1.B12 spectral theorem). So the columns of V\mathbf{V} are eigenvectors of A⊤A\mathbf{A}^\top\mathbf{A} and the eigenvalues are σi2\sigma_i^2. In the same way, AA⊤=U(ΣΣ⊤)U⊤\mathbf{A}\mathbf{A}^\top=\mathbf{U}(\boldsymbol{\Sigma}\boldsymbol{\Sigma}^\top)\mathbf{U}^\top: the columns of U\mathbf{U} are eigenvectors of AA⊤\mathbf{A}\mathbf{A}^\top with the same non-zero eigenvalues. A one-line reason that the σi2\sigma_i^2 are never negative: ∥Av∥2=v⊤A⊤Av≥0\lVert\mathbf{A}\mathbf{v}\rVert^2=\mathbf{v}^\top\mathbf{A}^\top\mathbf{A}\mathbf{v}\ge0 (P1.B12: A⊤A\mathbf{A}^\top\mathbf{A} is positive semi-definite).

The recipe, for the shear:

  1. A⊤A=(1112)\mathbf{A}^\top\mathbf{A}=\begin{pmatrix}1&1\\1&2\end{pmatrix}, with trace 33 and determinant 11. The characteristic equation λ2−3λ+1=0\lambda^2-3\lambda+1=0 gives λ=12(3±5)≈2.618\lambda=\tfrac12(3\pm\sqrt5)\approx2.618 and 0.3820.382.
  2. σi=λi\sigma_i=\sqrt{\lambda_i}: 2.618=1.618\sqrt{2.618}=1.618 and 0.382=0.618\sqrt{0.382}=0.618. (Indeed (1+52)2=3+52\left(\tfrac{1+\sqrt5}{2}\right)^2=\tfrac{3+\sqrt5}{2}.)
  3. vi\mathbf{v}_i is a unit eigenvector. For λ=2.618\lambda=2.618: (1−λ)x+y=0(1-\lambda)x+y=0 gives y=1.618 xy=1.618\,x, so v1∝(1,1.618)\mathbf{v}_1\propto(1,1.618), which as a unit vector is (0.526,0.851)(0.526,0.851).
  4. ui=Avi/σi\mathbf{u}_i=\mathbf{A}\mathbf{v}_i/\sigma_i (when σi≠0\sigma_i\ne0). For the full SVD, complete the u\mathbf{u}‘s (and v\mathbf{v}‘s) with any extra perpendicular unit vectors. For the shear, u1=(1.376,0.851)/1.618=(0.851,0.526)\mathbf{u}_1=(1.376,0.851)/1.618=(0.851,0.526).

This is the method for hand work. Software does not form A⊤A\mathbf{A}^\top\mathbf{A} for real problems, because it squares the condition number (P1.B11), and it uses other algorithms that work on A\mathbf{A} directly.

Eigenvalues are not singular values

P1.B12 promised this comparison. The eigenvalues of a matrix are about directions that stay on their own line; the singular values are about the largest and smallest stretch over all directions.

  • Shear (1101)\begin{pmatrix}1&1\\0&1\end{pmatrix}: eigenvalues 1,11,1, singular values 1.6181.618 and 0.6180.618. It stretches (1,1)/2(1,1)/\sqrt2 (length 11) to (2,1)/2(2,1)/\sqrt2 (length 1.581.58), which no eigenvalue of 11 could explain.
  • Rotation: complex eigenvalues (P1.B12), singular values 11 and 11. A rotation does not stretch anything.
  • Symmetric matrix (1221)\begin{pmatrix}1&2\\2&1\end{pmatrix}: eigenvalues 33 and −1-1, singular values 33 and 11. For a symmetric matrix the singular values are the absolute values of the eigenvalues; the minus sign becomes a flip. And for a symmetric positive semi-definite matrix, such as (2112)\begin{pmatrix}2&1\\1&2\end{pmatrix} (eigenvalues 3,13,1), the SVD is the eigen-decomposition: U=V=Q\mathbf{U}=\mathbf{V}=\mathbf{Q} and Σ=Λ\boldsymbol{\Sigma}=\boldsymbol{\Lambda} (P1.B12 spectral theorem).
  • A non-square matrix has no eigenvalues at all, but it always has singular values.

Both U\mathbf{U} and V\mathbf{V} appear in the SVD, and they are different matrices in general: the eigen-decomposition VΛV−1\mathbf{V}\boldsymbol{\Lambda}\mathbf{V}^{-1} uses one basis for input and output, while the SVD lets the input and output axes differ and keeps both perpendicular.

A matrix as a sum of layers

Write A=UΣV⊤\mathbf{A}=\mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^\top column by column, using the outer-product view of P1.B6 (a product is a sum of columns times rows):

A=σ1u1v1⊤+σ2u2v2⊤+⋯+σrurvr⊤\begin{aligned} \mathbf{A}&=\sigma_1\mathbf{u}_1\mathbf{v}_1^\top+\sigma_2\mathbf{u}_2\mathbf{v}_2^\top\\ &\quad+\cdots+\sigma_r\mathbf{u}_r\mathbf{v}_r^\top \end{aligned}

with rr the rank. Each term σiuivi⊤\sigma_i\mathbf{u}_i\mathbf{v}_i^\top is a rank-1 layer (every row is a multiple of one row, P1.B6). The layers come in decreasing order of importance: the first has the largest σ\sigma. A low-rank approximation keeps only the first kk:

Ak=σ1u1v1⊤+⋯+σkukvk⊤\mathbf{A}_k=\sigma_1\mathbf{u}_1\mathbf{v}_1^\top+\cdots+\sigma_k\mathbf{u}_k\mathbf{v}_k^\top

It has rank at most kk. A first example uses the matrix M=(123456789)\mathbf{M}=\begin{pmatrix}1&2&3\\4&5&6\\7&8&9\end{pmatrix} of P1.B7. Its singular values are about 16.84816.848, 1.0681.068 and (almost exactly) 00, so its rank is 22, and the first layer already gets close:

M1≈(1.742.072.414.215.025.836.687.979.26)\mathbf{M}_1\approx\begin{pmatrix}1.74&2.07&2.41\\4.21&5.02&5.83\\6.68&7.97&9.26\end{pmatrix}

Every entry is within 0.750.75 of the entry of M\mathbf{M}, and the error (in the 2-norm) is exactly σ2\sigma_2.

How good is the truncation? Eckart–Young

The Eckart–Young theorem (Eckart and Young proved it for the Frobenius norm; the 2-norm version is usually credited to Mirsky) says that Ak\mathbf{A}_k is the best rank-kk approximation there is, in two senses. Among all matrices B\mathbf{B} of rank at most kk:

∥A−B∥2≥σk+1∥A−B∥F≥σk+12+σk+22+⋯\begin{aligned} \lVert\mathbf{A}-\mathbf{B}\rVert_2&\ge\sigma_{k+1}\\ \lVert\mathbf{A}-\mathbf{B}\rVert_F&\ge\sqrt{\sigma_{k+1}^2+\sigma_{k+2}^2+\cdots} \end{aligned}

and Ak\mathbf{A}_k reaches both values:

∥A−Ak∥2=σk+1∥A−Ak∥F=σk+12+σk+22+⋯\begin{aligned} \lVert\mathbf{A}-\mathbf{A}_k\rVert_2&=\sigma_{k+1}\\ \lVert\mathbf{A}-\mathbf{A}_k\rVert_F&=\sqrt{\sigma_{k+1}^2+\sigma_{k+2}^2+\cdots} \end{aligned}

(Both statements are given without a full proof; the 2-norm one is proved in the box below, for k<min⁡(m,n)k<\min(m,n).) For M\mathbf{M} and k=1k=1 both errors are σ2=1.068\sigma_2=1.068, because σ3≈0\sigma_3\approx0. A good rule: the error is made of the singular values you throw away. A relative error is ∥A−Ak∥F/∥A∥F\lVert\mathbf{A}-\mathbf{A}_k\rVert_F/\lVert\mathbf{A}\rVert_F, and it is small exactly when the discarded σ\sigma‘s are small next to the kept ones. For M\mathbf{M}, the first layer holds 283.86283.86 of the total 285285 in the sum of squares, about 99.6%99.6\%.

Go deeper Why no rank-k matrix beats the cut-off, in the 2-norm

Let B\mathbf{B} have rank at most kk. Its null space has dimension at least n−kn-k (P1.B7, rank plus nullity). The span of v1,…,vk+1\mathbf{v}_1,\dots,\mathbf{v}_{k+1} has dimension k+1k+1. In nn dimensions, a space of dimension n−kn-k and one of dimension k+1k+1 must share a non-zero vector x\mathbf{x} (their dimensions add up to n+1n+1), so take it with length 11. Then Bx=0\mathbf{B}\mathbf{x}=\mathbf{0}, so ∥(A−B)x∥=∥Ax∥\lVert(\mathbf{A}-\mathbf{B})\mathbf{x}\rVert=\lVert\mathbf{A}\mathbf{x}\rVert. Write x=c1v1+⋯+ck+1vk+1\mathbf{x}=c_1\mathbf{v}_1+\dots+c_{k+1}\mathbf{v}_{k+1} with ∑ci2=1\sum c_i^2=1. Then Ax=∑ciσiui\mathbf{A}\mathbf{x}=\sum c_i\sigma_i\mathbf{u}_i and ∥Ax∥2=∑ci2σi2≥σk+12\lVert\mathbf{A}\mathbf{x}\rVert^2=\sum c_i^2\sigma_i^2\ge\sigma_{k+1}^2, because each of σ1,…,σk+1\sigma_1,\dots,\sigma_{k+1} is at least σk+1\sigma_{k+1}. So ∥A−B∥2≥σk+1\lVert\mathbf{A}-\mathbf{B}\rVert_2\ge\sigma_{k+1}.

Storage: when does it really compress?

Keeping Ak\mathbf{A}_k means keeping the first kk columns of U\mathbf{U} (each has mm numbers), the first kk columns of V\mathbf{V} (each has nn numbers) and the kk singular values. That is

k (m+n+1)k\,(m+n+1)

numbers, against mnmn for the original. This is smaller only when k<mnm+n+1k<\dfrac{mn}{m+n+1}. For a 64×6464\times64 picture, mn=4096mn=4096 and m+n+1=129m+n+1=129, so k≤31k\le31 saves space (31⋅129=399931\cdot129=3999) and k=32k=32 does not (41284128). The larger the matrix, the more it saves: for a 1000×10001000\times1000 matrix, rank 88 needs 8⋅2001=16,0088\cdot2001=16{,}008 numbers instead of 1,000,0001{,}000{,}000.

Image compression

A grey picture is a matrix of brightness numbers (P1.B1 treated a picture as a list of numbers). The widget below makes a 64×6464\times64 picture in code (a house, a sun and a tree; no image file is loaded), computes its SVD in your browser, and rebuilds it from the first kk layers.

Keep only the k biggest terms of a picture

Original: 4,096 numbers
Rank 10: 1,290 numbers

The 64 singular values, largest first, left to right (linear scale). Tap a bar to set k.

Storage: k(m + n + 1) = 10 × 129 = 1,290 numbers, against m·n = 4,096 (31.5%)
Saves space only while k ≤ 31
Relative error from the singular values, √(σ(k+1)² + … + σ(64)²) / √(σ(1)² + … + σ(64)²) = 5.601%
Relative error measured on the pictures, ‖A − A(k)‖ / ‖A‖ = 5.601% (the same number, as the theory predicts; the Eckart–Young theorem adds that no other rank-k picture has a smaller error)
The next singular value σ(k+1) (the error in the 2-norm) = 171.2, against σ(1) = 8343.1. Share of the squared size kept: 99.69%.

k = 10: 1,290 numbers instead of 4,096 (31.5% of the original) with a relative error of 5.6%.

The picture is made by code (a house, a sun and a tree), and its SVD is computed in your browser. Grey levels run from 0 (black) to 255 (white); the rank-k picture is clipped to that range when drawn, but the error numbers use the unclipped values. The bars are blue for the k terms kept and grey for the rest.

Image compression. Left: the original 64 by 64 picture. Right: the picture rebuilt from its k largest singular layers. The slider and the buttons set k from 0 to 64; the bar chart shows the 64 singular values (blue for the layers kept; tap a bar to set k, or tick the log scale to see the small ones). The readouts give the storage k(m + n + 1) against m·n, the relative error computed from the singular values and the one measured on the pictures, and the next singular value.

Things to try:

  1. At k=1k=1 the picture is a pattern of horizontal and vertical bands: one layer is one column pattern times one row pattern. The relative error is about 26.5%26.5\%. At k=5k=5 the house and the sun are clear, and at k=10k=10 the picture is a good copy (about 5.6%5.6\% error) with 12901290 numbers instead of 40964096, about 31.5%31.5\%.
  2. Look at the bar chart. The first singular value is about 83438343, the second about 16131613, and by the tenth they are near 190190. A few big values and many small ones is why compression works: the small layers add little detail. Tick Log scale to see them.
  3. The two error lines agree to the digits shown at every kk: one is computed from the singular values with the formula, the other by subtracting the pictures. At each kk the next singular value σk+1\sigma_{k+1} is the 2-norm error.
  4. Slide to k=32k=32: the picture is almost exactly the original, but the storage line shows 41284128 numbers against 40964096: no saving, and the status says so. At k=64k=64 the picture is rebuilt exactly (up to rounding). At k=0k=0 the picture is black and the error is 100%100\%.
  5. This particular picture has about 3434 non-zero singular values; the last 3030 or so are zero up to rounding errors, because the picture is made of a few flat shapes. A photograph usually has many more non-zero values, with a slower fall.

A preview of LoRA

P1.B6 and P1.B7 met LoRA, the method behind P10.E3. The weight matrix W\mathbf{W} is frozen, and the update is learned as a product BA\mathbf{B}\mathbf{A} of a d×rd\times r and an r×kr\times k matrix (the paper’s letters: these A\mathbf{A} and B\mathbf{B} are new matrices, not the ones of this lesson), so the update has rank at most rr and only r(d+k)r(d+k) numbers to learn. For d=k=1000d=k=1000 and r=8r=8 that is 16,00016{,}000 numbers instead of 1,000,0001{,}000{,}000. (The SVD form above stores 16,00816{,}008: the extra 88 are the singular values. A product BA\mathbf{B}\mathbf{A} has no separate σ\sigma‘s.)

The SVD says what this restriction means: a rank-rr matrix is a sum of rr layers, and by Eckart–Young the best rank-rr stand-in for any matrix is its top rr layers. LoRA’s authors hypothesised that the change in weights during adaptation has a low “intrinsic rank”, so a small rr can be enough. That is an empirical bet that worked well in their experiments, not a theorem. Also note that LoRA does not compute an SVD. It learns B\mathbf{B} and A\mathbf{A} by gradient descent, starting from a random A\mathbf{A} and B=0\mathbf{B}=\mathbf{0} so that the update starts at zero. P10.E3 covers the method.

The pseudo-inverse

A non-square or singular matrix has no inverse (P1.B9). But its SVD still shows what to do. A matrix does three things: V⊤\mathbf{V}^\top turns, Σ\boldsymbol{\Sigma} stretches, U\mathbf{U} turns. To undo it, undo each step in reverse order: U⊤\mathbf{U}^\top undoes U\mathbf{U}, then divide by each σi\sigma_i, then V\mathbf{V} undoes V⊤\mathbf{V}^\top. The only problem is a zero σi\sigma_i, which cannot be divided by. The fix is to leave those directions at zero. The result is the pseudo-inverse:

A+=VΣ+U⊤\mathbf{A}^+=\mathbf{V}\boldsymbol{\Sigma}^+\mathbf{U}^\top

where Σ+\boldsymbol{\Sigma}^+ is made from Σ\boldsymbol{\Sigma} by inverting every non-zero singular value and transposing the shape (an m×nm\times n matrix becomes n×mn\times m). It exists for every matrix and is unique.

What it is for. x=A+b\mathbf{x}=\mathbf{A}^+\mathbf{b} is the minimum-length least-squares solution of Ax=b\mathbf{A}\mathbf{x}=\mathbf{b}: among all x\mathbf{x} that make ∥Ax−b∥\lVert\mathbf{A}\mathbf{x}-\mathbf{b}\rVert smallest (P1.B11), it is the shortest one. When A\mathbf{A} is square and invertible, A+=A−1\mathbf{A}^+=\mathbf{A}^{-1}.

Example 1: independent columns. The tall matrix A3=(100111)\mathbf{A}_3=\begin{pmatrix}1&0\\0&1\\1&1\end{pmatrix} of P1.B9 has A3⊤A3=(2112)\mathbf{A}_3^\top\mathbf{A}_3=\begin{pmatrix}2&1\\1&2\end{pmatrix}, with eigenvalues 33 and 11 (the matrix of P1.B12). So σ1=3\sigma_1=\sqrt3 and σ2=1\sigma_2=1. Inverting these and rebuilding gives

A3+=13(2−11−121)\mathbf{A}_3^+=\frac13\begin{pmatrix}2&-1&1\\-1&2&1\end{pmatrix}

which is the left inverse L\mathbf{L} that P1.B9 wrote down and promised to explain. It equals (A3⊤A3)−1A3⊤(\mathbf{A}_3^\top\mathbf{A}_3)^{-1}\mathbf{A}_3^\top, the least-squares formula of P1.B11. This is the general fact: if the columns of A\mathbf{A} are independent, then A+=(A⊤A)−1A⊤\mathbf{A}^+=(\mathbf{A}^\top\mathbf{A})^{-1}\mathbf{A}^\top, because then A⊤A=VΣ2V⊤\mathbf{A}^\top\mathbf{A}=\mathbf{V}\boldsymbol{\Sigma}^2\mathbf{V}^\top is invertible and its inverse times A⊤\mathbf{A}^\top gives VΣ−1U⊤\mathbf{V}\boldsymbol{\Sigma}^{-1}\mathbf{U}^\top (the shape version of Σ+\boldsymbol{\Sigma}^+). Then A+A=I\mathbf{A}^+\mathbf{A}=\mathbf{I}.

Example 2: dependent columns. The “same xx” matrix of P1.B11 has two columns that are multiples of one another:

B=(121212),y=(132)\mathbf{B}=\begin{pmatrix}1&2\\1&2\\1&2\end{pmatrix},\qquad\mathbf{y}=\begin{pmatrix}1\\3\\2\end{pmatrix}

It has rank 11 with σ1=15≈3.873\sigma_1=\sqrt{15}\approx3.873 and σ2=0\sigma_2=0. Only one layer exists, B=σ1u1v1⊤\mathbf{B}=\sigma_1\mathbf{u}_1\mathbf{v}_1^\top, so B+=1σ1v1u1⊤=115B⊤\mathbf{B}^+=\tfrac1{\sigma_1}\mathbf{v}_1\mathbf{u}_1^\top=\tfrac1{15}\mathbf{B}^\top, and

B+y=115(111222)(132)=615(12)=(0.40.8)\begin{aligned} \mathbf{B}^+\mathbf{y}&=\frac1{15}\begin{pmatrix}1&1&1\\2&2&2\end{pmatrix}\begin{pmatrix}1\\3\\2\end{pmatrix}\\ &=\frac{6}{15}\begin{pmatrix}1\\2\end{pmatrix}=\begin{pmatrix}0.4\\0.8\end{pmatrix} \end{aligned}

This is the answer np.linalg.lstsq gave in P1.B11. Many weights fit equally well (any c+2m=2c+2m=2): (2,0)(2,0) has length 22, (0,1)(0,1) has length 11, and (0.4,0.8)(0.4,0.8) has length 0.8≈0.894\sqrt{0.8}\approx0.894, the shortest. The pseudo-inverse picks it because it leaves the direction v2\mathbf{v}_2 (the null space) at zero: it never adds a null-space part, which could only make x\mathbf{x} longer.

Warnings. A+A\mathbf{A}^+\mathbf{A} is the identity only when the columns are independent (otherwise it is the projection onto the row space), and AA+\mathbf{A}\mathbf{A}^+ is the identity only when the rows are independent. In floating point, “zero” means “below a cutoff”: np.linalg.pinv treats singular values that are tiny compared with σ1\sigma_1 as zero, much as matrix_rank does, though the default cutoff differs (pinv drops singular values up to 10−1510^{-15} times σ1\sigma_1).

Conditioning and numerical rank

The condition number from P1.B8 and P1.B9 can now be written down. It is the ratio of the largest to the smallest stretch, κ(A)=σ1/σn\kappa(\mathbf{A})=\sigma_1/\sigma_n for a square n×nn\times n matrix (for any shape, the largest singular value over the smallest of the min⁡(m,n)\min(m,n) of them; if the smallest is 00, κ\kappa is infinite). The shear has κ=2.618\kappa=2.618, a rotation κ=1\kappa=1.

Why do small singular values matter? Inverting divides by each σi\sigma_i: A−1=VΣ−1U⊤\mathbf{A}^{-1}=\mathbf{V}\boldsymbol{\Sigma}^{-1}\mathbf{U}^\top. Any noise in b\mathbf{b} along un\mathbf{u}_n is multiplied by 1/σn1/\sigma_n, and if σn\sigma_n is tiny that is a huge number. The nearly parallel basis of P1.B9, C=(1100.001)\mathbf{C}=\begin{pmatrix}1&1\\0&0.001\end{pmatrix}, has σ1≈1.414\sigma_1\approx1.414 and σ2≈0.000707\sigma_2\approx0.000707, so κ≈2000\kappa\approx2000. With b=(3,1)\mathbf{b}=(3,1) the solution is (−997,1000)(-997,1000); changing b2b_2 by 0.0010.001 moves the answer to (−998,1001)(-998,1001), a change of 11 in each weight from a change of 0.0010.001 in the data.

Numerical rank is the count of singular values above a tolerance: np.linalg.matrix_rank uses (by default) σmax⁡⋅max⁡(m,n)⋅ε\sigma_{\max}\cdot\max(m,n)\cdot\varepsilon, with ε≈2.2×10−16\varepsilon\approx2.2\times10^{-16} (P1.B7). For M\mathbf{M} that is about 1.1×10−141.1\times10^{-14}, and the third singular value of M\mathbf{M} is far below it, so the rank is 22. In real data the question is rarely “is this exactly zero” but “where do the singular values fall off”, and a good cutoff is a modelling choice.

Notice the contrast: to approximate A\mathbf{A} the small singular values are the ones to drop, but to invert A\mathbf{A} they are the dangerous ones, because their inverses are the largest. Ridge regression (P3.A6) is a gentler way to handle them than cutting them off.

PCA via the SVD

Let X\mathbf{X} be an N×dN\times d data matrix whose columns have been centred (each column has mean 00, P1.D7), with one row per example. Its sample covariance matrix is C=1N−1X⊤X\mathbf{C}=\tfrac1{N-1}\mathbf{X}^\top\mathbf{X} (some books divide by NN instead; then the numbers below change by that factor). If X=UΣV⊤\mathbf{X}=\mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^\top, then

C=V Σ⊤ΣN−1 V⊤\mathbf{C}=\mathbf{V}\,\frac{\boldsymbol{\Sigma}^\top\boldsymbol{\Sigma}}{N-1}\,\mathbf{V}^\top

So the right singular vectors vi\mathbf{v}_i are the principal directions (the eigenvectors of the covariance matrix of P1.B12), and the variance along vi\mathbf{v}_i is σi2/(N−1)\sigma_i^2/(N-1). The coordinates of the data along them are the columns of XV=UΣ\mathbf{X}\mathbf{V}=\mathbf{U}\boldsymbol{\Sigma}. Keeping only the first kk is the best rank-kk approximation of the data (Eckart–Young), the idea of P3.C3.

Example: four points (−2,−1)(-2,-1), (1,2)(1,2), (1,−1)(1,-1), (0,0)(0,0) have mean (0,0)(0,0) and sample covariance (2112)\begin{pmatrix}2&1\\1&2\end{pmatrix}, the matrix of P1.B12. The SVD of the 4×24\times2 matrix X\mathbf{X} has σ1=3\sigma_1=3 and σ2=3\sigma_2=\sqrt3, so the variances are 9/3=39/3=3 and 3/3=13/3=1: the eigenvalues 33 and 11 of P1.B12, along (1,1)/2(1,1)/\sqrt2 and (1,−1)/2(1,-1)/\sqrt2 (up to sign). Why use the SVD rather than form C\mathbf{C} and take its eigenvectors? It works on X\mathbf{X} itself and so avoids squaring the condition number (P1.B11).

Try it in NumPy

np.linalg.svd(A) returns three things: U, a 1-D array S of singular values (largest first, not a matrix: use np.diag(S) to make Σ\boldsymbol{\Sigma}), and Vh, which is V⊤\mathbf{V}^\top (so the right singular vector vi\mathbf{v}_i is row Vh[i], not a column). By default it returns the full SVD; pass full_matrices=False for the thin one. The signs of the singular vectors are arbitrary: your U and Vh may differ in sign from the numbers below (a column of U and the matching row of Vh flip together). np.linalg.pinv(A) is the pseudo-inverse, np.linalg.norm(A, 2) is σ1\sigma_1, and np.linalg.cond(A) is σ1/σn\sigma_1/\sigma_n.

Try it: the SVD of the shear and the shapes

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

What to expect:

  • The singular values are 1.6181.618 and 0.6180.618. The rebuilt UΣV⊤\mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^\top equals A\mathbf{A} (True), and both lines A v_i and sigma_i u_i agree.
  • The σ2\sigma^2 values 2.6182.618 and 0.3820.382 are the eigenvalues of A⊤A\mathbf{A}^\top\mathbf{A}. The product of the σ\sigma‘s is 1=∣det⁡A∣1=|\det\mathbf{A}|, norm(A, 2) is 1.618=σ11.618=\sigma_1 and cond is 2.6182.618.
  • U and Vh on your machine may differ from the numbers in this lesson by signs (for example a column of U and the matching row of Vh both negated).
  • The shapes: full (3, 3) (2,) (2, 2) and thin (3, 2) (2,) (2, 2). The singular values of A3\mathbf{A}_3 are 3\sqrt3 and 11.

The second cell does the low-rank approximation, the pseudo-inverse and conditioning.

Try it: low rank, pseudo-inverse and conditioning

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

  • The singular values of M\mathbf{M} are about 16.84816.848, 1.0681.068 and a number that prints as 0. (really about 10−1610^{-16}). The rank-1 approximation matches the matrix shown earlier, has rank 11, and both error lines equal σ2=1.0684\sigma_2=1.0684. matrix_rank(M) is 22 and the tolerance is about 1.1×10−141.1\times10^{-14} (the exact digits can differ between machines).
  • pinv(B) has rows with entries 0.06670.0667 and 0.13330.1333 (=1/15=1/15 and 2/152/15), pinv(B) @ y and lstsq both give (0.4,0.8)(0.4,0.8).
  • 3 * pinv(A3) shows the left inverse of P1.B9 (a zero entry may print as -0.), and allclose prints True.
  • C has singular values 1.41421.4142 and about 0.00070.0007 and condition number about 20002000. The two solves return (−997,1000)(-997,1000) and (−998,1001)(-998,1001), up to rounding in the last printed digits.

Where you will meet it next

  • PCA (P3.C3). The SVD of the centred data matrix, above.
  • LoRA and low-rank fine-tuning (P10.E3). A weight update restricted to rank rr.
  • Recommender systems (P11.3). A table of user ratings is approximated by a product of two thin matrices (user features times item features). Real systems have missing ratings, so they fit the factors to the known entries instead of computing a plain SVD of the table, but the low-rank idea is the same.
  • Model compression (P11.8). A big weight matrix can be replaced by two thin factors, one of several compression ideas.
  • Linear regression and least squares (P3.A2). NumPy’s lstsq is computed from the SVD, and that is how it copes with dependent features (P1.B11).
  • Exploding gradients (P4.16). ∥Wx∥≤σ1∥x∥\lVert\mathbf{W}\mathbf{x}\rVert\le\sigma_1\lVert\mathbf{x}\rVert for every x\mathbf{x}, so σ1\sigma_1 (the 2-norm, or spectral norm) is the most a layer can stretch a vector. Keeping it near 11 is one way to keep signals and gradients stable.
  • Tensors and shapes (P1.B14) and the map of linear algebra in a neural network (P1.B15) come next.
Go deeper The four subspaces and the SVD

The SVD gives orthonormal bases for all four fundamental subspaces of an m×nm\times n matrix of rank rr. v1,…,vr\mathbf{v}_1,\dots,\mathbf{v}_r span the row space, vr+1,…,vn\mathbf{v}_{r+1},\dots,\mathbf{v}_n span the null space (Avi=0\mathbf{A}\mathbf{v}_i=\mathbf{0}), u1,…,ur\mathbf{u}_1,\dots,\mathbf{u}_r span the column space, and ur+1,…,um\mathbf{u}_{r+1},\dots,\mathbf{u}_m span the null space of A⊤\mathbf{A}^\top. The matrix is an invertible map from the row space to the column space (stretching vi\mathbf{v}_i to σiui\sigma_i\mathbf{u}_i) and sends the null space to zero. The pseudo-inverse runs the invertible part backwards and sends the null space of A⊤\mathbf{A}^\top to zero.

Go deeper Ridge regression as a soft cut-off of small singular values

For X=UΣV⊤\mathbf{X}=\mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^\top, ridge regression with penalty λ>0\lambda>0 (P3.A6) has the solution w=V diag ⁣(σiσi2+λ)U⊤y\mathbf{w}=\mathbf{V}\,\mathrm{diag}\!\left(\tfrac{\sigma_i}{\sigma_i^2+\lambda}\right)\mathbf{U}^\top\mathbf{y}. Compare with the pseudo-inverse, which uses 1/σi1/\sigma_i: for a large σi\sigma_i the two factors are almost equal, but for a tiny σi\sigma_i the ridge factor is near σi/λ\sigma_i/\lambda (small) instead of the huge 1/σi1/\sigma_i. So ridge turns the sharp “keep or drop” of the pseudo-inverse into a smooth shrinking of the directions with small singular values.

Common mistakes

Check your understanding

  1. The shear (1101)\begin{pmatrix}1&1\\0&1\end{pmatrix} has eigenvalues 11 and 11. What is the largest factor by which it can stretch a vector?

  2. A real 5×35\times3 matrix has rank 33. What are the shapes of U\mathbf{U}, Σ\boldsymbol{\Sigma} and V\mathbf{V} in its thin (economy) SVD?

  3. A matrix has singular values 55, 22 and 11. The best rank-1 approximation of it (the top layer) has what error in the Frobenius norm?

  4. The equation c+2m=2c+2m=2 has many solutions (c,m)(c,m) for the least-squares problem with the matrix B\mathbf{B} of this lesson. Which one does the pseudo-inverse B+y\mathbf{B}^+\mathbf{y} pick?

  5. A square matrix has singular values 100100 and 0.010.01. What is its condition number, and what does it mean?

  6. A real 2×22\times2 matrix has singular values 22 and 22. What is it?

  7. A data matrix X\mathbf{X} with N=101N=101 rows has centred columns, and its first singular value is σ1=50\sigma_1=50. Using the sample covariance with N−1N-1 in the denominator, what is the variance of the data along the first principal direction?

Exercises

  1. Find the singular values of (300−4)\begin{pmatrix}3&0\\0&-4\end{pmatrix} and say what the minus sign becomes.
    Show answerThe matrix stretches the axes by 33 and 44 and flips the second one. A⊤A=diag(9,16)\mathbf{A}^\top\mathbf{A}=\mathrm{diag}(9,16), so σ=4\sigma=4 and 33 (sorted). The minus sign is moved into U\mathbf{U} (or V\mathbf{V}) as a reflection, so det⁡=−12<0\det=-12<0 and σ1σ2=12=∣det⁡∣\sigma_1\sigma_2=12=|\det|. The eigenvalues are 33 and −4-4 and the singular values are their absolute values.
  2. Find the singular values of (1212)\begin{pmatrix}1&2\\1&2\end{pmatrix} and its rank, and say where v2\mathbf{v}_2 points.
    Show answerA⊤A=(2448)\mathbf{A}^\top\mathbf{A}=\begin{pmatrix}2&4\\4&8\end{pmatrix} has trace 1010 and determinant 00, so eigenvalues 1010 and 00 and σ=10≈3.162\sigma=\sqrt{10}\approx3.162 and 00. The rank is 11. v2\mathbf{v}_2 is the direction squashed to the origin: A(2,−1)=(0,0)\mathbf{A}(2,-1)=(0,0), so v2=(2,−1)/5\mathbf{v}_2=(2,-1)/\sqrt5 up to sign.
  3. A 2×22\times2 matrix has singular values 55 and 33. Give ∣det⁡A∣\lvert\det\mathbf{A}\rvert, ∥A∥2\lVert\mathbf{A}\rVert_2, κ(A)\kappa(\mathbf{A}) and the singular values of A−1\mathbf{A}^{-1}.
    Show answer∣det⁡∣=5⋅3=15\lvert\det\rvert=5\cdot3=15, ∥A∥2=σ1=5\lVert\mathbf{A}\rVert_2=\sigma_1=5, κ=5/3≈1.67\kappa=5/3\approx1.67. A−1=VΣ−1U⊤\mathbf{A}^{-1}=\mathbf{V}\boldsymbol{\Sigma}^{-1}\mathbf{U}^\top has singular values 1/31/3 and 1/51/5, so its largest is 1/σ2=1/31/\sigma_2=1/3.
  4. Find an SVD of A=(0210)\mathbf{A}=\begin{pmatrix}0&2\\1&0\end{pmatrix} by the recipe.
    Show answerA⊤A=diag(1,4)\mathbf{A}^\top\mathbf{A}=\mathrm{diag}(1,4), so the eigenvalues are 44 (eigenvector (0,1)(0,1)) and 11 (eigenvector (1,0)(1,0)): σ1=2\sigma_1=2, v1=(0,1)\mathbf{v}_1=(0,1) and σ2=1\sigma_2=1, v2=(1,0)\mathbf{v}_2=(1,0). Then u1=Av1/σ1=(2,0)/2=(1,0)\mathbf{u}_1=\mathbf{A}\mathbf{v}_1/\sigma_1=(2,0)/2=(1,0) and u2=Av2/σ2=(0,1)\mathbf{u}_2=\mathbf{A}\mathbf{v}_2/\sigma_2=(0,1). It swaps the axes while stretching one by 22. (The eigenvalues of A\mathbf{A} itself are ±2\pm\sqrt2, not the singular values.)
  5. A 200×100200\times100 matrix is approximated with rank 55. How many numbers are stored, against the original, and up to which kk does the approximation save space?
    Show answer5⋅(200+100+1)=15055\cdot(200+100+1)=1505 against 20,00020{,}000 (about 7.5%7.5\%). It saves space while k<20000/301≈66.4k<20000/301\approx66.4, so for k≤66k\le66.
  6. Find the pseudo-inverse of the column vector a=(34)\mathbf{a}=\begin{pmatrix}3\\4\end{pmatrix} (a 2×12\times1 matrix) and use it to solve ax≈b\mathbf{a}x\approx\mathbf{b} for b=(1,2)\mathbf{b}=(1,2) in the least-squares sense.
    Show answerThe only singular value is ∥a∥=5\lVert\mathbf{a}\rVert=5, with u1=(3,4)/5\mathbf{u}_1=(3,4)/5 and v1=(1)\mathbf{v}_1=(1). So a+=15⋅15(3,4)=125(3,4)\mathbf{a}^+=\tfrac15\cdot\tfrac15(3,4)=\tfrac1{25}(3,4), a 1×21\times2 matrix. Then x=a+b=(3+8)/25=0.44x=\mathbf{a}^+\mathbf{b}=(3+8)/25=0.44. This is also (a⊤a)−1a⊤b=11/25(\mathbf{a}^\top\mathbf{a})^{-1}\mathbf{a}^\top\mathbf{b}=11/25, since the one column is independent.
  7. Show that for any real matrix A\mathbf{A} and unit vector v\mathbf{v}, ∥Av∥2=v⊤A⊤Av\lVert\mathbf{A}\mathbf{v}\rVert^2=\mathbf{v}^\top\mathbf{A}^\top\mathbf{A}\mathbf{v}, and use it to say why σ12\sigma_1^2 is the largest eigenvalue of A⊤A\mathbf{A}^\top\mathbf{A}.
    Show answer∥Av∥2=(Av)⊤(Av)=v⊤A⊤Av\lVert\mathbf{A}\mathbf{v}\rVert^2=(\mathbf{A}\mathbf{v})^\top(\mathbf{A}\mathbf{v})=\mathbf{v}^\top\mathbf{A}^\top\mathbf{A}\mathbf{v}. For a symmetric matrix such as A⊤A\mathbf{A}^\top\mathbf{A} this is largest, over unit v\mathbf{v}, along the eigenvector of its largest eigenvalue, where it equals that eigenvalue (P1.B12, covariance and PCA). So the largest stretch squared, σ12\sigma_1^2, is the largest eigenvalue of A⊤A\mathbf{A}^\top\mathbf{A}.
  8. Give the eigenvalues and the singular values of (0100)\begin{pmatrix}0&1\\0&0\end{pmatrix}.
    Show answerIt is triangular, so the eigenvalues are the diagonal entries, 00 and 00. But A⊤A=diag(0,1)\mathbf{A}^\top\mathbf{A}=\mathrm{diag}(0,1), so the singular values are 11 and 00: the matrix sends (0,1)(0,1) to (1,0)(1,0), a stretch of 11. The eigenvalues say nothing about this stretch.
  9. Predict the shapes np.linalg.svd(np.ones((5, 3)), full_matrices=False) returns, then check with code.
    Show answerU is 5×35\times3 (5, 3), S is (3,) and Vh is (3, 3). The matrix has rank 11, so only the first value of S is clearly non-zero (it is 15≈3.873\sqrt{15}\approx3.873) and the other two are zero up to rounding (around 10−1610^{-16}, or exactly 00).
  10. For the four-point data of the PCA section, what are the principal directions and the variances, from the singular values 33 and 3\sqrt3?
    Show answerThe variances are σi2/(N−1)\sigma_i^2/(N-1) with N=4N=4: 9/3=39/3=3 along (1,1)/2(1,1)/\sqrt2 and 3/3=13/3=1 along (1,−1)/2(1,-1)/\sqrt2 (signs are arbitrary). Their sum 44 is the trace of the covariance matrix, the total variance.

Key takeaways

  • Every real matrix A\mathbf{A} can be written A=UΣV⊤\mathbf{A}=\mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^\top with U\mathbf{U} and V\mathbf{V} orthogonal and Σ\boldsymbol{\Sigma} holding the singular values σ1≥σ2≥⋯≥0\sigma_1\ge\sigma_2\ge\dots\ge0 on its diagonal. Read it right to left: rotate or reflect (V⊤\mathbf{V}^\top), stretch along the axes (Σ\boldsymbol{\Sigma}), rotate or reflect (U\mathbf{U}). The unit circle becomes an ellipse with semi-axes σiui\sigma_i\mathbf{u}_i, and Avi=σiui\mathbf{A}\mathbf{v}_i=\sigma_i\mathbf{u}_i.
  • The singular values are the square roots of the eigenvalues of A⊤A\mathbf{A}^\top\mathbf{A}, the columns of V\mathbf{V} are its eigenvectors, and the number of non-zero singular values is the rank. For a symmetric positive semi-definite matrix they equal the eigenvalues; in general they do not (the shear has eigenvalues 1,11,1 but singular values 1.6181.618 and 0.6180.618).
  • A=∑iσiuivi⊤\mathbf{A}=\sum_i\sigma_i\mathbf{u}_i\mathbf{v}_i^\top is a sum of rank-1 layers. Keeping the top kk gives the best rank-kk approximation (Eckart–Young): the error is σk+1\sigma_{k+1} in the 2-norm and σk+12+σk+22+⋯\sqrt{\sigma_{k+1}^2+\sigma_{k+2}^2+\cdots} in the Frobenius norm. It stores k(m+n+1)k(m+n+1) numbers instead of mnmn, which saves space only for small kk.
  • The pseudo-inverse A+=VΣ+U⊤\mathbf{A}^+=\mathbf{V}\boldsymbol{\Sigma}^+\mathbf{U}^\top inverts the non-zero singular values and leaves the zeros. It gives the minimum-length least-squares solution, equals (A⊤A)−1A⊤(\mathbf{A}^\top\mathbf{A})^{-1}\mathbf{A}^\top when the columns are independent, and equals A−1\mathbf{A}^{-1} when that exists.
  • The condition number is σ1/σn\sigma_1/\sigma_n: inverting divides by every singular value, so tiny ones blow up noise. Numerical rank counts the singular values above a tolerance. PCA is the SVD of the centred data matrix; LoRA bets that a useful weight update is close to low rank.
Found a mistake? Tell us