---
title: Bayesian linear regression
heading: A Gaussian prior on the weights and a Gaussian posterior after the data, the posterior mean as ridge regression, the predictive distribution and its error bars that widen away from the data, and how much data a desired precision needs
eyebrow: S14 · Lesson 10 · Bayesian linear regression
xp: 50
---

::: hook
A regression line by itself doesn't say how far to trust a prediction. Treat the weights as uncertain, with lesson 8's Gaussian rules, and the fit comes with error bars: narrow where the training data were dense, widening as the input moves away from them. That's the difference between a model that says "the temperature will be 41 °C" and one that says "41 ± 6 °C, and I've never seen inputs like this before." The posterior's mean turns out to be ridge regression, so lesson 9's regularization was Bayesian all along.
:::

::: play
- A one-weight model $y = wx$ with prior precision α on w and noise precision β = 4 (noise variance 0.25), after data with Σx² = S and Σxt = 1.2S (so the least-squares slope is 1.2). The predictive mean and the ±2σ band.
- The band never shrinks below the noise, $1/\sqrt{\beta}$ = 0.5, and widens away from x = 0 as the slope's uncertainty grows with |x|.
- More data (larger S) narrows the slope's uncertainty and pulls the mean toward 1.2.
:::

```sim plot
view: graph
x: x = -4..4
y: y = -8..8
slider: S = 0.5..50 step 0.5 = 5
slider: a = 0.1..10 step 0.1 = 1
plot: 4*1.2*S/(a + 4*S)*x | samples: 200 | label: predictive mean
plot: 4*1.2*S/(a + 4*S)*x + 2*sqrt(0.25 + x^2/(a + 4*S)) | samples: 200 | dashed | color: ch3 | label: +2σ
plot: 4*1.2*S/(a + 4*S)*x - 2*sqrt(0.25 + x^2/(a + 4*S)) | samples: 200 | dashed | color: ch3 | label: −2σ
readout: posterior mean slope = 4*1.2*S/(a + 4*S) | digits: 4
readout: slope standard deviation = 1/sqrt(a + 4*S) | digits: 4
```

::: predict
## With prior precision α = 1, noise precision β = 4, Σx² = 5 and Σxt = 6, what's the predictive standard deviation at x = 2?

- [ ] 0.5, the noise level
- [x] About 0.66
- [ ] 0.22
- [ ] 2.0

::: reveal
The posterior precision of the slope is α + βΣx² = 1 + 20 = 21, so its variance is 1/21. At x = 2 the prediction's variance is the noise, 1/β = 0.25, plus x² times the slope's variance, 4/21 = 0.190: a total of 0.440 and a standard deviation of 0.664. At x = 0 only the noise is left.
:::
:::

::: nameit
## Prior and posterior

With prior $\mathbf{w} \sim \mathcal{N}(0, \alpha^{-1}I)$ and Gaussian noise of precision β (variance 1/β), the posterior is Gaussian with

$$S_N^{-1} = \alpha I + \beta\Phi^T\Phi, \qquad \mathbf{m}_N = \beta S_N\Phi^T\mathbf{t}$$

For one weight through the origin: precision $\alpha + \beta\sum x^2$, mean $\beta\sum xt/(\alpha + \beta\sum x^2)$. The posterior mean is exactly ridge regression with $\lambda = \alpha/\beta$.

## The predictive distribution

A new input x gives a Gaussian prediction with mean $\mathbf{m}_N^T\boldsymbol{\phi}(x)$ and variance

$$\sigma_N^2(x) = \frac{1}{\beta} + \boldsymbol{\phi}(x)^TS_N\boldsymbol{\phi}(x)$$

the noise plus the uncertainty in the weights, which grows where the basis functions take values the data didn't pin down.

## How much data

For one weight, the posterior standard deviation is $1/\sqrt{\alpha + \beta\sum x^2}$, so halving it takes four times the data (in Σx²), and inputs far from zero are the most informative about a slope.
:::

::: example
## Complete the example: posterior mean and its ridge equivalent, and the band at x = 0 and x = 2.

::: solution
1. **Posterior mean.**
   $$\begin{aligned} m &= 4 \times 6/21 \\ &= 1.143 \end{aligned}$$
