SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 3.8.1 The singular value decompositionConcept · 35 min
      2. 3.8.2 Low-rank approximationConcept · 25 min
      3. 3.8.3 Practice set: the singular value decompositionPractice set · 20 min
      4. 3.8.4 Case study: compressing a factor modelCase study · 35 min

Level 3, Linear algebra 3.8 Singular value decomposition (optional) Lesson 1 of 4

ConceptLesson 3.8.1 in Level 3, Linear algebra. About 35 minutes.

The singular value decomposition

Turn, stretch, turn

Lesson 3.6.3 found that a symmetric matrix stretches the plane along two perpendicular axes: A=QΛQ⊤A = Q\Lambda Q^\top. Most matrices are not symmetric, and many have no real eigenvectors at all. Yet every matrix, of any shape, still stretches along perpendicular axes.

The difference is that the axes it stretches from are not the axes it stretches onto. Take

A=(22−12)A = \begin{pmatrix} 2 & 2 \\ -1 & 2 \end{pmatrix}

Its eigenvalues are 2±1.4142i2 \pm 1.4142i, not real (lesson 3.6.1), so no direction is kept. But it sends the unit vector v1=15(1,2)\mathbf{v}_1 = \frac{1}{\sqrt 5}(1, 2) to 15(6,3)\frac{1}{\sqrt 5}(6, 3), which is 3 times the unit vector u1=15(2,1)\mathbf{u}_1 = \frac{1}{\sqrt 5}(2, 1). It sends the perpendicular v2=15(−2,1)\mathbf{v}_2 = \frac{1}{\sqrt 5}(-2, 1) to 15(−2,4)\frac{1}{\sqrt 5}(-2, 4), twice u2=15(−1,2)\mathbf{u}_2 = \frac{1}{\sqrt 5}(-1, 2).

So two perpendicular directions go to two perpendicular directions, stretched by 3 and by 2. Collect the v\mathbf{v}'s as the columns of VV and the u\mathbf{u}'s as the columns of UU, and put 3 and 2 on the diagonal of SS:

U=15(2−112)S=(3002)V=15(1−221)\begin{gathered} U = \tfrac{1}{\sqrt 5}\begin{pmatrix} 2 & -1 \\ 1 & 2 \end{pmatrix} \\[6pt] S = \begin{pmatrix} 3 & 0 \\ 0 & 2 \end{pmatrix} \\[6pt] V = \tfrac{1}{\sqrt 5}\begin{pmatrix} 1 & -2 \\ 2 & 1 \end{pmatrix} \end{gathered}

Then A=USV⊤A = USV^\top. Read it right to left, as in lesson 3.2.2: V⊤V^\top turns the plane, SS stretches it along the axes, and UU turns it again.

Rule: The singular value decomposition

A=USV⊤Avi=σi ui\begin{gathered} A = U S V^\top \\[4pt] A\mathbf{v}_i = \sigma_i\,\mathbf{u}_i \end{gathered}

Every m×nm \times n matrix has one (stated here without proof). The columns vi\mathbf{v}_i of VV and ui\mathbf{u}_i of UU are orthonormal: unit length and at right angles. SS holds the singular values σ1≥σ2≥⋯≥0\sigma_1 \ge \sigma_2 \ge \cdots \ge 0 on its diagonal and zeros elsewhere.

The vi\mathbf{v}_i are the right singular vectors, directions in the input. The ui\mathbf{u}_i are the left singular vectors, directions in the output. A square matrix with orthonormal columns is orthogonal (lessons 3.5.2 and 3.6.3): Q⊤Q=IQ^\top Q = I, so Q−1=Q⊤Q^{-1} = Q^\top, and it keeps every length and angle.

An orthogonal 2×22 \times 2 matrix is a rotation, or a rotation and a reflection. Here VV is the rotation through 63.4° and UU the rotation through 26.6° (lesson 3.2.2), so V⊤V^\top turns back through 63.4°. A singular value has the units of the entries of AA: it is a stretch factor.

