Noise Injection as Implicit Ridge Regression
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Show empirically that least squares on Gaussian-noise-augmented inputs approximates ridge regression with `lam = n * sigma**2`. Implement the four functions below, with no intercept anywhere. `X` has shape `(n, p)` and `y` has shape `(n,)`.
Requirements:
- `ridge_closed_form(X, y, lam)` minimizes `||y - Xw||² + lam·||w||²` and returns `w` of shape `(p,)`.
- `equivalent_ridge_lambda(n, sigma)` returns `float(n * sigma**2)`.
- `noisy_least_squares(X, y, sigma, n_copies, seed)` draws the noise in one call, `np.random.default_rng(seed).normal(0, sigma, size=(n_copies, n, p))`, adds it to `X`, stacks the copies with `y` repeated, and returns the OLS solution.
- With `sigma = 0`, `noisy_least_squares` must reduce to plain OLS on `X`.
- `noise_injection_experiment(X, y, sigma, n_copies, seed)` returns a dict with exactly the keys `w_noise`, `w_ridge`, `lambda` and `relative_error` (`||w_noise - w_ridge|| / ||w_ridge||` as a float).
Example:
```python
X = [[1, 0], [0, 1], [1, 1]]
y = [1, 2, 3]
ridge_closed_form(X, y, 1.0) # -> [0.875, 1.375]
equivalent_ridge_lambda(50, 0.3) # -> 4.5
```
```hint Expected objective
Expand `||y - (X + E)w||²` and take the expectation over the noise matrix `E`. Count how many rows contribute a noise term.
```
```hint Stacked copies
OLS on the stacked data depends on only two sums over the copies. Work out what they are, and check that your answer at `sigma = 0` matches plain OLS.
```
### Constraints & Assumptions
- Use `numpy` only. Results are compared against a fixed draw order, so the noise must come from a single `rng.normal` call with shape `(n_copies, n, p)`.
- No intercept column is added to `X` in any function.
- At `sigma = 0`, `X` has full column rank, so the OLS solution is unique.
### What a Strong Answer Covers
- The derivation that the expected noisy objective equals the ridge objective with `lam = n * sigma**2`, including why the cross term vanishes.
- A ridge solver that uses the normal equations with no intercept.
- A noisy OLS solver that draws the noise in the single required call and solves the stacked problem correctly, with its time and memory cost.
- The `sigma = 0` case and how `relative_error` behaves as `n_copies` grows.
### Follow-up Questions
- Why is the equivalent ridge strength `n·σ²`, and when would it be `σ²` instead?
- Why does the noisy fit still differ from ridge with finitely many copies?
- How does this relate to noise injection in nonlinear networks?
Overview: Implement ridge regression and least squares on Gaussian-noise-augmented copies of the inputs in NumPy, then compare the two fits. It tests the derivation of the equivalent penalty, normal equations, reproducible noise draws and why the gap between the fits shrinks as the number of noisy copies grows.