Implement a Linear SVR with Epsilon-Insensitive Loss from Scratch

Quick Overview

Implement a linear support vector regressor from scratch in NumPy: the epsilon-insensitive loss, the primal objective with a regularized bias, its subgradient, and a fit precise enough to match a reference solver. Tests convex optimization for non-smooth losses, duality, and how the tube width controls which points become support vectors.

Implement a Linear SVR with Epsilon-Insensitive Loss from Scratch

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: hard

Interview Round: Other

Implement a linear SVR from scratch. With residuals `r = y - (Xw + b)`, the primal objective is $$ \frac{1}{2}\left(\lVert w \rVert^2 + b^2\right) + C \sum_i \max\bigl(0,\ \lvert r_i \rvert - \epsilon\bigr) $$ (the bias is regularized). Fill in the five functions below. - `epsilon_insensitive_loss(residuals, epsilon)`: elementwise `max(0, |r| - epsilon)` as an array. - `svr_objective(w, b, X, y, C, epsilon)`: the objective as a `float`. - `svr_subgradient(w, b, X, y, C, epsilon)`: returns `(grad_w, grad_b)` with `s_i = sign(r_i)` if `|r_i| > epsilon`, else `0` (points on the tube boundary count as inside). Use `grad_w = w - C * X.T @ s` and `grad_b = b - C * sum(s)`. - `fit_linear_svr(X, y, C, epsilon, epochs)`: returns `(w, b)` minimizing the objective, where `w` is an ndarray and `b` is a `float`. - `predict_linear_svr(X, w, b)`: returns `X @ w + b`. Example: ```python X = [[1, 0], [0, 1], [1, 1]] w = [0.5, 1.0] b = 0.2 C = 2.0 epsilon = 0.25 y = [1.0, 2.0, 0.5] svr_objective(w, b, X, y, C, epsilon) # 3.745 y = [1.0, 2.0, 3.0] svr_subgradient(w, b, X, y, C, epsilon) # (array([-3.5, -3.0]), -5.8) ``` ```hint The bias is one more weight Because `b` is penalized exactly like the weights, the problem does not change if you append a constant feature to `X`. Work out which constant makes the two problems identical. ``` ```hint How precise the fit must be Compare the tolerance the fit has to meet with what a fixed-step subgradient method reaches on a non-smooth objective. Ask whether the dual problem is easier to solve exactly. ``` ```hint One variable at a time In the dual, fix every variable but one. Look at the shape of the resulting one-dimensional problem and the bounds on that variable. ``` ### Constraints & Assumptions - `X` has shape `(n, d)` and `y` has length `n`. `C > 0` and `epsilon >= 0`. - The fitted objective must match scikit-learn's `LinearSVR` (with `intercept_scaling=1`) within `rtol=1e-4`, and `w` must match within `1e-2`. - The fitted `(w, b)` must be a local minimum under perturbations of norm `1e-3`. ### What a Strong Answer Covers - Implements the loss, objective and subgradient with the regularized bias and with boundary points (`|r| = epsilon`) counted as inside the tube, reproducing both example values. - Picks a fitting method that reaches the required precision and explains why fixed-step subgradient descent falls short. - Derives the solver's update correctly, including how bounds on the variables are enforced and how `w` and `b` are recovered. - Uses a sensible stopping rule and states the time per pass and the memory used. - Connects the tube width to which points end up as support vectors. ### Follow-up Questions - Why can plain subgradient descent struggle to hit the exact optimum here? - How does `epsilon` affect the sparsity of the solution? - What changes if the bias should not be regularized?

Overview: Implement a linear support vector regressor from scratch in NumPy: the epsilon-insensitive loss, the primal objective with a regularized bias, its subgradient, and a fit precise enough to match a reference solver. Tests convex optimization for non-smooth losses, duality, and how the tube width controls which points become support vectors.

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

Implement a linear SVR from scratch. With residuals r = y - (Xw + b), the primal objective is

12(∥w∥2+b2)+C∑imax⁡(0, ∣ri∣−ϵ)\frac{1}{2}\left(\lVert w \rVert^2 + b^2\right) + C \sum_i \max\bigl(0,\ \lvert r_i \rvert - \epsilon\bigr)

(the bias is regularized). Fill in the five functions below.

  • epsilon_insensitive_loss(residuals, epsilon) : elementwise max(0, |r| - epsilon) as an array.
  • svr_objective(w, b, X, y, C, epsilon) : the objective as a float .
  • svr_subgradient(w, b, X, y, C, epsilon) : returns (grad_w, grad_b) with s_i = sign(r_i) if |r_i| > epsilon , else 0 (points on the tube boundary count as inside). Use grad_w = w - C * X.T @ s and grad_b = b - C * sum(s) .
  • fit_linear_svr(X, y, C, epsilon, epochs) : returns (w, b) minimizing the objective, where w is an ndarray and b is a float .
  • predict_linear_svr(X, w, b) : returns X @ w + b .

Example:

X = [[1, 0], [0, 1], [1, 1]]
w = [0.5, 1.0]
b = 0.2
C = 2.0
epsilon = 0.25

y = [1.0, 2.0, 0.5]
svr_objective(w, b, X, y, C, epsilon)    # 3.745

y = [1.0, 2.0, 3.0]
svr_subgradient(w, b, X, y, C, epsilon)  # (array([-3.5, -3.0]), -5.8)

Constraints & Assumptions

  • X has shape (n, d) and y has length n . C > 0 and epsilon >= 0 .
  • The fitted objective must match scikit-learn's LinearSVR (with intercept_scaling=1 ) within rtol=1e-4 , and w must match within 1e-2 .
  • The fitted (w, b) must be a local minimum under perturbations of norm 1e-3 .

What a Strong Answer Covers Guidance

  • Implements the loss, objective and subgradient with the regularized bias and with boundary points ( |r| = epsilon ) counted as inside the tube, reproducing both example values.
  • Picks a fitting method that reaches the required precision and explains why fixed-step subgradient descent falls short.
  • Derives the solver's update correctly, including how bounds on the variables are enforced and how w and b are recovered.
  • Uses a sensible stopping rule and states the time per pass and the memory used.
  • Connects the tube width to which points end up as support vectors.

Follow-up Questions Guidance

  • Why can plain subgradient descent struggle to hit the exact optimum here?
  • How does epsilon affect the sparsity of the solution?
  • What changes if the bias should not be regularized?