Least squares fits a model by minimizing the sum of squared differences between observations and predictions. It is widely used in regression, curve fitting, system identification, and prediction.
The basic idea
If \(y_i\) is an observation and \(h_i^\mathsf T x\) is the prediction, form the residual \(r_i=y_i-h_i^\mathsf T x\). Ordinary least squares gives the residuals equal weight. Weighted least squares uses positive weights, often proportional to measurement precision when errors are independent.
Matrix form
Stack the rows from all measurements into \(H\) and the readings into \(y\). Minimize:
\[J(x)=(y-Hx)^\mathsf T(y-Hx).\]Setting the gradient to zero gives the normal equations \(H^\mathsf T H\hat x=H^\mathsf T y\). If \(H\) has full column rank:
\[\hat x=(H^\mathsf T H)^{-1}H^\mathsf T y.\]In numerical code, use QR or SVD rather than explicitly inverting \(H^\mathsf T H\). SVD also handles rank-deficient cases and can return a minimum-norm solution. With a positive definite weight matrix \(W\), the normal equations become \(H^\mathsf T WH\hat x=H^\mathsf T Wy\).
Historical note
Legendre published the method before Gauss’s 1809 account. Work in astronomy helped establish least squares as a practical method for extracting estimates from noisy observations.
For sequential data, see recursive least squares.


