Bayesian linear regression
A Gaussian prior on the weights and Gaussian noise make everything exact: the posterior $\mathcal N(m_N, S_N)$, the link to ridge regression, predictions with honest error bars, the evidence of the model and choosing $\alpha$ and $\beta$ from the data.
The model: noise, features and a prior
The topic Bayesian model selection left us with a tool, the evidence, and a promise to use it on the workhorse of machine learning. Linear regression is the right place: with Gaussian noise and a Gaussian prior every integral in it is taken in closed form. The posterior, the predictive distribution and the evidence all come out exactly, so nothing has to be approximated yet.
Each object is turned into a feature vector : the raw features, a constant and , powers of , any fixed functions. The rows form the design matrix ; for plain linear regression . The targets are
with independent noise terms, so the likelihood is . The model is linear in , not in : a cubic in is still linear regression.
The prior is the one from The Bayesian approach, where its MAP estimate turned out to be the L2 penalty:
Both and are precisions. The noise variance is , and the prior variance of each weight is . A large is a tight prior that holds the weights near zero; a large means we trust each target.
Running example. It stays with us for this topic and the next one. Three points and a line with an intercept:
Then
The columns of are orthogonal, so every matrix below stays diagonal and the arithmetic fits on a napkin. Least squares gives .
The posterior over the weights
The posterior is proportional to the likelihood times the prior. Take the logarithm and drop everything that does not depend on :
Each term carries its own precision. Some lecture notes pull a common factor out of the whole bracket; that is a typo, because the prior knows nothing about the noise.
The bracket is quadratic in , so the posterior is Gaussian. Expand it:
The exponent of is . Matching the quadratic and the linear terms gives
The first formula is the familiar "precisions add" from the Normal mean in Conjugate priors and the exponential family: the prior precision plus the data precision . Each new point adds its own term and sharpens the posterior along the direction it measures.
Sequential updating works as before. With a prior the same algebra gives and , so today's posterior is tomorrow's prior.
In the running example:
Least squares said . The prior pulls both weights toward zero, and with three points and a fairly tight prior (standard deviation ) it pulls hard. At the end of the topic we will ask the data whether was a sensible choice.
The posterior mean is ridge regression
Factor out of :
This is ridge regression with . The Bayesian approach already showed that a Gaussian prior gives an L2 penalty; now the coefficient is explicit: the noise variance divided by the prior variance. A Gaussian is symmetric with a single peak, so its mode and its mean coincide: is the MAP estimate and the posterior mean at once.
What the formula says:
- Only the ratio enters . depends on both precisions separately.
- As (a flat prior), tends to least squares, provided is invertible. As grows, grows with it while stays fixed, so the prior washes out.
- Every weight is shrunk, the intercept included. In the running example the intercept's posterior mean is instead of . Shift all targets by a constant and the predictions move by only three fifths of it. In practice one centres the targets or gives the intercept its own, much weaker prior.
So far the Bayesian answer is ridge regression. The difference shows up in the next blocks. Ridge returns a point; the posterior also returns , and with it error bars for predictions and a way to choose from the training data alone.
Prediction: noise plus uncertainty about the weights
For a new input with features we want the target, not the weights:
The integral can be skipped. Write the posterior weights as with , and the new noise as , independent of :
A linear combination of independent Gaussians is Gaussian; means add and variances add:
There are two sources of uncertainty. The first term is the noise of the new observation; it would remain even if we knew exactly. The second term is our uncertainty about . For the noise-free value , the regression line itself, only the second term remains.
Plugging in and keeping loses the second term. That is the gap between MAP and the posterior predictive from The Bayesian approach, now with a formula attached.
In the running example and is diagonal, so :
- at the mean is and the variance is ;
- at the mean is and the variance is .
Why uncertainty grows away from the data
In the running example the weight term is a parabola: smallest at the centre of the data and growing quadratically outside it. The general picture comes from an eigendecomposition. Let have eigenvalues and orthonormal eigenvectors . Then has the same eigenvectors with eigenvalues , and
A direction the data measured well (large ) contributes little. A direction the data did not measure () keeps the prior variance . Far from the data, points largely along such poorly measured directions, and the band widens.
Three consequences.
- Noise is irreducible. As data from the same range keep arriving, every grows in proportion to , the weight term goes to zero and .
- Error bars ignore the targets. contains no : the width depends on where the inputs are and on , , not on how well the line fits. The fit enters only once itself is learned from the residuals, in the last block.
- Error bars are only as good as the features. Take localized features, Gaussian bumps with centres inside the data. Far from every centre , so the mean falls back to and the weight term vanishes. The model is most confident exactly where it knows nothing. Gaussian processes, later in the course, fix this by putting the prior on functions instead of on a fixed set of bumps.
The evidence of the linear model
In Bayesian model selection the evidence scored a model by how well it predicted the data before seeing them. For linear regression it is available in closed form. Under the model, with independent and . A linear map of Gaussians is Gaussian, so
is , one row per object; is , one row per weight. They are different matrices. The two terms pull in opposite directions: rewards a model that can explain , and penalizes a model that spreads its probability over too many possible datasets. This is the automatic Occam's razor from that topic.
When it is cheaper to stay in weight space. The same quantity equals
Here is the fit at the best weights, and is the log Occam factor, the log of the ratio of posterior volume to prior volume. It is never positive, because every eigenvalue of is at least . The constants and come from normalizing the prior and the likelihood. They are easy to lose, and without them the evidence depends on and in the wrong way.
In the running example
and the weight-space formula gives the same number.
Choosing $\alpha$ and $\beta$ by the evidence
Now treat the evidence as a function of and and maximize it. This is empirical Bayes from Bayesian model selection: the weights are integrated out, the hyperparameters are point estimates.
Use the eigenvalues of from the previous block; then . Differentiate the weight-space form with respect to . The dependence through drops out, because minimizes :
The derivative with respect to is similar (each is proportional to ). Together they give the fixed-point updates
is the effective number of parameters. Each of its terms lies between and : close to for a direction the data determine well (), close to where the prior dominates. The update is the familiar unbiased noise-variance estimate with replaced by . Since and depend on and , we iterate: posterior, then , then new hyperparameters, then the posterior again.
In the running example at , : , so , and . The update says the prior was too tight. Run to convergence, the iteration reaches , and : both weights are now well determined. The log evidence rises from to .
Two caveats. With three points these estimates are rough, and in general the evidence need not be concave in the hyperparameters, so the iteration finds a local maximum. A fully Bayesian treatment would put priors on and as well.
One for all weights treats every feature alike: an irrelevant feature is shrunk exactly as much as a useful one. Give each weight its own precision and maximize the evidence again. The precisions of useless weights run off to infinity, their posteriors collapse onto zero, and those features switch off. That is the topic The relevance vector machine.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.