Dual Coordinate Descent for Linear SVM (LIBLINEAR-Style, Bias-Free Hinge Loss)
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: hard
Interview Round: Other
Implement `fit_dual_cd(X, y, C=1.0, epochs=1000, tol=1e-8, seed=None)` for a bias-free L2-regularized hinge-loss SVM using LIBLINEAR-style dual coordinate descent. Keep `w = X.T @ (alpha * y)` updated incrementally and return `(w, alpha, n_epochs)`.
Also implement `primal_objective(w, X, y, C)`, which returns $\frac{1}{2}\|w\|^2 + C \sum_i \max(0,\ 1 - y_i w^\top x_i)$, and `dual_objective(alpha, X, y)`, which returns $\frac{1}{2}\|X^\top(\alpha \odot y)\|^2 - \sum_i \alpha_i$. Both return a `float`.
The solver must follow these rules:
- Start from `alpha = 0`. The update is $\alpha_i \leftarrow \mathrm{clip}(\alpha_i - g/Q_{ii},\ 0,\ C)$ with $g = y_i w^\top x_i - 1$ and $Q_{ii} = \|x_i\|^2$. Skip rows with $Q_{ii} = 0$.
- With `seed=None`, visit indices in order 0 to n-1 every epoch. Otherwise create `np.random.default_rng(seed)` once and use `rng.permutation(n)` each epoch.
- Stop after an epoch when the max minus the min of the projected gradient over that epoch is at most `tol`. `n_epochs` counts the epochs run, including the stopping one.
- Labels not in {-1, +1}, mismatched shapes, or `C <= 0` raise `ValueError`.
Example:
```text
Input: X = [[2, 0], [1, 1]], y = [1, 1], C = 10, epochs = 1, tol = 0
Output: w = [0.75, 0.25], alpha = [0.25, 0.25], n_epochs = 1
```
```hint Cheap gradients
The gradient for coordinate i only needs $w^\top x_i$. If `w` is kept in sync after every change to `alpha[i]`, no step ever needs the full product with `X`.
```
```hint Gradients at the bounds
A coordinate sitting at 0 or at C can only move in one direction. Work out which part of the gradient still counts toward the stopping test at each bound.
```
```hint Empty epochs
Rows with $Q_{ii} = 0$ are skipped. Decide what the stopping test should do when an epoch visits no row at all.
```
### Constraints & Assumptions
- n ≤ 1,000 rows and d ≤ 100 features, all finite.
- Labels are -1 or +1.
- There is no bias term: the decision function is $w^\top x$.
- No kernel: the model is linear in the given features.
### What a Strong Answer Covers
- The closed-form coordinate step, its clipping to the box $[0, C]$, and the incremental update of `w` with the actual change in `alpha[i]`.
- The projected gradient at each bound, the spread-based stopping rule, and a correct epoch count.
- The visiting order for both seed cases, the skipped zero rows, and the input validation.
- Why each step never increases the dual objective, and how the primal and dual objectives relate at the optimum.
- Cost per coordinate update and per epoch.
### Follow-up Questions
- How would you adapt this to the squared-hinge (L2-loss) SVM?
- What is shrinking and why does LIBLINEAR use it?
- How would you support an intercept term?
Overview: Implement LIBLINEAR-style dual coordinate descent for a bias-free linear SVM with hinge loss, keeping the weight vector updated incrementally and stopping on the projected gradient. It tests the SVM dual, closed-form coordinate updates under box constraints, and careful handling of convergence, ordering and input checks.