Turn, stretch, turn
Lesson 3.6.3 found that a symmetric matrix stretches the plane along two perpendicular axes: . 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
Its eigenvalues are , not real (lesson 3.6.1), so no direction is kept. But it sends the unit vector to , which is 3 times the unit vector . It sends the perpendicular to , twice .
So two perpendicular directions go to two perpendicular directions, stretched by 3 and by 2. Collect the 's as the columns of and the 's as the columns of , and put 3 and 2 on the diagonal of :
Then . Read it right to left, as in lesson 3.2.2: turns the plane, stretches it along the axes, and turns it again.
Rule: The singular value decomposition
Every matrix has one (stated here without proof). The columns of and of are orthonormal: unit length and at right angles. holds the singular values on its diagonal and zeros elsewhere.
The are the right singular vectors, directions in the input. The 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): , so , and it keeps every length and angle.
An orthogonal matrix is a rotation, or a rotation and a reflection. Here is the rotation through 63.4° and the rotation through 26.6° (lesson 3.2.2), so turns back through 63.4°. A singular value has the units of the entries of : it is a stretch factor.
The picture: a circle becomes an ellipse
The unit circle holds every unit vector . Follow the three steps of . turns the circle, which leaves it a circle, with and now on the axes.
stretches it into an ellipse with semi-axes 3 and 2 along the axes. turns that ellipse so that its semi-axes point along and .
in three steps. The unit circle (dashed in the later panels) ends as an ellipse whose semi-axes, and , are the images of and .
So the singular values are the longest and shortest stretches. A table of unit vectors at different angles shows it:
| angle of | 0° | 30° | 63.4° | 90° | 120° | 153.4° |
|---|---|---|---|---|---|---|
| length of | 2.2361 | 2.7353 | 3 | 2.8284 | 2.349 | 2 |
The longest image, 3, comes from at 63.4°; the shortest, 2, from at 153.4°, a right angle further on. Unit vectors between them land between 2 and 3. Turning round to 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 and the ellipse , so multiplies areas by . That is .
Try it
The loop measures for the unit vectors of the table. np.arctan2(2, 1) is the angle of , 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°.
Try it
np.linalg.svd returns , the singular values and . The sign of each pair , is a free choice (see the NumPy section below). So the cell fixes it, making the last entry of each positive.
Times , the columns of are and , and the rows of are and .
Finding them: the eigenvalues of AᵀA
The squared length of an image is , by the transpose rule of lesson 3.2.3. So the stretches of are governed by , which is symmetric. By the spectral theorem (lesson 3.6.3) it has real eigenvalues and perpendicular eigenvectors.
Those eigenvectors are the , and the eigenvalues are the . In symbols, , since .
Rule: Singular values from AᵀA
is the diagonal matrix of the , and is the th eigenvalue of , largest first. Its eigenvectors are the ; each with follows from . In the same way , so the are its eigenvectors.
- 1
- 2
- 3
- 4
- 5
A tall example
The method works for any shape. Take the matrix
Then , with eigenvalues 6 and 4. So and , with and .
The left singular vectors live in . , so ; , so . maps the unit circle onto an ellipse with semi-axes and 2, lying flat in a plane of .
is , with eigenvalues 6, 4 and 0. The extra 0 belongs to , perpendicular to that plane: a direction 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 , has the extra eigenvalue 0. Rounding leaves a tiny number there, so the cell rounds; adding 0.0 turns a -0. into 0..
This is the method on paper, but a computer avoids forming . Squaring turns a singular value of , beside one of 1, into an eigenvalue of : the size of rounding errors, so its digits are lost. np.linalg.svd works on itself, as lesson 3.5.3's lstsq does; the last thing lstsq returns is the list of singular values of .
Rank, determinant and nearly flat ellipses
Lesson 3.2.3's singular matrix
has singular values 2.5 and 0. The ellipse has collapsed to a segment from to , where .
The segment lies on the line that lesson 3.2.3 found. The direction is sent to .
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
The second line is for a square 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, 's second singular value prints as a tiny number such as , not 0. np.linalg.matrix_rank counts the singular values above a tolerance: a few times , times the largest one. That is how it allowed for rounding in lesson 3.3.2, where came out with rank 1.
A singular value that is small but not 0 means a nearly flat ellipse. Lesson 3.3.2's has singular values 2.0005 and 0.0005. Their ratio 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 , two symmetric matrices, and maps from lessons 3.2.2 and 3.2.3:
| matrix | singular values | eigenvalues | |
|---|---|---|---|
| 3, 2 | none real | 6 | |
| 3, 1 | 3, 1 | 3 | |
| 3, 1 | 3, | 3 | |
| quarter turn | 1, 1 | none real | 1 |
| shear | 1.618, 0.618 | 1, 1 | 1 |
| 2.5, 0 | 2, 0 | 0 |
- Symmetric with eigenvalues at least 0: the SVD is the spectral decomposition, and .
- Symmetric with a negative eigenvalue: singular values are never negative. They are the sizes of the eigenvalues, and the minus sign moves into .
- 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 matrix from its trace and determinant (lesson 3.6.1), or says there are none real. Each line prints the singular values, the eigenvalues, and the product of the singular values.
The next lines count 's singular values above NumPy's tolerance, and work out the condition number.
A sum of rank-one pieces
Multiply out column by row, and the matrix splits into one piece per singular value. Each piece is a column times a row, a matrix (lesson 3.2.3). Every column of it is a multiple of , so its rank is 1.
Rule: Rank-one pieces
There are pieces, one for each nonzero singular value, where is the rank. The pieces come in order of size, largest first. Lesson 3.6.3 wrote a symmetric matrix the same way, as .
For the pieces are whole numbers:
Their sum is . The first piece, times a table of ones, is the part of that points along . For the pieces are and .
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 . The cell builds both pieces of and of (signs fixed as before), adds them up, and checks each piece has rank 1.
The SVD in NumPy
U, s, Vt = np.linalg.svd(A) returns three arrays:
sis a 1-D array of the singular values, largest first, not the matrix . For an matrix it holds of them.Vtis , so its rows are the .Uholds the as its columns.
By default is and is . For the tall , is : it includes , which no singular value uses. With full_matrices=False, keeps only the first columns, here , and U @ np.diag(s) @ Vt rebuilds .
Rule: Shapes from np.linalg.svd
These are the shapes with full_matrices=False, the form to use for rebuilding or keeping pieces. A wide matrix works the same way: has the same singular values as , with and swapped.
Signs. Flip both and and the piece 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 positive.
Try it
The first two lines compare the full and the reduced shapes for , and rebuild it. U * s scales column of by , the same as U @ np.diag(s).
Then the orthonormal checks and , the extra column (times ), and the singular values of .
Common mistakes
Vtis . Its rows are the . Taking its columns gives the wrong vectors.- Flip signs in pairs. Changing the sign of alone changes the matrix; flip and together, and fix a sign rule before comparing results.
- 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.
- Singular values are never negative, and come largest first. A minus sign belongs in or .
- The eigenvalues of are , not . Take square roots.
- A tiny singular value prints as , not 0. Count the rank with a tolerance, as
matrix_rankdoes. - This is not a covariance matrix. Books call it ; here stays the covariance matrix, and are not volatilities.
The singular value decomposition
0 of 12 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 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,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for 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 matrix is : turns, stretches along the axes by , and turns again. and have orthonormal columns, and .
- The unit circle lands on an ellipse with semi-axes : is the longest stretch, the smallest the shortest.
- are the eigenvalues of , with eigenvectors ; then .
- The rank is the number of nonzero singular values; for square ; is the condition number.
- , a sum of rank-one pieces, largest first.
np.linalg.svd(A, full_matrices=False)gives (),s(the ) andVt(, rows ). Signs come in pairs and are arbitrary: fix them.