The picture: a circle becomes an ellipse

The unit circle holds every unit vector x\mathbf{x}. Follow the three steps of A=USV⊤A = USV^\top. V⊤V^\top turns the circle, which leaves it a circle, with v1\mathbf{v}_1 and v2\mathbf{v}_2 now on the axes.

SS stretches it into an ellipse with semi-axes 3 and 2 along the axes. UU turns that ellipse so that its semi-axes point along u1\mathbf{u}_1 and u2\mathbf{u}_2.

A=USV⊤A = USV^\top in three steps. The unit circle (dashed in the later panels) ends as an ellipse whose semi-axes, 3u13\mathbf{u}_1 and 2u22\mathbf{u}_2, are the images of v1\mathbf{v}_1 and v2\mathbf{v}_2.

So the singular values are the longest and shortest stretches. A table of unit vectors at different angles shows it:

angle of x\mathbf{x}0°30°63.4°90°120°153.4°
length of AxA\mathbf{x}2.23612.735332.82842.3492

The longest image, 3, comes from v1\mathbf{v}_1 at 63.4°; the shortest, 2, from v2\mathbf{v}_2 at 153.4°, a right angle further on. Unit vectors between them land between 2 and 3. Turning x\mathbf{x} round to −x-\mathbf{x} gives the same length, so half the circle tells the whole story.

The ellipse also shows the area rule of lesson 3.4.1. The circle has area π\pi and the ellipse π×3×2\pi \times 3 \times 2, so AA multiplies areas by 3×2=63 \times 2 = 6. That is det⁡A=2⋅2−2⋅(−1)=6\det A = 2 \cdot 2 - 2 \cdot (-1) = 6.

Try it

The loop measures ∥Ax∥\lVert A\mathbf{x} \rVert for the unit vectors of the table. np.arctan2(2, 1) is the angle of (1,2)(1, 2), 63.4°.

Then 1,801 angles across half the circle find the longest and shortest images, 3 at 63.4° and 2 at 153.4°.

⌘/Ctrl + Enter runs the cell

Try it

np.linalg.svd returns UU, the singular values and V⊤V^\top. The sign of each pair ui\mathbf{u}_i, vi\mathbf{v}_i is a free choice (see the NumPy section below). So the cell fixes it, making the last entry of each vi\mathbf{v}_i positive.

Times 5\sqrt 5, the columns of UU are (2,1)(2, 1) and (−1,2)(-1, 2), and the rows of V⊤V^\top are (1,2)(1, 2) and (−2,1)(-2, 1).

⌘/Ctrl + Enter runs the cell

Finding them: the eigenvalues of AᵀA

The squared length of an image is ∥Ax∥2=(Ax)⊤(Ax)=x⊤(A⊤A)x\lVert A\mathbf{x} \rVert^2 = (A\mathbf{x})^\top(A\mathbf{x}) = \mathbf{x}^\top(A^\top A)\mathbf{x}, by the transpose rule of lesson 3.2.3. So the stretches of AA are governed by A⊤AA^\top A, which is symmetric. By the spectral theorem (lesson 3.6.3) it has real eigenvalues and perpendicular eigenvectors.

Those eigenvectors are the vi\mathbf{v}_i, and the eigenvalues are the σi2\sigma_i^2. In symbols, A⊤A=VS⊤U⊤USV⊤=V(S⊤S)V⊤A^\top A = VS^\top U^\top U S V^\top = V(S^\top S)V^\top, since U⊤U=IU^\top U = I.

Rule: Singular values from AᵀA

A⊤A=V (S⊤S) V⊤σi=λi(A⊤A)ui=Aviσi\begin{gathered} A^\top A = V\,(S^\top S)\,V^\top \\[4pt] \sigma_i = \sqrt{\lambda_i(A^\top A)} \\[4pt] \mathbf{u}_i = \frac{A\mathbf{v}_i}{\sigma_i} \end{gathered}

