📐Least Squares and Data Fitting
Linear Least Squares and the Normal Equations
Overdetermined systems, minimising ‖Ax − b‖, projection, normal equations and weighting
Palermo, 1 January 1801
On the first night of the nineteenth century, Giuseppe Piazzi, director of the Palermo Observatory, notices a faint star in Taurus that is not in his catalogue. The next night it has moved. He follows it for 41 days and records 24 positions — then it disappears into the glare of the Sun.
By autumn it should be visible again, but nobody knows where. Piazzi's observations cover a tiny arc of a long orbit; each is slightly wrong; and the orbit has six unknown elements. The astronomers of Europe look and do not find it.
In Brunswick, Carl Friedrich Gauss, 24 years old, takes up the problem. He develops a new method for determining an orbit from a few observations, fits it to Piazzi's data, and sends his prediction to Franz Xaver von Zach. On 31 December 1801, von Zach and Heinrich Olbers find the object close to where Gauss said it would be. It is Ceres, the largest body in the asteroid belt.
When Gauss finally explained his methods in 1809, at their heart was a principle he said he had used since 1795: when the data cannot all be satisfied, choose the answer that makes the sum of the squares of the misfits as small as possible.
A measurement model that is linear in its unknowns gives a system with of size . With — more measurements than unknowns, which is what every careful experiment does — the system is overdetermined, and with real data it has no exact solution. The residual cannot be made zero. The least-squares solution makes it as short as possible:
Squares rather than absolute values: the problem becomes smooth, has a unique solution when A has full column rank, and is solved by linear algebra alone. (With independent Gaussian errors it is also the maximum-likelihood estimate.)
💡Geometry: least squares is projection
As varies, sweeps out the column space of — an -dimensional plane inside . The data vector is generally not on it. The closest point of the plane to is its orthogonal projection , and the residual is perpendicular to the plane — perpendicular to every column of :
These are the normal equations — 'normal' in the sense of perpendicular. is , symmetric, and positive definite when the columns of are independent, so Cholesky (lesson 3-3) solves them.
Derivation
The same equations by calculus — and the straight-line fit
Multiply out the squared length of the residual:
At a minimum every partial derivative is zero. Differentiating the linear term gives and the quadratic term (the same rule as , since is symmetric):
The geometry and the calculus agree. The second derivative, , is positive definite when the columns are independent, so this stationary point is the minimum.
A straight line. For through points, the rows of are , and the normal equations are only :
and — the formulas behind every calculator's 'linear regression' key, and the tensile-test fit below.
📐Young's modulus from a tensile test
A steel bar, 500 mm long with a 100 mm² cross-section, is loaded from 0 to 35 kN in 5 kN steps; the extensometer reads 0.020, 0.150, 0.271, 0.383, 0.506, 0.627, 0.755 and 0.873 mm. The model is extension load, with absorbing the seating of the grips.
has rows : eight equations, two unknowns. The normal equations give mm and mm/kN, with a residual norm of 0.0106 mm, and to rounding. Then GPa — a typical structural steel.
When the number of unknown quantities is equal to the number of the observed quantities depending on them, the former may be so determined as exactly to satisfy the latter. But when the number of the former is less than that of the latter, an absolutely exact agreement cannot be obtained, unless the observations possess absolute accuracy.
Weighted least squares. Measurements are rarely equally good. If measurement has standard deviation , divide its equation by before solving — equivalently, minimise . Precise readings then count more:
A baseline measured six times by tape ( mm), three times by total station (2 mm) and twice by GNSS (10 mm) has a weighted estimate of 125.43335 m 1.00 mm — better than the total station alone ( 1.15 mm), because the other instruments still contribute. The covariance matrix is as important as the estimate: it is what a surveyor or a navigation filter carries forward.
Derivation
Weights, and the covariance of the estimate
The language of measurement error. Write each reading as . The error is random: on average zero, with variance , the mean of its square ( is its standard deviation). Errors of different instruments are independent: for . For a vector of errors these averages are collected in the covariance matrix : variances on the diagonal, the averages of products of different errors off it. Here it is . (Unit 12 develops all of this further.)
The weighted fit. Minimising is ordinary least squares on rows divided by , so the calculus above gives , that is with .
How errors pass through a linear formula. If , the error of is , and its covariance is
For the weighted estimate, with and :
The baseline. Every reading measures the same length, so is a column of ones and per mm². The standard deviation of the estimate is mm; the three total-station readings alone give mm. Precisions add as , which is why even the poor instruments help a little.
⚠The normal equations square the condition number
(lesson 5-3 proves it with the singular value decomposition). Every digit costs, the normal equations cost twice. Fitting a degree-9 polynomial in the monomial basis on 50 points of : but , and the coefficients come out with relative error from the normal equations against from an orthogonal-factorisation solver. For small, well-conditioned problems the normal equations are fine and fast. For anything else, use QR or the SVD.
🕰Gauss, Legendre and priority
Adrien-Marie Legendre published the method of least squares first, in an 1805 appendix on comet orbits, and named it. Gauss's 1809 claim to have used it since 1795 started one of the famous priority disputes of mathematics. Gauss's lasting contribution was the justification: in 1809 he connected least squares to the normal distribution of errors, and in 1821–23 he proved that it gives the minimum-variance linear unbiased estimate whatever the error distribution — what is now the Gauss–Markov theorem.
💡Where you will use this
GNSS Navigation: every position fix is a (weighted, then nonlinear) least-squares problem, and orbit determination of the satellites themselves is Gauss's Ceres problem with better data. Attitude Determination: Wahba's problem — the rotation that best maps measured star and Sun directions onto catalogue directions — is a weighted least-squares problem. Materials and Structures: calibration, modulus and creep-law fits. Surveying and Geotechnics: network adjustment, settlement trend fitting.
📚Further reading
- Michael Heath, Scientific Computing: An Introductory Survey (2nd ed.; SIAM Classics 2018), chapter 3, 'Linear Least Squares'.
- Åke Björck, Numerical Methods for Least Squares Problems (SIAM, 1996) — the standard reference, from normal equations to weighted and constrained problems.
- Stephen Stigler, 'Gauss and the invention of least squares', Annals of Statistics 9(3), 465–474 (1981) — the Gauss–Legendre priority question, examined from the sources.
↓But there is a problem
The normal equations are elegant: multiply by , and an problem becomes an SPD system. But that multiplication squares the condition number, and an ill-conditioned fitting problem — a high-degree polynomial, nearly collinear regressors, a GNSS fix with satellites bunched together — loses twice the digits it should.
In 1965 Gene Golub, then a young researcher at Stanford, published a short paper showing how to solve least-squares problems without ever forming — using reflections.