Computing the SVD
The singular value decomposition exists for every matrix, and it answers the questions other factorizations struggle with: the numerical rank, the 2-norm and condition number, the best low-rank approximation, and least squares for rank-deficient problems. This lesson looks at how it is computed and used numerically. The theory is in Singular Value Decomposition.
The decomposition
For (): is orthogonal, is orthogonal, is diagonal with . Key facts:
- , .
- The rank is the number of nonzero singular values.
- are the eigenvalues of , but computing them that way squares the condition number.
How it is computed
Like the QR algorithm, the SVD is computed in two stages:
- Bidiagonalization: Householder reflections from the left and the right reduce to an upper bidiagonal matrix , about flops.
- Singular values of by an implicitly shifted QR iteration on that never forms (Golub-Kahan), or by divide and conquer.
Forming explicitly loses the small singular values:
import numpy as np
rng = np.random.default_rng(81)
U, _ = np.linalg.qr(rng.normal(size=(50, 6)))
V, _ = np.linalg.qr(rng.normal(size=(6, 6)))
sig = np.array([1.0, 1e-2, 1e-4, 1e-6, 1e-8, 1e-10])
A = U @ np.diag(sig) @ V.T
via_svd = np.linalg.svd(A, compute_uv=False)
via_eig = np.sqrt(np.abs(np.linalg.eigvalsh(A.T @ A)))[::-1]
for s, a, b in zip(sig, via_svd, via_eig):
print(f"true {s:.0e}: svd {a:.6e}, sqrt(eig(A^T A)) {b:.6e}")true 1e+00: svd 1.000000e+00, sqrt(eig(A^T A)) 1.000000e+00
true 1e-02: svd 1.000000e-02, sqrt(eig(A^T A)) 1.000000e-02
true 1e-04: svd 1.000000e-04, sqrt(eig(A^T A)) 1.000000e-04
true 1e-06: svd 1.000000e-06, sqrt(eig(A^T A)) 1.000015e-06
true 1e-08: svd 1.000000e-08, sqrt(eig(A^T A)) 3.318475e-09
true 1e-10: svd 1.000000e-10, sqrt(eig(A^T A)) 5.807072e-09
Numerical rank
In floating point, a rank-deficient matrix has tiny but nonzero singular values. The numerical rank counts the singular values above a tolerance, typically (NumPy's default in matrix_rank).
import numpy as np
rng = np.random.default_rng(82)
B = rng.normal(size=(100, 3)) @ rng.normal(size=(3, 8)) # rank 3 in exact arithmetic
s = np.linalg.svd(B, compute_uv=False)
print("singular values:", np.array2string(s, precision=2))
print("matrix_rank:", np.linalg.matrix_rank(B))singular values: [3.73e+01 2.42e+01 1.42e+01 7.41e-15 5.97e-15 2.62e-15 1.75e-15 9.31e-16]
matrix_rank: 3
Best low-rank approximation
The truncated SVD is the best rank- approximation in both the 2-norm and the Frobenius norm (Eckart-Young), with . This underlies PCA, compression and denoising.
import numpy as np
x = np.linspace(0, 1, 120)
A = np.exp(-((x[:, None] - x[None, :]) ** 2) / 0.05) # a smooth kernel matrix
U, s, Vt = np.linalg.svd(A)
for k in (2, 5, 10, 20):
Ak = (U[:, :k] * s[:k]) @ Vt[:k]
print(f"rank {k:2d}: ||A - A_k||_2 = {np.linalg.norm(A - Ak, 2):.2e}, sigma_(k+1) = {s[k]:.2e}")rank 2: ||A - A_k||_2 = 2.18e+01, sigma_(k+1) = 2.18e+01
rank 5: ||A - A_k||_2 = 2.39e+00, sigma_(k+1) = 2.39e+00
rank 10: ||A - A_k||_2 = 5.12e-03, sigma_(k+1) = 5.12e-03
rank 20: ||A - A_k||_2 = 6.17e-11, sigma_(k+1) = 6.17e-11
Rank-deficient least squares
If lacks full column rank, the least-squares solution is not unique. The SVD picks the one of minimum norm: , dropping singular values below a cutoff . Truncating also regularizes nearly rank-deficient problems, where tiny would amplify noise.
In software
np.linalg.svd(A, full_matrices=False)(thin SVD),scipy.linalg.svd(lapack_driver="gesdd")(divide and conquer, default) or"gesvd"(QR iteration, slower but more robust).np.linalg.lstsqandnp.linalg.pinvare SVD-based with a cutoffrcond.- For large sparse matrices or only a few singular triplets:
scipy.sparse.linalg.svds, randomized SVD (sklearn.utils.extmath.randomized_svd).
Exercises
Exercise 1: Norms from singular values
A matrix has singular values . Find , and .
Answer
, , .
Solution
; .
Exercise 2: Rank-1 error
What is the 2-norm error of the best rank-1 approximation of the matrix in exercise 1?
Answer
.
Solution
.
Exercise 3: Minimum-norm solution (code)
For (rank 1) and , compute the minimum-norm least-squares solution with lstsq and with pinv.
Answer
from both.
Solution
Any (the mean of ) minimizes the residual; the shortest such vector is .
import numpy as np
A = np.ones((3, 2)); y = np.array([1.0, 2, 3])
print(np.round(np.linalg.lstsq(A, y, rcond=None)[0], 10), np.round(np.linalg.pinv(A) @ y, 10))[1. 1.] [1. 1.]
Test yourself
Why not compute singular values as square roots of the eigenvalues of ?
- It is slower only
- It squares the condition number and loses small singular values
- may not be square
- It gives the wrong largest value
The best rank- approximation error in the 2-norm is…
The numerical rank of a matrix is…
- the number of rows
- the number of singular values above a tolerance
- the determinant
- always full