Adaptive Lasso via Column Rescaling
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: hard
Interview Round: Other
Implement three NumPy functions: a plain lasso solver, adaptive penalty weights from a centered ridge/OLS fit, and an adaptive lasso that solves the weighted problem by rescaling columns and calling the plain solver. `adaptive_lasso` returns `(coef, intercept)`.
```python
def lasso_cd(X, y, alpha, max_iter=10000, tol=1e-10): ...
def adaptive_weights(X, y, ridge_lambda=0.0, gamma=1.0, eps=1e-12): ...
def adaptive_lasso(X, y, alpha, ridge_lambda=0.0, gamma=1.0, eps=1e-12,
max_iter=10000, tol=1e-10): ...
```
The functions must meet these requirements:
- `lasso_cd` runs cyclic coordinate descent from `b = 0`, minimizing $\frac{1}{2n}\lVert y - Xb \rVert_2^2 + \alpha \lVert b \rVert_1$ with no intercept (the objective of scikit-learn's `Lasso(fit_intercept=False)`). It skips all-zero columns and stops when the max coefficient change in a sweep is below `tol`.
- `adaptive_weights` centers `X` and `y`, solves $(X_c^\top X_c + \lambda I)\,\beta = X_c^\top y_c$ where $\lambda$ is `ridge_lambda`, and returns $w_j = 1 / (\lvert \beta_j \rvert + \epsilon)^{\gamma}$ with $\epsilon$ equal to `eps` and $\gamma$ equal to `gamma`.
- `adaptive_lasso` centers the data, fits `theta = lasso_cd(Xc / w, yc, alpha)`, and returns `coef = theta / w` and the unpenalized `intercept = mean(y) - mean(X) @ coef` as a Python `float`.
- With `alpha` at or above the weighted `lambda_max`, `coef` is all zeros and `intercept == mean(y)`. With `alpha = 0`, the result is OLS with intercept.
Example:
```python
import numpy as np
X = [[1, 1], [1, -1], [-1, 1], [-1, -1]]
y = [3, 1, -1, -3]
adaptive_weights(X, y) # [0.5, 1.0]
adaptive_weights(X, y, gamma=2.0) # [0.25, 1.0]
adaptive_weights(X, y, ridge_lambda=4) # [1.0, 2.0]
adaptive_lasso(X, np.add(y, 10), alpha=0.5)
# -> (array([1.75, 0.5]), 10.0)
```
The outputs are rounded; the `eps` term changes them by less than 1e-12. The columns of `X` have mean zero and are orthogonal, and the OLS fit on this data is $\beta = (2, 1)$.
```hint Change of variables
Write the weighted penalty in terms of $\theta_j = w_j b_j$ and see what that substitution does to each column of the design matrix.
```
```hint One coordinate at a time
With the residual kept up to date, each coordinate update is a one-dimensional lasso problem. Work out its closed form, including where $\lVert x_j \rVert^2 / n$ enters.
```
```hint Keeping the intercept out of the penalty
Centering both `X` and `y` before the penalized fit lets you recover the intercept afterward from the means.
```
### Constraints & Assumptions
- `X` is `(n, p)`, `y` is `(n,)`, array-likes of finite floats.
- `XcᵀXc + ridge_lambda·I` is invertible, `alpha ≥ 0` and `gamma > 0`.
- Coefficients must match a scikit-learn reference to `rtol=1e-4`.
- Only NumPy is available in the solution. Do not call scikit-learn.
### What a Strong Answer Covers
- A correct coordinate-descent update for the stated objective: soft-thresholding, residual bookkeeping, the zero-column skip and the stopping rule.
- The change of variables that turns the weighted L1 penalty into a plain lasso, and the mapping back to the original scale.
- Centering so the intercept stays unpenalized, with the intercept recovered from the means.
- The boundary behavior: the weighted `lambda_max` and the `alpha = 0` OLS limit.
- The cost of the initial fit and of each coordinate-descent sweep.
### Follow-up Questions
- What happens to the initial estimate when `p > n`, and how would you handle it?
- How would you choose `alpha` and `gamma` in practice?
- Why does the adaptive lasso shrink large coefficients less than the plain lasso, and what does `gamma` change about that?
Overview: Implement a coordinate-descent lasso solver in NumPy, compute adaptive penalty weights from a centered ridge or OLS fit, and solve the adaptive lasso by rescaling columns. Tests soft-thresholding, the change of variables behind weighted L1 penalties, and keeping the intercept unpenalized.