Bayesian Posterior Mean Equals Ridge
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Given a design matrix `X` of shape (n, p) and a target vector `y` of shape (n,), implement three functions under the Bayesian linear model $y \sim \mathcal{N}(Xw, \sigma^2 I)$ with prior $w \sim \mathcal{N}(0, \tau^2 I)$ and no intercept. Then show numerically that the posterior mean of `w` equals the ridge solution with `lam = sigma2 / tau2`.
- `ridge_closed_form(X, y, lam)` minimises $\lVert y - Xw \rVert^2 + \lambda \lVert w \rVert^2$ and returns an array of shape `(p,)`. Raise `ValueError` when `lam < 0`. `lam = 0` is ordinary least squares.
- `bayesian_posterior(X, y, sigma2, tau2)` returns `(mean, cov)` with shapes `(p,)` and `(p, p)`. `cov` must be the inverse of the posterior precision $X^\top X / \sigma^2 + I / \tau^2$, and it must be exactly symmetric. Raise `ValueError` when `sigma2 <= 0` or `tau2 <= 0`.
- `posterior_ridge_gap(X, y, sigma2, tau2)` returns the maximum absolute difference, as a Python `float`, between the posterior mean and the matching ridge solution.
Example:
```python
X = [[1.0], [2.0]]
y = [1.0, 2.0]
bayesian_posterior(X, y, 1.0, 1.0) # mean [5/6], cov [[1/6]]
bayesian_posterior(X, y, 2.0, 0.5) # mean [5/9], cov [[2/9]]
```
```hint Compare the two linear systems
Write down the equation the posterior mean solves next to the ridge normal equations, then see what multiplying one of them by `sigma2` does.
```
```hint Floating-point care
A matrix inverse computed in floating point is not guaranteed to come out exactly symmetric, and multiplying by an explicit inverse is a less accurate way to get the mean than a linear solve.
```
### Constraints & Assumptions
- `X` and `y` may arrive as nested Python lists or NumPy arrays. Convert them to float arrays.
- `X` has shape (n, p) and `y` has n entries. There is no intercept column and no centering.
- NumPy is available.
- With `lam = 0` the ridge system can only be solved when `X` has full column rank.
### What a Strong Answer Covers
- The posterior precision and mean derived from the Gaussian likelihood and prior, and why they reduce to the ridge system with $\lambda = \sigma^2 / \tau^2$.
- Linear solves for the mean and the ridge weights, and a covariance that is made exactly symmetric after inversion.
- The validation rules, return shapes and return types the spec asks for.
- Time and space complexity in terms of n and p.
- Behavior at the edges: `lam = 0` with a rank-deficient `X`, and very large or very small `tau2`.
### Follow-up Questions
- How would you get the posterior predictive distribution for a new input `x*`?
- How would you choose `sigma2` and `tau2` without cross-validation?
- Which prior gives the Lasso as its MAP estimate, and is the posterior mean under that prior sparse?
Overview: Implement closed-form ridge regression and the Gaussian posterior of Bayesian linear regression in NumPy, then check numerically that the posterior mean matches ridge with lambda set to sigma2 over tau2. Tests the link between priors and penalties, careful linear algebra, and input validation.