---
title: Learning from data
heading: Fitting a model to examples, the sum-of-squares error and its root-mean-square form, least squares for a line, training and test error, overfitting and the gap between them, and how more data or regularization closes it
eyebrow: S14 · Lesson 1 · Learning from data
xp: 50
---

::: hook
An engineer fitting a calibration curve to measurements, a modem adapting its equalizer, and a speech recognizer trained on hours of audio are all doing the same thing: choosing a model's parameters so it fits examples, in the hope it will also fit cases it hasn't seen. The hope is the hard part. A flexible enough model can fit any training set perfectly and still predict badly, and the whole of machine learning is about managing that gap. This course builds the tools, from least squares through Bayesian inference to neural networks, with every number checked.
:::

::: play
- For a linear model with p parameters fitted by least squares to N noisy examples (noise variance σ²), the expected training and test errors when the model's form is correct: $\sigma^2(1 - p/N)$ and $\sigma^2(1 + p/N)$.
- Training error always falls as parameters are added; test error rises. The gap, $2\sigma^2p/N$, is the cost of fitting the noise.
- More data (larger N) shrinks the gap for the same model.
:::

```sim plot
view: graph
x: p = 1..40
y: E = 0..0.1
slider: N = 20..400 step 10 = 50
slider: s = 0.01..0.05 step 0.005 = 0.04
plot: s*(1 - p/N) | samples: 200 | label: expected training error
plot: s*(1 + p/N) | samples: 200 | color: ch3 | label: expected test error
plot: s + 0*p | dashed | color: ch4 | label: noise floor σ²
readout: gap at 10 parameters = 2*s*10/N | digits: 3
```

::: predict
## A model with 5 parameters is fitted to 20 examples whose noise variance is 0.04. If the model's form is right, what mean squared error should you expect on new data?

- [ ] 0.03, the same as on the training data
- [ ] 0.04, the noise variance
- [x] 0.05
- [ ] 0.2

::: reveal
Least squares fits part of the noise along with the signal, lowering the training error to $\sigma^2(1 - p/N)$ = 0.03. On new data that fitted noise hurts instead of helping: $\sigma^2(1 + p/N)$ = 0.05. The gap of 0.02 is $2\sigma^2p/N$, and only more data or fewer effective parameters shrinks it.
:::
:::

::: nameit
## The setup

Given N examples $(x_n, t_n)$, choose parameters **w** of a model $y(x, \mathbf{w})$ to minimize the sum-of-squares error

$$E(\mathbf{w}) = \frac{1}{2}\sum_{n=1}^N\left[y(x_n, \mathbf{w}) - t_n\right]^2$$

and report the **root-mean-square** error $E_\text{RMS} = \sqrt{2E/N}$, in the units of t.

## Least squares for a line

For $y = w_0 + w_1x$, setting the derivatives to zero gives

$$w_1 = \frac{N\sum xt - \sum x\sum t}{N\sum x^2 - (\sum x)^2}, \qquad w_0 = \bar{t} - w_1\bar{x}$$

## Generalization

The **training error** measures the fit to the examples; the **test error**, on fresh data, measures what matters. For a correct linear model with p parameters, N examples and noise variance σ², their expectations are $\sigma^2(1 \mp p/N)$. A model with too many parameters for its data **overfits**: it matches the noise.

## Remedies

More data; fewer parameters; or **regularization**, adding a penalty $\frac{\lambda}{2}\|\mathbf{w}\|^2$ to E that shrinks weights toward zero. For a single weight through the origin, regularized least squares gives $w = \sum xt/(\sum x^2 + \lambda)$.
:::

::: example
## Fit a line to (0, 1), (1, 3), (2, 4) and find its RMS error.

::: solution
1. **Sums.** For the three points:
   $$N = 3,\ \sum x = 3,\ \sum t = 8$$
   $$\sum x^2 = 5,\ \sum xt = 11$$
2. **Slope.**
   $$\begin{aligned} w_1 &= \frac{3 \times 11 - 3 \times 8}{3 \times 5 - 9} \\ &= 1.5 \end{aligned}$$
3. **Intercept.**
   $$\begin{aligned} w_0 &= 8/3 - 1.5 \times 1 \\ &= 1.167 \end{aligned}$$
