10 Bayesian Regression

10.1 Recap

10.1.1 Bayes’ Rule

Bayes’ Rule (assuming P(B)>0P(B)>0): P(A∣B)=P(B∣A)P(A)P(B)P(A \mid B) = \frac{P(B \mid A)\, P(A)}{P(B)}

The PDF version: f(x∣y)=f(y∣x)f(x)f(y)f(x \mid y) = \frac{f(y \mid x)\, f(x)}{f(y)} where f(y)=∫f(y|x)f(x)dxf(y) = \int f(y | x)f(x) \mathrm{d}x is the normalising constant.
The point is that in Bayesian inference and regression, we will use the form posterior∝likelihood×prior\text{posterior} \propto \text{likelihood} \times \text{prior} ignoring the normalisation constant.

10.1.2 Unnormalised density functions

A PDF is defined as: f(x)=g(x)Z,Z=∫g(x)dxf(x) = \frac{g(x)}{Z}, \quad Z = \int g(x)\, dx ZZ is the normalising constant. If two functions g1g_1 and g2g_2 differ by only a constant, i.e. g1∝g2g_1 \propto g_2, then they define the exact same PDF.
ZZ is often intractable to compute, hence we can just write f∝gf \propto g and work with the unnormalised form gg.

10.1.2.1 Example

The full PDF of a 1D Gaussian 𝒩(μ,σ2)\mathcal{N}(\mu, \sigma^2) is f(x)=12πσ2exp⁡(−(x−μ)22σ2)f(x) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\!\left(-\frac{(x-\mu)^2}{2\sigma^2}\right) Expanding the square in the exponent: f(x)∝exp⁡(−12σ2x2+μσ2x)f(x) \propto \exp\!\left(-\frac{1}{2\sigma^2}x^2 + \frac{\mu}{\sigma^2}x\right) We dropped terms not involving xx.
Hence if we observe this quadratic form in the exponent, we know it’s Gaussian.

10.1.3 Frequentist vs Bayesian

Frequentist approach treats Parameters like μ\mu as fixed, unknown constants, wuth no distributions.
MLE is frequentist approach. It finds the parameter that makes the observed data most probable.

10.1.3.1 Example

A factory produces products labeled 100g. The actual weight XX follows a Gaussian distribution with known variance 1, but unknown mean μ\mu. Three products are sampled with weights 99g, 110g, 107g. What is the best estimate of μ\mu?
Given variance = 1 and the independent samples, the likelihood is p(xi∣μ)=12πexp⁡(−(xi−μ)22)p(x_i \mid \mu) = \frac{1}{\sqrt{2\pi}} \exp\left(-\frac{(x_i - \mu)^2}{2}\right) The joint likelihood of all observations is (by independence): ℒ(μ)=∏i=1np(xi∣μ)=∏i=1n12πexp⁡(−(xi−μ)22)\mathcal{L}(\mu) = \prod_{i=1}^n p(x_i \mid \mu) = \prod_{i=1}^n \frac{1}{\sqrt{2\pi}} \exp\left(-\frac{(x_i - \mu)^2}{2}\right) Taking the log-likelihood: log⁡ℒ(μ)=−n2log⁡(2π)−12∑i=1n(xi−μ)2\log \mathcal{L}(\mu) = -\frac{n}{2}\log(2\pi) - \frac{1}{2}\sum_{i=1}^n (x_i - \mu)^2 Maximising this is equivalent to minimising ∑i=1n(xi−μ)2\sum_{i=1}^n (x_i - \mu)^2: ddμ∑i=1n(xi−μ)2=−2∑i=1n(xi−μ)=0\frac{d}{d\mu} \sum_{i=1}^n (x_i - \mu)^2 = -2\sum_{i=1}^n (x_i - \mu) = 0 μ̂n=1n∑i=1nxi\boxed{\hat{\mu}_n = \frac{1}{n}\sum_{i=1}^n x_i} MLE says that the sample mean is the best estimate for μ\mu. However, this approach says nothing about our confidence in this estimate. Also, we cannot incorporate any prior knowledge we may have.

