Constrained Lasso and Radius-to-Lambda Mapping
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: hard
Interview Round: Other
Implement three functions for the constrained form of the Lasso, where the L1 norm of the coefficients is bounded by a radius `t` instead of penalized, and map each radius to the equivalent penalty.
- `project_l1_ball(v, t)` returns the Euclidean projection of `v` onto $\{w : \lVert w \rVert_1 \le t\}$. For `t <= 0` return zeros. When `v` is already inside the ball, return a copy of `v`.
- `constrained_lasso(X, y, t, max_iter=10000, tol=1e-10)` minimises $\frac{1}{2n}\lVert y_c - X_c b \rVert^2$ subject to $\lVert b \rVert_1 \le t$ by projected gradient descent. Centre `X` by its column means and `y` by its overall mean, so the intercept is unpenalized and `intercept = mean(y) - mean(X) @ b`. Return `(intercept, coef)`, with the intercept as a Python `float` and `coef` as an ndarray of shape `(p,)`. Stop when the maximum absolute coefficient change is below `tol`, or after `max_iter` iterations.
- `radius_to_lambda(X, y, radii)` returns, for each radius, the equivalent scikit-learn-style Lasso `alpha`, read off the KKT conditions as $\lambda(t) = \lVert X_c^\top (y_c - X_c b_t) \rVert_\infty / n$. Return an ndarray of shape `(len(radii),)`.
At `t = 0` the mapping gives $\lambda_{\max} = \lVert X_c^\top y_c \rVert_\infty / n$, and a radius beyond the L1 norm of the OLS solution gives the OLS coefficients and $\lambda \approx 0$.
Example:
```python
project_l1_ball(np.array([3.0, 2.0, 1.0]), 3.0) # -> [2.0, 1.0, 0.0]
project_l1_ball(np.array([3.0, -1.0, 0.5]), 2.0) # -> [2.0, 0.0, 0.0]
project_l1_ball(np.array([-0.2, 0.3]), 1.0) # -> [-0.2, 0.3]
```
```hint The projection
Outside the ball, the projection has a closed form once you know one scalar threshold. Sorting the magnitudes lets you find that threshold in a single pass.
```
```hint Step size
Projected gradient descent converges with a fixed step of one over the Lipschitz constant of the gradient. Work out that constant for this quadratic.
```
```hint Reading off the penalty
Write the KKT conditions of the constrained problem next to the optimality conditions of the penalized Lasso, and look at what the residual correlations must equal on the support.
```
### Constraints & Assumptions
- `X` has shape `(n, p)` and `y` has length `n`, with finite values.
- Implement the solver with NumPy. scikit-learn's `Lasso`, which minimises $\frac{1}{2n}\lVert y - Xw - b \rVert^2 + \alpha \lVert w \rVert_1$ with an unpenalized intercept, is only the reference for checking.
- Coefficients from `constrained_lasso` should match `sklearn.linear_model.Lasso(alpha=λ)` to about 1e-4 relative tolerance, so the solver has to converge tightly.
- For the OLS endpoint, assume the centred `X` has full column rank.
### What a Strong Answer Covers
- A correct L1-ball projection: the threshold search and both early returns.
- Projected gradient descent on centred data, with a step size justified by the Lipschitz constant, the stopping rule and the intercept recovery.
- The KKT argument that links the constraint's multiplier to the Lasso `alpha`, including the ${1/n}$ scaling scikit-learn uses.
- The behavior at the two endpoints, `t = 0` and `t` at or above the OLS L1 norm.
- Setup and per-iteration cost, and how slow convergence affects the accuracy target.
### Follow-up Questions
- Why does the mapping from t to λ fail to be one-to-one at the extremes?
- How would you speed up projected gradient descent here?
- How would you handle feature scaling, and how does it affect the equivalence?
Overview: Implement Euclidean projection onto the L1 ball, solve the L1-constrained Lasso with projected gradient descent, and map each constraint radius to the equivalent penalized Lasso alpha. Tests convex optimization, KKT conditions and careful numerical convergence.