Early-Stopped Gradient Descent as Implicit Ridge
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: hard
Interview Round: Other
Given `X` with shape `(n, p)` and `y` with shape `(n,)`, run full-batch gradient descent from `w_0 = 0` on the least-squares loss `(1/(2n)) * ||y - Xw||^2` (no intercept), and compare each iterate `w_t` with the ridge solution at `lam = 1/(eta*t)`. Ridge minimizes the same loss plus `(lam/2) * ||w||^2`.
Implement these functions in NumPy:
- `gd_path(X, y, eta, n_steps)` returns an array of shape `(n_steps+1, p)` whose rows are `w_0, ..., w_T` with `T = n_steps`.
- `ridge_coef(X, y, lam)` returns the closed-form ridge solution. When `lam == 0` it returns the `np.linalg.lstsq` solution.
- `matched_ridge_path(X, y, eta, steps)` returns an array of shape `(len(steps), p)` whose row `i` is the ridge solution at `lam = 1/(eta*steps[i])`.
- `spectral_filters(eigvals, eta, t)` returns a pair `(gd, ridge)` with `gd = (1 - (1 - eta*s)**t) / s` (equal to `eta*t` at `s = 0`) and `ridge = eta*t / (1 + eta*t*s)`, evaluated at each eigenvalue `s`.
- `compare_paths(X, y, eta, n_steps)` returns an array of shape `(n_steps,)` whose entry `t-1` is `||w_t - r_t|| / ||r_t||` for `t = 1..T`, where `r_t` is the matched ridge solution. When `||r_t|| = 0`, return the raw `||w_t - r_t||` instead.
Example:
```python
X = [[2, 0], [0, 1], [0, 0], [0, 0]]
y = [2, 1, 0, 0]
gd_path(X, y, 0.5, 2)
# -> [[0, 0], [0.5, 0.125], [0.75, 0.234375]]
matched_ridge_path(X, y, 0.5, [1, 2])
# -> [[1/3, 1/9], [0.5, 0.2]]
spectral_filters([1, 0.25, 0], 0.5, 2)
# -> ([0.75, 0.9375, 1.0], [0.5, 0.8, 1.0])
compare_paths(X, y, 0.5, 2)
# -> approximately [0.4760, 0.4686]
```
```hint Rewrite the update
Write one gradient step as an affine map of `w_t` using `S = X.T @ X / n` and `b = X.T @ y / n`, then look at it along one eigenvector of `S` at a time.
```
```hint Match the scaling
The loss here divides by `n` and sklearn's ridge objective does not. Work out which `alpha` gives the same minimizer before comparing results.
```
```hint Zero eigenvalues
The GD filter has a removable singularity at `s = 0`. Take the limit and avoid dividing by zero in code.
```
### Constraints & Assumptions
- `X` must be 2-D with shape `(n, p)`, and `y` must have `n` entries.
- Raise `ValueError` if `X` is not 2-D or the row counts differ, if `eta <= 0`, `n_steps < 0` or `lam < 0`, or if any entry of `steps` is below 1.
- Ridge results must match sklearn's `Ridge(alpha=n*lam, fit_intercept=False)` to `rtol=1e-7`.
### What a Strong Answer Covers
- A correct GD path from zero with the `1/n` gradient scaling and `n_steps + 1` stored rows, starting with `w_0`.
- A closed-form ridge solver on the same scale as the loss, the `alpha = n*lam` mapping to sklearn, and the `lstsq` fallback at `lam = 0`.
- A derivation of both spectral filters in the eigenbasis of `X.T @ X / n`, including the `s = 0` limit.
- Why the two filters agree when `eta*t*s` is small or large and differ by a bounded factor in between.
- Input validation and the zero-norm case in `compare_paths`.
### Follow-up Questions
- Why does the gap between the GD and ridge paths stay bounded without vanishing at intermediate `t`?
- How would you choose the stopping time `t` in practice?
- What changes if `eta` exceeds `2/s_max`, where `s_max` is the largest eigenvalue of `X.T @ X / n`?
Overview: Implement full-batch gradient descent on least squares in NumPy and compare each iterate with the ridge solution at a matched penalty of one over step size times steps. It tests the spectral filter view of early stopping, the scaling that matches sklearn's ridge, and why the two paths agree only up to a bounded factor.