Warm-Started Lasso Regularization Path

Quick Overview

Implement lasso regression with cyclic coordinate descent, an unpenalized intercept, and a warm-started regularization path over a geometric grid of penalties. It tests deriving the coordinate update, finding the penalty at which every coefficient is zero, sweep counting and convergence, and efficient residual bookkeeping.

Warm-Started Lasso Regularization Path

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: hard

Interview Round: Other

Implement `compute_lambda_max`, `lasso_coordinate_descent` and `lasso_path` for the objective `(1/(2n))·||y - b - Xw||² + alpha·||w||₁` with an unpenalized intercept `b`. Fit on centered `X` and `y`, then recover `b = mean(y) - mean(X) @ w`. The functions take these arguments: ```python def compute_lambda_max(X, y): ... def lasso_coordinate_descent(X, y, alpha, w_init=None, max_iter=10000, tol=1e-10): ... def lasso_path(X, y, n_alphas=100, eps=1e-3, max_iter=10000, tol=1e-10): ... ``` For example: ```python X = [[1, 2], [2, 0], [3, 1]] y = [1, 2, 6] compute_lambda_max(X, y) # 5/3 ≈ 1.6667 alphas, coefs, intercepts = lasso_path(X, y) coefs[0] # array([0., 0.]): all zero at lambda_max intercepts[0] # 3.0, which is mean(y) lasso_coordinate_descent(X, y, 1.0) # w ≈ [1, 0], intercept ≈ 1.0, n_iter == 2 ``` ```hint Isolate one coordinate Hold every other coefficient fixed and write the objective as a function of `w_j` alone. It becomes a one-dimensional quadratic plus an absolute value term, which has a closed-form minimizer. ``` ```hint Keep a running residual Recomputing `y - Xw` for every coordinate costs a full matrix-vector product. Work out what happens to the residual when only `w_j` moves. ``` ### Constraints & Assumptions - `compute_lambda_max(X, y)` returns float `max|Xcᵀ yc| / n` (centered data). - `lasso_coordinate_descent` runs cyclic coordinate descent from `w_init` (zeros if `None`) and returns `(w, intercept, n_iter)`. - `n_iter` counts full sweeps: stop after the first sweep whose max absolute coefficient change is `< tol` (a warm start at the optimum gives `n_iter == 1`). - `lasso_path` uses `alphas = np.geomspace(lambda_max, lambda_max*eps, n_alphas)`, warm-starting each fit from the previous `w`. - `lasso_path` returns `(alphas, coefs, intercepts)` with shapes `(n_alphas,)`, `(n_alphas, p)`, `(n_alphas,)`. Zero-variance columns get coefficient `0`. - Results should match `sklearn.linear_model.Lasso` at the same `alpha` (same scaling) and satisfy the KKT conditions. Using sklearn in the solution is not allowed. ### What a Strong Answer Covers - Why centering removes the intercept from the penalized problem, and how the intercept is recovered afterwards - The one-coordinate update derived from the objective, with a residual update that keeps each coordinate step linear in n - Where lambda_max comes from and how the alpha grid and warm starts use it - Stopping and sweep counting that match the spec, including the one-sweep warm start case and zero-variance columns - The cost of the full path, and a way to check the result against the optimality conditions ### Follow-up Questions - How would you add strong or safe screening rules to the path? - Why is a duality-gap stopping criterion better than max coefficient change? - How would you extend this to elastic net or pick alpha by cross-validation?

Overview: Implement lasso regression with cyclic coordinate descent, an unpenalized intercept, and a warm-started regularization path over a geometric grid of penalties. It tests deriving the coordinate update, finding the penalty at which every coefficient is zero, sweep counting and convergence, and efficient residual bookkeeping.

PracHub logo
General
Nov 24, 2026
hardMachine Learning EngineerOtherMachine Learning
0
0

Implement compute_lambda_max, lasso_coordinate_descent and lasso_path for the objective (1/(2n))·||y - b - Xw||² + alpha·||w||₁ with an unpenalized intercept b. Fit on centered X and y, then recover b = mean(y) - mean(X) @ w.

The functions take these arguments:

def compute_lambda_max(X, y): ...
def lasso_coordinate_descent(X, y, alpha, w_init=None, max_iter=10000, tol=1e-10): ...
def lasso_path(X, y, n_alphas=100, eps=1e-3, max_iter=10000, tol=1e-10): ...

For example:

X = [[1, 2], [2, 0], [3, 1]]
y = [1, 2, 6]

compute_lambda_max(X, y)              # 5/3 ≈ 1.6667

alphas, coefs, intercepts = lasso_path(X, y)
coefs[0]                              # array([0., 0.]): all zero at lambda_max
intercepts[0]                         # 3.0, which is mean(y)

lasso_coordinate_descent(X, y, 1.0)   # w ≈ [1, 0], intercept ≈ 1.0, n_iter == 2

Constraints & Assumptions

  • compute_lambda_max(X, y) returns float max|Xcᵀ yc| / n (centered data).
  • lasso_coordinate_descent runs cyclic coordinate descent from w_init (zeros if None ) and returns (w, intercept, n_iter) .
  • n_iter counts full sweeps: stop after the first sweep whose max absolute coefficient change is < tol (a warm start at the optimum gives n_iter == 1 ).
  • lasso_path uses alphas = np.geomspace(lambda_max, lambda_max*eps, n_alphas) , warm-starting each fit from the previous w .
  • lasso_path returns (alphas, coefs, intercepts) with shapes (n_alphas,) , (n_alphas, p) , (n_alphas,) . Zero-variance columns get coefficient 0 .
  • Results should match sklearn.linear_model.Lasso at the same alpha (same scaling) and satisfy the KKT conditions. Using sklearn in the solution is not allowed.

What a Strong Answer Covers Guidance

  • Why centering removes the intercept from the penalized problem, and how the intercept is recovered afterwards
  • The one-coordinate update derived from the objective, with a residual update that keeps each coordinate step linear in n
  • Where lambda_max comes from and how the alpha grid and warm starts use it
  • Stopping and sweep counting that match the spec, including the one-sweep warm start case and zero-variance columns
  • The cost of the full path, and a way to check the result against the optimality conditions

Follow-up Questions Guidance

  • How would you add strong or safe screening rules to the path?
  • Why is a duality-gap stopping criterion better than max coefficient change?
  • How would you extend this to elastic net or pick alpha by cross-validation?