Householder QR and least squares
Use orthogonal transformations to solve full-rank least squares without squaring the matrix condition number.
On this page
The question for today
What you’ll understand
- Construct a Householder reflector with a cancellation-avoiding sign.
- Reduce a full-column-rank least-squares problem to a triangular solve.
- Distinguish numerical stability of QR from conditioning of the underlying fit.
Least squares asks for the vector whose image is closest to observed data. The normal equations describe the optimum algebraically, but forming them can discard information when columns are nearly dependent. Orthogonal transformations preserve Euclidean lengths and provide another route. Householder QR eliminates entries using reflections, carries the data through the same transformations, and leaves a triangular problem whose solution exposes both fitted values and residual structure.
A reflection removes a column tail without stretching vectors
For a nonzero vector x, choose α as minus the sign of its first component times its Euclidean norm, taking the sign of zero as positive. Form v=x−αe₁. The reflector H=I−2vvᵀ/(vᵀv) is symmetric and orthogonal, and maps x to αe₁. The chosen sign makes the first component of v a sum of like-signed magnitudes rather than a nearly cancelling difference. A zero column tail is handled separately; one need not divide by a zero reflector vector.
Apply such reflectors to successive trailing column segments of a rectangular matrix. Their product produces an upper trapezoidal form while their orthogonality preserves two-norms. An implementation stores reflector vectors and scale factors compactly, then applies their action to other vectors without forming a dense full Q. Scaled norm calculations are also important to avoid overflow or underflow in constructing reflectors. The symbolic formula describes the geometry, while stable norm evaluation and sign choice make that geometry numerically useful.
Orthogonality separates the fit from the unavoidable residual
Let A have m rows, n columns, m at least n, and full column rank. Its thin QR factorization is A=Q₁R, where Q₁ has orthonormal columns and R is nonsingular upper triangular. Complete Q₁ to an orthogonal matrix Q=(Q₁,Q₂). Applying Qᵀ to Ax−b splits the squared norm into the squared triangular mismatch and the squared component Q₂ᵀb. The second term cannot be altered by any x. Setting the first term to zero gives the unique least-squares solution.
The normal equations AᵀAx=Aᵀb give the same exact minimizer under full column rank, but the two-norm condition number of AᵀA is the square of that of A. Forming this product in finite precision can therefore make weak column directions difficult to resolve. Householder QR is normwise backward stable under standard arithmetic assumptions, avoiding that explicit squaring step. It does not remove intrinsic sensitivity when data perturbations change nearly dependent columns or when the residual geometry makes the fitted coefficients sensitive.
Rank decisions need more than a small diagonal entry
For a rank-deficient matrix, R is not invertible and the previous triangular conclusion cannot be used unchanged. Column-pivoted QR can reveal approximate dependence and rearrange the important columns, while the SVD gives a direct singular-value view and a minimum-norm solution after a rank decision. Numerical rank depends on a tolerance tied to data uncertainty and scale; it is not simply the count of floating-point entries that happen to equal zero. A full-rank routine's assumptions should be checked before treating its output as a rank-aware answer.
After computing a least-squares solution, the residual generally is not zero. The correct optimality check is that it is orthogonal to the column space, so Aᵀr is small relative to appropriate scales. Report the residual norm because it records model mismatch or noise as well as numerical effects. A tiny optimality residual need not imply accurately determined coefficients when the design is ill conditioned. For repeated fits with the same design and different observations, the QR factors can be reused, making the stable factorization an efficient computational object.
Work it through
An overdetermined fit with a nonzero optimal residual
Solve least squares for A=[[1,0],[1,1],[0,1]] and b=(1,2,2) using a thin QR factorization.
The first normalized column is q₁=(1,1,0)/√2. Removing its projection from the second column gives (−1/2,1/2,1), whose norm is √(3/2); hence q₂=(−1,1,2)/√6.
The triangular factor is R=[[√2,1/√2],[0,√(3/2)]], and Q₁ᵀb=(3/√2,5/√6). Back substitution gives x₂=5/3 and x₁=2/3.
The residual b−Ax is (1/3,−1/3,1/3), with norm 1/√3. Dotting it with either original column gives zero, verifying least-squares optimality.
The unique minimizer is (2/3,5/3), with a genuine nonzero residual. The orthonormal basis is the same thin QR geometry produced by Householder elimination up to sign choices.
The living notebook
Explore the model
Interactive lab · vectors
Check the length a reflector must preserve
Reset, set magnitude 2 and angle 60°. Predict Euclidean length 2 despite projections 1 and √3. For a fresh run, Reset, set magnitude 2 and angle 0°, again predicting length 2. This projection instrument illustrates orthogonal length preservation, but does not itself execute a Householder reflection or a QR factorization.
- x component
- 1.393
- y component
- 0.975
- Magnitude
- 1.7
Playback traces a fixed vector and its projections. Angle and magnitude set the geometry; the moving marker does not change them. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forOrthogonal transformations rearrange components while preserving the norm used by least squares.
Pause & reason
Check your understanding
Your turn
Try it yourself
For x=(3,4), construct the cancellation-avoiding Householder reflector and check its action on x.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Use α=−5 and v=(8,4); simplifying vvᵀ/(vᵀv) avoids carrying unnecessarily large numbers.
Compare with the solution
Here vᵀv=80, so H=I−(1/40)[[64,32],[32,16]]=[[-3/5,−4/5],[−4/5,3/5]]. Multiplication gives H(3,4)=(−5,0). The columns are orthonormal and H is symmetric. The alternative raw difference x−5e₁ would be less safe when x is already nearly aligned with the positive first axis; the selected sign avoids that near cancellation.
Keep the idea
Take it with you
- Householder reflectors eliminate column tails while preserving Euclidean norms.
- Full-rank QR separates the adjustable triangular fit from an orthogonal residual.
- Backward-stable factorization does not eliminate data sensitivity or decide numerical rank automatically.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.