Implement dual ridge and Lasso coordinate descent for wide data (p ≫ n)
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement five functions for a wide (`p ≫ n`) sparse regression study: a seeded data generator, ridge via the `n×n` dual system, the lasso `lambda_max`, lasso by cyclic coordinate descent, and a warm-started support-size path. No intercept anywhere.
```python
def make_wide_data(n, p, k, noise, seed): ...
def ridge_dual(X, y, lam): ...
def lasso_lambda_max(X, y): ...
def lasso_cd(X, y, alpha, max_iter=5000, tol=1e-10, beta_init=None): ...
def lasso_support_sizes(X, y, alphas, max_iter=5000, tol=1e-10): ...
```
The functions must behave as follows.
- `make_wide_data` creates `rng = np.random.default_rng(seed)`, draws `X = rng.standard_normal((n, p))` and then `eps = rng.standard_normal(n)`. It sets `beta_true[:k] = 2.0` (the rest 0) and `y = X @ beta_true + noise * eps`, and returns `(X, y, beta_true)`.
- `ridge_dual` minimizes `||y - Xb||² + lam·||b||²` through `b = Xᵀ(XXᵀ + lam·I_n)⁻¹y`. Solve the linear system; do not form the inverse. `lam < 0` raises `ValueError`, and `lam = 0` gives the minimum-norm interpolant.
- `lasso_lambda_max` returns `max|Xᵀy| / n` as a `float`.
- `lasso_cd` minimizes `(1/(2n))||y - Xb||² + alpha·||b||₁` with cyclic updates over `j = 0..p-1`, starting from `beta_init` or zeros. It stops after a sweep whose largest coefficient change is `< tol`, or after `max_iter` sweeps. The result must match sklearn `Lasso(fit_intercept=False)` and contain exact zeros.
- `lasso_support_sizes` fits the alphas in the given order, warm-starting each fit from the previous solution, and returns an int array of nonzero counts.
Example:
```python
X, y, _ = make_wide_data(20, 200, 5, 0.5, 0)
lmax = lasso_lambda_max(X, y)
lasso_support_sizes(X, y, lmax * np.array([1.0001, 0.5, 0.2, 0.1, 0.05]))
# -> first entry 0, last >= 5, all entries <= 20 (= n)
ridge_dual(X, y, 1.0) # all 200 coefficients nonzero
```
```hint One coordinate at a time
With every other coefficient fixed, the objective in b_j is a one-dimensional quadratic plus alpha·|b_j|. Work out its minimizer using the residual that leaves feature j out.
```
```hint Keep a sweep at O(np)
Recomputing y - Xb for every coordinate costs O(np) per update. Find a quantity you can update in O(n) after each coefficient changes.
```
### Constraints & Assumptions
- `X` has shape `(n, p)` with `p` much larger than `n` (the example uses `n = 20`, `p = 200`), and `y` has length `n`.
- No function fits an intercept.
- Assume `alpha > 0` for the lasso functions. `lam` must be at least 0.
- The draw order in `make_wide_data` is fixed, so the same seed gives the same data.
### What a Strong Answer Covers
- Reproducible data generation with the specified draw order.
- Why the dual ridge formula equals the primal solution, what happens at `lam = 0`, and why solving beats inverting.
- Why `lambda_max` is the point at and above which the zero vector is optimal.
- A correct coordinate update with soft-thresholding, an incremental residual, exact zeros and warm starts.
- How ridge and lasso differ in sparsity when `p ≫ n`, including the limit on the lasso support size.
### Follow-up Questions
- Why can lasso select at most `n` features here?
- What is the cost of dual vs primal ridge, and when would you switch?
- How would you speed up the lasso path for large `p`?
Overview: Implement ridge regression through its n-by-n dual system and Lasso by cyclic coordinate descent on data with far more features than samples, then track support size along a warm-started path. Tests the push-through identity, lambda_max, soft-thresholding, and the limit on how many features Lasso can select.