Bayesian approach treats parameters as random variables with its own distribution. We can encode our prior beliefs about μ\mu and then update those beliefs.
Treating each observation as Gaussian around μ\mu, the likelihood is: Xi∼N(μ,σ2),i=1,2,…,nX_i \sim N(\mu, \sigma^2), \quad i = 1, 2, \ldots, n Our prior Gaussian belief about μ\mu before seeing data: μ∼p(μ)=N(μ0,σ02)\mu \sim p(\mu) = N(\mu_0, \sigma_0^2) Here μ0\mu_0 is our prior best guess for μ\mu, and σ02\sigma_0^2 is our uncertainty about that guess.
Applying Bayes’ Rule, p(μ∣x1,…,xn)∝∏i=1np(xi∣μ)p(μ)p(\mu \mid x_1, \ldots, x_n) \propto \prod_{i=1}^n p(x_i \mid \mu)\, p(\mu) This is the posterior, i.e. the update belief about μ\mu after nn observations.
The posterior is also Gaussian. Its parameters are μ̂n=σ2σ02nσ02+σ2(∑i=1nxiσ2+μ0σ02)\boxed{\hat{\mu}_n = \frac{\sigma^2 \sigma_0^2}{n\sigma_0^2 + \sigma^2}\left(\frac{\sum_{i=1}^n x_i}{\sigma^2} + \frac{\mu_0}{\sigma_0^2}\right)} σ̂n2=σ2σ02nσ02+σ2\boxed{\hat{\sigma}_n^2 = \frac{\sigma^2 \sigma_0^2}{n\sigma_0^2 + \sigma^2}} The posterior mean can be rewritten as μ̂n=nσ02nσ02+σ2x‾+σ2nσ02+σ2μ0\hat{\mu}_n = \frac{n\sigma_0^2}{n\sigma_0^2 + \sigma^2}\bar{x} + \frac{\sigma^2}{n\sigma_0^2 + \sigma^2}\mu_0 where x‾=1nΣxi\bar{x} = \frac{1}{n}\Sigma x_i is the MLE estimate. Note that this is just the weighted average of the MLE estimate and the prior mean. The weights depend on the relative uncertainty.
If σ02\sigma_0^2 is large, there is more weight on x‾\bar{x}. If σ2\sigma^2 is large, more weight on μ0\mu_0. As n→∞n\to\infty, μ̂n→x‾\hat{\mu}_n \to \bar{x}.
The posterior variance σ̂n2→0\hat{\sigma}^2_n \to 0 as n→∞n \to \infty. Compared to MLE, where σ̂n2=0\hat{\sigma}^2_n = 0 always.

10.1.4 Regression

Recall that Least Squares Method (LSM) finds a linear function of xx that best predicts yy, given nn data points, each consisting of input xi∈ℝdx_i \in \mathbb{R}^d and output yi∈ℝy_i \in \mathbb{R}.
Organising the inputs and outputs: 𝒚=(y1,…,yn)⊤∈ℝn\mathbf{y} = (y_1, \ldots, y_n)^\top \in \mathbb{R}^n 𝑿⊤=(x1,…,xn)∈ℝd×n\mathbf{X}^\top = (x_1, \ldots, x_n) \in \mathbb{R}^{d \times n} We want a weight vector 𝒘∈ℝd\mathbf{w} \in \mathbb{R}^d such that 𝑿𝒘≈𝒚\mathbf{Xw} \approx \mathbf{y}.
LSM finds 𝒘\mathbf{w} by minimising the total squared error: ŵ=arg⁡minw∈ℝd∥𝒚−𝑿w∥2\hat{w} = \arg\min_{w \in \mathbb{R}^d} \|\mathbf{y} - \mathbf{X}w\|^2 By differentiating and setting to zero, we get ŵ=Σ̂−1𝑿⊤𝒚\boxed{\hat{w} = \hat{\Sigma}^{-1}\mathbf{X}^\top\mathbf{y}} where Σ̂=𝑿T𝑿\hat{\Sigma} = \mathbf{X}^T\mathbf{X} and assuming it is invertible. This is the normal equation, the closed-form solution to least squares.
If we use MLE by supposing the true relationship between xx and yy is linear and corrupted by Gaussian noise, y=w*⊤x+ε,ε∼N(0,σ2I)y = w^{*\top}x + \varepsilon, \quad \varepsilon \sim N(0, \sigma^2 I) where w*∈ℝdw^* \in \mathbb{R}^d is the true unknown weight vector.
The output yy is Gaussian: P(y∣w,x)=N(w⊤x,σ2I)(y)P(y \mid w, x) = N(w^\top x, \sigma^2 I)(y) By MLE we will also get ŵMLE=Σ̂−1𝑿⊤𝒚\hat{w}_{\text{MLE}} = \hat{\Sigma}^{-1}\mathbf{X}^\top\mathbf{y} The implication is that LSM has the same limitation as MLE. It just gives a single estimate without the uncertainty about the prediction. It also doesn’t incorporate prior beliefs. This leads into Bayesian Linear Regression which address these gaps.

10.2 Bayesian Linear Regression

10.2.1 1-D case