S⊤SS^\top S is the diagonal matrix of the σi2\sigma_i^2, and λi(A⊤A)\lambda_i(A^\top A) is the iith eigenvalue of A⊤AA^\top A, largest first. Its eigenvectors are the vi\mathbf{v}_i; each ui\mathbf{u}_i with σi>0\sigma_i > 0 follows from AviA\mathbf{v}_i. In the same way AA⊤=U(SS⊤)U⊤AA^\top = U(SS^\top)U^\top, so the ui\mathbf{u}_i are its eigenvectors.

  1. 1
    A⊤A=(5228)A^\top A = \begin{pmatrix} 5 & 2 \\ 2 & 8 \end{pmatrix}
  2. 2
    λ2−13λ+36=(λ−9)(λ−4)\lambda^2 - 13\lambda + 36 = (\lambda - 9)(\lambda - 4)
  3. 3
    σ1=3,σ2=2\sigma_1 = 3, \quad \sigma_2 = 2
  4. 4
    v1=15(1,2)v2=15(−2,1)\begin{gathered} \mathbf{v}_1 = \tfrac{1}{\sqrt 5}(1, 2) \\[4pt] \mathbf{v}_2 = \tfrac{1}{\sqrt 5}(-2, 1) \end{gathered}
  5. 5
    u1=135(6,3)=15(2,1)u2=125(−2,4)=15(−1,2)\begin{gathered} \mathbf{u}_1 = \tfrac{1}{3\sqrt 5}(6, 3) = \tfrac{1}{\sqrt 5}(2, 1) \\[4pt] \mathbf{u}_2 = \tfrac{1}{2\sqrt 5}(-2, 4) = \tfrac{1}{\sqrt 5}(-1, 2) \end{gathered}

A tall example

The method works for any shape. Take the 3×23 \times 2 matrix

B=(201102)B = \begin{pmatrix} 2 & 0 \\ 1 & 1 \\ 0 & 2 \end{pmatrix}

Then B⊤B=(5115)B^\top B = \begin{pmatrix} 5 & 1 \\ 1 & 5 \end{pmatrix}, with eigenvalues 6 and 4. So σ1=6≈2.4495\sigma_1 = \sqrt 6 \approx 2.4495 and σ2=2\sigma_2 = 2, with v1=12(1,1)\mathbf{v}_1 = \frac{1}{\sqrt 2}(1, 1) and v2=12(−1,1)\mathbf{v}_2 = \frac{1}{\sqrt 2}(-1, 1).

The left singular vectors live in R3\mathbb{R}^3. Bv1=12(2,2,2)B\mathbf{v}_1 = \frac{1}{\sqrt 2}(2, 2, 2), so u1=13(1,1,1)\mathbf{u}_1 = \frac{1}{\sqrt 3}(1, 1, 1); Bv2=12(−2,0,2)B\mathbf{v}_2 = \frac{1}{\sqrt 2}(-2, 0, 2), so u2=12(−1,0,1)\mathbf{u}_2 = \frac{1}{\sqrt 2}(-1, 0, 1). BB maps the unit circle onto an ellipse with semi-axes 6\sqrt 6 and 2, lying flat in a plane of R3\mathbb{R}^3.

BB⊤BB^\top is 3×33 \times 3, with eigenvalues 6, 4 and 0. The extra 0 belongs to u3=16(1,−2,1)\mathbf{u}_3 = \frac{1}{\sqrt 6}(1, -2, 1), perpendicular to that plane: a direction BB never reaches.

Try it

eigh (lesson 3.6.3) lists eigenvalues smallest first, so [::-1] reverses them. Their square roots match the singular values from svd.

For BB, BB⊤BB^\top has the extra eigenvalue 0. Rounding leaves a tiny number there, so the cell rounds; adding 0.0 turns a -0. into 0..

