Implement Lasso and Relaxed Lasso with Coordinate Descent and Measure Shrinkage Bias

Quick Overview

Implement lasso regression with cyclic coordinate descent in NumPy, a relaxed lasso that refits on the selected support with a scaled penalty, and a signed metric for coefficient shrinkage bias. It tests the coordinate update, intercept handling, and how the refit penalty changes the bias of L1-penalized coefficients.

Implement Lasso and Relaxed Lasso with Coordinate Descent and Measure Shrinkage Bias

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement lasso via coordinate descent, a relaxed lasso that refits on the lasso-selected support, and a signed shrinkage-bias metric. Each fit returns `(intercept, coef)` with `coef` of length `p`. ```python def lasso_coordinate_descent(X, y, alpha, max_iter=10000, tol=1e-10): ... def relaxed_lasso(X, y, alpha, phi=0.0, max_iter=10000, tol=1e-10): ... def coefficient_bias(coef, true_coef): ... ``` The functions must behave as follows. - `lasso_coordinate_descent` minimizes $\frac{1}{2n}\lVert y - b_0 - Xb \rVert_2^2 + \alpha \lVert b \rVert_1$ with an unpenalized intercept $b_0$, which matches `sklearn.linear_model.Lasso`. - `relaxed_lasso` takes the support to be the nonzero lasso coefficients at `alpha`. It refits on the support columns only, with lasso penalty `phi * alpha`, so `phi = 0` gives OLS on the support and `phi = 1` gives the plain lasso. Coefficients off the support are 0. - When the support is empty, `relaxed_lasso` returns `(mean(y), zeros(p))`. When `phi` is outside `[0, 1]`, it raises `ValueError`. - `coefficient_bias` returns the mean of `sign(true) * (coef - true)` over the entries where `true != 0`, as a `float`. It returns `0.0` if all true coefficients are zero. Example: ```python coefficient_bias([1.5, 0.2, -0.5], [2.0, 0.0, -1.0]) # -0.5 (shrunk toward 0) coefficient_bias([4.0, -3.0], [3.0, -2.0]) # 1.0 (inflated) ``` The middle entry of the first call is skipped because its true value is 0, and both remaining estimates sit 0.5 closer to zero than the truth. In the second call both estimates are 1.0 farther from zero. ```hint The one-coordinate problem Fix every coefficient except one and minimize over it. Work out what the L1 term does to that one-dimensional minimizer, and keep a running residual so that a single update costs O(n). ``` ```hint Intercept and refit An unpenalized intercept drops out once X and y are centred. In the refit, centre only the support columns and recover the intercept from their means at the end. ``` ### Constraints & Assumptions - Use NumPy only. - Iterate until the largest coefficient change in a full sweep is below `tol`. Results should agree with `sklearn` to about `1e-4` relative. - `alpha > 0`, and `n` exceeds the support size. ### Clarifying Questions to Ask - Should the columns of `X` be standardized first, or used on their original scale as `sklearn.Lasso` does? - Is the support defined by exact zeros from the lasso fit, or by a numerical threshold? - How should a constant column (zero variance after centring) be treated? ### What a Strong Answer Covers - A coordinate update derived for this exact objective, including the 1/n scaling that makes it match sklearn. - Handling of the unpenalized intercept in both the lasso fit and the refit. - The relaxed-lasso edge cases: `phi` validation, an empty support, and placing the refit coefficients back into a length-`p` vector. - Why lasso coefficients are biased toward zero and how the refit penalty `phi * alpha` changes that bias. - A bias metric that keeps the sign and ignores truly zero coefficients, plus the cost of each fit. ### Follow-up Questions - How would you pick `alpha` and `phi` by cross-validation, and why must the support selection be redone inside each fold? - If the selected columns are highly correlated, what happens to the `phi = 0` refit, and how would you stabilize it? - How would you speed up coordinate descent when fitting a whole path of `alpha` values? - What does a `phi = 0` refit do to noise variables that the lasso let into the support?

Overview: Implement lasso regression with cyclic coordinate descent in NumPy, a relaxed lasso that refits on the selected support with a scaled penalty, and a signed metric for coefficient shrinkage bias. It tests the coordinate update, intercept handling, and how the refit penalty changes the bias of L1-penalized coefficients.

General logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement lasso via coordinate descent, a relaxed lasso that refits on the lasso-selected support, and a signed shrinkage-bias metric. Each fit returns (intercept, coef) with coef of length p.

def lasso_coordinate_descent(X, y, alpha, max_iter=10000, tol=1e-10): ...
def relaxed_lasso(X, y, alpha, phi=0.0, max_iter=10000, tol=1e-10): ...
def coefficient_bias(coef, true_coef): ...

The functions must behave as follows.

  • lasso_coordinate_descent minimizes 12n∥y−b0−Xb∥22+α∥b∥1\frac{1}{2n}\lVert y - b_0 - Xb \rVert_2^2 + \alpha \lVert b \rVert_1 with an unpenalized intercept b0b_0 , which matches sklearn.linear_model.Lasso .
  • relaxed_lasso takes the support to be the nonzero lasso coefficients at alpha . It refits on the support columns only, with lasso penalty phi * alpha , so phi = 0 gives OLS on the support and phi = 1 gives the plain lasso. Coefficients off the support are 0.
  • When the support is empty, relaxed_lasso returns (mean(y), zeros(p)) . When phi is outside [0, 1] , it raises ValueError .
  • coefficient_bias returns the mean of sign(true) * (coef - true) over the entries where true != 0 , as a float . It returns 0.0 if all true coefficients are zero.

Example:

coefficient_bias([1.5, 0.2, -0.5], [2.0, 0.0, -1.0])  # -0.5 (shrunk toward 0)
coefficient_bias([4.0, -3.0], [3.0, -2.0])           #  1.0 (inflated)

The middle entry of the first call is skipped because its true value is 0, and both remaining estimates sit 0.5 closer to zero than the truth. In the second call both estimates are 1.0 farther from zero.

Constraints & Assumptions

  • Use NumPy only.
  • Iterate until the largest coefficient change in a full sweep is below tol . Results should agree with sklearn to about 1e-4 relative.
  • alpha > 0 , and n exceeds the support size.

Clarifying Questions to Ask Guidance

  • Should the columns of X be standardized first, or used on their original scale as sklearn.Lasso does?
  • Is the support defined by exact zeros from the lasso fit, or by a numerical threshold?
  • How should a constant column (zero variance after centring) be treated?

What a Strong Answer Covers Guidance

  • A coordinate update derived for this exact objective, including the 1/n scaling that makes it match sklearn.
  • Handling of the unpenalized intercept in both the lasso fit and the refit.
  • The relaxed-lasso edge cases: phi validation, an empty support, and placing the refit coefficients back into a length- p vector.
  • Why lasso coefficients are biased toward zero and how the refit penalty phi * alpha changes that bias.
  • A bias metric that keeps the sign and ignores truly zero coefficients, plus the cost of each fit.

Follow-up Questions Guidance

  • How would you pick alpha and phi by cross-validation, and why must the support selection be redone inside each fold?
  • If the selected columns are highly correlated, what happens to the phi = 0 refit, and how would you stabilize it?
  • How would you speed up coordinate descent when fitting a whole path of alpha values?
  • What does a phi = 0 refit do to noise variables that the lasso let into the support?