Applying Bayesian inference to the regression setting. Start with 1-D inputs, xi∈ℝx_i\in\mathbb{R} and w∈ℝw\in\mathbb{R}.
Likelihood: Each output is Gaussian around wxiwx_i: yi∼p(yi∣xi,w)=N(wxi,σ2),i=1,2,…,ny_i \sim p(y_i \mid x_i, w) = N(wx_i, \sigma^2), \quad i = 1, 2, \ldots, n Prior: Treat ww as a random variable with a Gaussian prior: w∼p(w)=N(0,σ02)w \sim p(w) = N(0, \sigma_0^2) The prior is centered at 0, encoding a belief that weights are small.
The posterior: By Bayes’ Rule, p(w∣𝒚)∝p(𝒚∣w,𝑿)p(w)=∏i=1np(yi∣xi,w)p(w)p(w \mid \mathbf{y}) \propto p(\mathbf{y} \mid w, \mathbf{X})\, p(w) = \prod_{i=1}^n p(y_i \mid x_i, w)\, p(w) The result is that the posterior is Gaussian with μ̂n=1σ2(1σ02+1σ2𝑿⊤𝑿)−1𝑿⊤𝒚=(σ2σ02I+𝑿⊤𝑿)−1𝑿⊤𝒚\begin{align*} \hat{\mu}_n &= \frac{1}{\sigma^2}\left(\frac{1}{\sigma_0^2} + \frac{1}{\sigma^2}\mathbf{X}^\top\mathbf{X}\right)^{-1}\mathbf{X}^\top\mathbf{y}\\ &= \left(\frac{\sigma^2}{\sigma_0^2}I + \mathbf{X}^\top\mathbf{X}\right)^{-1}\mathbf{X}^\top\mathbf{y} \end{align*} σ̂n2=(1σ02+1σ2𝑿⊤𝑿)−1\hat{\sigma}_n^2 = \left(\frac{1}{\sigma_0^2} + \frac{1}{\sigma^2}\mathbf{X}^\top\mathbf{X}\right)^{-1} Comparing with ridge regression which solves arg⁡min⁡w∥𝒚−𝑿w∥2+λ∥w∥2\arg \min_w \|\mathbf{y} - \mathbf{X}w\|^2 + \lambda \|w\|^2, giving: ŵridge=(λI+𝑿⊤𝑿)−1𝑿⊤𝒚\hat{w}_{\text{ridge}} = \left(\lambda I + \mathbf{X}^\top\mathbf{X}\right)^{-1}\mathbf{X}^\top\mathbf{y} Note that they are identical with λ=σ2σ02\lambda = \frac{\sigma^2}{\sigma^2_0}. So the Gaussion prior 𝒩(0,σ02)\mathcal{N}(0,\sigma_0^2) is mathematically equivalent to L2L_2 regularisation.

σ̂n2\hat{\sigma}^2_n represents the remaining uncertainty about ww after the observations. It shrinks as nn increases.
Also, as σ02→∞\sigma_0^2 \to \infty (no prior conviction), σ2σ02→0\frac{\sigma^2}{\sigma_0^2} \to 0 and μ̂n→ŵLSM\hat{\mu}_n \to \hat{w}_{\text{LSM}}. Hence LSM is just a special case of Bayesian regression with an infintely weak prior.

10.2.2 Derivation

p(w|𝒚)∝p(w)p(𝒚|𝑿,w)∝exp⁡(−12σ02w2−12σ2∑i=1n(yi−wxi)2)∝exp⁡(−12Snw2+1σ2∑i=1nxiyiw),Sn=1σ02+∑i=1nxi2σ2∝exp⁡(−12Sn(w−Sn−1σ2∑i=1nxiyi)2)(completing the square)∝N(μ̂n,σ̂n)\begin{align*} p(w|\mathbf{y}) &\propto p(w)p(\mathbf{y}|\mathbf{X}, w) \\ &\propto \exp\left(-\frac{1}{2\sigma_0^2}w^2 - \frac{1}{2\sigma^2}\sum_{i=1}^n(y_i - wx_i)^2\right) \\ &\propto \exp\left(-\frac{1}{2}S_n w^2 + \frac{1}{\sigma^2}\sum_{i=1}^n x_i y_i w\right), \qquad S_n = \frac{1}{\sigma_0^2} + \frac{\sum_{i=1}^n x_i^2}{\sigma^2} \\ &\propto \exp\left(-\frac{1}{2}S_n\left(w - \frac{S_n^{-1}}{\sigma^2}\sum_{i=1}^n x_i y_i\right)^2\right) \qquad \text{(completing the square)} \\ &\propto N(\hat{\mu}_n, \hat{\sigma}_n) \end{align*} SnS_n collects the w2w^2 coefficients from both the prior and likelihood: Sn=1σ02+∑i=1nxi2σ2=1σ02+𝑿⊤𝑿σ2S_n = \frac{1}{\sigma_0^2} + \frac{\sum_{i=1}^n x_i^2}{\sigma^2} = \frac{1}{\sigma_0^2} + \frac{\mathbf{X}^\top\mathbf{X}}{\sigma^2} Since a Gaussian has the form exp⁡(−12σ̂n2(w−μ̂n)2)\exp (-\frac{1}{2\hat{\sigma}_n^2}(w - \hat{\mu}_n)^2), by matching Sn=1σ̂n2S_n = \frac{1}{\hat{\sigma}_n^2} and reading off μ̂n\hat{\mu}_n, μ̂n=1σ2(1σ02+1σ2𝑿⊤𝑿)−1𝑿⊤𝒚=(σ2σ02I+𝑿⊤𝑿)−1𝑿⊤𝒚\hat{\mu}_n = \frac{1}{\sigma^2}\left(\frac{1}{\sigma_0^2} + \frac{1}{\sigma^2}\mathbf{X}^\top\mathbf{X}\right)^{-1}\mathbf{X}^\top\mathbf{y} = \left(\frac{\sigma^2}{\sigma_0^2}I + \mathbf{X}^\top\mathbf{X}\right)^{-1}\mathbf{X}^\top\mathbf{y} σ̂n2=Sn−1=(1σ02+1σ2𝑿⊤𝑿)−1\hat{\sigma}_n^2 = S_n^{-1} = \left(\frac{1}{\sigma_0^2} + \frac{1}{\sigma^2}\mathbf{X}^\top\mathbf{X}\right)^{-1}