2. **Ridge equivalent.** Ridge with λ = α/β = 0.25 gives the same:
   $$\begin{aligned} w &= 6/(5 + 0.25) \\ &= 1.143 \end{aligned}$$
3. **Predictive band.** The predictive standard deviation is 0.5 at x = 0 and 0.664 at x = 2, so the prediction at x = 2, with two standard deviations, is
   $$2.29 \pm 1.33$$

**Answer:** the posterior mean is 1.143, the same as ridge with λ = 0.25; the predictive standard deviation is 0.5 at x = 0 and 0.664 at x = 2, where the prediction is 2.29 ± 1.33.
:::
:::

::: example
## How much data (Σx²) brings the slope's standard deviation down to 0.1 with α = 1 and β = 4?

::: solution
1. **Set the standard deviation.**
   $$\frac{1}{\sqrt{1 + 4\sum x^2}} = 0.1$$
2. **Solve for the data.**
   $$\begin{aligned} 1 + 4\sum x^2 &= 100 \\ \sum x^2 &= 24.75 \end{aligned}$$
3. **In points.** That's about five times the example's data. With measurements at x = ±2, it's about six points.

**Answer:** Σx² = 24.75, about six points at x = ±2.
:::
:::

::: example
## A line through the origin has α = 1 and β = 4. One batch of data has Σx² = 2 and Σxt = 3, a second Σx² = 3 and Σxt = 5. Show that updating on the batches one after the other gives the same posterior as using both at once.

::: solution
1. **Both batches at once.**
   $$\begin{aligned} \text{precision} &= 1 + 4 \times 5 \\ &= 21 \\ \text{mean} &= 4 \times 8/21 \\ &= 1.524 \end{aligned}$$
2. **First batch alone.**
   $$\begin{aligned} \text{precision} &= 1 + 8 \\ &= 9 \\ \text{mean} &= 4 \times 3/9 \\ &= 1.333 \end{aligned}$$
3. **Second batch.** That posterior becomes the prior for the second:
   $$\begin{aligned} \text{precision} &= 9 + 12 \\ &= 21 \\ \text{mean} &= \frac{9 \times 1.333 + 4 \times 5}{21} \\ &= 1.524 \end{aligned}$$
4. **Learning online.** The same answer either way, so a Bayesian model can learn online, keeping only its current mean and precision rather than all the data.

**Answer:** both ways give precision 21 and mean 1.524.
:::
:::

::: check
given: a = 0.1 | 1 | 10; b = 1 | 4 | 25; S = 2 | 10 | 50
answer: a + b*S
tolerance: 0.5%

## A one-weight model has prior precision {a}, noise precision {b} and data with Σx² = {S}. What's the posterior precision of the weight?

::: right
Right. $\alpha + \beta\sum x^2$ = {answer}.
:::

::: misses
- [any] Not yet. Precisions add.
:::
:::

::: check
given: a = 0.1 | 1 | 10; b = 1 | 4 | 25; S = 2 | 10 | 50; T = 1 | 8
answer: b*T/(a + b*S)
tolerance: 0.5%

## With prior precision {a}, noise precision {b}, Σx² = {S} and Σxt = {T}, what's the posterior mean of the weight?

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

::: misses
- [any] Not yet. $\beta S_N\sum xt$.
:::
:::

::: check
given: a = 0.5 | 2; b = 1 | 4 | 25; S = 2 | 10; x = 0.5 | 3 | 10
answer: sqrt(1/b + x^2/(a + b*S))
tolerance: 0.5%

## For that model (α = {a}, β = {b}, Σx² = {S}), what's the predictive standard deviation at x = {x}?

::: right
Right. $\sqrt{1/\beta + x^2/(\alpha + \beta\sum x^2)}$ = {answer}.
:::

::: misses
- [sqrt(1/b)] Add the weight's uncertainty, scaled by x².
- [any] Not yet. Noise plus weight uncertainty.
:::
:::

::: check
given: a = 0.1 | 2 | 10; b = 1 | 4 | 25
answer: a/b
tolerance: 0.5%

## A Bayesian regression has prior precision {a} and noise precision {b}. What ridge λ gives the same weights?

::: right
Right. $\alpha/\beta$ = {answer}.
:::

::: misses
- [b/a] It's the prior's precision over the noise's.
- [any] Not yet. Compare the log-posterior with the ridge error.
:::
:::

