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

SVM Dual Formulation and KKT Conditions

Every SVM can be solved in two equivalent ways. The primal optimises the weight vector w directly; the dual optimises one weight αᵢ per training point, under box constraints and one equality constraint. Lagrangian duality turns the margin problem into the dual, the KKT conditions explain which points become support vectors, and strong duality guarantees both give the same answer. Crucially, the dual depends on the data only through inner products xᵢ·xⱼ, which is what makes kernels possible. This page derives the dual step by step, states and interprets the KKT conditions, shows how to recover w and b, and discusses when the primal or the dual is cheaper. In the exercises you solve the dual with a QP solver and verify strong duality and the KKT conditions; then you compare primal and dual cost on wide and tall problems.

Level 0: Foundation

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

Weigh the examples instead of the features

The SVM can be described two ways. In the primal, you choose a weight for each feature (w). In the dual, you choose a weight for each training example (α), and w is built as a weighted sum of the examples. Most examples end up with weight zero; only the support vectors count. The two descriptions always give the same classifier. The dual needs only the similarities between examples (their dot products), which opens the door to kernels.

The dual: one weight per training point
α = C
primal 1.354 · dual 1.229 · gap 0.1244 · support vectors 4 of 24
Dot size = αᵢ/C. Points with α = 0 (faint) do not affect w at all; the rest are support vectors, and those at the dashed line α = C are margin violators. The primal objective (orange) only ever bounds the dual (blue) from above; at the optimum they meet: strong duality.
Why engineers care

The dual tells you which training cases the model actually relies on: the support vectors, the borderline examples worth reviewing. It is the form that kernel SVMs (non-linear boundaries) and solvers like SMO use. Duality and KKT conditions are also the standard tools for constrained optimisation in trajectory planning and control.

📝 Quick check (Level 0)
0/3 answered
  1. 1. In the dual, the variables are…
  2. 2. Examples with α = 0…
  3. 3. In the demo, the primal and dual objectives…

Level 1: Beginner

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

Deriving the dual (hard margin)

Primal: minimise ½‖w‖² subject to yᵢ(w·xᵢ + b) ≥ 1.

1. Lagrangian. Introduce a multiplier αᵢ ≥ 0 for each constraint: L = ½‖w‖² − Σαᵢ[yᵢ(w·xᵢ + b) − 1].

2. Minimise over w and b:

  • ∂L/∂w = 0 gives w = Σαᵢyᵢxᵢ;
  • ∂L/∂b = 0 gives Σαᵢyᵢ = 0.

3. Substitute back: L = Σαᵢ − ½ΣᵢΣⱼ αᵢαⱼyᵢyⱼ xᵢ·xⱼ.

4. Dual problem: maximise this over αᵢ ≥ 0 with Σαᵢyᵢ = 0. For the soft margin, the slacks add the upper bound αᵢ ≤ C.

Check on the 6-point example (positives (1, 1), (2, 2), (2, 0); negatives (−1, −1), (−2, 0), (0, −2)). The dual solution has α = 0 for (2, 2) and positive weights for the other five, and w = Σαᵢyᵢxᵢ = (0.5, 0.5), exactly the primal answer.

The steps in plain English:

  1. Write one multiplier per constraint (per training point).
  2. Setting the derivatives to zero expresses w as a weighted sum of training points.
  3. Substitute to get a problem in the multipliers alone, involving only dot products of points.
  4. Solve it (QP, SMO or coordinate ascent). Most αᵢ end at 0.
  5. Recover w = Σαᵢyᵢxᵢ, and b from any margin support vector, where yᵢ(w·xᵢ + b) = 1.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Setting ∂L/∂w = 0 gives…
  2. 2. Setting ∂L/∂b = 0 gives…
  3. 3. The dual depends on the data only through…

Level 2: Intermediate

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

The soft-margin dual and KKT

Soft-margin dual: maximise Σαᵢ − ½ΣᵢΣⱼ αᵢαⱼyᵢyⱼ xᵢ·xⱼ subject to 0 ≤ αᵢ ≤ C and Σαᵢyᵢ = 0. It is a concave quadratic program with n variables.

KKT conditions (necessary and sufficient here, by convexity and Slater’s condition):

  1. Stationarity: w = Σαᵢyᵢxᵢ, Σαᵢyᵢ = 0, and αᵢ + μᵢ = C (μᵢ is the multiplier of ξᵢ ≥ 0).
  2. Primal feasibility: yᵢfᵢ ≥ 1 − ξᵢ and ξᵢ ≥ 0, where fᵢ = w·xᵢ + b.
  3. Dual feasibility: αᵢ ≥ 0 and μᵢ ≥ 0.
  4. Complementary slackness: αᵢ(yᵢfᵢ − 1 + ξᵢ) = 0 and μᵢξᵢ = 0.

Consequences:

αᵢ Where the point is
0 outside the margin (yᵢfᵢ ≥ 1)
strictly between 0 and C exactly on the margin (yᵢfᵢ = 1)
C inside the margin or misclassified (ξᵢ > 0)

Bias. For any margin support vector, b = yᵢ − w·xᵢ. Average over all of them for numerical stability.

Strong duality. The primal and dual optimal values are equal. The duality gap, P(w, b) − D(α) ≥ 0, is a certificate of optimality and a stopping criterion.

from sklearn.svm import SVC
clf = SVC(kernel="linear", C=1.0).fit(X, y)
clf.dual_coef_          # αᵢyᵢ for the support vectors
clf.support_vectors_    # the xᵢ with αᵢ > 0
w = clf.dual_coef_ @ clf.support_vectors_    # = clf.coef_