10.2.3 dd-dimension case

For dd dimensions, scalars just become vectors and scalars.
The likelihood and prior become: yi∼N(w⊤xi,σ2I),w∼N(𝟎,σ02I)y_i \sim N(w^\top x_i, \sigma^2 I), \quad w \sim N(\mathbf{0}, \sigma_0^2 I) The posterior is a multivariate Gaussian. μ̂n=1σ2(1σ02I+1σ2𝑿⊤𝑿)−1𝑿⊤𝒚\hat{\mu}_n = \frac{1}{\sigma^2}\left(\frac{1}{\sigma_0^2}I + \frac{1}{\sigma^2}\mathbf{X}^\top\mathbf{X}\right)^{-1}\mathbf{X}^\top\mathbf{y} The variance σ̂n2\hat{\sigma}_n^2 becomes a covariance matrix Σ̂n\hat{\Sigma}_n: Σ̂n=(1σ02I+1σ2𝑿⊤𝑿)−1\hat{\Sigma}_n = \left(\frac{1}{\sigma_0^2}I + \frac{1}{\sigma^2}\mathbf{X}^\top\mathbf{X}\right)^{-1} Again, as n→∞n \to \infty, 1σ2𝑿T𝑿\frac{1}{\sigma^2}\mathbf{X}^T \mathbf{X} dominates and Σ̂→0\hat{\Sigma} \to 0. The uncertainty drops with more data.

10.3 Predictive Distribution

So far Bayesian Linear Regression gives the posterior distribution over weights p(w|𝒚)p(w | \mathbf{y}). But we want to make predictions for new inputs.
Given a new input xx, we want to know the distribution of its output yy. This is the predictive distribution p(y|𝒚,x)p(y | \mathbf{y}, x) (probability of yy given all the training data).

Here ww is a distribution, so every plausible value of ww gives a different prediction for yy. We need to average over all of them, weighted by how probable each ww is: p(y∣𝒚,x)=∫p(y∣w,x)p(w∣𝒚)dwp(y \mid \mathbf{y}, x) = \int p(y \mid w, x)\, p(w \mid \mathbf{y})\, dw This is also called marginalising out ww.
Since both p(y|w,x)=𝒩(w⊤x,σ2)p(y | w,x) = \mathcal{N}(w^\top x, \sigma^2) and p(w|𝒚)=𝒩(μ̂n,Σ̂n)p(w | \mathbf{y}) = \mathcal{N}(\hat{\mu}_n, \hat{\Sigma}_n) are Gaussian, the result is Gaussian and can be computed as: p(y∣𝒚,x)=N(μ̂n⊤x,σ2+x⊤Σ̂nx)\boxed{p(y \mid \mathbf{y}, x) = N(\hat{\mu}_n^\top x,\ \sigma^2 + x^\top \hat{\Sigma}_n x)}

10.3.1 Basis Functions

xx can be replaced by basis functions ϕ(x)\phi(x). First transform the raw input: x↦ϕ(x)=(ϕ1(x),ϕ2(x),…,ϕk(x))⊤x \mapsto \phi(x) = (\phi_1(x), \phi_2(x), \ldots, \phi_k(x))^\top Then the model is still linear in ϕ(x)\phi(x) but can still capture nonlinear relationships in the original xx.