⌘/Ctrl + Enter runs the cell

This is the method on paper, but a computer avoids forming A⊤AA^\top A. Squaring turns a singular value of 10−810^{-8}, beside one of 1, into an eigenvalue of 10−1610^{-16}: the size of rounding errors, so its digits are lost. np.linalg.svd works on AA itself, as lesson 3.5.3's lstsq does; the last thing lstsq returns is the list of singular values of AA.

Rank, determinant and nearly flat ellipses

Lesson 3.2.3's singular matrix

Z=(120.51)Z = \begin{pmatrix} 1 & 2 \\ 0.5 & 1 \end{pmatrix}

has singular values 2.5 and 0. The ellipse has collapsed to a segment from −2.5u1-2.5\mathbf{u}_1 to 2.5u12.5\mathbf{u}_1, where u1=15(2,1)\mathbf{u}_1 = \frac{1}{\sqrt 5}(2, 1).

The segment lies on the line x2=x1/2x_2 = x_1/2 that lesson 3.2.3 found. The direction v2=15(−2,1)\mathbf{v}_2 = \frac{1}{\sqrt 5}(-2, 1) is sent to 0\mathbf{0}.

Each zero singular value is a direction squashed flat. So the rank, the number of pivots in lesson 3.3.2, is also the number of singular values that are not 0.

Rule: Rank and determinant

rank⁡A=number of σi>0∣det⁡A∣=σ1σ2⋯σn\begin{gathered} \operatorname{rank} A = \text{number of } \sigma_i > 0 \\[4pt] \lvert\det A\rvert = \sigma_1\sigma_2\cdots\sigma_n \end{gathered}

The second line is for a square n×nn \times n matrix: the ellipse's area (or the box's volume) over the circle's, as in lesson 3.4.1. One zero singular value makes the determinant 0.

On a computer, ZZ's second singular value prints as a tiny number such as 10−1710^{-17}, not 0. np.linalg.matrix_rank counts the singular values above a tolerance: a few times 10−1610^{-16}, times the largest one. That is how it allowed for rounding in lesson 3.3.2, where (0.10.70.32.1)\begin{pmatrix} 0.1 & 0.7 \\ 0.3 & 2.1 \end{pmatrix} came out with rank 1.

A singular value that is small but not 0 means a nearly flat ellipse. Lesson 3.3.2's (1111.001)\begin{pmatrix} 1 & 1 \\ 1 & 1.001 \end{pmatrix} has singular values 2.0005 and 0.0005. Their ratio σ1/σn\sigma_1/\sigma_n is the condition number that np.linalg.cond printed there: 4002.

Singular values against eigenvalues

For most matrices the two lists differ. The table compares them for AA, two symmetric matrices, and maps from lessons 3.2.2 and 3.2.3:

matrixsingular valueseigenvalues∣det⁡∣\lvert\det\rvert
AA3, 2none real6
(2112)\begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix}3, 13, 13
(1221)\begin{pmatrix} 1 & 2 \\ 2 & 1 \end{pmatrix}3, 13, −1-13
quarter turn1, 1none real1
shear (1101)\begin{pmatrix} 1 & 1 \\ 0 & 1 \end{pmatrix}1.618, 0.6181, 11
ZZ2.5, 02, 00
  • Symmetric with eigenvalues at least 0: the SVD is the spectral decomposition, U=V=QU = V = Q and S=ΛS = \Lambda.
  • Symmetric with a negative eigenvalue: singular values are never negative. They are the sizes of the eigenvalues, and the minus sign moves into UU.
  • A rotation keeps every length, so the circle stays a circle: both singular values are 1.
  • The shear keeps only one direction, with eigenvalue 1 twice (lesson 3.6.1), yet it stretches by up to 1.618.

Try it

