Skip to content

Fitting lines and curves

Build the design matrix A for a line and for a parabola, solve AᵀA x̂ = Aᵀb, and read the fit and its residuals; see why a model with as many columns as points fits exactly yet predicts badly.

It opens on

Four measurements drift upward but not along a straight line: how much better is a parabola, and what is the price of making the model as flexible as the data?

Why it matters

Almost every trend line, calibration curve and regression in data work is one design matrix and one normal equation; choosing its columns is choosing the model.

Good to know first

  • The normal equations: AᵀA x̂ = Aᵀb and e orthogonal to the columns
  • Matrix-vector multiplication: Ax combines the columns of A
  • Rank: independent columns make AᵀA invertible

Step by step

  1. The design matrix and the best line

    Write A and b for four points, solve the 2 × 2 normal equations and read the line and its residuals.

    For the four points, A = [[1, 0], [1, 1], [1, 2], [1, 3]] and b = (1, 2, 2, 5).

    A common mix-up

    The slope is the slope between the first and last points.

    In fact: The first and last points give 4/3, but the least squares slope uses all four points and is 6/5.

  2. Add a column: the parabola

    Fit y = C + Dx + Ex² by adding an x² column to A and solving the 3 × 3 normal equations.

    A = [[1, 0, 0], [1, 1, 1], [1, 2, 4], [1, 3, 9]] and b = (1, 2, 2, 5).

    A common mix-up

    A parabola fit is not a linear problem because x² is not linear.

    In fact: x² is a fixed column of numbers; the model is linear in C, D, E.

  3. When a model is too flexible

    See that more columns only lower E, that n columns for n points fit exactly, and that an exact fit can mislead.

    The cubic y = 1 + (17/6)x − (5/2)x² + (2/3)x³ gives 1, 2, 2, 5 at x = 0, 1, 2, 3; for example at x = 2: 1 + 17/3 − 10 + 16/3 = 2.

    A common mix-up

    The lowest error is the best model.

    In fact: E = 0 for the cubic, yet its predictions off the data are the least believable.

Try it

Fit y = C + Dx to (0, 2), (1, 3), (2, 7).Answer

y = 1.5 + 2.5x

Hint: 3C + 3D = 12 and 3C + 5D = 17

For the points (−1, 2), (0, 1), (1, 2), (2, 5), write A for y = C + Dx + Ex² and find x̂ by inspection.Answer

x̂ = (1, 0, 1), since b = (2, 1, 2, 5) = 1 + x² exactly

Hint: Compare b with the columns

Explain in two sentences why a parabola fit to five noisy points is more trustworthy than a degree-4 fit.Answer

The degree-4 curve passes through all five points exactly, so it fits the noise and predicts wildly; the parabola uses 3 parameters and keeps the trend

Hint: Count columns against points

Solve 4C + 6D = 10 and 6C + 14D = 21: what are C and D?Answer

D = 6/5 and C = 7/10 (the determinant is 56 − 36 = 20).

Why are these still the normal equations of the last lesson?Answer

The unknowns appear linearly, so the same recipe AᵀA x̂ = Aᵀb applies to any list of columns.

Fitting lines and curves

30 minutes every month. No card.