Use the dual when you need kernels, when n is small relative to p, or to inspect support vectors. Use the primal (or linear solvers) when n ≫ p.

🐍 Exercise 1: solving and checking the dual
Solve the soft-margin SVM dual (with the equality constraint) by SLSQP, recover w and b, compute the primal and dual objectives and count KKT violations; verify that the dual reproduces the hard-margin primal on the hand example and that strong duality and the KKT conditions hold for C = 0.1, 1 and 10.
📝 Quick check (Level 2)
0/3 answered
  1. 1. A point with α strictly between 0 and C lies…
  2. 2. The duality gap at the optimum is…
  3. 3. SVC.dual_coef_ stores…

Level 3: Advanced

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

Why duality matters

Strong duality. The SVM primal is convex with affine constraints, so Slater’s condition holds (trivially for the soft margin, which is always feasible). Hence min primal = max dual, and the KKT conditions are necessary and sufficient. Any (w, b, α) satisfying them is optimal.

The kernel trick. Both the dual and the decision function f(x) = Σαᵢyᵢ xᵢ·x + b use the data only through inner products. Replacing xᵢ·xⱼ with a kernel k(xᵢ, xⱼ) = φ(xᵢ)·φ(xⱼ) trains a linear SVM in a feature space φ that may be infinite-dimensional, without ever computing φ (D5.49).

Cost:

  • the primal has p + 1 (+ n slack) variables;
  • the dual has n variables and needs the n × n Gram matrix (O(n²) memory).

With generic solvers, a tall problem (n = 200, p = 4) took about 600 times longer in the dual. A wide one (n = 40, p = 1000) was about twice as fast in the dual (exercise 2). Specialised solvers (SMO, D5.50; dual coordinate descent, D5.47) exploit sparsity in α.

Representer theorem. The optimal w lies in the span of the training points. This holds for any regularised loss with an ‖w‖² penalty, which is why kernelised methods (kernel ridge regression, D5.54) also work.

Interview questions.

  • Why does the dual have the constraint Σαᵢyᵢ = 0? It comes from ∂L/∂b = 0. Without a free bias (bias absorbed into w), it disappears.
  • How do you get b from the dual? Average yᵢ − w·xᵢ over the support vectors with α strictly between 0 and C.
📝 Quick check (Level 3)
0/3 answered
  1. 1. The kernel trick is possible because the dual uses…
  2. 2. The representer theorem says w lies in…
  3. 3. Without a bias term, the dual loses…

Level 4: Expert

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

Primal or dual: which is cheaper?

History.

  • Lagrange multipliers (1788) and the KKT conditions (Karush, 1939; Kuhn and Tucker, 1951) underlie the derivation.
  • Boser, Guyon and Vapnik (1992) used the dual to introduce kernel SVMs.
  • Osuna, Freund and Girosi (1997) introduced decomposition methods.
  • Platt (1998) introduced SMO.
  • Chapelle (2007) argued for training in the primal when n is large.
  • Linear solvers (liblinear, Pegasos) choose primal or dual by problem shape.

Exercise 1. On the hand example (C = 100) the dual QP gives w = (0.5, 0.5) and b = 0, with α = 0 for the non-support point (2, 2). On 80 noisy points, the primal and dual objectives agree to within 10⁻³ for C = 0.1, 1 and 10, with zero KKT violations.

Exercise 2: same SVM, two formulations, one generic solver (SLSQP). Timings vary by machine; these are from a run of the reference solution.

Problem Primal variables Primal time Dual variables Dual time Support vectors
Wide: 40 spectra × 1000 bins 1001 17.6 ms 40 8.5 ms 40
Tall: 200 points × 4 features 5 0.5 ms 200 300 ms 5
  • Both formulations give the same w, to within numerical precision.
  • Wide data favour the dual (40 variables instead of 1001). With p > n, every point becomes a support vector, as expected.
  • Tall data overwhelmingly favour the primal, about 600 times faster here. The dual must handle a 200 × 200 Gram matrix to find just 5 support vectors.
  • The dual’s real payoff is not speed but kernels: it needs only inner products, as the next page shows.
🐍 Exercise 2 (research): primal versus dual cost
Solve the same separable linear SVM in the primal (p + 1 variables) and the dual (n variables) with SLSQP for a wide problem (40 × 1000) and a tall problem (200 × 4); compare variables, wall time, support vectors and the recovered weights.

Duality in AI, robotics and aerospace

  • Kernel methods: non-linear SVMs, kernel ridge regression and Gaussian processes rely on dual or kernel forms.
  • Support-vector inspection: the αᵢ highlight the critical training cases.
  • Constrained trajectory optimisation: KKT conditions and duals in model-predictive control.
  • Certificates: the duality gap as a provable optimality bound in embedded solvers.

References

  1. W. Karush (1939). “Minima of functions of several variables with inequalities as side conditions.” MSc thesis, University of Chicago.
  2. H. W. Kuhn, A. W. Tucker (1951). “Nonlinear programming.” Proceedings of the Second Berkeley Symposium.
  3. B. E. Boser, I. M. Guyon, V. N. Vapnik (1992). “A training algorithm for optimal margin classifiers.” COLT.
  4. O. Chapelle (2007). “Training a support vector machine in the primal.” Neural Computation 19(5).
  5. S. Boyd, L. Vandenberghe (2004). Convex Optimization, chapter 5. Cambridge University Press.