MAP Linear Regression With Laplace Prior
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Model `y = Xw + b + ε` with Gaussian noise of variance `noise_var` and an independent Laplace prior of scale `prior_scale` on each weight (flat prior on `b`). Implement the three functions below so the MAP estimate equals sklearn's `Lasso` fit with the mapped `alpha`.
- `laplace_prior_to_lasso_alpha(noise_var, prior_scale, n_samples)`: return the float `alpha` for sklearn's objective `(1/(2n))||r||² + alpha·||w||₁`.
- `negative_log_posterior(X, y, coef, intercept, noise_var, prior_scale)`: return `||y - Xw - b||² / (2·noise_var) + ||w||₁ / prior_scale` as a float (no constants).
- `map_laplace_regression(X, y, noise_var, prior_scale, max_iter=10000, tol=1e-10)`: return `(coef, intercept)` (ndarray of shape `(p,)`, float) minimising the negative log posterior. Inactive weights must be exactly `0.0`.
Example:
```python
laplace_prior_to_lasso_alpha(2.0, 0.5, 8) # 0.5
X = [[1, 0], [0, 1]]
y = [1, 2]
negative_log_posterior(X, y, [0.5, 1.0], 0.5, 2.0, 0.5) # 3.0625
# r = [0, 0.5] -> 0.25/4 + 1.5/0.5
```
```hint From posterior to objective
Take the negative log of likelihood times prior, drop the constants, then rescale the result so its squared-error term carries sklearn's `1/(2n)` factor.
```
```hint Intercept
A flat prior on `b` means the intercept is unpenalised. Centring `X` and `y` takes it out of the coordinate updates.
```
### Constraints & Assumptions
- `X` has shape `(n, p)` and `y` has length `n`, with `n` about 50 and `p ≤ 10`.
- `noise_var > 0` and `prior_scale > 0`. `prior_scale` may be as large as `1e12`, which must recover OLS.
- Coefficients and intercept must match `Lasso` within `rtol=1e-4, atol=1e-6`. Shifting `y` by a constant must shift only the intercept.
- When the prior is tight enough that all weights are zero, return `intercept = mean(y)`.
### What a Strong Answer Covers
- The derivation from the Gaussian likelihood and Laplace prior to an L1-penalised objective, and the resulting `alpha`.
- A coordinate-descent solver with soft-thresholding that leaves inactive weights at exactly zero.
- Correct treatment of the unpenalised intercept, including shift invariance and the all-zero case.
- An argument that the converged solution satisfies the lasso optimality conditions, and the behaviour at a very large `prior_scale`.
### Follow-up Questions
- With the prior held fixed, how does the mapped `alpha` change as `n` grows, and what does that say about the prior's influence on the estimate?
- Which prior on the weights gives ridge regression, and what is the matching sklearn `Ridge` penalty?
- The MAP estimate is sparse. Is the posterior mean sparse too, and how would you get uncertainty estimates for the weights?
Overview: Derive the MAP estimate for linear regression with Gaussian noise and a Laplace prior on the weights, map it to the scikit-learn Lasso penalty, and solve it by coordinate descent. Tests the Bayesian view of L1 regularization, intercept handling and soft-thresholding.