📐Least Squares and Data Fitting
QR Factorisation
Orthogonal matrices, Gram–Schmidt and its instability, Householder reflections, Givens rotations
Stanford, 1965
In the early 1960s, statistical software solves regression problems the way the textbooks say: form , solve the normal equations. On the computers of the day, with data that are often nearly collinear, the results are sometimes nonsense — and nothing warns the user.
Gene Golub, a young numerical analyst, publishes a nine-page paper in Numerische Mathematik: Numerical methods for solving linear least squares problems. Its idea comes from Alston Householder's 1958 work on reflections. Transform by a sequence of mirror reflections — each one exactly orthogonal, so it cannot amplify any error — until it is upper triangular. Then the least-squares problem is a single back substitution, and the condition number is never squared.
Within a decade, QR by Householder reflections had become the standard way numerical libraries solve least-squares problems. It is what MATLAB's backslash does with a rectangular matrix, and what LAPACK's QR-based least-squares drivers do.
A square matrix is orthogonal if : its columns are orthonormal. Orthogonal matrices are rotations and reflections, and they preserve length: . Two consequences make them the numerical analyst's favourite tool:
- — the best possible;
- multiplying by never magnifies errors already present.
So transforming a problem by orthogonal matrices does not change its sensitivity — unlike forming .
💡The QR factorisation
Any matrix () can be written
with orthogonal () and upper triangular (). Because preserves length,
The second term does not depend on . The first is zero when — one triangular solve. And , not .
Gram–Schmidt is the obvious way to compute : take the columns of in turn and subtract from each its projections onto the orthonormal vectors already found, then normalise. It is taught in every linear algebra course — and in floating point it can fail badly. When columns are nearly parallel, the subtraction removes almost everything and the small remainder is dominated by rounding error; the computed is no longer orthogonal. Modified Gram–Schmidt reorders the same operations (subtract each new direction from all later columns immediately) and loses orthogonality only in proportion to , not .
Derivation
Gram–Schmidt produces and together
Take the columns of in turn. Normalise the first: , . For column , remove its components along the directions already found, then normalise what is left:
Rearranged, each column is a combination of the s found so far:
Column uses only , so the coefficients form an upper-triangular matrix: .
Where it goes wrong. If is nearly a combination of earlier columns, is a tiny difference of nearly equal vectors, . The rounding errors in that subtraction are of size , so relative to they are — catastrophic cancellation (lesson 1-2) — and they point in arbitrary directions, including along the earlier s. Normalising divides by the small , so those errors survive in at that relative size — close to 1 when the columns are nearly dependent — and is no longer orthogonal to its predecessors.
💡Householder reflections
For a vector , the reflection
maps to : it zeroes every entry below the first in one step. is symmetric and orthogonal; it is a mirror. Apply one to column 1 of , the next to column 2 of what remains, and so on: after reflections is upper triangular, and is the product of the mirrors (never formed explicitly — the vectors are stored instead). The sign choice avoids cancellation in forming . Cost: operations, about twice the normal equations — the price of stability.
Derivation
The mirror is orthogonal, and sends onto the axis
Orthogonal. is clearly symmetric. Square it, writing ; then , so
Geometrically, projects onto the line of and reflects through the plane perpendicular to ; reflecting twice changes nothing.
Onto the axis. Let , so . Using :
The second is exactly half the first, so
The sign. With of the opposite sign to , the quantity is a sum of two positive numbers. The other choice would subtract them — and when already points almost along they are nearly equal: cancellation.
📐The polynomial fit, done right
The degree-9 fit of lesson 5-1, solved by a hand-written Householder QR: relative coefficient error , against from the normal equations — six more correct digits from the same data, the same arithmetic and the same answer in exact arithmetic.
The orthogonality of the computed tells the same story. For 12 columns (), is for classical Gram–Schmidt — not orthogonal at all — for modified Gram–Schmidt and for Householder.
Symmetry is a vast subject, significant in art and nature. Mathematics lies at its root, and it would be hard to find a better one on which to demonstrate the working of the mathematical intellect.
💡Givens rotations
A Givens rotation acts on two rows only, rotating them so that one chosen entry becomes zero:
Where Householder clears a whole column at once, Givens clears one entry at a time — which is exactly right for sparse matrices and for updating: when a new measurement row arrives, a few rotations fold it into without revisiting the old data. A sensor calibration refined over 200 incoming readings this way, storing only the factor and two numbers, matches the batch solution to .
💡Where you will use this
GNSS Navigation and Attitude Control: square-root information filters propagate by Householder and Givens transformations instead of covariance matrices, because they stay accurate in single precision on flight computers. System identification: recursive least squares with Givens updates tracks changing parameters online. Structures: QR inside eigenvalue solvers for vibration (Unit 6). Data analysis: every regression routine in a serious statistics package.
📚Further reading
- Lloyd N. Trefethen and David Bau, Numerical Linear Algebra (SIAM, 1997), Lectures 7, 8, 10 and 11 — QR, Gram–Schmidt, Householder triangularisation and least squares.
- Gene Golub, 'Numerical methods for solving linear least squares problems', Numerische Mathematik 7, 206–216 (1965) — the paper of the cold open.
- Åke Björck, Numerical Methods for Least Squares Problems (SIAM, 1996), chapters 2–3 — QR methods, modified Gram–Schmidt and updating a factorisation as data arrive.
↓But there is a problem
QR solves full-rank least-squares problems stably. But what if the columns of are nearly dependent — two sensors that measure almost the same thing, a model with one parameter too many? Then has a tiny diagonal entry, the solution is enormous and meaningless, and QR does not tell you which combination of parameters the data cannot determine.
In October 2006 Netflix offered one million dollars to anyone who could predict its customers' film ratings 10% better than its own system. The data were a matrix of 480,000 customers by 17,770 films — almost entirely empty. The ideas that won were built on one factorisation.