Bayesian Linear Regression
A point estimate says "the slope is 0.7". A Bayesian model says "the slope is probably between 0.6 and 0.8, and here is how sure I am at every input". Bayesian linear regression keeps the full posterior over parameters, which is Gaussian in closed form, and turns it into predictions with error bars that grow where data is scarce.
Prior, likelihood, posterior
Prior , likelihood . Both are Gaussian in , so the posterior is Gaussian (conjugacy):
Precisions (inverse covariances) add: prior precision plus data precision. With and , the posterior mean is exactly the ridge / MAP estimate with . The new information is .
The predictive distribution
For a new input with features , average the prediction over the posterior:
The variance has two parts: parameter uncertainty , which shrinks with more data, and noise , which never does.
import numpy as np
rng = np.random.default_rng(0)
x = np.concatenate([rng.uniform(-3, -1, 8), rng.uniform(1, 2, 8)]) # a gap in (-1, 1), nothing beyond 2
y = 0.5 * x - 0.3 * x**2 + 0.3 * rng.normal(size=16)
Phi = lambda x: np.column_stack([np.ones_like(x), x, x**2])
sigma2, b2 = 0.3**2, 4.0
S0_inv = np.eye(3) / b2
SN = np.linalg.inv(S0_inv + Phi(x).T @ Phi(x) / sigma2)
mN = SN @ (Phi(x).T @ y / sigma2)
print("posterior mean:", np.round(mN, 3))
print("posterior std: ", np.round(np.sqrt(np.diag(SN)), 3))
for xs in (-2.0, 0.0, 1.5, 4.0):
p = Phi(np.array([xs]))[0]
print(f"x*={xs:4}: mean {p @ mN:7.3f} std {np.sqrt(p @ SN @ p + sigma2):.3f} (parameter part {np.sqrt(p @ SN @ p):.3f})")posterior mean: [-0.038 0.466 -0.295]
posterior std: [0.148 0.05 0.039]
x*=-2.0: mean -2.148 std 0.319 (parameter part 0.107)
x*= 0.0: mean -0.038 std 0.334 (parameter part 0.148)
x*= 1.5: mean -0.002 std 0.317 (parameter part 0.104)
x*= 4.0: mean -2.890 std 0.712 (parameter part 0.646)
Inside the data the predictive standard deviation is close to the noise level 0.3; in the gap it grows a little, and far outside the data (at ) the parameter uncertainty dominates. A plain least-squares fit would give no warning at all.
Sequential learning
Because the posterior is Gaussian, today's posterior is tomorrow's prior: feeding data in batches and updating each time gives exactly the same result as using all data at once.
import numpy as np
rng = np.random.default_rng(1)
x = rng.uniform(-1, 1, 40); y = 1.0 + 2.0 * x + 0.2 * rng.normal(size=40)
Phi = np.column_stack([np.ones_like(x), x]); s2 = 0.04
def update(m, S, P, t):
S_new = np.linalg.inv(np.linalg.inv(S) + P.T @ P / s2)
return S_new @ (np.linalg.inv(S) @ m + P.T @ t / s2), S_new
m, S = np.zeros(2), np.eye(2) * 10
for k in range(0, 40, 10):
m, S = update(m, S, Phi[k:k + 10], y[k:k + 10])
m_all, S_all = update(np.zeros(2), np.eye(2) * 10, Phi, y)
print("sequential:", np.round(m, 6), " all at once:", np.round(m_all, 6), " same:", np.allclose(m, m_all) and np.allclose(S, S_all))sequential: [0.97368 1.932839] all at once: [0.97368 1.932839] same: True
In software
sklearn.linear_model.BayesianRidgeandARDRegressionalso learn and the prior scale from the data (by maximizing the evidence).- Gaussian processes are Bayesian linear regression with infinitely many features.
- Bandits and active learning query where the predictive variance is large.
Exercises
Exercise 1: One parameter
Model , prior , , one data point . Find the posterior.
Answer
.
Solution
, . The ML estimate would be ; the prior pulls it towards 0.
Exercise 2: Never below the noise
Can the predictive variance ever be smaller than ?
Answer
No.
Solution
It is and is positive semidefinite, so the first term is . Infinite data can remove parameter uncertainty, not noise.
Exercise 3: Posterior shrinks with data (code)
For with prior and , print the posterior standard deviation of after points with .
Answer
: about , , .
Solution
because each point adds precision .
for N in (1, 10, 100):
print(N, round((1 / (1 + N)) ** 0.5, 4))1 0.7071
10 0.3015
100 0.0995
Test yourself
With prior , the posterior mean of Bayesian linear regression equals…
- the ML estimate
- the ridge (MAP) estimate with
- zero
- the data mean
The predictive variance is largest…
- where training data is dense
- far from the training data
- at the origin always
- it is constant
In the posterior covariance formula, the precision of the posterior is…
- the prior precision minus the data precision
- the prior precision plus the data precision
- always the identity
- the data precision only