Keep the biggest pieces
Lesson 3.8.1 wrote every matrix as a sum of rank-one pieces, largest first:
Here are the singular values, and the left and right singular vectors, and the rank. Keep only the first pieces and you get a matrix of rank that is close to .
Rule: The rank-k copy
holds the first columns of , the first singular values, and the first rows of . When , has rank .
Lesson 3.8.1's tall matrix has two pieces:
is the first piece, and the error is the piece left out, . To say how good is, we need a way to measure the size of a matrix.
The size of a matrix
The Frobenius norm. Read the entries of as one long vector and take its length (lesson 3.1.2): the square root of the sum of the squares of all the entries. It is written .
For the squares are , which add up to 10, so . That is also . The turns and keep lengths, so only the stretches in count.
The 2-norm. The largest stretch: the longest for a unit vector , written . Lesson 3.8.1 found it is the longest semi-axis of the ellipse, . For it is .
Rule: Two sizes of a matrix
Both are in the units of the entries, and both are 0 only for the zero matrix. The Frobenius norm counts every entry; the 2-norm only the worst direction, so .
Try it
np.linalg.norm(B) is the Frobenius norm; np.linalg.norm(B, 2) is the 2-norm. The first line prints both, then from the singular values.
The next lines build from the first piece and measure the error both ways.
The best copy, and its error
No matrix of rank is closer to than , in either norm. This result is the Eckart–Young theorem, stated here without proof. The error is made of the pieces left out, so its size comes straight from the singular values.
Rule: The error of the rank-k copy
In the 2-norm the error is the first singular value left out. In the Frobenius norm it is the root of the sum of the squares of all those left out. For there is nothing left out and .
For only is left out, so both errors are 2. The cell above agrees: the four entries of give .
Other rank-1 copies do worse. Keeping only the first column of , , leaves an error of . Even times the table of ones, a small change to , leaves 2.0149.
The share kept
The squared Frobenius norm splits exactly between the copy and the error, like the two sides of a right angle: . For , . So the copy keeps a share of the squared size.
Rule: Share kept by the rank-k copy
A number between 0 and 1, often quoted in percent. The share left out is 1 minus it, and the error relative to the size, , is the square root of the share left out.
keeps of the squared size of . Its relative error is , which is .
Try it
The two other rank-1 copies and their errors, then the split and the share kept by .
A larger example: choosing k
Here is a matrix of whole numbers. Its rows run from falling, , through flat to rising, :
Its singular values are 10.876, 6.016, 0.649 and 0.320, and the squares of its entries add up to 155. The squares of the singular values add up to 155 too: 118.28, 36.19, 0.42 and 0.10.
The singular values of drop sharply after the second. The rank-2 copy already keeps 99.66% of the squared size; the last two pieces add only 0.34%.
| 0 | 1 | 2 | 3 | 4 | |
|---|---|---|---|---|---|
| share kept | 0% | 76.31% | 99.66% | 99.93% | 100% |
| 10.8758 | 6.0162 | 0.6488 | 0.3195 | 0 | |
| 12.4499 | 6.0595 | 0.7232 | 0.3195 | 0 |
The rank-2 copy differs from by at most 0.34 in any entry. Its first row, for example, is against .
The two pieces it keeps have a meaning: has every entry positive, a common level. runs from negative to positive: a tilt from the first column to the last. Each row of is close to some amount of the level plus some amount of the tilt.
How to choose k
- A target share. Pick the smallest whose share reaches a target, such as 90% or 99%. Here 90% needs , and so does 99%.
- A gap. Look for a sharp drop in the singular values. From 6.016 to 0.649 is a fall by a factor of more than 9, so .
- What the small pieces are. If the entries carry measurement or rounding errors, the smallest pieces are often just those errors. Dropping them can make the copy closer to the truth, not further from it.
The choice is a judgement, and the error rules say what it costs. With the worst direction is off by .
Try it
The loop builds for each as (U[:, :k] * s[:k]) @ Vt[:k]: the first columns, values and rows. It prints the share kept and both errors, which match the rules; then the gap , and in percent, a wrong share (see the common mistakes).
Then to two decimals and its largest entry error. The signs of the are fixed so their last entries are positive.
Storage
A rank- copy of an matrix is stored as , and : numbers instead of .
For that is no saving: , the same as its 20 entries. The saving comes with size. A matrix holds 500,000 numbers, and a rank-10 copy needs 15,010, about 3%.
Try it
The counts for and for the large matrix, and the large copy's share of the full storage.
Cases to know
- keeps nothing: is the zero matrix and the error is all of .
- equal to the rank keeps everything. The table has singular values 14, 0 and 0, so is the table itself, stored as 7 numbers instead of 9.
- Ties. If , the best copy is not unique. The identity has singular values 1 and 1, and every direction gives an equally good rank-1 copy.
- Scaling. Multiplying by multiplies every singular value by . The errors scale too, but the shares do not change: keeps 76.31% at , like .
Case study 3.8.4 uses this to compress a table of asset returns into a few factors.
Try it
The rank-1 table: its singular values (adding 0.0 turns a -0. into 0.), and its rank-1 copy, which is the table. Then the tie for , and the shares of .
Common mistakes
- Shares use squares. The share kept is over the top against the total. For , is 60.89%, but the true share is 76.31%.
- The 2-norm error is the first value left out, , not the last one kept, .
- The Frobenius error is a root of a sum of squares, not the sum of the singular values left out.
- Keep matching parts. Take the first columns of , the first singular values and the first rows of .
- Low rank is not small entries. Setting the smallest entries of to 0, or dropping a row or column, does not give the best copy: keeping 's first column leaves 2.2361 against 2.
- Small matrices save nothing. can equal or pass .
Low-rank approximation
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
- The rank- copy keeps the first pieces: .
- Sizes: counts every entry; is the largest stretch.
- is the best copy of rank (Eckart–Young): and .
- It keeps the share of the squared size. Choose by a target share, a gap in the singular values, or the size of the errors in the data.
- Storage: numbers instead of , a saving only for large matrices and small .