eig2 finds the eigenvalues of a 2×22 \times 2 matrix from its trace and determinant (lesson 3.6.1), or says there are none real. Each line prints the singular values, the eigenvalues, ∣det⁡∣\lvert\det\rvert and the product of the singular values.

The next lines count ZZ's singular values above NumPy's tolerance, and work out the condition number.

⌘/Ctrl + Enter runs the cell

A sum of rank-one pieces

Multiply out USV⊤USV^\top column by row, and the matrix splits into one piece per singular value. Each piece uivi⊤\mathbf{u}_i\mathbf{v}_i^\top is a column times a row, a matrix (lesson 3.2.3). Every column of it is a multiple of ui\mathbf{u}_i, so its rank is 1.

Rule: Rank-one pieces

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

There are rr pieces, one for each nonzero singular value, where rr is the rank. The pieces come in order of size, largest first. Lesson 3.6.3 wrote a symmetric matrix the same way, as ∑λiqiqi⊤\sum \lambda_i\mathbf{q}_i\mathbf{q}_i^\top.

For BB the pieces are whole numbers:

6 u1v1⊤=(111111)2 u2v2⊤=(1−100−11)\begin{gathered} \sqrt 6\,\mathbf{u}_1\mathbf{v}_1^\top = \begin{pmatrix} 1 & 1 \\ 1 & 1 \\ 1 & 1 \end{pmatrix} \\[6pt] 2\,\mathbf{u}_2\mathbf{v}_2^\top = \begin{pmatrix} 1 & -1 \\ 0 & 0 \\ -1 & 1 \end{pmatrix} \end{gathered}

Their sum is BB. The first piece, 6⋅13⋅12=1\sqrt 6 \cdot \frac{1}{\sqrt 3} \cdot \frac{1}{\sqrt 2} = 1 times a table of ones, is the part of BB that points along (1,1,1)(1, 1, 1). For AA the pieces are (1.22.40.61.2)\begin{pmatrix} 1.2 & 2.4 \\ 0.6 & 1.2 \end{pmatrix} and (0.8−0.4−1.60.8)\begin{pmatrix} 0.8 & -0.4 \\ -1.6 & 0.8 \end{pmatrix}.

Lesson 3.8.2 keeps only the first few pieces, to make the closest copy of a matrix with a lower rank. Case study 3.8.4 does it with a table of asset returns.

Try it

np.outer(u, v) is uv⊤\mathbf{u}\mathbf{v}^\top. The cell builds both pieces of BB and of AA (signs fixed as before), adds them up, and checks each piece has rank 1.

⌘/Ctrl + Enter runs the cell

The SVD in NumPy

U, s, Vt = np.linalg.svd(A) returns three arrays:

  • s is a 1-D array of the singular values, largest first, not the matrix SS. For an m×nm \times n matrix it holds min⁡(m,n)\min(m, n) of them.
  • Vt is V⊤V^\top, so its rows are the vi\mathbf{v}_i.
  • U holds the ui\mathbf{u}_i as its columns.

By default UU is m×mm \times m and V⊤V^\top is n×nn \times n. For the tall BB, UU is 3×33 \times 3: it includes u3\mathbf{u}_3, which no singular value uses. With full_matrices=False, UU keeps only the first min⁡(m,n)\min(m, n) columns, here 3×23 \times 2, and U @ np.diag(s) @ Vt rebuilds BB.

Rule: Shapes from np.linalg.svd

A: m×n,k=min⁡(m,n)U: m×k,s: k,V⊤: k×n\begin{gathered} A:\ m \times n, \quad k = \min(m, n) \\[4pt] U:\ m \times k, \quad s:\ k, \quad V^\top:\ k \times n \end{gathered}

These are the shapes with full_matrices=False, the form to use for rebuilding AA or keeping pieces. A wide matrix works the same way: B⊤B^\top has the same singular values as BB, with UU and VV swapped.

