Before this lesson:lesson 03 (inverse,
determinant, dot product and orthogonal projection), lesson 04
(orthogonal matrices and change of basis) and lesson 08 (the SVD and the
pseudoinverse). The condition number κ(A) appears here and gets its full treatment in
lesson 13.
Time: about 90 minutes, plus the notebook.
Least squares, minx∥Ax−b∥2, is the most common computation in data analysis:
linear regression, curve fitting, linear probes, every Gauss–Newton step, and many inverse
problems in science. In exact arithmetic all standard methods give the same answer. In
floating point they do not. The textbook formula (A⊤A)−1A⊤b squares the
condition number: with κ(A)=107 it keeps about 2–3 correct digits, while QR keeps
about 9–10. QR is also a building block you will meet again: it orthonormalizes the sketch in
randomized SVD (lesson 09), it drives the QR eigenvalue algorithm, and Arnoldi is
Gram–Schmidt on a Krylov sequence (lesson 14).
One column first. Fit b≈xa with one column a∈Rm and a scalar x.
The best x makes the error b−xa orthogonal to a:a⊤(b−x^a)=0, so
x^=a⊤b/a⊤a. The fitted vector x^a is the orthogonal projection of
b onto the line through a.
Many columns.Ax=x1a1+⋯+xnan ranges over the column space C(A). The
closest point to b is the orthogonal projection p=Ax^, and the residualr=b−Ax^ is orthogonal to every column. In the language of lesson 02: r∈N(A⊤).
Interactive figure: loading…
Least squares is a projection. The green plane is C(A), spanned by the columns
a1 and a2. The vector b is not in the plane, so
Ax=b has no solution. The closest point is p=Ax^, and the
residual r=b−p meets the plane at a right angle: A⊤r=0.
Rotate the figure and move b with the sliders. With line through 3 points, the same picture
fits a line c0+c1t to three data points: the three values bi are one vector in
R3.
Orthonormal columns make this easy. If A=Q with Q⊤Q=I, the coordinates are
plain dot products, x^=Q⊤b: no system to solve. QR is the step that makes the
columns orthonormal and records the change in an upper triangular matrix R.
Three ways to get there.
Gram–Schmidt builds q1,q2,… one at a time: take ak, remove its components
along q1,…,qk−1, normalize ("triangular orthogonalization").
Householder multiplies A on the left by reflections until it is upper triangular
("orthogonal triangularization").
Givens does the same with 2-D rotations, each one zeroing a single entry.
Quick check
A is 3×2 with independent columns, b is not in C(A), and x^ is the least-squares solution. Which statement about r=b−Ax^ is always true?
Why.
The closest point Ax^ is the orthogonal projection of b onto the plane C(A), so the error r is perpendicular to the plane, that is, to every column: a1⊤r=a2⊤r=0.r=0 would need b∈C(A).r is orthogonal to p=Ax^, not to b (in general b⊤r=∥r∥2>0).
For A∈Rm×n,m≥n, the reduced (thin) QR is A=QR with
Q∈Rm×n,Q⊤Q=In, and R∈Rn×n upper triangular.
The full QR is A=Q~(0R) with Q~=[QQ2] orthogonal
(m×m). For full column rank, the columns of Q2 are an orthonormal basis of N(A⊤).
Check one entry: row 3 of Q times column 2 of R is 0⋅21+621.5=62⋅26=1=a32 ✓.
R is upper triangular because a1 uses only q1, and a2 uses only q1 and q2.
Interactive figure: loading…
Step through Gram–Schmidt. Press Next step. The grey arrows are the columns of A: the two
vectors above and a3=(0,1,1). At each step the magenta vector v is what
is left of a column after its parts along the earlier q's are removed (dashed); then it is
normalized to q1, q2 or q3.
Watch R fill in column by column. Then choose nearly dependent, set δ=10−8, go to the
last step and switch between CGS and MGS: CGS gives a q3 that is 45° away from orthogonal to q2.
For the three columns of the figure, step 4 and 5 continue the same way: r13=q1⊤a3=1/2,r23=q2⊤a3=1/6,v3=a3−r13q1−r23q2=(−32,32,32),r33=2/3 and q3=(−1,1,1)/3.
Theorem (QR). If A has independent columns a1,…,an, then A=QR with
orthonormal columns in Q and R upper triangular with rkk>0, and this factorization is
unique.
Proof of existence (Gram–Schmidt).
a1=0 (independent columns). Set r11=∥a1∥ and q1=a1/r11.
Assume q1,…,qk−1 are orthonormal and
span(q1,…,qk−1)=span(a1,…,ak−1).
Set rjk=qj⊤ak for j<k and vk=ak−∑j<krjkqj. Then
qi⊤vk=qi⊤ak−rik=0 for every i<k.
vk=0: otherwise ak∈span(q1,…,qk−1)=span(a1,…,ak−1),
which contradicts independence. Set rkk=∥vk∥>0 and qk=vk/rkk. The span
property now holds for k.
Rearranged, ak=∑j<krjkqj+rkkqk: column k of A is Q times column k
of R, and it uses only q1,…,qk, so R is upper triangular. Hence A=QR.■
Proof of uniqueness
Let A=Q1R1=Q2R2, both of this form. Then Q1=Q2T with T=R2R1−1, which is
upper triangular with positive diagonal. Also I=Q1⊤Q1=T⊤Q2⊤Q2T=T⊤T,
so T is orthogonal.
T−1=T⊤ is upper triangular (inverse of an upper triangular matrix) and lower triangular
(transpose of one), so T is diagonal with entries ±1. A positive diagonal forces T=I:R1=R2 and Q1=Q2.■
Another route: A⊤A=R⊤R is the Cholesky factorization of the positive definite
A⊤A, and the Cholesky factor with positive diagonal is unique (lesson 07).
rkk=∥vk∥ is the distance from ak to the span of the earlier columns. A tiny rkk
warns that ak is nearly a combination of earlier columns. Without independence, QR still
exists (Householder computes it for any A), but some rkk=0 and uniqueness is lost.
The two versions differ only in which vector each projection is computed from. Look at column 3:
Classical Gram–Schmidt (CGS) computes all coefficients from the original column:
r13=q1⊤a3 and r23=q2⊤a3. Then it subtracts both:
v3=a3−r13q1−r23q2.
Modified Gram–Schmidt (MGS) subtracts one projection at a time and computes the next
coefficient from what is left: v←a3−r13q1 with r13=q1⊤a3, then
r23=q2⊤v (the current, partly reduced vector) and v3=v−r23q2.
In exact arithmetic q2⊤q1=0, so q2⊤(a3−r13q1)=q2⊤a3: the two
methods give the same numbers. In floating point they do not:
Why CGS fails. If ak is nearly in the span of the earlier columns, vk is a small
difference of large, nearly equal vectors (rkk≪∥ak∥). Rounding errors of size
about ε∥ak∥(ε≈2.2×10−16, the machine epsilon
of double precision, lesson 13) survive the subtraction and are divided by rkk. So qk
keeps a component of size about ε∥ak∥/rkk along the earlier qj, and the
errors grow from column to column.
Why MGS helps. MGS takes each coefficient from the vector that is actually being reduced.
So each subtraction also removes the qj-part of the rounding errors made in the earlier
subtractions. CGS fixes all coefficients in advance and never corrects these errors.
How large. The loss of orthogonality ∥Q⊤Q−I∥2 is about εκ(A)2
for CGS (order 1 from κ≈108 on), about εκ(A) for MGS, and about
ε for Householder, independent of κ. Orthogonalizing each column twice
(CGS2) brings Gram–Schmidt back to order ε when εκ(A)≪1.
In the nearly dependent case of the figure, a1=(1,δ,0),a2=(1,0,δ),a3=(1,0,0) with δ=10−8(κ(A)≈3×108): CGS gives
∥Q⊤Q−I∥2≈0.71 (the angle between q2 and q3 is 45°), MGS gives about 10−8.
A Householder reflection is H=I−2vv⊤/(v⊤v) for a nonzero v. It reflects
across the hyperplane orthogonal to v:Hv=−v, and Hw=w for w⊥v. So H is
symmetric and orthogonal, and H2=I.
To map x to a multiple of e1, the target must be ±∥x∥e1 (reflections keep length),
and v=x−target. Choose
v=x+sign(x1)∥x∥e1,Hx=−sign(x1)∥x∥e1,
with the convention sign(0)=+1.
One step by hand. Let x=(1,2,2) be the first column of a 3×3 matrix.
∥x∥=1+4+4=3 and x1>0, so the target is −3e1.
v=x+3e1=(4,2,2); here v1=1+3 adds two positive numbers.
v⊤v=16+4+4=24 and v⊤x=4+4+4=12.
Hx=x−2v⊤vv⊤xv=x−v=(−3,0,0) ✓: two entries are zeroed.
Apply the same H to another column y=(1,0,1), without building H:v⊤y=6, so Hy=y−2⋅246v=y−21v=(−1,−1,0). Check:
∥Hy∥=2=∥y∥ ✓.
With the other sign, v=x−3e1=(−2,2,2) and Hx=(3,0,0). That is also correct in
exact arithmetic. The problem appears when x is close to the e1 axis: then
v1=x1−∥x∥ subtracts two nearly equal numbers (cancellation). Example:
x=(1,10−9,0).∥x∥ rounds to exactly 1, so the wrong sign gives v1=1−1=0 (true
value −5×10−19), and the computed H maps x to (1,−10−9,0): nothing was zeroed.
The right sign gives v=(2,10−9,0) and Hx=(−1,0,0).
Interactive figure: loading…
Reflect x onto the axis. The mirror is the line orthogonal to
v. Reflecting x across it gives Hx, a point on
the e1 axis at distance ∥x∥ from 0. Drag x close to the positive e1 axis and switch to
other sign: v becomes very short, which is where cancellation hits in floating point (the
readout repeats the x=(1,10−9,0) example in real double precision). Givens rotation
reaches the axis by turning instead of reflecting.
Quick check
v=0 and H=I−2vv⊤/(v⊤v). What is H2?
Why.
Reflecting twice across the same mirror brings every vector back. In algebra: with P=vv⊤/(v⊤v),P2=P, so H2=I−4P+4P2=I. The last option forgets the term 4P2. Since H is also symmetric, H⊤H=H2=I:H is orthogonal.
Householder QR. For k=1,…,n: let x be column k on and below the diagonal,
build vk, and update the remaining block X by the rank-1 formula
X←X−2vk(vk⊤X)/(vk⊤vk). Never form the m×m matrix Hk.
After n steps Hn⋯H1A=(0R), so Q~=H1⋯Hn. For least
squares you do not need Q itself: apply the same reflections to b. Cost: about
2mn2−32n3 flops.
Why Householder QR is backward stable.Backward stable means: the computed R^ is
the exact R-factor of a nearby matrix, Q~R^=A+δA with Q~ exactly
orthogonal and ∥δA∥≤cε∥A∥(c a modest constant depending on m,n).
Each step multiplies by an orthogonal matrix. Its rounding error is a perturbation of
relative size O(ε) of the current matrix.
Orthogonal maps keep 2-norms (∥Hy∥=∥y∥), so later steps carry earlier errors along
without enlarging them. The errors only add up: no division by a small rkk, no cancelling
subtraction.
The computed Q^ is a product of reflections, so ∥Q^⊤Q^−I∥=O(ε)
for every κ(A). Orthogonality is built in, not produced by subtraction.
Gram–Schmidt also gives a small ∥A−Q^R^∥, but for large κ(A) its Q^ is
far from every orthogonal matrix. So CGS (and MGS) are not backward stable as QR algorithms.
Interactive figure: loading…
Four QR algorithms, one test. Each dot is a random 60×15 matrix with condition number
κ, factored in double precision by classical Gram–Schmidt,
modified Gram–Schmidt, Cholesky QR(R from the
Cholesky factor of A⊤A, then Q=AR−1) and Householder. The
dashed lines are εκ and εκ2. CGS and Cholesky QR follow
εκ2 and reach order 1 near κ=108 (Cholesky QR then breaks down:
A⊤A is no longer numerically positive definite). MGS follows εκ.
Householder stays at 10−15. Move the κ slider: the table shows that all four have a
tiny residual ∥A−QR∥.
Quick check
Your Gram–Schmidt code returns Q,R with ∥A−QR∥/∥A∥=10−16 for a matrix with κ(A)=1010. What do you know about Q?
Why.
Gram–Schmidt always produces a small residual: each column is rebuilt from the computed q's and r's. But for κ=1010 classical Gram–Schmidt gives ∥Q⊤Q−I∥ of order 1 (it follows εκ2). The value εκ is the MGS rate, not a guarantee for every Gram–Schmidt code.
A Givens rotation changes two coordinates and leaves the others alone. On the pair (a,b):
G=(c−ssc),r=a2+b2,c=a/r,s=b/r,G(ab)=(r0).
Example: (3,4) gives r=5,c=3/5,s=4/5, and G(3,4)=(9/5+16/5,−12/5+12/5)=(5,0).
Givens QR zeros the subdiagonal entries one by one.
Givens rotations in practice
Compute r with hypot(a, b) so that a2 cannot overflow.
Givens QR is as stable as Householder but costs about 1.5 times more on a dense matrix
(≈3mn2−n3 flops).
It wins when only a few entries must be zeroed: Hessenberg matrices (one rotation per column in
the QR eigenvalue algorithm, lesson 14), sparse matrices, and updating R when a new data row
arrives (one sweep of n rotations instead of a new factorization).
Rotations on disjoint pairs of rows commute, so many of them can run in parallel.
Theorem (least squares). Let A have full column rank. Then x^ minimizes
∥Ax−b∥2 if and only if A⊤Ax^=A⊤b (the normal equations), and x^
solves Rx^=Q⊤b, where A=QR is the reduced QR.
Proof.
Projection. The closest point Ax^∈C(A) to b is the orthogonal projection, so the
residual is orthogonal to every column: A⊤(b−Ax^)=0, i.e. A⊤Ax^=A⊤b.
Global minimum. For any x:b−Ax=(b−Ax^)+A(x^−x). The first part is in
N(A⊤), the second in C(A), so they are orthogonal. By Pythagoras
∥b−Ax∥2=∥b−Ax^∥2+∥A(x^−x)∥2, which is smallest only at x=x^(A has full column rank, so A(x^−x)=0 forces x=x^).
QR.A⊤A=R⊤Q⊤QR=R⊤R and A⊤b=R⊤Q⊤b, so
R⊤Rx^=R⊤Q⊤b.R⊤ is invertible, hence Rx^=Q⊤b: one back substitution.
Full-QR view, without normal equations.Q~⊤ keeps norms, so
∥Ax−b∥2=∥Rx−Q⊤b∥2+∥Q2⊤b∥2. The first term is zero at
Rx^=Q⊤b; the second is the minimal squared residual. ■
Interactive figure: loading…
Fit a line, then try to beat it. The model is y≈c0+c1t, so A has the columns
(1,…,1) and (t1,…,tm). Drag the data points. The
least-squares line keeps A⊤r=0: the
residuals add up to 0, and so do the tiri. Switch to your own line and
drag its two grey points: A⊤r is no longer 0, and by the Pythagoras step of the proof its
∥r∥2 is always at least the least-squares value, with equality only on the least-squares line.
Squared condition number. From the SVD A=UΣV⊤:A⊤A=VΣ2V⊤, so
κ(A⊤A)=κ(A)2 (proof in lesson 13).
Error sizes. For a small residual, a backward-stable method has relative error in x^
of about εκ(A). Householder QR and SVD solvers are backward stable for least
squares. The normal equations are not: their error is about εκ(A)2, even when
the residual is zero. (For a large residual, the problem itself has sensitivity of order
κ+κ2tanθ, with θ the angle between b and C(A); QR reaches this,
and no method does better.)
Total failure. For κ(A)≳1/ε≈108, the computed
A⊤A can be exactly singular. Läuchli example: A=1δ010δ,δ=10−8, has full rank, but A⊤A=(1+δ2111+δ2)
and 1+10−16 rounds to 1.
Cost. Normal equations ≈mn2+n3/3 flops, Householder QR ≈2mn2−32n3
(up to 2× more), SVD ≈2mn2+11n3. For small κ the normal equations are fine
(κ≤103 gives errors near 10−10).
Quick check
A regression has κ(A)=106 and a tiny residual. About how many correct digits does x^ have in double precision (ε≈2×10−16)?
Why.
The normal equations work with A⊤A, whose condition number is κ2=1012: error ≈εκ2≈2×10−4. Householder QR is backward stable: error ≈εκ≈2×10−10. Backward stability does not remove the factor κ: it is the sensitivity of the problem itself.
Answers to section 1. (1) Normal equations: εκ2≈10−16⋅1014=10−2,
so about 2–3 digits (a test with a random A gave 10−3). QR: εκ≈10−9,
about 9–10 digits (the test gave 7×10−11). (2) No. Classical Gram–Schmidt's Q has lost
orthogonality completely (∥Q⊤Q−I∥ of order 1); Householder's is at 10−15. A small
∥A−QR∥ says nothing about orthogonality.
If A is m×n with m<n and full row rank, Ax=b has infinitely many solutions
xp+N(A).
Theorem. The solution of smallest ∥x∥2 is the unique solution in the row space:
x+=A⊤(AA⊤)−1b=A+b. We call it the minimum-norm solution.
Try x=A⊤y: then AA⊤y=b, and AA⊤ is invertible (full row rank). So x+
solves Ax=b and lies in C(A⊤).
Any other solution is x++z with z∈N(A)⊥C(A⊤), so
∥x++z∥2=∥x+∥2+∥z∥2≥∥x+∥2, with equality only for z=0.■
Simplest case: the plane x1+2x2+2x3=9. The closest point to the origin lies along the
normal a=(1,2,2):x+=9a/(a⊤a)=(1,2,2), norm 3. The solution (9,0,0) has norm 9.
To avoid forming AA⊤ (again κ2), use A⊤=QR: then A=R⊤Q⊤; set
x=Qy and solve R⊤y=b by forward substitution.
Rank-deficient A (any shape). The least-squares solutions are x^+N(A); the
minimum-norm one is A+b=∑σi>0(ui⊤b/σi)vi (the pseudoinverse,
lesson 08). Plain QR fails here: back substitution divides by
rkk≈0. Use instead
a truncated SVD (singular values below a tolerance count as 0), as np.linalg.lstsq does with rcond;
QR with column pivoting, AP=QR: at each step the remaining column of largest norm goes
first, so ∣r11∣≥∣r22∣≥…, and a drop to about ε∥A∥ usually reveals the numerical rank;
This is the worked example case of the first figure.
Gram–Schmidt.
r11=∥a1∥=4+1+4=3, so q1=(2,1,2)/3.
r12=q1⊤a2=(2+1+0)/3=1, and v2=(1,1,0)−(2,1,2)/3=(1,2,−2)/3.
r22=∥v2∥=1+4+4/3=1, so q2=(1,2,−2)/3. Check: q1⊤q2=(2+2−4)/9=0.
Q=3121212−2,R=(3011).
Least squares by QR.Q⊤b=((2+2+6)/3,(1+4−6)/3)=(10/3,−1/3).
Back substitution, from the last row up: row 2 says x^2=−1/3; row 1 says
3x^1+x^2=10/3, so 3x^1=10/3+1/3=11/3 and x^1=11/9.
Check with the normal equations.A⊤A=(9332)=R⊤R
and A⊤b=(10,3). Solving 9x^1+3x^2=10,3x^1+2x^2=3 gives the
same x^=(11/9,−1/3).
Residual.r=b−Ax^=(−10,10,5)/9, and A⊤r=0 ✓. ∥r∥=225/9=5/3.
The full QR gives this without x^:q3=(2,−2,−1)/3 completes the orthonormal basis,
and ∣q3⊤b∣=∣2−4−3∣/3=5/3 ✓.
Householder on the same A.x=a1=(2,1,2),∥x∥=3,x1>0, so
v1=(5,1,2),v1⊤v1=30,H1a1=a1−2v1(15/30)=a1−v1=(−3,0,0) and
H1a2=(1,1,0)−302⋅6(5,1,2)=(−1,3/5,−4/5). Step 2 on x=(3/5,−4/5),∥x∥=1:v2=(8/5,−4/5) and H2x=(−1,0). So Householder gives
(−30−1−1)=−R: the same factorization up to the signs
D=−I, as uniqueness predicts.
Quick check
A least-squares problem has R=(2013) and Q⊤b=(5,6). What is x^?
Why.
Solve Rx^=Q⊤b from the last row up. Row 2: 3x^2=6, so x^2=2. Row 1: 2x^1+x^2=5, so x^1=(5−2)/2=1.5. The value 2.5 forgets to subtract x^2;(1.5,6) forgets to divide by r22=3.
NumPy checks the hand computation. The notebook for this lesson has these cells and the exercises.
importnumpyasnpA=np.array([[2.,1.],[1.,1.],[2.,0.]])b=np.array([1.,2.,3.])Q,R=np.linalg.qr(A)# Householder (LAPACK geqrf); the signs of diag(R) are freeprint(R.round(12))# [[-3. -1.] [ 0. -1.]]: our R with D = -Id=np.sign(np.diag(R))print((d[:,None]*R).round(12)+0.0)# [[3. 1.] [0. 1.]]: the unique R with positive diagonalx,res,rank,sv=np.linalg.lstsq(A,b,rcond=None)# SVD-based LAPACK solver (gelsd)print(x,res)# [ 1.2222 -0.3333] [2.7778]: (11/9, -1/3), ‖r‖² = 25/9print(A.T@(b-A@x))# ~1e-15: the residual is orthogonal to C(A)
Orthogonality that is built in (Householder) against orthogonality computed through
A⊤A ("Cholesky QR": R from the Cholesky factor of A⊤A, then Q=AR−1):
importnumpyasnpfromscipy.linalgimportsolve_triangularrng=np.random.default_rng(0)defwith_condition(kappa,m=80,n=20):# random A with singular values from 1 down to 1/kappaU,_=np.linalg.qr(rng.standard_normal((m,n)))V,_=np.linalg.qr(rng.standard_normal((n,n)))return(U*np.logspace(0,-np.log10(kappa),n))@V.Tforkappain[1e2,1e4,1e6]:A=with_condition(kappa)I=np.eye(A.shape[1])Qh,_=np.linalg.qr(A)# HouseholderL=np.linalg.cholesky(A.T@A)# Cholesky QR: R = Lᵀ, Q = A R⁻¹Qc=solve_triangular(L,A.T,lower=True).Tprint(f"kappa {kappa:.0e} cond(AᵀA) {np.linalg.cond(A.T@A):.1e} "f"Householder {np.abs(Qh.T@Qh-I).max():.0e} Cholesky-QR {np.abs(Qc.T@Qc-I).max():.0e}")# cond(AᵀA) = 1e4, 1e8, 1e12 = kappa²; Householder stays near 1e-15; Cholesky-QR grows like# eps·kappa² (2e-13, 1e-9, 2e-5)
The Läuchli matrix, a rank-deficient matrix with pivoted QR, and the minimum-norm solution:
importnumpyasnpfromscipy.linalgimportqrd=1e-8A=np.array([[1.,1.],[d,0.],[0.,d]])print(A.T@A)# [[1. 1.] [1. 1.]]: 1 + d² rounded to 1print(np.linalg.matrix_rank(A),np.linalg.matrix_rank(A.T@A))# 2 1print(np.linalg.lstsq(A,np.array([2.,d,d]),rcond=None)[0])# [1. 1.]: fine on A itselfB=np.array([[1.,0.,1.],[0.,1.,1.],[1.,1.,2.],[1.,0.,1.]])# col 3 = col 1 + col 2Q,R,perm=qr(B,pivoting=True)# column-pivoted QR: B[:, perm] = Q Rprint(np.abs(np.diag(R)))# [2.6 0.85 ~2e-16]: numerical rank 2x=np.linalg.lstsq(B,np.array([1.,2.,3.,4.]),rcond=None)[0]# minimum-norm LS solutionprint(x@np.array([1.,1.,-1.]))# ~0: x is orthogonal to N(B) = span(1, 1, -1)
Regression software. R's lm uses Householder QR; NumPy's lstsq and scikit-learn's
LinearRegression use the SVD-based LAPACK driver gelsd. None of them forms (X⊤X)−1.
Random orthogonal matrices.torch.nn.init.orthogonal_ takes the QR of a Gaussian
matrix and multiplies the columns of Q by sign(diagR).
Without this sign fix, Q is not uniformly (Haar) distributed.
ML for science. Gauss–Newton and Levenberg–Marquardt steps in inverse problems and data
assimilation are linear least-squares problems. Square-root Kalman filters update a triangular
factor with orthogonal transformations, so the covariance stays positive semidefinite.
Streaming sensor data can update R with Givens rotations, one sweep per new row.
Overparameterized models. Gradient descent on ∥Ax−b∥2 from x0=0 moves only along
gradients A⊤(Ax−b)∈C(A⊤), so with a small enough step it converges to the
minimum-norm solution A+b: the simplest example of the implicit bias of gradient descent.
Computing inv(A.T @ A) @ A.T @ b. It squares κ and adds an explicit inverse. Use
QR or lstsq.
The wrong Householder sign.v=x−sign(x1)∥x∥e1 cancels when x is
near the e1 axis.
Building the m×m matrix H. Apply H as a rank-1 update instead.
Testing a QR code only with ∥A−QR∥. Gram–Schmidt passes this test even when Q is far
from orthogonal. Check ∥Q⊤Q−I∥ too, and fix the signs with
D=diag(signrkk) before comparing with a library QR.
Computing every CGS coefficient from the original column and calling it MGS. MGS takes
rjk from the partly reduced vector; this one change is the difference between
εκ2 and εκ.
Plain QR on a rank-deficient matrix. Use pivoted QR, a truncated SVD or ridge.
Give the Householder vector v that maps x=(3,4) to a multiple of e1, and Hx. Give the
Givens (c,s) for the same vector.Answerx1>0, so v=(3+5,4)=(8,4).v⊤x=40,v⊤v=80, so Hx=x−v=(−5,0). Givens: r=5,c=3/5,s=4/5, and Gx=(5,0).
A regression has κ(A)=105 and a small residual. About how many correct digits do the
normal equations and Householder QR give in double precision?AnswerNormal equations: εκ2≈2×10−6, about 5–6 digits. QR: εκ≈2×10−11, about 10–11 digits.
Find the minimum-norm solution of x1+x3=1,x2+x3=2, and check it against N(A).AnswerAA⊤=(2112),y=(AA⊤)−1(1,2)=(0,1),x+=A⊤y=(0,1,1).N(A)=span(1,1,−1) and (0,1,1)⋅(1,1,−1)=0. All solutions: (0,1,1)+t(1,1,−1), with ∥x∥2=2+3t2, smallest at t=0.
CGS and MGS give the same numbers in exact arithmetic. How do they compare with each other and
with Householder in floating point, for κ(A)=108?AnswerLoss of orthogonality: CGS ≈εκ2, i.e. order 1 (orthogonality gone); MGS ≈εκ≈10−8; Householder ≈10−16, independent of κ. MGS is better than CGS but not as good as Householder.
Run Gram–Schmidt on a1=(3,4),a2=(1,0). Give Q and R.Answerr11=5,q1=(3,4)/5.r12=q1⊤a2=3/5,v2=(1,0)−53⋅51(3,4)=(16,−12)/25,r22=∥v2∥=20/25=4/5,q2=(4,−3)/5. So Q=51(344−3) and R=(503/54/5). Check: r11r22=4=∣detA∣.
Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
State the QR theorem for a matrix with independent columns.
A=QR with Q⊤Q=I and R upper triangular with rkk>0, and this factorization is unique.
Write one Gram–Schmidt step for column k.
rjk=qj⊤ak for j<k;vk=ak−∑j<krjkqj;rkk=∥vk∥;qk=vk/rkk.
Gram–Schmidt on a1=(1,1,0),a2=(1,0,1): what are q2 and r22?
v2=(21,−21,1),r22=1.5,q2=(1,−1,2)/6.
Which vector v defines the Householder reflection that maps x to a multiple of e1, and why this sign?
v=x+sign(x1)∥x∥e1, giving Hx=−sign(x1)∥x∥e1. The sign adds two numbers of the same sign, so there is no cancellation.
Give the Givens rotation that maps (a,b) to (r,0).
r=hypot(a,b),c=a/r,s=b/r,G=(c−ssc).
Why do the normal equations A⊤Ax^=A⊤b hold at the least-squares solution?
The residual b−Ax^ of the orthogonal projection is perpendicular to every column of A:A⊤(b−Ax^)=0.
How do you solve least squares with a QR factorization?
Compute A=QR, then solve Rx^=Q⊤b by back substitution.
What is κ(A⊤A), and what does it cost the normal equations?
κ(A)2. Their error is about εκ2 instead of εκ: about twice as many lost digits.
What is the minimum-norm solution of Ax=b for a wide A with full row rank?
x+=A⊤(AA⊤)−1b=A+b: the unique solution in the row space C(A⊤).
Why is Householder QR backward stable while Gram–Schmidt is not?
Householder applies only orthogonal maps, which do not enlarge errors, so Q is orthogonal to O(ε) for every κ. Gram–Schmidt makes orthogonality by subtraction, which cancels for nearly dependent columns.
"How do you fit linear regression in practice? Why not (X⊤X)−1X⊤y?" — QR or an SVD
solver (lstsq). The closed form squares κ and fails near κ≈108. For
small κ, the normal equations (or ridge with Cholesky) are fine and cheaper.
"Gram–Schmidt or Householder?" — Gram–Schmidt builds orthogonality by subtraction (CGS
≈εκ2, MGS ≈εκ). Householder reaches triangular form
with reflections: backward stable, orthogonal to O(ε). Gram–Schmidt (with
reorthogonalization) stays useful when vectors arrive one at a time, as in Krylov methods.
"What does np.linalg.lstsq return for a rank-deficient X?" — the minimum-norm least-squares
solution; singular values below rcond·σmax count as zero.
Open on YouTube · the player loads from youtube-nocookie.com when you press playOpen on YouTube · the player loads from youtube-nocookie.com when you press playOpen on YouTube · the player loads from youtube-nocookie.com when you press playOpen on YouTube · the player loads from youtube-nocookie.com when you press play
Trefethen and Bau, Numerical Linear Algebra
(SIAM 1997), Lectures 7–8 (QR, Gram–Schmidt), 10 (Householder), 11 (least squares), 16 and 19
(stability of Householder QR and of least-squares algorithms).
Gilbert Strang, MIT 18.065 (2018),
lectures 9–11 (four ways to solve least squares, difficulties with Ax=b, minimum norm).