Foundations to Frontier
Tests
D. Classical Machine Learning › D5 Support Vector Machines and Kernel Methods

Soft-Margin SVM (Hinge Loss and Slack Variables)

Real data are rarely perfectly separable. The soft-margin SVM keeps the maximum-margin idea but lets individual points violate the margin, paying a penalty C for each unit of violation (slack). The resulting problem is equivalent to minimising the hinge loss plus an L2 penalty, a convex problem with a unique solution. C trades margin width against training violations: small C gives a wide, robust margin; large C chases every point, including mislabelled ones. This page covers slack variables, the hinge-loss form, the role of C, which points become support vectors, solving by dual coordinate descent, and choosing C. In the exercises you implement a linear soft-margin SVM by dual coordinate descent and check its optimality conditions; then you study how C interacts with label noise in a high-dimensional anomaly detector, using an exact solver after discovering that the fast one had not converged.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Allow a few points inside the corridor

One mislabelled point can make a dataset impossible to separate, or force a hard-margin SVM to squeeze its corridor to almost nothing. The soft-margin SVM lets some points sit inside the corridor or even on the wrong side, at a cost. The parameter C sets the price. A low price means a wide, calm corridor that ignores a few awkward points. A high price means the SVM bends over backwards to fit every point, mistakes included.

Soft margin: how much should one bad point matter?
margin 1/‖w‖ = 0.912 · support vectors 17 (violators 15) · training errors 5
Dashed lines are the margin w·x + b = ±1. Small C: a wide margin containing many points, all of them support vectors with α = C; the single orange square far in the blue region barely matters. Large C: the margin shrinks to reduce violations, and the line tilts to accommodate the mislabelled point. Tick the box off to see the boundary without it.
Why engineers care

Labels from operators and automated tests are never perfect, and classes overlap physically. The soft margin makes SVMs robust to both, with a single interpretable knob. It is the version of the SVM that is actually used, from linear text classifiers to kernel SVMs on sensor data.

📝 Quick check (Level 0)
0/3 answered
  1. 1. The soft-margin SVM allows points to…
  2. 2. A small C gives…
  3. 3. In the demo, a large C makes the mislabelled point…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Slack by hand

Take w = 1, b = 0 in 1-D and three positive points at x = 2, 0.5 and −1. The slack is ξ = max(0, 1 − y(wx + b)):

x y·f(x) Slack ξ Status
2 2 0 outside the margin
0.5 0.5 0.5 inside the margin, correct side
−1 −1 2 wrong side (misclassified)

With C = 1, the objective is ½w² + CΣξ = 0.5 + (0 + 0.5 + 2) = 3.0.

Which points are support vectors? In the dual each point has a weight αᵢ in [0, C]:

  • α = 0: outside the margin, no influence;
  • α strictly between 0 and C: exactly on the margin (y·f = 1);
  • α = C: a margin violator (ξ > 0).

The weights are w = Σᵢ αᵢyᵢxᵢ.

The steps in plain English:

  1. Standardise the features and encode labels as ±1.
  2. Choose C (by cross-validation, on a log grid such as 10⁻³…10³).
  3. Minimise ½‖w‖² + C Σ max(0, 1 − yᵢ(w·xᵢ + b)): a convex problem with a unique solution.
  4. Points on or inside the margin become support vectors; the rest do not affect the solution.
  5. Predict sign(w·x + b). Use the decision value as a confidence score; calibrate it if you need probabilities.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Slack of a correctly classified point with y·f = 0.3?
  2. 2. A point with α = C is…
  3. 3. Objective for w = 1 and slacks 0, 0.5, 2 with C = 1?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

The problem and its solution

Primal with slacks: minimise ½‖w‖² + C Σξᵢ subject to yᵢ(w·xᵢ + b) ≥ 1 − ξᵢ and ξᵢ ≥ 0.

Hinge-loss form: at the optimum ξᵢ = max(0, 1 − yᵢ(w·xᵢ + b)). So the SVM minimises ½‖w‖² + C·Σ hinge, which is L2-regularised hinge-loss classification (compare D2.26).

Dual: maximise Σαᵢ − ½ΣᵢΣⱼ αᵢαⱼyᵢyⱼ xᵢ·xⱼ subject to 0 ≤ αᵢ ≤ C (and Σαᵢyᵢ = 0 if b is unregularised). See D5.48.

KKT conditions, with fᵢ = w·xᵢ + b:

  • αᵢ = 0 ⇒ yᵢfᵢ ≥ 1;
  • αᵢ strictly between 0 and C ⇒ yᵢfᵢ = 1;
  • αᵢ = C ⇒ yᵢfᵢ ≤ 1.

Dual coordinate descent (Hsieh et al., 2008; liblinear). Absorb b into w with a constant feature, which removes the equality constraint. Each αᵢ then has a closed-form update, clip(αᵢ − (yᵢw·xᵢ − 1)/‖xᵢ‖², 0, C), and w is kept in sync. It is very fast, but convergence slows dramatically for large C. Monitor the objective or the duality gap, or use an exact QP solver.

from sklearn.svm import LinearSVC, SVC
LinearSVC(C=1.0, loss="hinge", dual=True, max_iter=20000).fit(X, y)   # liblinear dual coordinate descent
SVC(kernel="linear", C=1.0).fit(X, y)                                  # libsvm (SMO); exposes dual_coef_, support_

Use it as the default SVM. Tune C, and keep it small when labels are noisy. Watch convergence warnings (they are real) and feature scaling.

