Least squares is a family of methods for estimating unknown quantities or approximating data by minimizing the sum of squared differences between observed and predicted values. These differences, called residuals, measure how closely a mathematical model reproduces the available observations. The method is fundamental to statistics, numerical analysis, and scientific measurement because it converts many problems involving inconsistent or noisy information into precisely defined optimization problems.
The best-known example is fitting a straight line to a scatter of points, but least squares also determines orbital parameters, calibrates instruments, reconstructs signals, and approximates functions. Its simplest forms admit explicit solutions through linear algebra; more general forms require iterative computation. Its interpretation depends on the setting: least squares can be a purely geometric approximation principle, a statistically justified estimator, or one component of a constrained or regularized inverse problem. These interpretations share the same quadratic criterion but differ in their assumptions and in what their solutions establish.
The squared-error criterion
Suppose observations are associated with known inputs , and a model predicts , where is a vector of unknown parameters. Defining the residual as , the least-squares problem is
The criterion is an objective function. Multiplying it by a positive constant, such as or , does not change its minimizers. The first normalization produces the empirical mean squared error, while the second simplifies differentiation. The word “least” refers to the smallest attainable value over the allowed parameter set, not necessarily to zero error.
Squaring prevents positive and negative residuals from canceling and gives larger discrepancies disproportionately greater influence. A residual of magnitude 4 contributes sixteen times as much as one of magnitude 1. This distinguishes squared error from absolute error, for which the contribution grows proportionally with magnitude. It also explains both the analytical convenience of least squares and its sensitivity to extreme observations. Squared error is smooth in each residual, with first derivative and constant second derivative 2; nevertheless, a nonlinear dependence of residuals on parameters can make the full optimization problem nonconvex.
The simplest example estimates a constant from observations by minimizing . Differentiation gives , the sample mean, and the identity
proves that this is the unique minimizer when . Thus the arithmetic mean is itself a least-squares solution. By contrast, minimizing the sum of absolute deviations gives a median. The choice of loss function therefore determines which feature of the observations is being estimated.
Least squares does not inherently require a probability model. A polynomial approximation to tabulated values can be defined without treating any quantity as random. When observations are random, however, the same criterion acquires statistical interpretations. At the population level, the conditional mean minimizes expected squared prediction error:
provided the relevant second moments exist. This connection to conditional expectation explains why least-squares prediction targets an average response rather than, for example, a conditional median or a full probability distribution.
Linear models and projection geometry
A least-squares problem is linear when its predictions are linear in the unknown coefficients:
Here contains observations, is the design matrix, and contains coefficients. The columns of may contain constants, powers of an input, trigonometric functions, or other fixed features. Consequently, fitting is linear least squares despite the curvature of the fitted parabola. “Linear” describes the coefficients’ role, not necessarily the shape of the fitted relationship.
The objective is
Its gradient is , so every minimizer satisfies the normal equations
The Hessian matrix is , which is positive semidefinite. Thus the objective is convex, and every stationary point is a global minimizer. If has full column rank, the Hessian is positive definite and the solution is unique:
This formula characterizes the solution, although numerical algorithms generally avoid explicitly forming the inverse.
Geometrically, the possible fitted vectors form the column space of , a linear subspace of . Least squares selects the orthogonal projection of onto that subspace. The residual is orthogonal to every column of , precisely the statement . For any coefficient vector ,
This is a form of the Pythagorean theorem and directly establishes optimality. If an intercept column is included, orthogonality to that column implies that the residuals sum to zero.
When columns are linearly dependent, different coefficient vectors can produce the same fitted vector. The fit remains unique, but the coefficients are not uniquely identified. If is one solution, every with is another. The Moore–Penrose pseudoinverse selects the solution having minimum Euclidean norm. This distinction matters in overparameterized models: perfect interpolation can coexist with coefficient nonuniqueness. It also separates two questions often confused in practice—whether the data determine the predictions and whether they determine the individual parameters.
Statistical justification and uncertainty
In ordinary least squares, a standard model assumes
The conditional zero-mean assumption states that the predictors do not systematically encode the unexplained component. The covariance assumption specifies equal error variances and zero pairwise error covariances. It does not, by itself, require independence or a particular distribution. With full column rank,
Thus ordinary least squares is unbiased under these assumptions, and its precision depends on the arrangement of the design points.
The Gauss–Markov theorem states that this estimator has the smallest covariance matrix, in the positive-semidefinite ordering, among estimators that are linear in and conditionally unbiased. “Best linear unbiased” is a restricted optimality statement: it does not establish superiority over every nonlinear or biased estimator. In particular, shrinkage can reduce prediction error by accepting some bias. The theorem also does not require normally distributed errors, an assumption frequently added for a different purpose.
If the errors are independently normally distributed with variance , their joint likelihood is proportional to
Maximizing this expression over is equivalent to minimizing the residual sum of squares, linking least squares to maximum likelihood estimation. Under normality,
For , the residual variance estimator
is unbiased, and has a chi-squared distribution with degrees of freedom. These results yield exact coefficient tests and confidence intervals using Student’s -distribution.
Parameter uncertainty is distinct from uncertainty about a new observation. For a new predictor vector , the estimated mean response has conditional variance . An independent future response has an additional noise variance , so its prediction interval is wider. Without normal errors, exact small-sample formulas generally disappear, although suitable large-sample approximations may remain valid. Crucially, a small residual sum of squares does not establish the zero-mean assumption: omitted variables, reverse relationships, or measurement processes can violate it while leaving an apparently convincing fit.
Unequal precision and correlated errors
When observations differ in precision, treating all squared residuals equally can be inefficient or scientifically inappropriate. Weighted least squares minimizes
where is a specified symmetric positive-definite matrix. For diagonal , this is . If independent errors have known variances , inverse-variance weights make residuals comparable in units of their uncertainties. An observation with standard deviation twice as large then receives one-quarter the weight, not one-half.
For full column rank, the weighted solution is
If , the problem is equivalent to ordinary least squares applied to the transformed data and design . This transformation also supplies a geometric interpretation: projection now uses the inner product . The residual is orthogonal to the design columns in this weighted geometry, even though it need not be orthogonal under the usual Euclidean inner product.
Generalized least squares addresses a known error covariance matrix , including off-diagonal correlations, by taking . Its estimator has covariance
A transformation “whitens” the errors. This is useful for repeated measurements on the same unit, spatial observations, or time series whose errors are serially correlated. With multivariate normal errors and known covariance, generalized least squares is also maximum likelihood estimation for the mean parameters.
In practice, the covariance structure is often unknown. Feasible generalized least squares estimates it and then fits a weighted problem; its properties depend on how accurately that structure is estimated. Estimated weights cannot simply be treated as known in every finite-sample calculation. Moreover, weighting is not the only response to unequal variances: ordinary least-squares coefficients may remain unbiased while their conventional standard errors become incorrect, in which case heteroskedasticity-consistent covariance estimates address inference rather than changing the fitted coefficients. Precision weights must also be distinguished from survey sampling weights or weights that define a target population. Although each can appear in a quadratic objective, the justification and uncertainty calculation are not interchangeable.
Numerical algorithms and conditioning
The normal equations are mathematically compact but can be numerically hazardous. For a full-column-rank matrix, the spectral condition number satisfies
Thus forming can magnify the effects of nearly dependent columns and rounding in floating-point arithmetic. For example, a design matrix with condition number produces a normal-equation matrix with condition number , comparable to the reciprocal of machine precision in standard double-precision arithmetic. This does not imply that every such computation fails, but it makes the loss of reliable coefficient digits a serious possibility.
A standard alternative is QR decomposition. In a thin factorization , the columns of are orthonormal and is upper triangular. Orthogonality reduces the problem to
which is solved by back substitution. Implementations commonly use Householder transformations; plane rotations are especially useful for updating a fit when observations arrive sequentially. Column pivoting helps expose rank deficiency, although deciding an effective numerical rank still requires a tolerance related to precision and data uncertainty.
The singular value decomposition gives a more explicit account of sensitivity. If , then
A small singular value amplifies perturbations along its associated data direction. This explains why coefficients can be unstable even when fitted values change little: large coefficient changes may largely cancel after multiplication by . Singular value decomposition is particularly valuable for rank-deficient problems, diagnosing near-dependence, and computing minimum-norm solutions, though it is often more computationally expensive than QR for a dense full-rank system.
Large problems require additional choices. A dense QR factorization with has leading computational cost of order , while explicitly storing the design may dominate memory use. For sparse matrices, methods such as LSQR use products with and without forming the normal-equation matrix. Iterative convergence depends on the singular-value distribution and can improve with preconditioning. Column scaling may improve numerical behavior, but it changes the meaning of a minimum-norm solution unless parameter scaling is accounted for. Numerical stability must also be distinguished from statistical precision: a stable algorithm can accurately compute a coefficient estimate that remains highly uncertain because the observations contain little information about it.
Nonlinear least squares
In nonlinear least squares, the model is nonlinear in one or more unknown parameters. Examples include
for exponential decay and
when frequency and phase are unknown. Unlike linear least squares, these problems can have multiple local minima, flat directions, or parameter combinations producing nearly identical predictions. A minimum may also fail to exist over an unbounded parameter domain. The optimization problem therefore involves both computational questions and questions of parameter identifiability.
Let be the residual vector and its Jacobian matrix, with entries . For the objective ,
The Gauss–Newton algorithm neglects the second term and computes a step by solving
It then updates by that step, often with a step-length adjustment. The approximation is especially effective near a solution with small residuals and a well-conditioned Jacobian. It can be poor when residuals remain large or the neglected curvature is important.
The Levenberg–Marquardt algorithm stabilizes the local step through a system such as
With large damping, the direction resembles a scaled gradient step; with small damping, it approaches Gauss–Newton. Modern implementations often interpret damping through a trust-region constraint and adapt it according to how accurately the local model predicts the achieved decrease. Bounds and other constraints can require specialized methods. Derivatives may be obtained analytically, by finite differences, or by automatic differentiation, whose accuracy and computational cost differ.
Near an identifiable optimum, an approximate covariance matrix is often written , using the Jacobian evaluated at the fitted parameters. This is a local linearization, not a universal exact uncertainty formula. Strong curvature, weak identification, parameter boundaries, and multiple minima can make the actual uncertainty asymmetric or disconnected. Some models permit variable projection: in , the parameters and can be solved by linear least squares for each fixed , leaving a lower-dimensional nonlinear search. This reduces computational effort while preserving the original objective, although it does not remove difficulties caused by an uninformative range of observation times.
Regularization, constraints, and inverse problems
Least squares becomes problematic when many parameter vectors explain the observations nearly equally well. In an inverse problem, this can arise because the measurement process suppresses some components of the unknown signal. Small observational errors then produce large changes in the unregularized solution. Regularization changes the optimization problem by adding information about acceptable solutions:
This is Tikhonov regularization. The matrix may penalize coefficient magnitude, first differences, or curvature, depending on the application.
When , the method is ridge regression, with solution
for . In singular-vector coordinates, the unregularized factor becomes , damping poorly determined directions. With a general , uniqueness for positive requires that and have no nonzero vector in their common null space. The regularization parameter controls the balance between agreement with the observations and the penalty, and its interpretation depends on how the loss and predictors are scaled.
Regularization usually introduces bias but may substantially reduce variance and predictive error, illustrating the bias–variance tradeoff. In Bayesian inference, a Gaussian observation model combined with a suitable Gaussian prior produces a quadratic penalty; the minimizer is then a posterior mode. Different penalties encode different assumptions. Lasso regression uses an penalty and can set coefficients exactly to zero, while quadratic ridge penalties generally shrink rather than eliminate coefficients. Choosing a penalty therefore affects both numerical stability and the kind of structure favored in the solution.
Constraints provide another way to express prior knowledge. Nonnegative least squares requires , appropriate when coefficients represent amounts that cannot be negative. Linear equality constraints can enforce conservation laws; monotonicity or shape constraints can restrict fitted curves. These problems remain convex when the model is linear and the feasible set is convex, but their solutions need not satisfy the unconstrained normal equations. Regularization strengths are often compared through cross-validation or estimated-noise criteria, with validation schemes reflecting dependence in the data. In function reconstruction, early stopping of an iterative solver or truncation of small singular values can also act as regularization, so the numerical procedure itself may influence the effective estimator.
Model assessment, influence, and alternatives
A small training residual sum of squares is not by itself evidence that a model predicts well or describes the relevant mechanism. Adding columns to a linear design cannot increase the minimum training error, even if the new variables contain no useful signal. In a full-rank model with an intercept, the coefficient of determination is
provided the denominator is positive. It measures improvement over fitting a constant on the same observations, not causal validity or guaranteed out-of-sample performance. Adjusted measures and held-out prediction errors address different aspects of model comparison.
For ordinary least squares, the fitted vector is , where
is the hat matrix. Its diagonal entries measure leverage: observations with unusual predictor combinations can strongly affect their own fitted values. Under equal independent error variance,
Raw residuals therefore do not all have the same variance, even under the simplest model. Measures of influence combine leverage with residual size. A high-leverage point need not be inconsistent with the fitted model, while an observation with a large residual need not greatly change the coefficients.
Residual patterns can reveal aspects of misspecification. Curvature against a predictor suggests an inadequate mean function; changing spread suggests unequal variance; temporal runs can indicate correlated errors. Such patterns do not uniquely identify their causes. Least squares is especially sensitive to outliers because its squared penalty grows rapidly. Robust regression uses alternatives such as absolute loss or Huber loss, which is quadratic near zero and linear for sufficiently large residuals. These methods change the objective and often require iterative algorithms. They address particular forms of contamination but do not automatically correct omitted-variable bias, dependence, or errors in predictors.
Ordinary regression treats predictor values as fixed or adequately measured relative to the modeled error. If predictors themselves contain measurement error, minimizing vertical residuals can be inappropriate. Total least squares permits corrections to both the predictor matrix and response, commonly through a singular value decomposition of the augmented matrix . Its basic form assumes a particular isotropic error geometry and is sensitive to variable scaling. Finally, predictive association must be separated from causal inference: confounding and endogeneity can invalidate a causal interpretation even when residual diagnostics appear satisfactory. Causal conclusions depend on research design and identifying assumptions beyond the least-squares calculation.
Function approximation and scientific applications
Least squares extends from fitting finite observations to approximating functions. Given a function , basis functions , and a nonnegative weight function , an approximation
can be defined by minimizing
The normal equations use the Gram matrix . If the basis is orthonormal under this inner product, , and each coefficient is simply the inner product of with its corresponding basis function. This is the continuous counterpart of projecting an observation vector onto a design matrix’s column space.
In a Hilbert space, every element has a unique closest point in any closed linear subspace. This projection theorem supplies a general setting for least-squares approximation. Closedness matters for infinite-dimensional subspaces: without it, the infimum approximation error need not be attained. Fourier series provide an important example, with finite sets of orthogonal trigonometric functions producing best squared-integral approximations within their spans. Sampled least squares and continuous approximation are related, but the sampling locations and weights determine whether a discrete fit accurately represents the intended integral norm.
In scientific measurement, least squares combines redundant observations into parameter estimates while quantifying disagreement. Surveying and geodesy use adjustment calculations to reconcile angle and distance measurements subject to geometric relationships. Orbit determination compares predicted celestial positions with observations and usually produces a nonlinear least-squares problem. Instrument calibration fits a relationship between known reference inputs and measured outputs; inverse-variance weighting is relevant when reference measurements have unequal precision. In spectroscopy, fixed component spectra can form columns of a design matrix, and their estimated amplitudes can represent mixture contributions, sometimes subject to nonnegativity constraints.
Least squares also appears in signal reconstruction, system identification, and machine learning. Linear filters can be selected to minimize average squared prediction error, while low-rank matrix approximation minimizes a squared Frobenius norm and leads to principal component analysis through singular value decomposition. Experimental design uses matrices such as to compare how candidate measurement arrangements determine parameters: designs with nearly dependent columns leave some combinations poorly estimated. Across these applications, the residual coordinates and their scaling are substantive choices. Minimizing squared errors in raw measurements, logarithms, frequencies, or transformed outputs generally produces different estimators because each formulation assigns importance to different kinds of discrepancy.
Historical development
Least squares emerged from the practical problem of combining more observations than there were unknown quantities, especially in astronomy and geodesy. Measurements rarely satisfied an exact set of equations because of observational error. Earlier approaches combined observations through averaging, selected equations, or other adjustment rules. The eventual least-squares criterion supplied a systematic way to use all the measurements simultaneously, while differentiation converted the minimization into equations suitable for calculation. Its development was therefore tied both to theories of observational error and to the expansion of quantitative astronomy.
Adrien-Marie Legendre published the first explicit account of the method in 1805, in an appendix to Nouvelles méthodes pour la détermination des orbites des comètes. He presented the minimization of the sum of squared errors and the resulting linear equations. Carl Friedrich Gauss published his treatment in 1809 in Theoria motus corporum coelestium in sectionibus conicis solem ambientium and stated that he had used the method since 1795. Their competing claims produced a priority dispute. Legendre’s publication priority and Gauss’s substantial role in developing the method are distinct historical facts; Gauss’s claimed earlier use does not alter the date of the first published exposition.
Gauss’s 1809 treatment connected the method with a normal error law, using assumptions concerning the most probable value of a measured quantity. Pierre-Simon Laplace subsequently developed probability-based justifications associated with the aggregation of observational errors and large-sample reasoning. These arguments helped move least squares beyond a convenient computational prescription toward a theory of estimation. In work published in 1823, Gauss gave a minimum-variance justification under assumptions on error moments rather than a full normal distribution. This line of reasoning became central to the result now known as the Gauss–Markov theorem.
During the nineteenth century, least squares became a standard instrument of astronomical and geodetic adjustment, while its connections to correlation and regression extended its use in statistics. Twentieth-century developments separated and strengthened its computational, statistical, and optimization foundations. Kenneth Levenberg introduced a damped approach to nonlinear least squares in 1944, and Donald Marquardt developed a related influential algorithm in 1963. Householder transformations, matrix factorizations, and singular value methods enabled more reliable electronic computation than direct reliance on normal equations. Later large-scale iterative methods made it possible to fit sparse systems with numbers of observations and unknowns far beyond the reach of hand calculation, while regularization connected least-squares estimation to modern inverse problems and high-dimensional prediction.