4. **Residuals.** They are −0.167, 0.333 and −0.167, so E = 0.0833 and
   $$\begin{aligned} E_\text{RMS} &= \sqrt{2 \times 0.0833/3} \\ &= 0.236 \end{aligned}$$

**Answer:** the line is $y = 1.167 + 1.5x$, with an RMS error of 0.236.
:::
:::

::: example
## A single weight through the origin has Σx² = 10 and Σxt = 8. How does regularization change it?

::: solution
1. **Without regularization.**
   $$\begin{aligned} w &= 8/10 \\ &= 0.8 \end{aligned}$$
2. **With λ = 1.**
   $$\begin{aligned} w &= 8/11 \\ &= 0.727 \end{aligned}$$
3. **With λ = 10.**
   $$\begin{aligned} w &= 8/20 \\ &= 0.4 \end{aligned}$$
4. **What it trades.** Larger λ trades a worse fit to the training data for a smaller, steadier weight, which lesson 9 shows can lower the test error.

**Answer:** regularization shrinks the weight from 0.8 to 0.727 at λ = 1 and to 0.4 at λ = 10.
:::
:::

::: example
## A thermistor's calibration fits 8 points whose noise has σ = 0.02 (in the log of resistance). A correct model has 3 parameters. What training and test RMS errors should you expect with it, and with a 7-parameter polynomial that also contains the right model?

::: solution
1. **Expected errors.** For a correct linear model the expected training and test errors are
   $$\sigma^2(1 - p/N) \quad\text{and}\quad \sigma^2(1 + p/N)$$
2. **Three parameters.**
   $$\begin{aligned} \text{train RMS} &= 0.02\sqrt{5/8} \\ &= 0.0158 \\ \text{test RMS} &= 0.02\sqrt{11/8} \\ &= 0.0235 \end{aligned}$$
3. **Seven parameters.** The training error is less than half the smaller model's, but the test error is larger:
   $$\begin{aligned} \text{train RMS} &= 0.02\sqrt{1/8} \\ &= 0.0071 \\ \text{test RMS} &= 0.02\sqrt{15/8} \\ &= 0.0274 \end{aligned}$$
4. **Compare.** The bigger model looks better on the data it was fitted to and is worse on new data: training error alone can't choose a model.

**Answer:** with 3 parameters, expect a training RMS error of 0.0158 and a test RMS error of 0.0235; with 7, a training error of 0.0071 and a test error of 0.0274.
:::
:::

::: check
given: a = 1 | 2 | 5; b = 2 | 4; c = 3 | 7 | 10
answer: (3*(0*a + 1*b + 3*c) - 4*(a + b + c))/(3*10 - 16)
tolerance: 0.5%

## Fit a least-squares line to the points (0, {a}), (1, {b}), (3, {c}). What's its slope?

::: right
Right. $w_1$ = {answer}.
:::

::: misses
- [any] Not yet. $w_1 = (N\sum xt - \sum x\sum t)/(N\sum x^2 - (\sum x)^2)$.
:::
:::

::: check
given: E = 0.5 | 2 | 12; N = 10 | 50 | 200
answer: sqrt(2*E/N)
tolerance: 0.5%

## A fit has sum-of-squares error E = {E} over {N} examples. What's its RMS error?

::: right
Right. $\sqrt{2E/N}$ = {answer}.
:::

::: misses
- [sqrt(E/N)] E carries a factor of 1/2.
- [any] Not yet. $E_\text{RMS} = \sqrt{2E/N}$.
:::
:::

::: check
given: s = 0.01 | 0.04 | 0.25; p = 3 | 10; N = 20 | 100
answer: s*(1 + p/N)
tolerance: 0.5%

## A correct linear model with {p} parameters is fitted to {N} examples with noise variance {s}. What test mean squared error should you expect?

::: right
Right. $\sigma^2(1 + p/N)$ = {answer}.
:::

::: misses
- [s*(1 - p/N)] That's the training error.
- [s] The fitted noise raises the test error above σ².
- [any] Not yet. $\sigma^2(1 + p/N)$.
:::
:::

::: check
given: s = 0.01 | 0.04 | 0.25; p = 3 | 10; N = 20 | 100
answer: 2*s*p/N
tolerance: 0.5%

## For the same fit ({p} parameters, {N} examples, noise variance {s}), what's the expected gap between test and training error?

::: right
Right. $2\sigma^2p/N$ = {answer}.
:::