::: check
given: a = 0.5 | 2; b = 1 | 4; s = 0.05 | 0.1 | 0.2
answer: (1/s^2 - a)/b
tolerance: 0.5%

## With α = {a} and β = {b}, how large must Σx² be for the weight's posterior standard deviation to reach {s}?

::: right
Right. $(1/s^2 - \alpha)/\beta$ = {answer}.
:::

::: misses
- [any] Not yet. Solve $1/\sqrt{\alpha + \beta\sum x^2} = s$.
:::
:::

::: check
given: a = 0.5 | 2; b = 1 | 4; S = 2 | 10; x = 2 | 6
answer: abs(x)/sqrt(a + b*S)
tolerance: 0.5%

## For the same kind of model (α = {a}, β = {b}, Σx² = {S}), how large is the part of the predictive standard deviation that comes from weight uncertainty alone at x = {x}?

::: right
Right. $|x|/\sqrt{\alpha + \beta\sum x^2}$ = {answer}, growing linearly with distance.
:::

::: misses
- [any] Not yet. x times the weight's standard deviation.
:::
:::

::: check
kind: parts
given: a = 2 | 10; m = 0.5 | 1.5; b = 1 | 4; S = 3 | 20; T = 2 | 10

## A one-weight model's current posterior has precision {a} and mean {m}. A new batch arrives with Σx² = {S} and Σxt = {T}, noise precision {b}.

::: part
answer: a + b*S
tolerance: 0.5%

### What is the new precision?

::: misses
- [any] Add $\beta\Sigma x^2$.
:::
:::

::: part
answer: (a*m + b*T)/(a + b*S)
tolerance: 0.5%

### What is the new mean?

::: misses
- [b*T/(a + b*S)] The old mean enters too, weighted by the old precision.
- [any] $(\alpha m + \beta\Sigma xt)/(\alpha + \beta\Sigma x^2)$.
:::
:::
:::

::: check
kind: derivation
vars: a = 0.1..10, b = 0.1..100, x = -3..3, t = -3..3

## A one-weight model $t = wx + \epsilon$ has prior precision α on w and noise precision β; it sees one observation t at x.

::: step
answer: 1/b + x^2/a

### What is the variance of t under the model, before seeing it?
:::

::: step
answer: -0.5*ln(2*pi*(1/b + x^2/a)) - t^2/(2*(1/b + x^2/a))

### What is the log of the evidence (the marginal likelihood of t)?
:::

::: right
Right. The evidence rewards a model that predicted t without spreading its probability too thin, the basis of comparing models by their evidence.
:::
:::

::: explain
## Why do a Bayesian model's error bars widen away from the training data?

::: model
The model's uncertainty is in its weights, and the data constrain the weights only in the directions the training inputs explored. Near the training inputs, the predictions from all plausible weight settings agree closely, because each was required to fit the data there. Far from them, the same small differences between plausible weights are multiplied by large basis-function values, so the plausible predictions fan out. The predictive variance adds this weight uncertainty, $\boldsymbol{\phi}(x)^TS_N\boldsymbol{\phi}(x)$, to the fixed noise, so it's smallest where data were dense and grows wherever the model would be extrapolating.
:::
:::

::: practice

::: check
given: a = 0.01 | 20; b = 0.5 | 100; S = 1 | 200; T = 0.5 | 100
answer: b*T/(a + b*S)
tolerance: 0.5%

## Posterior mean: α = {a}, β = {b}, Σx² = {S}, Σxt = {T}?

::: misses
- [any] $\beta T/(\alpha + \beta S)$.
:::
:::

::: check
given: a = 0.1 | 5; b = 0.5 | 50; S = 1 | 40; x = 0.1 | 20
answer: sqrt(1/b + x^2/(a + b*S))
tolerance: 0.5%

## Predictive sd: α = {a}, β = {b}, Σx² = {S}, x = {x}?

::: misses
- [any] $\sqrt{1/\beta + x^2/(\alpha + \beta S)}$.
:::
:::

::: check
given: a = 0.1 | 3; b = 0.5 | 10; s = 0.02 | 0.3
answer: (1/s^2 - a)/b
tolerance: 0.5%

## Σx² needed for sd {s}: α = {a}, β = {b}?

::: misses
- [any] $(1/s^2 - \alpha)/\beta$.
:::
:::
:::
