S1410 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 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/β = 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?

Name it

Prior and posterior

With prior 𝐰∼𝒩(0,α−1I) and Gaussian noise of precision β (variance 1/β), the posterior is Gaussian with

SN−1=αI+βΦTΦ,𝐦N=βSNΦT𝐭

For one weight through the origin: precision α+β∑x2, mean β∑xt/(α+β∑x2). The posterior mean is exactly ridge regression with λ=α/β.

The predictive distribution

A new input x gives a Gaussian prediction with mean 𝐦NT𝝓(x) and variance

σN2(x)=1β+𝝓(x)TSN𝝓(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/α+β∑x2, 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
  1. Posterior mean. m=4×6/21=1.143
  2. Ridge equivalent. Ridge with λ = α/β = 0.25 gives the same: w=6/(5+0.25)=1.143
  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±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.

Worked example

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

Show the solution
  1. Set the standard deviation. 11+4∑x2=0.1
  2. Solve for the data. 1+4∑x2=100∑x2=24.75
  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.

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
  1. Both batches at once. precision=1+4×5=21mean=4×8/21=1.524
  2. First batch alone. precision=1+8=9mean=4×3/9=1.333
  3. Second batch. That posterior becomes the prior for the second: precision=9+12=21mean=9×1.333+4×521=1.524
  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

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 t=wx+ϵ 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°.

Explain why

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

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.