Least Squares Lines

← Numerical Methods

Source inspiration: (Mathew 2000-2019).

Description

A least-squares line fits a straight line \(y = b + a x\) to \(n\) data points \((x_k, y_k)\), \(k = 1, \ldots, n\). It chooses the coefficients \(a\) and \(b\) that minimize the sum of the squared residuals, the vertical distances from the points to the line:

\[ E(a, b) = \sum_{k=1}^{n} \bigl(b + a x_k - y_k\bigr)^2 . \]

Setting \(\partial E/\partial a\) and \(\partial E/\partial b\) to zero gives two linear equations, the normal equations, whose solution is

\[ a = \frac{n \sum x_k y_k - \sum x_k \sum y_k}{n \sum x_k^2 - \bigl(\sum x_k\bigr)^2}, \qquad b = \frac{1}{n}\Bigl(\sum y_k - a \sum x_k\Bigr), \]

with every sum over \(k = 1, \ldots, n\). The second normal equation says that the line passes through the centroid \((\bar x, \bar y)\) of the points and that the residuals sum to zero. Mathews’ power fit \(y = a x^m\), with \(m\) fixed, is linear in its one coefficient, so least squares gives

\[ a = \frac{\sum x_k^m y_k}{\sum x_k^{2m}} . \]

The case \(m = 1\) is a line through the origin (Case 3), and \(m = 3/2\) is Kepler’s third law \(T = a r^{3/2}\) (Case 4).

The formulas for \(a\) and \(b\) need at least two distinct \(x_k\): the denominator equals \(n \sum (x_k - \bar x)^2\), which is zero when all the \(x_k\) are equal. The model treats the \(x_k\) as exact and puts all the error in the \(y_k\).

The method is direct, with no convergence order: when the \(x_k\) are not all equal, \(E\) has a unique minimum, and the formulas give it exactly, up to rounding. In general the minimum is \(E = (1 - r^2) \sum (y_k - \bar y)^2\), where \(r\) is the correlation coefficient, so for \(y_k\) not all equal the line passes through every point only when \(|r| = 1\). Adding a point never lowers the minimum: in Case 1 it rises from 0 with two points to \(39/28 \approx 1.3929\) with seven, and the eighth point lands on that line.

The line depends on which variable is treated as dependent: fitting \(x\) as a function of \(y\) minimizes horizontal residuals and gives a different line. In general, if \(x = c y + d\) is the fit of \(x\) on \(y\), then \(a c = r^2\), so the lines coincide only for collinear points, as in the first frame of Case 2; for all eight points \(r^2 \approx 0.9873\). A constraint the data do not satisfy biases the fit: Case 3’s free line is \(y \approx 0.3571 + 1.4762 x\), and forcing it through the origin raises the residual sum of squares from \(0.4762\) to \(0.6863\). In Case 4, \(a\) is the average of the ratios \(T_k / r_k^{3/2}\) weighted by \(r_k^3\), so Mars carries 71% of the weight and Mercury about 1%.

Least Squares Polynomials fits polynomials of higher degree through a larger system of normal equations, and Nonlinear Curve Fitting fits exponential and power curves by fitting a least-squares line to the logarithms of the data.

Animations

Each animation below shows how the fitted model changes as data points are added. Cases 1, 3 and 4 print the fitted coefficients and the residual sum of squares in each frame, and Cases 1 and 3 also draw each vertical residual as a dark blue segment from the point to the line, so you can see the distances whose squares the least-squares criterion adds up. These data lie close to their lines (the largest residual in any frame is about 0.79 in Case 1 and 0.46 in Case 3), so the segments are short and some are barely visible.

Case 1 - Standard Least-Squares Line, \(y = b + a x\)

Behavior: The unconstrained fit minimizes vertical residuals to a straight line. With all eight points the fit is \(a = -45/28 \approx -1.6071\) and \(b = 121/14 \approx 8.6429\). The eighth point \((6, -1)\) lies exactly on the seven-point line, so the last two frames show the same fit.

Julia source

Animation showing the standard least-squares line updating as points are added, with each vertical residual drawn as a short dark blue segment from the point to the line.

Case 2 - Two Regression Lines and Their Intersection

Behavior: With only the first two points, fitting \(y\) as a function of \(x\) and fitting \(x\) as a function of \(y\) give the same line, through both points. From three points on they give two distinct lines in the \(x\)-\(y\) plane, and their intersection coincides with the centroid of the points used, which for all eight points is \((\bar{x}, \bar{y}) = (2.5, 4.625)\). These data are strongly correlated, so the two lines nearly overlap: with all eight points their slopes are \(-1.6071\) and \(-1.6278\).

Julia source

Animation comparing y-on-x and x-on-y regression lines as more points are included, with their intersection (black diamond) drawn on the centroid of the points used (orange star).

Case 3 - Constrained Fit Through the Origin, \(y = a x\)

Behavior: Constraining the intercept to zero changes the optimization geometry and produces a single-parameter fit. With all eight points of the legacy dataset the fit is \(a = 157/102 \approx 1.5392\); as points are added the coefficient zig-zags downward from 2.

Julia source

Animation showing a constrained least-squares fit through the origin for y equals a times x as points are added, with each vertical residual drawn as a short dark blue segment from the point to the line.

Case 4 - Kepler-Style Power Fit, \(T = a r^{3/2}\)

Behavior: With exponent fixed at \(3/2\), the power law is linear in \(a\) after choosing basis function \(r^{3/2}\). Using Mercury, Venus, Earth, and Mars data gives \(a \approx 0.19977\). These are the legacy module’s data; its Mercury distance of 57.59 million km is about 0.55% below its modern semi-major axis of about 57.91 million km, which is why Mercury’s ratio \(T/r^{3/2} \approx 0.2013\) stands apart from the other three planets’ \(0.1997\) to \(0.1999\).

Julia source

Animation showing a power-law least-squares fit T equals a times r to the three-halves for planetary distance and orbital period data.

References

Mathew, John H. 2000-2019. Numerical Analysis - Numerical Methods Modules. https://web.archive.org/web/20190808102217/http://mathfaculty.fullerton.edu/mathews/n2003/NumericalUndergradMod.html.