Implement soft, hard, SCAD and MCP thresholding operators and penalties in NumPy

Quick Overview

Implement vectorized NumPy versions of the soft, hard, SCAD and MCP thresholding operators along with the SCAD and MCP penalty functions. It tests careful piecewise code at the boundaries, parameter validation, and an understanding of how nonconvex penalties reduce the bias the lasso puts on large coefficients.

Implement soft, hard, SCAD and MCP thresholding operators and penalties in NumPy

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement vectorized univariate thresholding operators for soft, hard, SCAD and MCP, plus the SCAD and MCP penalty values. Each thresholding operator returns, elementwise, the minimizer over $b$ of $\frac{1}{2}(z - b)^2 + P(b)$ for its penalty $P$. Implement these functions: ```python soft_threshold(z, lam) hard_threshold(z, lam) scad_threshold(z, lam, a=3.7) mcp_threshold(z, lam, gamma=3.0) scad_penalty(beta, lam, a=3.7) mcp_penalty(beta, lam, gamma=3.0) threshold_curves(z, lam, a=3.7, gamma=3.0) ``` `threshold_curves(z, lam, a, gamma)` returns a dict with keys `"soft"`, `"hard"`, `"scad"` and `"mcp"`, each an array shaped like `z`. Use these exact piecewise definitions, with the boundaries included as shown. Each line applies only when the lines above it do not. ```text soft(z) = sign(z) * max(|z| - lam, 0) hard(z) = z if |z| > lam else 0 scad(z) = soft(z) if |z| <= 2*lam = ((a-1)*z - sign(z)*a*lam) / (a-2) if |z| <= a*lam = z otherwise mcp(z) = soft(z) / (1 - 1/gamma) if |z| <= gamma*lam = z otherwise scad_penalty(b) = lam*|b| if |b| <= lam = (2*a*lam*|b| - b**2 - lam**2) / (2*(a-1)) if |b| <= a*lam = lam**2 * (a+1) / 2 otherwise mcp_penalty(b) = lam*|b| - b**2 / (2*gamma) if |b| <= gamma*lam = gamma * lam**2 / 2 otherwise ``` For example: ```python z = [-3.0, 1.5, 2.5, 4.0] lam = 1.0 scad_threshold(z, lam, a=3.7) # [-2.588, 0.5, 1.794, 4.0] (rounded) mcp_threshold(z, lam, gamma=3.0) # [-3.0, 0.75, 2.25, 4.0] soft_threshold(z, lam) # [-2.0, 0.5, 1.5, 3.0] ``` ```hint Vectorize the branches Compute each branch for the whole array, then choose per element with masks, testing the conditions in the order the definitions list them. ``` ```hint Test the knots Evaluate each function just below and just above every boundary (lam, 2*lam, a*lam, gamma*lam). SCAD, MCP and both penalties should be continuous there; hard thresholding should not. ``` ### Constraints & Assumptions - Inputs may be scalars, lists or arrays. Convert them with `np.asarray(..., dtype=float)`. - `lam < 0` raises `ValueError` in every function. - `a <= 2` (SCAD) or `gamma <= 1` (MCP) raises `ValueError`. Defaults are `a=3.7` and `gamma=3.0`. - Plotting the curves is optional and not graded. Only the returned arrays are tested. ### What a Strong Answer Covers - Vectorized piecewise code that follows the given boundaries exactly and returns arrays shaped like the input, including scalar input. - Parameter validation in every function, with the stated defaults. - Why each operator minimizes $\frac{1}{2}(z - b)^2 + P(b)$, and why the limits on `a` and `gamma` make that minimizer unique. - How SCAD and MCP behave compared with soft and hard thresholding, for small and for large $|z|$. ### Follow-up Questions - How would you use these operators inside a coordinate descent solver for penalized regression? - How would you pick `lam` and the concavity parameter in practice? - What happens to the MCP operator as `gamma` approaches 1, and as it grows without bound?

Overview: Implement vectorized NumPy versions of the soft, hard, SCAD and MCP thresholding operators along with the SCAD and MCP penalty functions. It tests careful piecewise code at the boundaries, parameter validation, and an understanding of how nonconvex penalties reduce the bias the lasso puts on large coefficients.

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

Implement vectorized univariate thresholding operators for soft, hard, SCAD and MCP, plus the SCAD and MCP penalty values. Each thresholding operator returns, elementwise, the minimizer over bb of 12(z−b)2+P(b)\frac{1}{2}(z - b)^2 + P(b) for its penalty PP.

Implement these functions:

soft_threshold(z, lam)
hard_threshold(z, lam)
scad_threshold(z, lam, a=3.7)
mcp_threshold(z, lam, gamma=3.0)
scad_penalty(beta, lam, a=3.7)
mcp_penalty(beta, lam, gamma=3.0)
threshold_curves(z, lam, a=3.7, gamma=3.0)

threshold_curves(z, lam, a, gamma) returns a dict with keys "soft", "hard", "scad" and "mcp", each an array shaped like z. Use these exact piecewise definitions, with the boundaries included as shown. Each line applies only when the lines above it do not.

soft(z)  = sign(z) * max(|z| - lam, 0)
hard(z)  = z if |z| > lam else 0
scad(z)  = soft(z)                                   if |z| <= 2*lam
         = ((a-1)*z - sign(z)*a*lam) / (a-2)          if |z| <= a*lam
         = z                                         otherwise
mcp(z)   = soft(z) / (1 - 1/gamma)                   if |z| <= gamma*lam
         = z                                         otherwise

scad_penalty(b) = lam*|b|                               if |b| <= lam
                = (2*a*lam*|b| - b**2 - lam**2) / (2*(a-1)) if |b| <= a*lam
                = lam**2 * (a+1) / 2                    otherwise
mcp_penalty(b)  = lam*|b| - b**2 / (2*gamma)            if |b| <= gamma*lam
                = gamma * lam**2 / 2                    otherwise

For example:

z = [-3.0, 1.5, 2.5, 4.0]
lam = 1.0
scad_threshold(z, lam, a=3.7)     # [-2.588, 0.5, 1.794, 4.0] (rounded)
mcp_threshold(z, lam, gamma=3.0)  # [-3.0, 0.75, 2.25, 4.0]
soft_threshold(z, lam)            # [-2.0, 0.5, 1.5, 3.0]

Constraints & Assumptions

  • Inputs may be scalars, lists or arrays. Convert them with np.asarray(..., dtype=float) .
  • lam < 0 raises ValueError in every function.
  • a <= 2 (SCAD) or gamma <= 1 (MCP) raises ValueError . Defaults are a=3.7 and gamma=3.0 .
  • Plotting the curves is optional and not graded. Only the returned arrays are tested.

What a Strong Answer Covers Guidance

  • Vectorized piecewise code that follows the given boundaries exactly and returns arrays shaped like the input, including scalar input.
  • Parameter validation in every function, with the stated defaults.
  • Why each operator minimizes 12(z−b)2+P(b)\frac{1}{2}(z - b)^2 + P(b) , and why the limits on a and gamma make that minimizer unique.
  • How SCAD and MCP behave compared with soft and hard thresholding, for small and for large ∣z∣|z| .

Follow-up Questions Guidance

  • How would you use these operators inside a coordinate descent solver for penalized regression?
  • How would you pick lam and the concavity parameter in practice?
  • What happens to the MCP operator as gamma approaches 1, and as it grows without bound?