Linear SVM Margin Diagnostics: Margins, Slack, Support Vector Partition and Primal Objective
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `svm_margin_diagnostics(X, y, w, b, C=1.0, tol=1e-8)` for a trained linear SVM with decision function `f(x) = x·w + b` and labels in {-1, +1}. Return a dict of margin statistics and an index partition of the samples. Below, `y*f` means `y_i * f(x_i)` for each sample.
- Keys: `margin_width` (`2 / ||w||`), `decision_values`, `functional_margins` (`y*f`), `geometric_margins` (`y*f / ||w||`), `slack` (`max(0, 1 - y*f)`).
- Index arrays (int64, ascending): `non_support` (`y*f > 1 + tol`), `on_margin` (`|y*f - 1| <= tol`), `violators` (`y*f < 1 - tol`).
- `misclassified`: indices with `y*f < 0` strictly (a subset of violators).
- `primal_objective`: float, `0.5 * ||w||^2 + C * sum(slack)`.
- A zero weight vector raises `ValueError`.
Example:
```python
X = [[2, 0], [1, 0], [0.5, 0], [-1, 0], [-3, 0], [0, 0], [-0.5, 7]]
y = [1, 1, 1, -1, -1, 1, 1]
w = [1, 0]
b = 0
C = 2.0
# functional_margins = [2, 1, 0.5, 1, 3, 0, -0.5]
# slack = [0, 0, 0.5, 0, 0, 1, 1.5]
# non_support = [0, 4], on_margin = [1, 3], violators = [2, 5, 6], misclassified = [6]
# margin_width = 2.0, primal_objective = 0.5 + 2 * 3 = 6.5
```
```hint One quantity first
Compute the functional margin `y_i * f(x_i)` once for every sample. Look at how each requested output can be written as a function of that one array.
```
```hint Boundaries of the groups
Check that your three comparisons against 1 never overlap and leave no gap, and decide where a point with a functional margin of exactly 0 belongs.
```
### Constraints & Assumptions
- `X` has shape (n, d); `y` has length n with values in {-1, +1}; `w` has length d.
- Inputs may be lists or arrays, and `b` may be a scalar or a length-1 array.
- Per-sample outputs are NumPy arrays of length n.
- `tol` is a small non-negative number (default `1e-8`).
### What a Strong Answer Covers
- Converts list or array inputs (including a length-1 `b`) to float arrays and raises `ValueError` before dividing by a zero norm.
- Computes decision values, functional and geometric margins, and slack as vectorised operations in one pass.
- Builds the three index groups with tolerance comparisons so each sample lands in exactly one group, and keeps the misclassified test strict at zero.
- Gets the primal objective right (squared norm plus the C-weighted slack sum) and returns ascending int64 index arrays.
- States the time and space cost and explains which groups can hold support vectors under the KKT conditions.
### Follow-up Questions
- If `(w, b)` came from an optimal soft-margin solver, what values can the dual variables take in each of the three groups, and how would you use that to sanity-check the solver's output?
- How would you choose `tol` when the features are on very different scales or the solver stopped at a loose convergence tolerance?
- How would you recover `b` from the support vectors if a solver returned only `w` and the dual coefficients?
- How would you compute the same diagnostics for a kernel SVM, where `w` is never formed explicitly?
Overview: Implement margin diagnostics for a trained linear SVM in NumPy: decision values, functional and geometric margins, hinge slack, margin width and the primal objective. Tests whether you can split samples into non-support, on-margin and violating groups with a tolerance and connect those groups to the KKT conditions.