S14 · Lesson 10 · Bayesian linear regression
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
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.
- A one-weight model 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, = 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.
Predict
With prior precision α = 1, noise precision β = 4, Σx² = 5 and Σxt = 6, what's the predictive standard deviation at x = 2?
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.
Name it
Prior and posterior
With prior and Gaussian noise of precision β (variance 1/β), the posterior is Gaussian with
For one weight through the origin: precision , mean . The posterior mean is exactly ridge regression with .
The predictive distribution
A new input x gives a Gaussian prediction with mean and variance
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 , so halving it takes four times the data (in Σx²), and inputs far from zero are the most informative about a slope.
Worked example
Complete the example: posterior mean and its ridge equivalent, and the band at x = 0 and x = 2.
Show the solution
- Posterior mean.
- Ridge equivalent. Ridge with λ = α/β = 0.25 gives the same:
- 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
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.
Worked example
How much data (Σx²) brings the slope's standard deviation down to 0.1 with α = 1 and β = 4?
Show the solution
- Set the standard deviation.
- Solve for the data.
- 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.
Worked 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.
Show the solution
- Both batches at once.
- First batch alone.
- Second batch. That posterior becomes the prior for the second:
- 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
A one-weight model has prior precision 1, noise precision 1 and data with Σx² = 50. What's the posterior precision of the weight?
Check
With prior precision 1, noise precision 1, Σx² = 50 and Σxt = 8, what's the posterior mean of the weight?
Check
For that model (α = 0.5, β = 1, Σx² = 10), what's the predictive standard deviation at x = 10?
Check
A Bayesian regression has prior precision 2 and noise precision 1. What ridge λ gives the same weights?
Check
With α = 0.5 and β = 1, how large must Σx² be for the weight's posterior standard deviation to reach 0.2?
Check
For the same kind of model (α = 0.5, β = 1, Σx² = 10), how large is the part of the predictive standard deviation that comes from weight uncertainty alone at x = 6?
Check
A one-weight model's current posterior has precision 2 and mean 0.5. A new batch arrives with Σx² = 20 and Σxt = 2, noise precision 4.
(a)
What is the new precision?
(b)
What is the new mean?
Check
A one-weight model has prior precision α on w and noise precision β; it sees one observation t at x.
Step 1
What is the variance of t under the model, before seeing it?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What is the log of the evidence (the marginal likelihood of t)?
Explain why
Why do a Bayesian model's error bars widen away from the training data?
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, , to the fixed noise, so it's smallest where data were dense and grows wherever the model would be extrapolating.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.