When there are more data points than unknowns, no curve passes through all of them. Least squares picks the curve that makes the total squared error smallest: the normal equations, their matrix form, how to judge a fit, and where the method breaks.
A food-delivery company records six orders: distance (km) and delivery time (minutes). How long will a 15 km order take?
| distance (km) | time (min) |
|---|---|
| 2 | 14 |
| 4 | 21 |
| 5 | 24 |
| 7 | 30 |
| 9 | 37 |
| 11 | 42 |
| 15 | ? |
Interpolation: a polynomial through every data point (Lectures 16–18); solving linear systems (Lectures 12–13).
A simple curve that passes close to all points, chosen by minimising the sum of squared errors.
input x[1..n], y[1..n] Sx ← Σ x; Sy ← Σ y Sxx ← Σ x²; Sxy ← Σ x·y D ← n·Sxx − Sx² b ← (n·Sxy − Sx·Sy) / D a ← (Sy − b·Sx) / n S ← Σ (y − a − b·x)² R² ← 1 − S / Σ (y − ȳ)² return a, b, S, R²
One pass over the data: work.
| 1 | 2 | 14 | 4 | 28 |
| 2 | 4 | 21 | 16 | 84 |
| 3 | 5 | 24 | 25 | 120 |
| 4 | 7 | 30 | 49 | 210 |
| 5 | 9 | 37 | 81 | 333 |
| 6 | 11 | 42 | 121 | 462 |
| 38 | 168 | 296 | 1237 |
. Every number on the right comes from the bottom row of the table.
There is no hand formula for 81 coefficients. Numerically: an 81 × 81 system , or a QR factorisation of , solved in well under a second. This is linear regression as used in industry.
| matrix | condition number | digits lost (of 16) |
|---|---|---|
| (years as given) | about 6 | |
| (normal equations) | about 13 | |
| with centred | 2.9 | none |
Fixes: centre and scale the inputs, or solve by QR (, then ), which works with cond() instead of its square. Library routines (numpy.linalg.lstsq) use QR or the SVD.
Years as given:
determinant out of numbers near : nearly singular.
Centred, :
Rule of thumb: fit, then look at the residuals before trusting ; solve by QR, not by forming , when columns are nearly dependent.
| Approach | Passes through points? | Work ( points, coefficients) | Accuracy | Use when |
|---|---|---|---|---|
| Interpolation (L16–18) | yes, all | exact at data | data exact, few points | |
| Least-squares line by sums | no | sums may cancel | one input, by hand | |
| Normal equations + Cholesky | no | loses digits | well-conditioned, | |
| QR factorisation | no | loses digits | default in libraries | |
| Gradient descent (L25) | no | per pass | iterative | huge and |
Interpolation trusts every point; least squares trusts the trend. With noisy data, least squares predicts better.
Least squares is the foundation of data fitting. Its weak points lead to the last two lectures.
Click a card to see least squares at work.
Where exactly: scikit-learn's LinearRegression and numpy.linalg.lstsq. Training means solving ; the "features" are the columns of .
The loss used to train it, mean squared error, is .
Where exactly: instrument labs. Load a spring with known forces = 0, 1, 2, 3, 4 N and read the length = 10.0, 10.9, 12.1, 13.0, 13.9 cm. Fit .
: natural length 10.0 cm, stiffness N/cm. The calibration line then converts readings into forces.
Where exactly: every phone. The unknowns are the position and the receiver clock error: 4 unknowns. With 8–12 satellites in view there are more equations than unknowns.
Extra satellites reduce the error: least squares averages the noise.
Where exactly: any scatter plot with a trend line, e.g. Excel's "Add trendline" or a regression in a research paper.
A trend line describes the data; it does not prove cause and effect.
Least squares gives (), so a 15 km order takes about 55 minutes, an extrapolation to be checked with new data. The degree-5 interpolant's 32.9 min at 13 km shows why we do not interpolate noisy data.
Lecture 24: polynomial, exponential and trigonometric fitting. Curved trends, growth laws, periodic data, and the danger of too many coefficients.