Bayesian Linear Regression
Posterior predictive variance and the evidence.
Ridge regression gives you one line. Bayesian linear regression gives you a distribution over lines, and with it three things ridge cannot: error bars that widen where data are scarce, a way to choose the regularization strength from the training data alone, and models that switch off their own useless features.
What you’ll learn
- Derive the Gaussian posterior over the weights, and identify its mean with ridge regression at .
- Derive the predictive variance , separate its two sources, and describe the equivalent kernel and the function-space view.
- Derive the log evidence and MacKay's fixed-point updates for and , and interpret as an effective number of parameters.
- Explain automatic relevance determination and the relevance vector machine, including how they fail.
The posterior over weights
Model the targets as with fixed basis functions and Gaussian noise ; is the noise precision, the inverse variance. Stack the features into the matrix with rows , and put an isotropic Gaussian prior on the weights, . This is the notation of Bishop (2006, chapter 3).
The log-posterior is the log-likelihood plus the log-prior, a quadratic in :
A quadratic log-density is a Gaussian: read off the precision matrix from the quadratic term and the mean from the linear term.
The mean is also the mode, and dividing through by shows it is ridge regression with (Ridge Regression in Depth proves the posterior-mean property). As it tends to least squares.
With a general prior the same algebra gives the update below. Applied batch after batch, the posterior from one batch is the prior for the next: Bayesian linear regression learns online, exactly.
Predictions with honest error bars
A new target is with , where and the noise is independent. A linear function of Gaussians is Gaussian:
The variance has two parts: noise, which no amount of data removes, and uncertainty about the weights, which shrinks as data arrive (adding a data point never increases it).
The predictive mean is a weighted sum of the training targets, with the equivalent kernel as weights: the prediction is linear in the targets, as for least squares and ridge, and nearby training points get the largest weights.
One feature, no intercept: with prior precision and noise precision . You observe targets at and . What is the predictive variance at ?
Show the answer
0.68
, so and . Then . At , beyond the data, parameter uncertainty already exceeds the noise: the uncertainty about the slope is multiplied by .
The function-space view removes the weights altogether. If , then is a Gaussian process with covariance , and the predictive equations above are exactly the Gaussian process regression equations for that kernel (Rasmussen & Williams, 2006, chapter 2). The widget below is a GP with the linear kernel : Bayesian linear regression on the features , with intercept prior variance and slope prior variance .
Choosing α and β by the evidence
Cross-validation can choose , but the Bayesian model offers a criterion computed from the training data alone. Integrating out the weights, is Gaussian with covariance , and its density at the observed is the evidence, or marginal likelihood, . Maximizing it over the hyperparameters is empirical Bayes, also called type-II maximum likelihood (MacKay, 1992).
Write , and integrate the Gaussian in :
Let be the eigenvalues of , so . Differentiate in ; since minimizes , its own dependence on drops out (an envelope argument):
Multiply by and rearrange. The same steps for (using ) give the noise update:
Each eigendirection contributes nearly 1 to if the data determine it () and nearly 0 if the prior does. So is the number of well-determined parameters, identical to ridge's effective degrees of freedom at , and dividing the residual sum of squares by mirrors dividing by in classical regression.
import numpy as np
def evidence_fit(Phi, y, alpha=1.0, beta=1.0, iters=100):
"""MacKay's fixed-point updates for the prior precision alpha and the noise precision beta."""
N, M = Phi.shape
eig = np.linalg.eigvalsh(Phi.T @ Phi) # eigenvalues of Phi^T Phi
for _ in range(iters):
A = alpha * np.eye(M) + beta * Phi.T @ Phi # posterior precision S_N^{-1}
m = beta * np.linalg.solve(A, Phi.T @ y) # posterior mean m_N
lam = beta * eig # eigenvalues of beta Phi^T Phi
gamma = np.sum(lam / (alpha + lam)) # number of well-determined parameters
alpha = gamma / (m @ m)
beta = (N - gamma) / np.sum((y - Phi @ m) ** 2)
return m, alpha, beta, gammaEach iteration costs one solve. scikit-learn's BayesianRidge runs updates of this kind (with optional Gamma hyperpriors), but mind its naming: its alpha_ is the noise precision and lambda_ the weight precision, the reverse of the notation here.
Relevance determination and the RVM
Give every weight its own prior precision, , and maximize the evidence over all . For features the data do not support, the optimum sends , which pins to zero and removes the feature: automatic relevance determination (ARD), developed by MacKay and Neal for neural networks. For one with the others fixed the maximization is explicit (Faul & Tipping, 2002): with a "sparsity" and a "quality" computed from the other features,
so a feature survives only if its alignment with the unexplained part of beats its overlap with the features already in the model.
The relevance vector machine (Tipping, 2001) applies ARD to a kernel expansion with one basis function per training point. Most weights are pruned, leaving a sparse set of "relevance vectors", typically far fewer than an SVM's support vectors, together with predictive variances. Jointly in all the the evidence is not concave, so ARD and the RVM can stop at local optima, and the features kept can depend on the starting point.
An RVM with Gaussian basis functions centered on a few relevance vectors is asked to predict far from all of them. What does its predictive variance do there?
- It grows without bound, signaling extrapolation
- It falls to roughly the noise variance, so the model looks confident exactly where it has no data
- It equals the prior variance of the weights
- It is undefined, because no basis function is active
Show the answer
B. It falls to roughly the noise variance, so the model looks confident exactly where it has no data
Far from every center all basis functions are near zero, so and only remains. Rasmussen & Quiñonero-Candela (2005) traced this to the degenerate, finite-dimensional covariance the RVM implies, and repaired it by adding a basis function centered at the test point. A GP with a stationary kernel instead returns to its prior variance far from data.
Key takeaways
- With prior and noise precision , the posterior is with and ; its mean and mode are ridge with .
- The predictive variance is : noise plus parameter uncertainty. The second part shrinks with data and grows away from it; in function space the model is a Gaussian process with kernel .
- Maximizing the evidence gives the fixed point , , where counts well-determined parameters and equals ridge's effective degrees of freedom.
- ARD gives each weight its own precision and prunes features the evidence does not support; the RVM applies it to kernel expansions. Both are non-convex, and localized basis functions make the RVM overconfident far from its relevance vectors.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
In Bayesian linear regression with prior precision and noise precision , the posterior mean equals ridge regression with which (in )?
Show the answer
D.
, so . In variances, with noise variance and prior variance .
With fixed features and more and more training data spread over the input range, what does the predictive variance at a typical input approach?
- The noise variance
- Zero
- The prior variance of the function
- It keeps growing with
Show the answer
A. The noise variance
as the data pin down every direction the features use, but the noise term is a property of the world and stays. Even a perfectly known line cannot predict a noisy target exactly.
In MacKay's evidence framework, with the eigenvalues of . What happens to when the prior precision is much larger than every ?
- : every parameter is well determined
- : the prior dominates every direction, and the data determine essentially nothing
- does not depend on
Show the answer
C. : the prior dominates every direction, and the data determine essentially nothing
Each term is near 0 when . A very strong prior pins the weights near zero, the model has almost no effective parameters, and the noise estimate divides the residuals by nearly .
Further reading
- C. M. Bishop, Pattern Recognition and Machine Learning (Springer, 2006), sections 3.3–3.5.
- D. J. C. MacKay, "Bayesian interpolation," Neural Computation 4(3), 415–447 (1992).
- M. E. Tipping, "Sparse Bayesian learning and the relevance vector machine," Journal of Machine Learning Research 1, 211–244 (2001).
- C. E. Rasmussen & J. Quiñonero-Candela, "Healing the relevance vector machine through augmentation," Proceedings of the 22nd International Conference on Machine Learning, 689–696 (2005).
- C. E. Rasmussen & C. K. I. Williams, Gaussian Processes for Machine Learning (MIT Press, 2006), chapter 2.