::: misses
- [s*p/N] Training error is that much below σ² and test error that much above.
- [any] Not yet. The difference of $\sigma^2(1 \pm p/N)$.
:::
:::

::: check
given: S = 5 | 10 | 40; T = 3 | 8 | 30; l = 0.5 | 5 | 20
answer: T/(S + l)
tolerance: 0.5%

## A single weight through the origin has Σx² = {S} and Σxt = {T}. What weight does regularization with λ = {l} give?

::: right
Right. $\sum xt/(\sum x^2 + \lambda)$ = {answer}.
:::

::: misses
- [T/S] That's the unregularized weight.
- [any] Not yet. Minimize $E + \frac{\lambda}{2}w^2$.
:::
:::

::: check
given: s = 0.02 | 0.1; p = 5 | 20; g = 0.005 | 0.01
answer: 2*s*p/g
tolerance: 0.5%

## A correct model with {p} parameters on data with noise variance {s}: how many examples keep the expected test–training gap down to {g}?

::: right
Right. $N = 2\sigma^2p/\text{gap}$ = {answer}.
:::

::: misses
- [any] Not yet. Solve $2\sigma^2p/N$ = gap for N.
:::
:::

::: check
kind: parts
given: N = 10 | 20; X = 20 | 50; T = 30 | 80; v = 1 | 4; c = -2 | 1 | 3; let Y = X^2/N + N*v; let Z = X*T/N + N*c

## A least-squares line $y = w_0 + w_1x$ is fitted to {N} points with Σx = {X}, Σx² = {Y}, Σt = {T} and Σxt = {Z}.

::: part
answer: (N*Z - X*T)/(N*Y - X^2)
tolerance: 0.5%

### What is the slope $w_1$?

::: misses
- [Z/Y] That's the fit through the origin; the intercept is free here.
- [any] $w_1 = (N\Sigma xt - \Sigma x\Sigma t)/(N\Sigma x^2 - (\Sigma x)^2)$.
:::
:::

::: part
answer: (T - (N*Z - X*T)/(N*Y - X^2)*X)/N
tolerance: 0.5%

### What is the intercept $w_0$?

::: misses
- [any] The line passes through the means: $w_0 = \bar t - w_1\bar x$.
:::
:::
:::

::: check
kind: derivation
vars: s = 0.01..10, p = 1..50, N = 60..1000

## A correct linear model with p parameters is fitted by least squares to N examples whose noise has variance s.

::: step
answer: s*(1 - p/N)

### What mean squared training error do you expect?
:::

::: step
answer: s*(1 + p/N)

### What mean squared test error do you expect?
:::

::: step
answer: 2*s*p/N

### How large is the gap between them?
:::

::: right
Right. The gap shrinks as 1/N, which is why more data, not a smaller training error, is the cure for overfitting.
:::
:::

::: explain
## Why does a model's training error understate how well it will do on new data?

::: model
Least squares picks the parameters that make the model fit the training examples as closely as possible, and the examples contain noise as well as the underlying relationship. With p adjustable parameters, the fit can bend to follow part of that particular noise, roughly a fraction p/N of it, so the training residuals come out smaller than the true noise. New examples carry fresh, independent noise that the fitted bends don't match, and the parameters' own estimation error now adds to the noise. So test error sits above the noise level by about as much as training error sits below it, and the more parameters per example, the more misleading the training error becomes.
:::
:::

::: practice

::: check
given: E = 0.1 | 50; N = 5 | 1000
answer: sqrt(2*E/N)
tolerance: 0.5%

## RMS error: E = {E}, N = {N}?

::: misses
- [any] $\sqrt{2E/N}$.
:::
:::

::: check
given: s = 0.005 | 1; p = 2 | 30; N = 40 | 500
answer: s*(1 + p/N)
tolerance: 0.5%

## Expected test error: σ² = {s}, p = {p}, N = {N}?

::: misses
- [any] $\sigma^2(1 + p/N)$.
:::
:::

::: check
given: S = 1 | 100; T = 0.5 | 80; l = 0.1 | 50
answer: T/(S + l)
tolerance: 0.5%

## Ridge weight: Σx² = {S}, Σxt = {T}, λ = {l}?

::: misses
- [any] $\sum xt/(\sum x^2 + \lambda)$.
:::
:::
:::
