Guides / Linear algebra
Least squares and the SVD: solve AᵀAx̂ = Aᵀb, check the residual, and read the rank off the singular values
Short answer
Least squares: solve the normal equations ATAx̂ = ATb. Check that the residual r = b − Ax̂ satisfies ATr = 0. SVD: the singular values σi are the square roots of the eigenvalues of ATA, the columns vi of V are its unit eigenvectors, and ui = Avi/σi for each σi > 0. The rank is the number of nonzero singular values. The best rank-k approximation keeps the first k terms of σ1u1v1T + σ2u2v2T + ⋯, with error σk+1. When ATA is singular, the pseudoinverse A+ = VΣ+UT gives the shortest least-squares solution.
Why it works
Every vector Ax lies in the column space of A. The closest such vector to b is the perpendicular projection of b onto that space, and what is left over, r = b − Ax̂, must be perpendicular to every column: AT(b − Ax̂) = 0. Rearranged, that is exactly the normal equations. If the columns of A are independent, ATA is invertible and x̂ is unique.
For the SVD, ATA is symmetric with nonnegative eigenvalues, so it has orthonormal eigenvectors vi. Then ‖Avi‖2 = viTATAvi = λi, so A stretches vi by σi = √λi, and the images Avi are perpendicular to each other too. Normalizing them gives the ui. A maps one orthonormal basis to another, stretching each direction by its own σi.
Example 1: fitting a line by least squares
Fit y = c + mx to the points (0, 6), (1, 0), (2, 0).
- A = 101112, b = 600One row per point: c · 1 + m · x = y. Three equations, two unknowns.
- ATA = 3335, ATb = 60Entries of ATA are dot products of columns: N = 3, Σx = 3, Σx2 = 5. ATb holds Σy = 6 and Σxy = 0.
- 3c + 3m = 6, 3c + 5m = 0 ⇒ m = −3, c = 5Subtract the equations: 2m = −6.
- Ax̂ = (5, 2, −1), r = b − Ax̂ = (1, −2, 1)The fitted values and the residual.
- ATr = (1 − 2 + 1, 0 − 2 + 2) = (0, 0), ‖r‖2 = 6The residual is perpendicular to both columns, which confirms x̂. No other line through these points has a sum of squared errors below 6.
Example 2: an SVD by hand
Find the SVD of A = 221−1.
- ATA = 5335Column dot products: 4 + 1 = 5, 4 − 1 = 3, 4 + 1 = 5.
- λ1 = 8, λ2 = 2Trace 10, determinant 16: λ2 − 10λ + 16 = (λ − 8)(λ − 2). The unit eigenvectors are v1 = (1, 1)/√2 and v2 = (1, −1)/√2.
- σ1 = √8 = 2√2, σ2 = √2Square roots of the eigenvalues.
- u1 = Av1/σ1 = (1, 0)Av1 = (4, 0)/√2, divided by 2√2.
- u2 = Av2/σ2 = (0, 1)Av2 = (0, 2)/√2, divided by √2. The u's come out perpendicular, as they must.
- A = 10012√200√2 · 1√2111−1Check the product: the first row is 2√2 · (1, 1)/√2 = (2, 2) and the second is √2 · (1, −1)/√2 = (1, −1).
Example 3: a rank-one matrix and the pseudoinverse
For A = 122436 and b = (1, 0, 1), find the rank, the null space and the shortest least-squares solution.
- ATA = 14282856, eigenvalues 70 and 0The second column is twice the first, so the determinant is 14 · 56 − 282 = 0.
- σ1 = √70, σ2 = 0, rank 1One nonzero singular value. v1 = (1, 2)/√5 and u1 = (1, 2, 3)/√14.
- null space: multiples of v2 = (2, −1)/√5A(2, −1) = (0, 0, 0). Adding any multiple of it to x leaves Ax unchanged, so least squares has infinitely many solutions.
- x+ = A+b = v1(u1 · b)/σ1 = (2/35)(1, 2)Only the σ1 term survives: u1 · b = 4/√14, and (4/√14)/√70 · (1, 2)/√5 = 4(1, 2)/70.
- Ax+ = (2/7)(1, 2, 3)The projection of b onto the line through (1, 2, 3), as least squares requires. x+ has no component along the null space, which makes it the shortest of all the solutions.
Example 4: the best rank-one approximation
Find the best rank-one approximation to the A of Example 2, and its error.
- A1 = σ1u1v1T = 2√2 · (1, 0) · (1, 1)/√2Keep the largest term of the SVD.
- A1 = 2200An outer product: column (1, 0) times row (2, 2).
- A − A1 = 001−1, ‖A − A1‖2 = √2 = σ2The error is the part that was dropped, σ2u2v2T, and its size is σ2. No rank-one matrix gets closer: that is the Eckart–Young theorem, the basis of PCA and low-rank compression.
Common mistakes, and the exact line they happen on
1. The eigenvalues of AᵀA taken as the singular values
The third step of Example 2.
- ATA has eigenvalues 8 and 2Right so far.
- σ1 = 8, σ2 = 2This is the mistake. ATA applies A twice, so its eigenvalues are the squared stretches. Check with v1: ‖Av1‖ = ‖(4, 0)/√2‖ = 2√2 ≈ 2.83, not 8. The singular values are √8 and √2.
2. The eigenvalues of A taken as the singular values
The same matrix, without forming AᵀA.
- A = 221−1, det(A − λI) = λ2 − λ − 4Right so far, as a calculation of eigenvalues.
- σ = |(1 ± √17)/2| ≈ 2.56 and 1.56This is the mistake. For a matrix that isn't symmetric, the eigenvectors aren't perpendicular and the eigenvalues don't measure stretching. The largest stretch is σ1 = 2√2 ≈ 2.83, more than 2.56. The two only agree, up to sign, for a symmetric matrix.
3. Inverting AᵀA when the columns are dependent
The least-squares problem of Example 3.
- ATA = 14282856, ATb = (4, 8)Right so far.
- x̂ = (ATA)−1ATbThis is the mistake. det(ATA) = 14 · 56 − 282 = 0, so there is no inverse. The normal equations reduce to the single equation 14x1 + 28x2 = 4, a whole line of solutions. In floating point the "inverse" exists only through rounding error and gives huge, meaningless weights. Use the pseudoinverse, or
np.linalg.lstsq, which gives the shortest solution (2/35)(1, 2).
Practice
Fit y = mx, a line through the origin, to (1, 2), (2, 3), (3, 7).
Show answer
A is the column (1, 2, 3), so m = (A · b)/(A · A) = (2 + 6 + 21)/14 = 29/14 ≈ 2.071.
Find the constant c that best fits the values 3, 5, 10 in the least-squares sense.
Show answer
A is a column of ones, so the normal equation is 3c = 18 and c = 6, the mean. The residual (−3, −1, 4) adds to 0, which is ATr = 0.
Find the singular values of 300−2.
Show answer
3 and 2. ATA = diag(9, 4); singular values are never negative.
Find the singular values, rank and null space of 1111.
Show answer
ATA has eigenvalues 4 and 0, so σ = 2 and 0, and the rank is 1. The null space is spanned by (1, −1).
Find the best rank-one approximation to 3001, and its error.
Show answer
Keep σ1 = 3: 3000. The error is σ2 = 1.