Least Squares Lines

Numerical Methods

Source inspiration: (Mathew 2000-2019).

Description

Least squares line fitting chooses parameters that minimize the sum of squared residuals between measured data and a model. The standard linear model is \(y = b + a x\), where residuals are vertical distances from points to the fitted line.

This module also highlights two important variations:

  • Regressing \(x\) on \(y\) instead of \(y\) on \(x\) produces a different line after rearrangement, even for the same data.
  • Constrained models such as \(y = a x\) and \(y = a x^{3/2}\) remain linear in the unknown coefficient \(a\), so least squares still applies directly.

Animations

Each animation below shows how the fitted model changes as data points are added. Residuals and fitted parameters update frame-by-frame so you can see what the least-squares criterion is doing geometrically.

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

Behavior: The unconstrained fit minimizes vertical residuals to a straight line. As points are added, the slope and intercept stabilize near \(a \approx -1.6071\) and \(b \approx 8.6429\).

Julia source

Animation showing the standard least-squares line updating as points are added, with residual segments drawn vertically to the line.

Case 2 - Two Regression Lines and Their Intersection

Behavior: Fitting \(y\) as a function of \(x\) and fitting \(x\) as a function of \(y\) produce two distinct lines in the \(x\)-\(y\) plane. Their intersection coincides with the centroid of the data, here approximately \((\bar{x}, \bar{y}) = (2.5, 4.625)\).

Julia source

Animation comparing y-on-x and x-on-y regression lines, showing their moving intersection and the data centroid as more points are included.

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. The coefficient settles near \(a \approx 1.5392\) for the legacy dataset.

Julia source

Animation showing a constrained least-squares fit through the origin for y equals a times x, with residuals updating as points are added.

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\).

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.

Derivation Notes (Planned)

Short derivations will be added to explain the core equations and assumptions.

Worked Example (Planned)

A compact numerical example with intermediate steps will be included.

Implementation Notes (Planned)

Implementation details, numerical stability notes, and practical pitfalls will be added.

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.