Curved trends with the same least-squares machinery: polynomial regression, straightening growth and power laws with logarithms, fitting periodic data, and choosing a model that follows the trend without memorising the noise.
A lab counts bacteria every hour. Each hour the count is multiplied by about 1.5. When does the culture double, and how many cells will there be after 8 hours?
| time (h) | count |
|---|---|
| 0 | 100 |
| 1 | 150 |
| 2 | 230 |
| 3 | 340 |
| 4 | 510 |
| 5 | 770 |
| 8 | ? |
Least squares for ; matrix form ; conditioning (Lecture 23).
Same normal equations with new columns: , , ; and logarithms for growth laws.
input x[1..n], y[1..n], degree m
for k = 0 … 2m: P[k] ← Σ x^k
for k = 0 … m: r[k] ← Σ x^k·y
for j, k = 0 … m:
M[j][k] ← P[j + k]
a ← solve(M, r) # Gauss / Cholesky
S ← Σ (y − poly(a, x))²
return a, SFor large use QR on centred, scaled instead (see the limitations).
| sum | value | sum | value |
|---|---|---|---|
| 6 | 93.8 | ||
| 21 | 454.4 | ||
| 91 | 2354 | ||
| 441 | |||
| 2275 |
No logarithm turns this into a straight line in , and together. The only way is to minimise iteratively: Gauss–Newton or gradient descent (Lecture 25). This is how epidemic and market-adoption curves are fitted.
| problem | what happens | remedy |
|---|---|---|
| High degree, normal equations | cond() for 10 points in : degree 3: , degree 5: , degree 9: | centre and scale ; QR; orthogonal polynomials |
| Overfitting | degree 9 through 10 noisy points: train error 0, test error 0.58 | choose the degree by validation; regularise (ridge) |
| Extrapolating a polynomial | Lecture 23 data, degree 5: 40.4 min at 12 km, 32.9 at 13 km | use a model with physical meaning; do not extrapolate far |
| Log transform | needs ; a count of 0 gives | add a small constant, or fit directly |
| Log transform weights | minimises relative error: small dominate, large fit worse | weights , or Gauss–Newton (L25) |
Rule of thumb: pick the model from the physics or from the shape on log axes, keep the degree low, and check it on data not used for fitting.
| Model | Linear in unknowns? | How to fit | Size of system | Typical use |
|---|---|---|---|---|
| yes | sums formulas (L23) | 2 × 2 | trends | |
| yes | normal equations / QR | square | smooth curves, low | |
| no, but linearisable | line fit to | 2 × 2 | growth, decay, cooling | |
| no, but linearisable | line fit to | 2 × 2 | scaling laws | |
| yes (ω known) | decoupled formulas | 3 diagonal | cycles, seasons | |
| no | Gauss–Newton (L25) | iterative | saturation, adoption |
"Linear least squares" means linear in the coefficients, not a straight-line curve.
Every fit so far was a single linear solve. The last lecture replaces the linear solve with an iteration that can minimise anything.
Click a card to fit real-world laws.
Where exactly: astronomy. Orbit size (AU) and period (years) for Mercury to Saturn. Fit on log–log axes.
, : , the law Kepler found by hand in 1619.
Where exactly: the chip industry. Transistor counts of processors 1971–2018 grow from 2300 to 20 billion; a straight line fits against the year.
Logarithmic vertical axis: the exponential is a straight line. per year (years counted from 1971): doubling time years. Rough counts for typical chips, rounded.
Where exactly: weather and power-grid planning. Readings every 3 hours over a day, fitted with one cosine and one sine of period 24 h.
Mean 27.4 °C, amplitude 5.5 °C, warmest at about 2:45 pm. Grid operators add weekly and yearly terms the same way.
Where exactly: pharmacokinetics. After a dose, concentration decays like ; the half-life sets how often a medicine is taken.
Same calculation as the bacteria, with .
: the bacteria double every 1.70 hours, and after 8 hours there will be about 2600 cells (direct nonlinear fit: 2609).
Lecture 25: numerical optimisation for machine learning. Golden-section search, gradient descent, Newton and Gauss–Newton: minimising any error function.