🐍 Exercise 1: soft-margin SVM by dual coordinate descent
Implement the linear soft-margin SVM by dual coordinate descent (bias as a constant feature), the hinge objective and slacks; check that w = Σ αᵢyᵢxᵢ, the KKT conditions (α = 0 outside the margin, α strictly between 0 and C on it), near-optimality against perturbations, and that smaller C gives a wider margin with more support vectors.
📝 Quick check (Level 2)
0/3 answered
  1. 1. The soft-margin SVM minimises…
  2. 2. In the dual, each αᵢ lies in…
  3. 3. Dual coordinate descent is slow to converge when…

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

The role of C

Regularisation path.

  • As C → 0, the margin widens until every point is a support vector at α = C, and w → 0.
  • As C → ∞ on separable data, the solution approaches the hard margin. On non-separable data the misclassified points stay at α = C and the margin shrinks to accommodate them.

The ν-parameterisation (D5.52) replaces C by an interpretable bound on the fraction of support vectors.

C as inverse regularisation. Dividing by Cn shows the objective is (1/n)Σ hinge + (1/(2Cn))‖w‖². So C ∝ 1/λ for the L2 penalty λ. Large C means weak regularisation, which fits label noise in high dimensions (exercise 2: with 20% flipped labels, accuracy fell from 0.69 at C = 0.01 to 0.60 at C = 100).

Hinge loss is classification-calibrated. Minimising the expected hinge loss yields the Bayes decision sign(P(y = 1 | x) − ½). But SVM scores are not probabilities; use Platt scaling (D0.10).

Robustness. The hinge grows linearly, so a mislabelled point exerts a bounded pull (at most C per point). That is still unbounded in aggregate, unlike truncated or ramp losses, which give more robust but non-convex SVMs.

Interview questions.

  • How should you choose C? Cross-validate on a log grid, after standardising. Prefer smaller C for noisy labels and high-dimensional, small-n data.
  • Why do support-vector counts grow with label noise? Mislabelled points violate the margin, so they get α = C and become support vectors. A large support-vector fraction is a hint that the data are noisy or C is too small.
📝 Quick check (Level 3)
0/3 answered
  1. 1. C relates to the L2 penalty λ as…
  2. 2. As C → 0 the fraction of support vectors tends to…
  3. 3. SVM decision values are…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

Choosing C when labels are noisy

History.

  • Cortes and Vapnik (1995), “Support-vector networks”, introduced slack variables and the soft margin.
  • Platt (1998) introduced SMO, and libsvm (Chang and Lin) made SVMs practical.
  • Joachims (1998) applied SVMs to text classification.
  • Hsieh et al. (2008) introduced dual coordinate descent, and liblinear made linear SVMs scale to millions of documents.
  • Bartlett, Jordan and McAuliffe (2006) proved the hinge loss is classification-calibrated.

Exercise 1. On two overlapping Gaussian classes, C = 1 gives 14 support vectors with Σξ = 10.2. w equals Σαᵢyᵢxᵢ, and the KKT conditions hold. From C = 0.01 to 0.1 to 1, the support vectors fall 54 → 24 → 14 and the margin narrows 1.88 → 1.07 → 0.61.

Exercise 2: an anomaly detector with noisy labels. Setup: 50 features (3 informative), 100 training events, clean test labels, 4 repetitions.

An honest note on the method. The first version used dual coordinate descent with 30 passes. It reported that large C was far worse on clean data too. Re-running with 300–1000 passes showed that the solver simply had not converged at C ≥ 10. The study below uses an exact box-constrained dual QP (L-BFGS-B).

The table shows test accuracy by C.

Flipped labels C = 0.001 0.01 0.1 1 10 100 CV-chosen C → accuracy
0% 0.758 0.769 0.781 0.787 0.784 0.784 0.01 → 0.768
10% 0.712 0.713 0.718 0.680 0.675 0.675 0.005 → 0.716
20% 0.683 0.692 0.676 0.644 0.606 0.603 0.005 → 0.687
  • With clean labels, C hardly matters above 0.1. The data are separable in 50-D, so large C is simply a hard margin.
  • With noisy labels, large C fits the noise: −9 points at 20% noise. The best C moves down as noise grows.
  • Cross-validation on the noisy labels picks small C and lands within 2 points of the best fixed C in every case.
  • Check your solver’s convergence before interpreting a hyperparameter study.
🐍 Exercise 2 (research): C and label noise
Simulate a 50-feature anomaly detector with 0–20% flipped training labels; train exact soft-margin SVMs (box-constrained dual QP) for C from 0.001 to 100 and report clean-test accuracy, support-vector share and the C chosen by 5-fold cross-validation.

Soft-margin SVMs in AI, robotics and aerospace

  • Anomaly and fault classification with imperfect operator labels.
  • Text and log classification: linear SVMs on TF-IDF features.
  • Vision: HOG + linear SVM pedestrian and object detectors (Dalal and Triggs, 2005).
  • Terrain classification on rovers from engineered features.

References

  1. C. Cortes, V. Vapnik (1995). “Support-vector networks.” Machine Learning 20(3).
  2. C.-J. Hsieh, K.-W. Chang, C.-J. Lin, S. S. Keerthi, S. Sundararajan (2008). “A dual coordinate descent method for large-scale linear SVM.” ICML.
  3. T. Joachims (1998). “Text categorization with support vector machines.” ECML.
  4. P. L. Bartlett, M. I. Jordan, J. D. McAuliffe (2006). “Convexity, classification, and risk bounds.” JASA 101(473).
  5. N. Dalal, B. Triggs (2005). “Histograms of oriented gradients for human detection.” CVPR.