Implement Simplified SMO for the Soft-Margin SVM Dual

Quick Overview

Implement simplified sequential minimal optimization for a soft-margin SVM on a precomputed kernel matrix: pair bounds, the two-multiplier update with threshold recomputation, and a seeded training loop. It tests understanding of the SVM dual, KKT conditions and careful numerical implementation.

Implement Simplified SMO for the Soft-Margin SVM Dual

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: hard

Interview Round: Other

Implement three functions that train a soft-margin SVM dual on a precomputed Gram matrix `K` with labels `y` in {-1, +1}. The decision function is `f(x_k) = sum(alpha * y * K[:, k]) + b`. The SVM is trained by `simplified_smo`, which returns `(alpha, b)` with `alpha` a float array of shape `(n,)` and `b` a Python float. The first two functions follow these update rules. Here `a_i` and `a_j` are the current values of `alpha[i]` and `alpha[j]`, `d_i = a_i_new - a_i` and `d_j = a_j_new - a_j`. ```text # compute_bounds(alpha_i, alpha_j, y_i, y_j, C) -> (L, H) as floats y_i != y_j: L = max(0, a_j - a_i), H = min(C, C + a_j - a_i) y_i == y_j: L = max(0, a_i + a_j - C), H = min(C, a_i + a_j) # take_step(K, y, alpha, b, i, j, C) -> (new_alpha, new_b) E_k = sum(alpha * y * K[:, k]) + b - y[k] eta = 2*K[i,j] - K[i,i] - K[j,j] # return unchanged (copy, b) if i == j, L == H, or eta >= 0 a_j_new = clip(a_j - y[j]*(E_i - E_j)/eta, L, H) a_i_new = a_i + y[i]*y[j]*(a_j - a_j_new) b1 = b - E_i - y[i]*d_i*K[i,i] - y[j]*d_j*K[i,j] b2 = b - E_j - y[i]*d_i*K[i,j] - y[j]*d_j*K[j,j] b_new = b1 if 0 < a_i_new < C else b2 if 0 < a_j_new < C else (b1 + b2)/2 ``` `take_step` computes `E_i` and `E_j` from the `alpha` and `b` it receives and never modifies the input array; it returns a new one. The trainer is `simplified_smo(K, y, C, tol=1e-3, max_passes=10, seed=0, max_iter=10000)`. It starts from `alpha = 0` and `b = 0`, creates `rng = np.random.default_rng(seed)` once, and repeats sweeps over `i = 0, ..., n-1`: - Compute `E_i` with the current `alpha` and `b`. Index `i` violates the KKT conditions when `y[i] * E_i < -tol` and `alpha[i] < C`, or when `y[i] * E_i > tol` and `alpha[i] > 0`. - For a violator, draw `j = int(rng.integers(0, n - 1))` and add 1 when `j >= i`, which picks uniformly among the other `n - 1` indices. Call `take_step` and keep its result only if `alpha[j]` moved by at least `1e-5`. - A sweep with no accepted step adds one to a pass counter, and any accepted step resets it to zero. Stop when the counter reaches `max_passes` or after `max_iter` sweeps, whichever comes first. - With fewer than two samples, return an all-zero `alpha` and `b = 0.0`. ```hint Why the pair moves on a segment With every other multiplier fixed, the constraint `sum(alpha * y) = 0` keeps `y_i * alpha_i + y_j * alpha_j` constant. Combine that line with the box `0 <= alpha <= C` and look at what the dual objective becomes as a function of `alpha_j` alone. ``` ```hint Where the threshold update comes from For a multiplier strictly between 0 and `C`, the KKT conditions force its margin `y_k * f(x_k)` to be exactly 1. Write the new error `E_i` after the step in terms of the old one, `d_i`, `d_j` and the change in `b`. ``` ```hint Keep the random draws reproducible The result depends on the sequence of `j` values, so consume exactly one draw per violator and never redraw. ``` ### Constraints & Assumptions - `K` is a symmetric positive semidefinite `n x n` Gram matrix and `y` has length `n` with values in {-1, +1}. - `C > 0`. Inputs may be lists or NumPy arrays. ### What a Strong Answer Covers - Bounds that keep the pair inside the box while preserving the equality constraint, for both label cases. - A `take_step` that uses the pre-step errors, skips degenerate pairs, clips `alpha_j`, updates `alpha_i` to keep `sum(alpha * y)` fixed, and returns a copy. - The threshold rule and why a free multiplier pins `b`. - A training loop that follows the KKT check, the seeded choice of `j`, the `1e-5` acceptance test and both stopping rules exactly. - Per-sweep cost and what the stopping rule does and does not guarantee. ### Follow-up Questions - How would you choose `j` better than at random, and why does that speed up convergence? - How would you cache the error vector so each step avoids recomputing every `E_k` from scratch? - What does `eta >= 0` mean geometrically, and how does full SMO handle that case instead of skipping it? - After training, how do you identify the support vectors and predict on new points, and how do you recover `w` for a linear kernel?

