Implement ISTA proximal gradient descent for Lasso regression
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement Lasso regression with ISTA (iterative shrinkage-thresholding), a proximal gradient method. Minimize
$$
\frac{1}{2n}\lVert y - Xb - b_0 \rVert_2^2 + \alpha \lVert b \rVert_1
$$
where `X` is an `n x p` design matrix, `b` is the coefficient vector and `b0` is an unpenalized intercept. Write three functions:
```python
def soft_threshold(z, threshold): ...
def lipschitz_constant(X): ...
def ista_lasso(X, y, alpha, max_iter=10000, tol=1e-10, fit_intercept=True): ...
```
`ista_lasso` returns `(coef, intercept, n_iter)`. The functions must behave as follows.
- `soft_threshold(z, threshold)` returns `sign(z) * max(|z| - threshold, 0)` elementwise, as a float array.
- `lipschitz_constant(X)` returns `sigma_max(X)² / n` as a Python `float`.
- With `fit_intercept=True`, center `X` and `y`, run ISTA on the centered data, then set `intercept = y_mean - x_mean @ coef`. Otherwise use the raw data and return `intercept = 0.0`.
- Start from `b = 0` and use the step `1/L`, with `L` computed from the (centered) design. Each iteration is `b = soft_threshold(b - grad/L, alpha/L)`.
- Count every update in `n_iter`. Stop when `max|b_new - b| <= tol` or after `max_iter` updates. If `L == 0`, return zero coefficients, `y_mean` (0.0 without an intercept) and `n_iter = 0`.
At convergence the result must match `sklearn.linear_model.Lasso` with the same `alpha`, and the code must not call it.
Example:
```python
soft_threshold([-3.0, -0.5, 0.0, 0.5, 2.0], 1.0)
# -> [-2.0, 0.0, 0.0, 0.0, 1.0]
lipschitz_constant([[1, 2], [3, 4], [5, 6]])
# -> largest eigenvalue of X.T @ X divided by 3 (about 30.245)
```
```hint Scaling of the gradient
Differentiate only the smooth squared-error term and keep its 1/n factor. The same factor has to show up in L, or the step size and the comparison with sklearn drift apart.
```
```hint Order inside the loop
Measure the change between the old and new coefficients before you overwrite the old vector. The update that meets the tolerance is still an update.
```
### Constraints & Assumptions
- `X` is a numeric array-like of shape `(n, p)` and `y` has length `n`. Plain Python lists must work, as in the example.
- Assume `alpha >= 0`. The defaults are `max_iter=10000` and `tol=1e-10`.
- Use NumPy only. Do not call scikit-learn.
### What a Strong Answer Covers
- Soft-thresholding with correct sign handling and an exact zero region.
- The gradient of the scaled squared error and a step size tied to the Lipschitz constant of the centered design.
- How centering removes the intercept from the iteration, how it is recovered, and the path without an intercept.
- The stopping rule, the iteration count, and the `L == 0` guard.
- Cost per iteration and why a fixed point of the update is the Lasso solution.
### Follow-up Questions
- How would you accelerate this to FISTA, and what happens to the convergence rate?
- How would you compute a regularization path over many values of `alpha` and warm-start it?
- What if `X` is too large to compute its spectral norm exactly?
Overview: Implement Lasso regression in NumPy with ISTA: soft-thresholding, a step size from the Lipschitz constant, intercept handling by centering, and a tolerance-based stopping rule. Tests proximal gradient methods, L1 optimality conditions, and matching the scikit-learn Lasso objective.