Signs. Flip both ui\mathbf{u}_i and vi\mathbf{v}_i and the piece σiuivi⊤\sigma_i\mathbf{u}_i\mathbf{v}_i^\top is unchanged, so NumPy may return either sign, and two computers can differ. Fix a rule before printing or comparing; this lesson makes the last entry of each vi\mathbf{v}_i positive.

Try it

The first two lines compare the full and the reduced shapes for BB, and rebuild it. U * s scales column ii of UU by σi\sigma_i, the same as U @ np.diag(s).

Then the orthonormal checks U⊤U=IU^\top U = I and V⊤V=IV^\top V = I, the extra column u3\mathbf{u}_3 (times 6\sqrt 6), and the singular values of B⊤B^\top.

⌘/Ctrl + Enter runs the cell

Common mistakes

  1. Vt is V⊤V^\top. Its rows are the vi\mathbf{v}_i. Taking its columns gives the wrong vectors.
  2. Flip signs in pairs. Changing the sign of ui\mathbf{u}_i alone changes the matrix; flip ui\mathbf{u}_i and vi\mathbf{v}_i together, and fix a sign rule before comparing results.
  3. Singular values are not eigenvalues. They agree only for symmetric matrices with no negative eigenvalues. The shear has eigenvalues 1 and 1 but singular values 1.618 and 0.618.
  4. Singular values are never negative, and come largest first. A minus sign belongs in UU or VV.
  5. The eigenvalues of A⊤AA^\top A are σi2\sigma_i^2, not σi\sigma_i. Take square roots.
  6. A tiny singular value prints as 10−1710^{-17}, not 0. Count the rank with a tolerance, as matrix_rank does.
  7. This SS is not a covariance matrix. Books call it Σ\Sigma; here Σ\Sigma stays the covariance matrix, and σi\sigma_i are not volatilities.

The singular value decomposition

0 of 12 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9.
  10. 10.
  11. 11.
  12. 12.
How to type answers
  • Powers with ^: x^7, 2^(1/3), a^-2.
  • Multiply with * or by writing side by side: 3x^2, 2(x+1).
  • ln(x) is the natural log, log(x, 2) is log base 2, e is Euler's number, sqrt(x) is a square root.
  • sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for sin²x.
  • Your first answer to each question is saved: it counts towards your score and brings the question back for review.
  • Exact answers like ln(20)/ln(3) are best. Decimals need about 4 significant figures.

Summary card

  • Every m×nm \times n matrix is A=USV⊤A = USV^\top: V⊤V^\top turns, SS stretches along the axes by σ1≥σ2≥⋯≥0\sigma_1 \ge \sigma_2 \ge \cdots \ge 0, and UU turns again. UU and VV have orthonormal columns, and Avi=σiuiA\mathbf{v}_i = \sigma_i\mathbf{u}_i.
  • The unit circle lands on an ellipse with semi-axes σiui\sigma_i\mathbf{u}_i: σ1\sigma_1 is the longest stretch, the smallest σ\sigma the shortest.
  • σi2\sigma_i^2 are the eigenvalues of A⊤AA^\top A, with eigenvectors vi\mathbf{v}_i; then ui=Avi/σi\mathbf{u}_i = A\mathbf{v}_i/\sigma_i.
  • The rank is the number of nonzero singular values; ∣det⁡A∣=σ1⋯σn\lvert\det A\rvert = \sigma_1\cdots\sigma_n for square AA; σ1/σn\sigma_1/\sigma_n is the condition number.
  • A=∑iσiuivi⊤A = \sum_i \sigma_i\mathbf{u}_i\mathbf{v}_i^\top, a sum of rank-one pieces, largest first.
  • np.linalg.svd(A, full_matrices=False) gives UU (m×km \times k), s (the σi\sigma_i) and Vt (V⊤V^\top, rows vi\mathbf{v}_i). Signs come in pairs and are arbitrary: fix them.

Free further reading

Optional. This lesson covers what you need; these add more examples and practice. All are free to read.