Overview: Implement simplified sequential minimal optimization for a soft-margin SVM on a precomputed kernel matrix: pair bounds, the two-multiplier update with threshold recomputation, and a seeded training loop. It tests understanding of the SVM dual, KKT conditions and careful numerical implementation.

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

Implement three functions that train a soft-margin SVM dual on a precomputed Gram matrix K with labels y in {-1, +1}. The decision function is f(x_k) = sum(alpha * y * K[:, k]) + b. The SVM is trained by simplified_smo, which returns (alpha, b) with alpha a float array of shape (n,) and b a Python float.

The first two functions follow these update rules. Here a_i and a_j are the current values of alpha[i] and alpha[j], d_i = a_i_new - a_i and d_j = a_j_new - a_j.

# compute_bounds(alpha_i, alpha_j, y_i, y_j, C) -> (L, H) as floats
y_i != y_j: L = max(0, a_j - a_i),     H = min(C, C + a_j - a_i)
y_i == y_j: L = max(0, a_i + a_j - C), H = min(C, a_i + a_j)

# take_step(K, y, alpha, b, i, j, C) -> (new_alpha, new_b)
E_k = sum(alpha * y * K[:, k]) + b - y[k]
eta = 2*K[i,j] - K[i,i] - K[j,j]
# return unchanged (copy, b) if i == j, L == H, or eta >= 0
a_j_new = clip(a_j - y[j]*(E_i - E_j)/eta, L, H)
a_i_new = a_i + y[i]*y[j]*(a_j - a_j_new)
b1 = b - E_i - y[i]*d_i*K[i,i] - y[j]*d_j*K[i,j]
b2 = b - E_j - y[i]*d_i*K[i,j] - y[j]*d_j*K[j,j]
b_new = b1 if 0 < a_i_new < C else b2 if 0 < a_j_new < C else (b1 + b2)/2

take_step computes E_i and E_j from the alpha and b it receives and never modifies the input array; it returns a new one.

The trainer is simplified_smo(K, y, C, tol=1e-3, max_passes=10, seed=0, max_iter=10000). It starts from alpha = 0 and b = 0, creates rng = np.random.default_rng(seed) once, and repeats sweeps over i = 0, ..., n-1:

  • Compute E_i with the current alpha and b . Index i violates the KKT conditions when y[i] * E_i < -tol and alpha[i] < C , or when y[i] * E_i > tol and alpha[i] > 0 .
  • For a violator, draw j = int(rng.integers(0, n - 1)) and add 1 when j >= i , which picks uniformly among the other n - 1 indices. Call take_step and keep its result only if alpha[j] moved by at least 1e-5 .
  • A sweep with no accepted step adds one to a pass counter, and any accepted step resets it to zero. Stop when the counter reaches max_passes or after max_iter sweeps, whichever comes first.
  • With fewer than two samples, return an all-zero alpha and b = 0.0 .

Constraints & Assumptions

  • K is a symmetric positive semidefinite n x n Gram matrix and y has length n with values in {-1, +1}.
  • C > 0 . Inputs may be lists or NumPy arrays.

What a Strong Answer Covers Guidance

  • Bounds that keep the pair inside the box while preserving the equality constraint, for both label cases.
  • A take_step that uses the pre-step errors, skips degenerate pairs, clips alpha_j , updates alpha_i to keep sum(alpha * y) fixed, and returns a copy.
  • The threshold rule and why a free multiplier pins b .
  • A training loop that follows the KKT check, the seeded choice of j , the 1e-5 acceptance test and both stopping rules exactly.
  • Per-sweep cost and what the stopping rule does and does not guarantee.

Follow-up Questions Guidance

  • How would you choose j better than at random, and why does that speed up convergence?
  • How would you cache the error vector so each step avoids recomputing every E_k from scratch?
  • What does eta >= 0 mean geometrically, and how does full SMO handle that case instead of skipping it?
  • After training, how do you identify the support vectors and predict on new points, and how do you recover w for a linear kernel?