How to Minimize a Sum-of-Squares Error Function
At a glance
- 1. Define residuals
Write each error as the model prediction minus the observed value, then square and add the residuals.
- 2. Differentiate
For a differentiable model, compute the gradient of the sum with respect to its parameters.
- 3. Solve for candidates
Set the gradient to zero; for a linear model, this gives the least-squares normal equations.
- 4. Check the result
Confirm the candidate minimizes the objective, and check whether it is unique or only one of several minimizers.
A sum-of-squares error measures how far a model’s predictions are from observed values. To minimize it, define the residuals, form the objective, and find parameter values that minimize that objective. The exact method depends on the model: linear models lead to a system of least-squares equations, while nonlinear models may require iterative numerical optimization.
Without a specific function or dataset, there is no single numerical minimizer to report. The method below covers the standard least-squares setup and shows a complete example.
Define the error function
Suppose you have observations and a model prediction for observation , where is the vector of parameters. The residual is the difference between the prediction and the observed value:
The sum-of-squares error, also called the sum of squared residuals, is
Minimizing means choosing parameters that make the total squared discrepancy as small as possible. Squaring prevents positive and negative residuals from canceling and gives larger errors more influence.
Differentiate and find candidate minima
For a differentiable model, differentiate the objective with respect to its parameters and set the gradient equal to zero. If is the Jacobian matrix of the residuals, then
Thus, any interior differentiable minimum must satisfy
This condition finds stationary points, not automatically global minima. For a linear model, the objective is a convex quadratic, so any solution of the normal equations is a global minimizer. For a nonlinear model, a stationary point could instead be a local minimum, a local maximum, or a saddle point; further checks or an optimization algorithm may be needed.
Linear least squares
If the model is linear in its parameters, write its predictions as , where is the design matrix. With observation vector , the objective is
Differentiating and setting the gradient to zero gives the normal equations:
If the columns of are linearly independent, the minimizer is unique and can be written as
The inverse formula is useful for theory, but is often not the best way to compute a result. In numerical work, QR factorization or singular value decomposition (SVD) is generally more stable, especially when columns of are nearly dependent. If the columns are dependent, a minimizer still exists, but there may be multiple parameter vectors giving the same minimum error.
Worked example: fit a straight line
Fit a line to the three observations , , and . The error function is
For a straight-line least-squares fit, the slope and intercept can be calculated from the sample means:
So the fitted line is . Its predictions are , , and . The residuals (prediction minus observation) are , , and , giving
The objective is a convex quadratic in and , and the design matrix has independent columns, so these parameters give the unique global minimum.
Nonlinear models and variants
When is nonlinear in the parameters, the gradient equation usually cannot be solved in a single step. Methods such as gradient descent, Gauss–Newton, and Levenberg–Marquardt search for a minimum iteratively. Their result can depend on the initial parameter values, so it is sensible to check convergence and, when practical, try more than one starting point.
If observations should have different importance, use a weighted sum of squares, , with nonnegative weights . The weights change the objective and therefore can change the minimizing parameters.
Common mistakes
- Forgetting to square the residuals: The sum is a different objective and allows errors to cancel.
- Assuming every stationary point is a global minimum: This follows for linear least squares because the objective is convex, but not for every nonlinear model.
- Assuming the minimizer is always unique: Uniqueness for a linear model requires the design matrix to have full column rank.
- Computing with an explicit matrix inverse by default: QR or SVD is usually a more reliable numerical approach.
- Confusing squared error with root-mean-square error: Dividing the sum by the number of observations, or taking its square root, changes the reported scale; for an unweighted fixed dataset, those transformations do not change the minimizing parameters.
References
- Least Squares Problems, Åke Björck
- Numerical Linear Algebra, Lloyd N. Trefethen and David Bau III
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.