Linear Algebra · Theory · M5
M5-T5 — QR Decomposition
QR solves least squares by rotating the problem into a triangle, with orthogonal steps that never amplify error.
The normal equations square the condition number. QR does not: it factors A into an orthonormal part Q, which keeps lengths intact, and a small triangular matrix R, which is solved by back substitution. It is the default method of every numerical library for fitting, and the reason you can fit ill-conditioned models at all.
Learning objectives
After this lesson, you should be able to:
- State the QR decomposition A = QR for a tall matrix and the shapes of Q and R.
- Derive that the least-squares solution satisfies R x̂ = Qᵀb and that the residual norm is the length of the leftover part of Qᵀb.
- Explain why QR is more accurate than the normal equations (orthogonal steps; κ(R) = κ(A)).
- Describe Householder QR as a sequence of reflections, and know to use a library routine.
Introduction and motivation
A robot fitting a trajectory, a calibration or a surface needs an answer it can trust to many digits even when the model is nearly redundant. The method behind nearly all production fitting code is QR. It is the natural continuation of the previous two modules: Gram-Schmidt showed that a matrix is an orthonormal part times a triangle, orthogonal matrices showed that orthonormal parts do no damage, and projections showed that least squares is a shadow. QR puts these together.
The intuition
Any tall matrix can be written A = QR, where Q has orthonormal columns and R is a small upper triangular matrix. Think of Q as a clean set of axes for the column space of A, and R as the coordinates of the original columns in those axes (this is what Gram-Schmidt computed).
Now the fit becomes easy. Rotating the data into the clean axes does not change any lengths; in those axes the part of b inside the column space is simply a list of numbers, Qᵀb, and what is left over is the residual. The unknowns solve a small triangular system R x = Qᵀb by back substitution.
Interactive figure. Gram-Schmidt in the plane: a1 becomes the unit vector q1; the part of a2 along q1 is subtracted, leaving a perpendicular vector scaled to the unit vector q2. The readout gives q1, q2 and the remainder length, which is the second diagonal entry of R. This figure needs JavaScript to be interactive.
Geometric and physical interpretation
Geometric view. Q rotates (and possibly reflects) the data space so that the column space of A lies along the first n coordinate axes. In the rotated frame the data vector has n components inside the column space (these determine the fit) and m − n components outside it (the residual). Rotations preserve lengths, so no error is amplified. Physical view. Imagine tilting a measurement table until the plane of possible predictions is flat on the table top. The fit is then read off from the position of the data over the table, and the residual is its height above it.
The mathematics
- A = QR
- the (economy) QR decomposition: Q is m × n with orthonormal columns, R is n × n upper triangular.
- Qᵀb
- the coordinates of the data in the clean axes: the first n entries of the rotated data.
- κ(R)
- the condition number of R, equal to κ(A).
The decomposition
A = Q R QᵀQ = I (m × n Q), R upper triangular (n × n), rii ≠ 0 when the columns of A are independent
What it means. Q holds orthonormal axes for the column space; R holds the coordinates of the columns of A in those axes. R is triangular because column k of A needs only the first k axes.
The solution
minimise ‖A x − b‖² ⇒ R x̂ = Qᵀ b ‖r‖ = ‖ (I − QQᵀ) b ‖
What it means. Solve the small triangular system by back substitution. The residual is whatever part of b lies outside the span of the columns of Q.
Why it is accurate
κ(R) = κ(A) (but κ(AᵀA) = κ(A)²)
What it means. Orthogonal factors do not change the singular values, so R has exactly the sensitivity of A. Solving with R faces κ(A); forming AᵀA faces κ(A)².
QR: A = QR, solve R x̂ = Qᵀb by back substitution. It avoids AᵀA, keeps the sensitivity at κ(A), and is the standard least-squares solver.
Step-by-step derivation: R x̂ = Qᵀb, and Householder reflections
From the cost to the triangular system. Let A = QR (economy size). From the normal equations AᵀAx̂ = Aᵀb:
- Substitute
A = QR:AᵀA = RᵀQᵀQR = RᵀR, sinceQᵀQ = I. - And
Aᵀb = RᵀQᵀb. So the normal equations readRᵀRx̂ = RᵀQᵀb. - If the columns are independent,
Ris invertible, and so isRᵀ; cancel it:R x̂ = Qᵀb.
We reached the same solution as the normal equations algebraically, but the computer never forms AᵀA.
A cleaner derivation of the residual. Extend Q to a full orthogonal matrix [Q Q⊥] (square). Rotation does not change length, so ‖Ax − b‖² = ‖[Q Q⊥]ᵀ(Ax − b)‖². Now QᵀAx = Rx and Q⊥ᵀAx = 0 (A's columns lie in the span of Q), so
‖Ax − b‖² = ‖R x − Qᵀb‖² + ‖Q⊥ᵀ b‖²
What it means. The first piece can be made exactly zero by choosing x, which solves Rx = Qᵀb. The second piece, the size of the part of b outside the column space, does not depend on x at all: it is the unavoidable residual.
So the minimum is attained at Rx̂ = Qᵀb, and the minimum cost is ‖Q⊥ᵀb‖² = ‖r‖².
Why not Gram-Schmidt in practice. Classical Gram-Schmidt can lose orthogonality completely (last module). Householder QR builds Q as a product of reflections H = I − 2uuᵀ: each one is exactly orthogonal, chosen to zero the entries of one column below the diagonal. Because every step is an orthogonal transformation, errors cannot grow, and the computed Q is orthonormal to machine precision (about 3·10⁻¹⁶ on the Hilbert example against 1 for classical Gram-Schmidt).
A worked example: the line fit again, through QR
Use A = [[1, 1], [2, 1], [3, 1]] and b = (1, 2, 2).
- Factor. Gram-Schmidt on the two columns gives
q₁ = (1, 2, 3)/√14 = (0.267, 0.535, 0.802), then the remainder of column 2 after removing its shadow givesq₂ = (0.873, 0.218, −0.436), andR = [[3.742, 1.604], [0, 0.655]]. - Rotate the data.
Qᵀb = (q₁·b, q₂·b) = (0.267 + 1.069 + 1.604, 0.873 + 0.436 − 0.873) = (2.940, 0.436). - Back substitute
R x = (2.940, 0.436): from the second row,0.655 c = 0.436givesc = 0.667. From the first,3.742 m + 1.604·0.667 = 2.940givesm = 0.5. - Residual size.
‖b‖² − ‖Qᵀb‖² = 9 − (8.644 + 0.190) = 0.167, matching‖r‖² = 0.1667from the normal equations. - Conditioning. The singular values of A are the same as those of R, so
κ(R) = κ(A) = 6.79, while the normal equations deal with 46.1. - The hard case. For the degree-9 polynomial fit, this solve returns a relative error near
7·10⁻¹¹, versus3·10⁻⁴for the normal equations.
Robotics application
Default solver. numpy.linalg.lstsq, MATLAB's backslash on tall matrices and most calibration toolboxes use QR (or the SVD) internally. When you call a library to fit a plane, a polynomial or a calibration, this is what is happening.
Plane and line fitting with data scaling. Fitting a plane z = ax + by + c to LiDAR points far from the origin gives a design matrix whose columns are nearly parallel; QR (after centring the data) handles it, where the normal equations may not. The SLAM lessons on LiDAR point clouds and point-cloud preprocessing use such fits.
Square-root filtering. Kalman filters in square-root form carry a triangular factor (the Cholesky or QR factor of the covariance) instead of the covariance itself, for exactly the same conditioning reason: square roots halve the dynamic range of the numbers. This is standard in flight code.
Rank detection. A very small diagonal entry rkk shows the k-th column is nearly dependent on the earlier ones, the same early warning as a small Gram-Schmidt remainder.
Engineering implications and limitations
Practical rules
- Call a library QR (Householder), not your own Gram-Schmidt. The results are orthonormal to machine precision.
- Use the economy factorisation. For a tall matrix store Q as m × n, not m × m. A full Q for a million points would be a trillion entries.
- Check the diagonal of R. Entries near zero relative to the largest mean dependent columns. Use column-pivoted QR or the SVD when the rank is in doubt.
- The cost is about 2mn² − 2n³/3 operations, roughly twice that of forming and solving the normal equations, a small price for accuracy; for extremely large sparse problems sparse QR or Cholesky on regularised normal equations may be used knowingly.
- Reuse the factorisation for several right-hand sides, as with LU: factor A once, apply Qᵀ to each b and back-substitute.
Limits
QR assumes independent columns. For rank-deficient problems it needs pivoting, and for the most delicate cases (an explicit rank decision, a minimum-norm answer) the SVD of Module 6 is the right tool. QR improves the algorithm; it cannot repair an ill-conditioned problem: if κ(A) is 10¹⁵ the answer is uncertain by nature.
Common misconceptions
“QR and the normal equations give different answers.”
In exact arithmetic they are identical. They differ in how rounding errors behave: QR is far more accurate on ill-conditioned problems.
“R is just the upper triangle of A.”
R is the triangular factor produced by orthogonalising A; its entries are dot products and norms from Gram-Schmidt, not entries of A.
“Q is square.”
In the economy form used for fitting, Q is m × n (tall) with orthonormal columns. A full square orthogonal Q exists but is rarely stored.
“QR makes any problem accurate.”
It removes the extra squaring of the condition number; it cannot remove the conditioning inherent in A.
Engineering takeaways
- A tall matrix factors as A = QR with orthonormal columns in Q and an upper triangular R.
- Least squares reduces to R x̂ = Qᵀb, solved by back substitution; the residual norm is the size of what lies outside the span of Q.
- Orthogonal steps do not amplify error: κ(R) = κ(A), whereas the normal equations face κ(A)².
- Householder QR (reflections) is the stable way to compute it; classical Gram-Schmidt is not.
- Use library QR, centre your data, and keep the SVD in mind for rank questions.
References and further reading
Primary papers and authoritative documentation.
- MIT 18.06SC Linear Algebra (OCW): lectures on orthogonal bases and Gram-Schmidt
- Boyd and Vandenberghe, Introduction to Applied Linear Algebra (VMLS): the chapter on QR factorisation and least squares; the site did not load when we last checked
- Mathematics for Machine Learning: the chapter on matrix decompositions
- Michigan ROB 101: Computational Linear Algebra: QR and least squares in a computational course
Knowledge check
Three conceptual questions. Write an answer, then reveal the explanation. Your answers stay in your browser.
Question 1
For A = [[1, 0], [0, 1], [0, 0]] (already orthonormal columns) and b = (3, 4, 5) find Q, R, the least-squares solution and the residual norm using QR.
Explanation
The columns are already orthonormal, so Q = A and R = I. Qᵀb = (3, 4), so R x̂ = Qᵀb gives x̂ = (3, 4). The prediction is (3, 4, 0), the residual is (0, 0, 5) with norm 5, which is the part of b outside the span of Q, exactly as the formula ‖Q⊥ᵀb‖ states.
Question 2
Explain in two sentences why QR is more accurate than the normal equations for a nearly dependent design matrix.
Explanation
QR applies orthogonal transformations to A directly; orthogonal matrices preserve lengths, so rounding errors are not amplified and the solve sees condition number κ(A). The normal equations first form AᵀA, whose condition number is κ(A)², so a problem that is merely hard becomes numerically almost impossible.
Question 3
After computing A = QR for a calibration, the second diagonal entry of R is 3·10⁻¹² while the first is 12. What does that mean, and what should you do?
Explanation
The second column is almost exactly a multiple of the first (its remainder after removing the shadow on q₁ is 10⁻¹³ of its size): the two parameters cannot be distinguished by this data. The fit is numerically meaningless in that direction. Collect data that separates the parameters, merge or fix one of them, or use a regularised or minimum-norm solution (SVD), not the blind QR answer.
What’s next
So far every measurement counted equally. Real sensors differ in quality. Weighted least squares lets a precise measurement count for more than a sloppy one, and connects the weights to covariance.
Related content
Nothing below is required to finish this lesson.
- BeforeM5-T4: Normal Equations
Derive and solve the normal equations, and state when they are valid and when they lose accuracy.
- BeforeM4-T4: The Gram-Schmidt Process
Turn independent vectors into an orthonormal basis step by step, and recognise its numerical weakness.
- LabM5-L1: LiDAR Plane Fitting
Fit the ground plane to a noisy LiDAR scan, find out why a naive fit fails on a wall, and fix it with a stable method.
Get notified when the next lesson and new labs are released.
One email per new lesson or lab.