## Least Squares Fitting

A mathematical procedure for finding the best fitting curve to a given set of points by minimizing the sum of the squares of the offsets (the residuals'') of the points from the curve. The sum of the squares of the offsets is used instead of the offset absolute values because this allows the residuals to be treated as a continuous differentiable quantity. However, because squares of the offsets are used, outlying points can have a disproportionate effect on the fit, a property which may or may not be desirable depending on the problem at hand.

In practice, the vertical offsets from a line are almost always minimized instead of the perpendicular offsets. This allows uncertainties of the data points along the - and -axes to be incorporated simply, and also provides a much simpler analytic form for the fitting parameters than would be obtained using a fit based on perpendicular distances. In addition, the fitting technique can be easily generalized from a best-fit line to a best-fit polynomial when sums of vertical distances are used (which is not the case using perpendicular distances). For a reasonable number of noisy data points, the difference between vertical and perpendicular fits is quite small.

The linear least squares fitting technique is the simplest and most commonly applied form of Linear Regression and provides a solution to the problem of finding the best fitting straight line through a set of points. In fact, if the functional relationship between the two quantities being graphed is known to within additive or multiplicative constants, it is common practice to transform the data in such a way that the resulting line is a straight line, say by plotting vs. instead of vs. . For this reason, standard forms for Exponential, Logarithmic, and Power laws are often explicitly computed. The formulas for linear least squares fitting were independently derived by Gauß and Legendre.

For Nonlinear Least Squares Fitting to a number of unknown parameters, linear least squares fitting may be applied iteratively to a linearized form of the function until convergence is achieved. Depending on the type of fit and initial parameters chosen, the nonlinear fit may have good or poor convergence properties. If uncertainties (in the most general case, error ellipses) are given for the points, points can be weighted differently in order to give the high-quality points more weight.

The residuals of the best-fit line for a set of points using unsquared perpendicular distances of points are given by

 (1)

Since the perpendicular distance from a line to point is given by
 (2)

the function to be minimized is
 (3)

Unfortunately, because the absolute value function does not have continuous derivatives, minimizing is not amenable to analytic solution. However, if the square of the perpendicular distances

 (4)

is minimized instead, the problem can be solved in closed form. is a minimum when (suppressing the indices)
 (5)

and

 (6)

The former gives
 (7)

and the latter
 (8)

But

 (9)

so (8) becomes

 (10)

 (11)

 (12)

Plugging (7) into (12) then gives

 (13)
After a fair bit of algebra, the result is

 (14)

So define
 (15)

 (16)

with found using (7). Note the rather unwieldy form of the best-fit parameters in the formulation. In addition, minimizing for a second- or higher-order Polynomial leads to polynomial equations having higher order, so this formulation cannot be extended.

Vertical least squares fitting proceeds by finding the sum of the squares of the vertical deviations of a set of data points

 (17)

from a function . Note that this procedure does not minimize the actual deviations from the line (which would be measured perpendicular to the given function). In addition, although the unsquared sum of distances might seem a more appropriate quantity to minimize, use of the absolute value results in discontinuous derivatives which cannot be treated analytically. The square deviations from each point are therefore summed, and the resulting residual is then minimized to find the best fit line. This procedure results in outlying points being given disproportionately large weighting.

The condition for to be a minimum is that

 (18)

for , ..., . For a linear fit,
 (19)

so
 (20)

 (21)

 (22)

 (23)

 (24)

where the subscripts have been dropped for conciseness. In Matrix form,
 (25)

so
 (26)

The Matrix Inverse is

 (27)

so
 (28) (29) (30) (31)

(Kenney and Keeping 1962). These can be rewritten in a simpler form by defining the sums of squares
 (32) (33) (34)

which are also written as
 (35) (36) (37)

Here, is the Covariance and and are variances. Note that the quantities and can also be interpreted as the Dot Products
 (38) (39)

In terms of the sums of squares, the Regression Coefficient is given by

 (40)

and is given in terms of using (24) as
 (41)

The overall quality of the fit is then parameterized in terms of a quantity known as the Correlation Coefficient, defined by
 (42)

which gives the proportion of which is accounted for by the regression.

The Standard Errors for and are

 (43) (44)

Let be the vertical coordinate of the best-fit line with -coordinate , so
 (45)

then the error between the actual vertical point and the fitted point is given by
 (46)

Now define as an estimator for the variance in ,
 (47)

Then can be given by
 (48)

(Acton 1966, pp. 32-35; Gonick and Smith 1993, pp. 202-204).

Generalizing from a straight line (i.e., first degree polynomial) to a th degree Polynomial

 (49)

the residual is given by
 (50)

The Partial Derivatives (again dropping superscripts) are
 (51)

 (52)

 (53)

 (54)

 (55)

 (56)

or, in Matrix form
 (57)
This is a Vandermonde Matrix. We can also obtain the Matrix for a least squares fit by writing
 (58)

Premultiplying both sides by the Transpose of the first Matrix then gives

 (59)

so

 (60)

As before, given points and fitting with Polynomial Coefficients , ..., gives
 (61)

In Matrix notation, the equation for a polynomial fit is given by

 (62)

This can be solved by premultiplying by the Matrix Transpose ,
 (63)

This Matrix Equation can be solved numerically, or can be inverted directly if it is well formed, to yield the solution vector
 (64)

Setting in the above equations reproduces the linear solution.

See also Correlation Coefficient, Interpolation, Least Squares Fitting--Exponential, Least Squares Fitting--Logarithmic, Least Squares Fitting--Power Law, Moore-Penrose Generalized Matrix Inverse, Nonlinear Least Squares Fitting, Regression Coefficient, Spline

References

Acton, F. S. Analysis of Straight-Line Data. New York: Dover, 1966.

Bevington, P. R. Data Reduction and Error Analysis for the Physical Sciences. New York: McGraw-Hill, 1969.

Gonick, L. and Smith, W. The Cartoon Guide to Statistics. New York: Harper Perennial, 1993.

Kenney, J. F. and Keeping, E. S. Linear Regression, Simple Correlation, and Contingency.'' Ch. 8 in Mathematics of Statistics, Pt. 2, 2nd ed. Princeton, NJ: Van Nostrand, pp. 199-237, 1951.

Kenney, J. F. and Keeping, E. S. Linear Regression and Correlation.'' Ch. 15 in Mathematics of Statistics, Pt. 1, 3rd ed. Princeton, NJ: Van Nostrand, pp. 252-285, 1962.

Lancaster, P. and Salkauskas, K. Curve and Surface Fitting: An Introduction. London: Academic Press, 1986.

Lawson, C. and Hanson, R. Solving Least Squares Problems. Englewood Cliffs, NJ: Prentice-Hall, 1974.

Nash, J. C. Compact Numerical Methods for Computers: Linear Algebra and Function Minimisation, 2nd ed. Bristol, England: Adam Hilger, pp. 21-24, 1990.

Press, W. H.; Flannery, B. P.; Teukolsky, S. A.; and Vetterling, W. T. Fitting Data to a Straight Line'' Straight-Line Data with Errors in Both Coordinates,'' and General Linear Least Squares.'' §15.2, 15.3, and 15.4 in Numerical Recipes in FORTRAN: The Art of Scientific Computing, 2nd ed. Cambridge, England: Cambridge University Press, pp. 655-675, 1992.

York, D. Least-Square Fitting of a Straight Line.'' Canad. J. Phys. 44, 1079-1086, 1966.