Implement ridge regression via augmented least squares and the normal equations
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement ridge regression (penalty `lam * ||coef||²`) two ways: as ordinary least squares (OLS) on an augmented dataset and via the normal equations. Both must return the same `(coef, intercept)` pair, up to floating-point rounding.
With coefficients $\beta$ and intercept $\beta_0$, both methods minimize
$$\lVert y - X\beta - \beta_0 \mathbf{1}\rVert_2^2 + \lambda \lVert \beta\rVert_2^2,$$
which is the objective of `sklearn.linear_model.Ridge(alpha=lam)`. When the intercept is not fitted, $\beta_0 = 0$.
- `augment_for_ridge(X, y, lam)` returns `X_aug` of shape `(n+p, p)`, which is `X` stacked over `sqrt(lam) * I_p`, and `y_aug` of shape `(n+p,)`, which is `y` followed by `p` zeros. No centering happens here.
- `ridge_augmented(X, y, lam, fit_intercept=True)` solves OLS on the augmented data.
- `ridge_closed_form(X, y, lam, fit_intercept=True)` solves $(X^\top X + \lambda I)\,\beta = X^\top y$.
- If `fit_intercept=True`, center `X` and `y` first (the intercept is unpenalized) and return `intercept = mean(y) - mean(X) @ coef` as a `float`, where `mean(X)` is the vector of column means. Otherwise `intercept = 0.0`.
- `coef` is a 1-D array of length `p`. Results must match `sklearn.linear_model.Ridge(alpha=lam)`.
- `lam < 0` raises `ValueError` in all three functions.
For example:
```python
X = [[1.0], [2.0], [3.0]]
y = [1.0, 2.0, 3.0]
ridge_augmented(X, y, 1.0, fit_intercept=False) # (array([0.93333333]), 0.0), coef = 14/15
ridge_augmented(X, y, 2.0, fit_intercept=True) # (array([0.5]), 1.0)
```
```hint Expand the augmented residual
Write the squared residual of the augmented system as two blocks, the original n rows and the p new rows, and compare the sum with the ridge objective.
```
```hint Keep the intercept out of the penalty
Ask what the best intercept is for a fixed coef, and what the problem looks like once X and y both have zero column means.
```
### Constraints & Assumptions
- `X` has shape `(n, p)` and `y` has length `n`. Inputs may be lists.
- `lam` may be `0` (the problem reduces to OLS) or very large, up to `1e12`.
- When `lam = 0`, assume the centered `X` has full column rank, so the OLS answer is unique.
### What a Strong Answer Covers
- Why least squares on the augmented data minimizes the ridge objective, shown by splitting the augmented residual into its two blocks.
- Handling the intercept by centering and recovering it from the means.
- Both solvers returning matching coefficients, with the `ValueError` check in all three functions.
- How the two solvers behave at `lam = 0` and at very large `lam`, and which one is numerically safer.
### Follow-up Questions
- Why is the augmented formulation numerically preferable to the normal equations?
- Why center the data instead of appending a column of ones to `X`?
- How would you use the same augmentation for a different penalty strength per feature, or for shrinking toward a nonzero prior coefficient vector?
- How would you solve the problem efficiently when `p` is much larger than `n`?
Overview: Implement ridge regression twice, as ordinary least squares on data augmented with scaled identity rows and through the normal equations, with an unpenalized intercept. It tests the algebra behind the augmentation trick, intercept handling by centering, and the numerical conditioning of each solver.