Course contents 12 / 27

12 / 27 · Linear Classification

Support vector machines

Among the many lines that separate two classes, the one with the widest margin: the hard-margin problem, slack variables and the soft margin as hinge loss plus an L2 penalty, the Lagrangian, the KKT conditions and the dual derived step by step, what the dual variables say about each training point, how C trades margin for errors, and why a wide margin generalizes.

On this page 16

The linear classifiers note replaced the 0–1 loss by convex surrogates, and the classification metrics note measured how the resulting classifiers err. One question was left open. When the two classes can be separated by a hyperplane, there are infinitely many separating hyperplanes, all with zero training error, and the training loss alone does not say which one to take. Logistic regression answers with probabilities; the support vector machine answers with geometry: take the hyperplane that stays as far as possible from the nearest training points (Boser, Guyon and Vapnik, 1992; Cortes and Vapnik, 1995). This note derives that rule, extends it to classes that overlap, and works out its dual problem, which shows that the solution depends on a few training points only and on nothing but inner products between them. The second fact is what the next note builds on.

Notation. Training points x1,…,xn∈Rdx_1, \dots, x_n \in \mathbb R^d with labels yi∈{−1,+1}y_i \in \{-1, +1\}. A linear classifier predicts sign⁡(⟨w,x⟩+b)\operatorname{sign}(\langle w, x\rangle + b) with weights w∈Rdw \in \mathbb R^d and bias bb; its score is f(x)=⟨w,x⟩+bf(x) = \langle w, x\rangle + b. As in the linear classifiers note, the margin of an example is Mi=yif(xi)M_i = y_i f(x_i): positive when xix_i is classified correctly. The margin of the hyperplane, γ\gamma, is the distance from the hyperplane to the nearest training point. ∥w∥\lVert w \rVert is the Euclidean norm. Dual variables are αi\alpha_i and μi\mu_i.

Which separating line?#

Figure

The maximum-margin line and its support vectors

The maximum-margin band between two classesBlue circles of class +1 at the top right, red squares of class −1 at the bottom left. A shaded diagonal band with dashed edges and a solid centre line runs between them. Three ringed points lie on the edges of the band. Two dotted lines also separate the classes but pass close to the points.−10+1⟨w, x⟩ + b =2/‖w‖ = 1.63● class +1■ class −1solid: the maximum-margin linedashed: ⟨w, x⟩ + b = ±1, the band edgesdotted: two other separating lines,margins 0.35 and 0.27w = (0.87, 0.87), b = 0.39margin 1/‖w‖ = 0.81 on each sideringed: the 3 support vectors,α = 0.63, 0.13, 0.76the other 17 points have α = 0The maximum-margin band between two classesBlue circles of class +1 at the top right, red squares of class −1 at the bottom left. A shaded diagonal band with dashed edges and a solid centre line runs between them. Three ringed points lie on the edges of the band. Two dotted lines also separate the classes but pass close to the points.−10+1⟨w, x⟩ + b =2/‖w‖ = 1.63● class +1■ class −1solid: the maximum-margin linedashed: ⟨w, x⟩ + b = ±1, the band edgesdotted: two other separating lines,margins 0.35 and 0.27w = (0.87, 0.87), b = 0.39margin 1/‖w‖ = 0.81 on each sideringed: the 3 support vectors,α = 0.63, 0.13, 0.76the other 17 points have α = 0
Twenty linearly separable points, 10 per class. All three lines separate the classes without error, but the dotted ones come within 0.35 and 0.27 of the nearest point. The hard-margin SVM picks the line whose nearest point is as far as possible: 0.81 on each side, a band of width 2/‖w‖ = 1.63. Three points on the edges of the band, the support vectors with α > 0, determine the solution; moving or deleting any other point without entering the band leaves the line unchanged. Both axes have the same scale.

All three lines in the figure classify the twenty training points correctly. The dotted ones pass within 0.350.35 and 0.270.27 of a training point; a new point drawn from the same clouds, slightly displaced from its neighbours, can easily land on the wrong side. The solid line keeps a distance of 0.810.81 from every training point. The idea of the SVM is to make this distance, the margin of the hyperplane, as large as possible. Why a wide margin generalizes gives the theoretical reason; the intuition is that a classifier whose decision does not change when every training point is moved a little is less likely to be fitting accidents of the sample.

The distance from a point xx to the hyperplane ⟨w,x⟩+b=0\langle w, x\rangle + b = 0 is

dist⁡(x)=∣⟨w,x⟩+b∣∥w∥:\operatorname{dist}(x) = \frac{\lvert \langle w, x\rangle + b \rvert}{\lVert w \rVert} :

for any x0x_0 on the hyperplane, the distance is the length of the projection of x−x0x - x_0 on the unit normal w/∥w∥w / \lVert w \rVert, which is ∣⟨w,x−x0⟩∣/∥w∥\lvert\langle w, x - x_0\rangle\rvert / \lVert w\rVert, and ⟨w,x0⟩=−b\langle w, x_0\rangle = -b. For a hyperplane that classifies every point correctly, ∣f(xi)∣=Mi\lvert f(x_i)\rvert = M_i, so its margin is γ=min⁡iMi/∥w∥\gamma = \min_i M_i / \lVert w\rVert.

The hard-margin problem#

Multiplying ww and bb by the same positive number changes neither the classifier nor γ\gamma, so one scale can be fixed for free. The SVM fixes it so that the nearest points have Mi=1M_i = 1: all points satisfy Mi≥1M_i \ge 1, with equality for the nearest ones. Then γ=1/∥w∥\gamma = 1/\lVert w\rVert, the band between the hyperplanes f(x)=−1f(x) = -1 and f(x)=+1f(x) = +1 contains no training point and has width 2/∥w∥2/\lVert w\rVert, and maximizing the margin is minimizing ∥w∥\lVert w\rVert. Squaring and halving make the objective smooth:

min⁡w, b 12∥w∥2subject to  yi(⟨w,xi⟩+b)≥1,i=1,…,n.\begin{aligned} &\min_{w,\, b}\ \tfrac12 \lVert w \rVert^2\\ &\text{subject to}\ \ y_i(\langle w, x_i\rangle + b) \ge 1, \quad i = 1, \dots, n . \end{aligned}

This is the hard-margin SVM. The objective is strictly convex in ww and the constraints are linear, so when the data are separable the optimal ww is unique, and the problem is a quadratic program that standard solvers handle.

Worked example. Four points: A=(0,0)A = (0, 0) and B=(2,0)B = (2, 0) with y=−1y = -1, C=(1,2)C = (1, 2) and D=(3,3)D = (3, 3) with y=+1y = +1. The negative points span the segment x2=0x_2 = 0, 0≤x1≤20 \le x_1 \le 2, and the closest positive point to it is CC, at distance 2. No separating line can be more than half of that, 1, from both classes, and the horizontal line x2=1x_2 = 1 achieves it. In the scaling of the SVM it is w=(0,1)w = (0, 1), b=−1b = -1: MA=MB=MC=1M_A = M_B = M_C = 1, MD=2M_D = 2, γ=1/∥w∥=1\gamma = 1/\lVert w\rVert = 1, and 12∥w∥2=0.5\tfrac12\lVert w\rVert^2 = 0.5. Three points touch the band; DD does not. The dual will recover the same answer from the other side.

When the classes overlap: slack variables#

Real classes overlap, and then no ww satisfies all the constraints Mi≥1M_i \ge 1. The soft-margin SVM (Cortes and Vapnik, 1995) lets each point violate its constraint by a slack ξi≥0\xi_i \ge 0 and pays CC per unit of slack:

min⁡w, b, ξ 12∥w∥2+C∑i=1nξisubject to  yi(⟨w,xi⟩+b)≥1−ξi,  ξi≥0.\begin{aligned} &\min_{w,\, b,\, \xi}\ \tfrac12 \lVert w \rVert^2 + C \sum_{i=1}^n \xi_i\\ &\text{subject to}\ \ y_i(\langle w, x_i\rangle + b) \ge 1 - \xi_i,\ \ \xi_i \ge 0 . \end{aligned}

A point with ξi=0\xi_i = 0 is on the correct side of its edge of the band; 0<ξi<10 \lt \xi_i \lt 1 means inside the band but still correctly classified; ξi=1\xi_i = 1 puts the point on the hyperplane; ξi>1\xi_i \gt 1 means on the wrong side of it. Each misclassified point contributes more than 1 to ∑iξi\sum_i \xi_i, so the total slack bounds the number of training errors from above. The constant C>0C \gt 0 sets the exchange rate between a wide band and few violations: a large CC makes violations expensive, a small CC accepts many violations to keep ∥w∥\lVert w\rVert small. On separable data the hard margin is the limit C→∞C \to \infty. On overlapping data it is not a limit of anything: the hard-margin constraints have no feasible point, and a larger CC only makes the unavoidable slack more expensive.

The soft margin is hinge loss plus L2#

For fixed ww and bb the best slack is the smallest one allowed: ξi≥1−Mi\xi_i \ge 1 - M_i and ξi≥0\xi_i \ge 0 give ξi=max⁡(0,1−Mi)\xi_i = \max(0, 1 - M_i). Substituting it removes the constraints:

min⁡w, b  C∑i=1nmax⁡(0, 1−yi(⟨w,xi⟩+b))+12∥w∥2.\min_{w,\, b}\ \ C \sum_{i=1}^n \max\big(0,\ 1 - y_i(\langle w, x_i\rangle + b)\big) + \tfrac12\lVert w\rVert^2 .

The first term is the hinge loss of the linear classifiers note, summed over the training set; the second is the ridge penalty of the regularization note. Dividing by CnCn gives the familiar form “average loss plus λ∥w∥2\lambda \lVert w\rVert^2” with λ=1/(2Cn)\lambda = 1/(2Cn): a large CC is weak regularization. So the SVM is not a new kind of model but a linear classifier trained with a particular surrogate loss and an L2 penalty. The geometry explains why this particular pair is natural: the penalty measures the inverse width of the band, and the hinge charges only the points that enter it.

The probabilistic reading of the two terms differs. The penalty is the negative log of a Gaussian prior on ww, as in MAP estimation. The hinge, however, is not a negative log-likelihood. A conditional model p(y∣x)∝e−Cmax⁡(0, 1−yf(x))p(y \mid x) \propto e^{-C \max(0,\, 1 - y f(x))} would need e−Cmax⁡(0, 1−f)+e−Cmax⁡(0, 1+f)e^{-C\max(0,\,1 - f)} + e^{-C\max(0,\,1 + f)} to be the same for every score ff, and it is not: 2e−C2e^{-C} at f=0f = 0 against 1+e−2C1 + e^{-2C} at f=1f = 1. For the logistic loss the same sum is exactly 1, which is why logistic regression outputs probabilities and the SVM does not.

What the hinge does estimate follows from the recipe of other losses, other targets: minimize the expected loss at each xx separately. With η=P(Y=1∣X=x)\eta = P(Y = 1 \mid X = x) the expected hinge loss of a score ff is ηmax⁡(0,1−f)+(1−η)max⁡(0,1+f)\eta \max(0, 1 - f) + (1 - \eta)\max(0, 1 + f). On [−1,1][-1, 1] it equals 1+(1−2η)f1 + (1 - 2\eta) f, a straight line, and outside that interval it only grows. So the minimizer is f∗=+1f^* = +1 when η>1/2\eta \gt 1/2 and f∗=−1f^* = -1 when η<1/2\eta \lt 1/2: the sign of the Bayes classifier and nothing more (Lin, 2002), the hinge row of the table in what each surrogate estimates. With η=0.7\eta = 0.7 and η=0.95\eta = 0.95 the hinge asks for the same score +1+1, while the logistic loss asks for the log-odds 0.850.85 and 2.942.94. The SVM targets the right decision and discards the confidence; when probabilities are needed, they are fitted afterwards on held-out scores, as in the calibration section of the logistic regression note (Platt, 1999).

The dual problem#

The constrained form is where the structure of the solution shows. The tool is Lagrange duality (Boyd and Vandenberghe, 2004, Chapter 5). Write each constraint as “something ≥0\ge 0”: Mi−1+ξi≥0M_i - 1 + \xi_i \ge 0 and ξi≥0\xi_i \ge 0. Give each a multiplier, αi≥0\alpha_i \ge 0 and μi≥0\mu_i \ge 0, and subtract them from the objective:

L(w,b,ξ,α,μ)=12∥w∥2+C∑iξi−∑iαi(yi(⟨w,xi⟩+b)−1+ξi)−∑iμiξi.\begin{aligned} L(w, b, \xi, \alpha, \mu) = {}&\tfrac12 \lVert w \rVert^2 + C \sum_i \xi_i\\ &- \sum_i \alpha_i \big(y_i(\langle w, x_i\rangle + b) - 1 + \xi_i\big) - \sum_i \mu_i \xi_i . \end{aligned}

Step 1: weak duality. At any feasible (w,b,ξ)(w, b, \xi) both subtracted sums are non-negative, so LL is at most the objective. Hence the dual function g(α,μ)=min⁡w,b,ξLg(\alpha, \mu) = \min_{w, b, \xi} L is a lower bound on the optimal value p∗p^* of the SVM problem, for every α,μ≥0\alpha, \mu \ge 0. The dual problem asks for the best lower bound, d∗=max⁡α,μ≥0g(α,μ)≤p∗d^* = \max_{\alpha, \mu \ge 0} g(\alpha, \mu) \le p^*.

Step 2: strong duality. The objective is convex and every constraint is affine, and the problem is feasible (w=0w = 0, b=0b = 0, ξi=1\xi_i = 1 satisfies all constraints). For such problems the refined Slater condition holds and gives d∗=p∗d^* = p^*, with the dual optimum attained (Boyd and Vandenberghe, 2004, §5.2.3). Solving the dual therefore solves the SVM.

Step 3: minimize LL over the primal variables. LL is a convex quadratic in ww and linear in bb and in each ξi\xi_i. A linear function has a finite minimum only if its slope is zero; otherwise g=−∞g = -\infty, which is useless as a bound. Setting the derivatives to zero:

∂L∂w=w−∑iαiyixi=0⟹ w=∑iαiyixi,∂L∂b=−∑iαiyi=0⟹ ∑iαiyi=0,∂L∂ξi=C−αi−μi=0⟹ μi=C−αi.\begin{aligned} \frac{\partial L}{\partial w} &= w - \sum_i \alpha_i y_i x_i = 0 &&\Longrightarrow\ w = \sum_i \alpha_i y_i x_i ,\\ \frac{\partial L}{\partial b} &= -\sum_i \alpha_i y_i = 0 &&\Longrightarrow\ \sum_i \alpha_i y_i = 0 ,\\ \frac{\partial L}{\partial \xi_i} &= C - \alpha_i - \mu_i = 0 &&\Longrightarrow\ \mu_i = C - \alpha_i . \end{aligned}

Step 4: substitute. With ∑iαiyi=0\sum_i \alpha_i y_i = 0 the term in bb vanishes, with C−αi−μi=0C - \alpha_i - \mu_i = 0 the terms in ξi\xi_i vanish, and ∑iαiyi⟨w,xi⟩=⟨w,w⟩\sum_i \alpha_i y_i \langle w, x_i\rangle = \langle w, w\rangle by the first condition:

g(α)=12∥w∥2−∥w∥2+∑iαi=∑iαi−12∑i,jαiαjyiyj⟨xi,xj⟩.g(\alpha) = \tfrac12\lVert w\rVert^2 - \lVert w\rVert^2 + \sum_i \alpha_i = \sum_{i} \alpha_i - \tfrac12 \sum_{i, j} \alpha_i \alpha_j y_i y_j \langle x_i, x_j \rangle .

Step 5: collect the constraints. μi=C−αi≥0\mu_i = C - \alpha_i \ge 0 and αi≥0\alpha_i \ge 0 together say 0≤αi≤C0 \le \alpha_i \le C. The dual of the soft-margin SVM is

max⁡α ∑iαi−12∑i,jαiαjyiyj⟨xi,xj⟩subject to  0≤αi≤C,∑iαiyi=0.\begin{aligned} &\max_{\alpha}\ \sum_{i} \alpha_i - \tfrac12 \sum_{i, j} \alpha_i \alpha_j y_i y_j \langle x_i, x_j \rangle\\ &\text{subject to}\ \ 0 \le \alpha_i \le C,\quad \sum_i \alpha_i y_i = 0 . \end{aligned}

It is a concave quadratic program in nn variables: the matrix (yiyj⟨xi,xj⟩)(y_i y_j \langle x_i, x_j\rangle) is the Gram matrix of the vectors yixiy_i x_i and therefore positive semidefinite. The hard margin gives the same dual without the upper bound CC. Two features matter for what follows. The data enter only through the inner products ⟨xi,xj⟩\langle x_i, x_j\rangle. And the solution is read off as w=∑iαiyixiw = \sum_i \alpha_i y_i x_i, so the classifier is

f(x)=∑iαiyi⟨xi,x⟩+b,f(x) = \sum_{i} \alpha_i y_i \langle x_i, x\rangle + b ,

again a function of inner products only.

The KKT conditions#

Strong duality ties the primal and dual solutions together. At the optimum, p∗=d∗=g(α)≤L(w,b,ξ,α,μ)≤p∗p^* = d^* = g(\alpha) \le L(w, b, \xi, \alpha, \mu) \le p^*, so both inequalities are equalities; the second one forces each subtracted product to be zero. Together with the stationarity of Step 3 and the constraints, these are the Karush–Kuhn–Tucker conditions (Boyd and Vandenberghe, 2004, §5.5.3), necessary and, for a convex problem, sufficient for optimality:

  • stationarity: w=∑iαiyixiw = \sum_i \alpha_i y_i x_i,  ∑iαiyi=0\ \sum_i \alpha_i y_i = 0,  αi+μi=C\ \alpha_i + \mu_i = C;
  • primal feasibility: Mi≥1−ξiM_i \ge 1 - \xi_i,  ξi≥0\ \xi_i \ge 0;
  • dual feasibility: αi≥0\alpha_i \ge 0,  μi≥0\ \mu_i \ge 0;
  • complementary slackness: αi(Mi−1+ξi)=0\alpha_i (M_i - 1 + \xi_i) = 0,  μiξi=0\ \mu_i \xi_i = 0.

Complementary slackness says that a multiplier can be non-zero only where its constraint is tight.

Worked example, continued. For the four points, DD has MD=2>1M_D = 2 \gt 1, so αD=0\alpha_D = 0. Stationarity gives w=−αA(0,0)−αB(2,0)+αC(1,2)=(αC−2αB, 2αC)w = -\alpha_A (0, 0) - \alpha_B (2, 0) + \alpha_C (1, 2) = (\alpha_C - 2\alpha_B,\ 2\alpha_C), and w=(0,1)w = (0, 1) requires αC=12\alpha_C = \tfrac12, αB=14\alpha_B = \tfrac14. Then ∑iαiyi=0\sum_i \alpha_i y_i = 0 gives αA=αC−αB=14\alpha_A = \alpha_C - \alpha_B = \tfrac14. All multipliers are non-negative, so the KKT conditions hold and w=(0,1)w = (0, 1), b=−1b = -1 is optimal. The dual objective is ∑iαi−12∥w∥2=1−0.5=0.5\sum_i \alpha_i - \tfrac12\lVert w\rVert^2 = 1 - 0.5 = 0.5, equal to the primal 12∥w∥2\tfrac12\lVert w\rVert^2. Summing complementary slackness αi(Mi−1)=0\alpha_i(M_i - 1) = 0 over the points gives ∑iαi=∥w∥2\sum_i \alpha_i = \lVert w\rVert^2 for any hard-margin solution, so the margin can be read from the dual alone: γ=1/∑iαi\gamma = 1/\sqrt{\sum_i \alpha_i}. In the first figure ∑iαi=1.5123\sum_i \alpha_i = 1.5123 and γ=0.81\gamma = 0.81.

Support vectors: what the dual variables tell you#

Figure

The soft margin: what the dual variables say

Support vectors of a soft-margin SVMBlue circles and red squares partly overlap. A shaded band with a solid centre line runs through them. Three points on the edges of the band are ringed; three points inside the band or beyond it are ringed in amber, each with a dashed amber segment to its own edge of the band.−10+1⟨w, x⟩ + b =ξ = 1.54ξ = 0.92ξ = 0.66● class +1■ class −1C = 1: w = (1.26, 0.76), b = −0.27margin 1/‖w‖ = 0.68 on each sideplain rings: on the edge, ξ = 0,0 < α < C: α = 0.64, 0.68, 0.96amber rings: α = C = 1, slack ξ > 0(segments): 0.66 and 0.92 inside the band,1.54 on the wrong sidethe other 14 points: α = 0, ξ = 0½‖w‖² + CΣξ = 1.08 + 3.13 = 4.20Support vectors of a soft-margin SVMBlue circles and red squares partly overlap. A shaded band with a solid centre line runs through them. Three points on the edges of the band are ringed; three points inside the band or beyond it are ringed in amber, each with a dashed amber segment to its own edge of the band.−10+1⟨w, x⟩ + b =ξ = 1.54ξ = 0.92ξ = 0.66● class +1■ class −1C = 1: w = (1.26, 0.76), b = −0.27margin 1/‖w‖ = 0.68 on each sideplain rings: on the edge, ξ = 0,0 < α < C: α = 0.64, 0.68, 0.96amber rings: α = C = 1, slack ξ > 0(segments): 0.66 and 0.92 inside the band,1.54 on the wrong sidethe other 14 points: α = 0, ξ = 0½‖w‖² + CΣξ = 1.08 + 3.13 = 4.20
Twenty points that no line separates, and the soft-margin SVM with C = 1. The dual variables split the points into three groups. The 14 points outside the band have α = 0 and do not affect the solution. Three points lie exactly on the edges of the band (0 < α < C), and b is computed from them. Three more have α = C and slack ξ = 1 − y(⟨w, x⟩ + b) > 0, the length of the amber segment to their own edge of the band in units of 1/‖w‖: two inside the band are still classified correctly (ξ = 0.66 and 0.92), one, with ξ = 1.54 > 1, is on the wrong side. The objective value 4.20 equals the dual one.

The KKT conditions sort the training points into three groups by their αi\alpha_i:

  • αi=0\alpha_i = 0. Then μi=C>0\mu_i = C \gt 0, so ξi=0\xi_i = 0 and Mi≥1M_i \ge 1: the point is on the correct side of its edge of the band. It does not appear in w=∑iαiyixiw = \sum_i \alpha_i y_i x_i.
  • 0<αi<C0 \lt \alpha_i \lt C. Then μi>0\mu_i \gt 0 forces ξi=0\xi_i = 0, and αi>0\alpha_i \gt 0 forces Mi=1−ξi=1M_i = 1 - \xi_i = 1: the point lies exactly on an edge of the band. These points give the bias: yi(⟨w,xi⟩+b)=1y_i(\langle w, x_i\rangle + b) = 1 and yi2=1y_i^2 = 1 yield b=yi−⟨w,xi⟩b = y_i - \langle w, x_i\rangle, averaged over all such points for numerical stability.
  • αi=C\alpha_i = C. Then μi=0\mu_i = 0, ξi\xi_i may be positive and Mi=1−ξi≤1M_i = 1 - \xi_i \le 1: the point is on the edge, inside the band, or on the wrong side.

Read backwards: a point strictly outside the band (Mi>1M_i \gt 1) must have αi=0\alpha_i = 0, and a point with positive slack must have αi=C\alpha_i = C. The points with αi>0\alpha_i \gt 0 are the support vectors; the solution is a weighted sum of them alone. In the figure 6 of 20 points are support vectors: three on the edges with α=0.64,0.68,0.96\alpha = 0.64, 0.68, 0.96, and three with α=C=1\alpha = C = 1 and slacks 0.660.66, 0.920.92 (inside the band) and 1.541.54 (misclassified).

Sparsity has two practical consequences. Deleting a point with αi=0\alpha_i = 0 and refitting leaves the KKT conditions satisfied by the same ww and bb, so the solution does not change; refitting the twenty separable points on their three support vectors alone gives the same line, as the Code section checks. And in leave-one-out validation every point with αi=0\alpha_i = 0 is classified correctly when held out, since the classifier trained without it is the same and puts it outside the band. The leave-one-out error is therefore at most the number of support vectors divided by nn, provided the solver returns the same solution when a point with αi=0\alpha_i = 0 is removed. This bounds the leave-one-out estimate, which, like cross-validation, describes the procedure on training sets of size n−1n - 1; few support vectors are a good sign, but not a finite-sample guarantee for the risk of the one deployed model.

How C trades margin for errors#

On the breast cancer data (569 tumours, 30 standardized features, 70% for training) the choice of CC moves the solution from a wide band with many violations to a narrow band that fits the training set exactly:

CCsupport vectorsof them with α=C\alpha = Cband width 2/∥w∥2/\lVert w\rVerttrain accuracytest accuracy
0.0011951896.380.9400.930
0.0192833.010.9800.959
0.144321.460.9920.959
127110.690.9920.953
102530.320.9950.953
1002510.091.0000.930

With small CC almost every support vector sits at the bound α=C\alpha = C: the band is wide and many points are inside it. As CC grows the band narrows, the training accuracy reaches 1, and the test accuracy falls back to 0.9300.930: large CC is weak regularization and overfits, exactly like a small ridge penalty. CC is a hyperparameter, chosen by cross-validation on a logarithmic grid. Because the band width is measured in the units of the features, the features must be standardized first; otherwise the feature with the largest scale dominates ∥w∥\lVert w\rVert.

Why a wide margin generalizes#

The first generalization bound of the probability course controlled the gap between training and true risk through the number of candidate models. Hyperplanes in Rd\mathbb R^d are infinitely many, and the classical replacement for ln⁡K\ln K grows with dd: in high dimension, separating the training set says little. Margins remove the dimension. Suppose every input has ∥x∥≤ρ\lVert x\rVert \le \rho and the distribution is separable with margin γ\gamma by a hyperplane through the origin. Then with probability at least 1−δ1 - \delta over a training set of size nn, the hard-margin SVM has 0–1 risk at most

4(ρ/γ)2n+2ln⁡(2/δ)n\sqrt{\frac{4 (\rho/\gamma)^2}{n}} + \sqrt{\frac{2 \ln (2/\delta)}{n}}

(Shalev-Shwartz and Ben-David, 2014, Theorem 15.4). The dimension dd does not appear; only the ratio of the data radius to the margin does. With ρ/γ=10\rho/\gamma = 10, n=10,000n = 10{,}000 and δ=0.05\delta = 0.05 the bound is 0.200+0.027=0.2270.200 + 0.027 = 0.227: loose, as such bounds are, but it holds in a million dimensions as well as in two. This is what makes it reasonable to map the data into a space of enormous dimension and separate it there with a wide margin, the programme of the next note.

Solving it in practice#

The dual has one equality constraint, so the smallest set of variables that can be changed while keeping ∑iαiyi=0\sum_i \alpha_i y_i = 0 is a pair. Sequential minimal optimization (Platt, 1998) repeatedly picks two multipliers, solves the two-variable problem in closed form and clips it to the box [0,C][0, C]. LIBSVM, the solver behind scikit-learn’s SVC, takes as the first multiplier the one that most violates the KKT conditions and as the second the one that, paired with it, gives the largest increase of the dual objective, as estimated from second-order information (LIBSVM minimizes the negative of the dual written above, so in its own terms this is the largest decrease) (Fan, Chen and Lin, 2005; Chang and Lin, 2011). The figures of this note are computed by the same method in a few dozen lines. For linear SVMs on large data, the unconstrained hinge-plus-L2 form is minimized directly by stochastic subgradient steps, as in Pegasos (Shalev-Shwartz et al., 2011), whose cost does not grow with nn for a fixed accuracy. A linear SVM on TF-IDF vectors was for years the standard strong baseline for text classification (Joachims, 1998), the setting of the bag-of-words note of the NLP course.

What changed#

Logistic regressionSupport vector machine
Which separating linethe maximum-likelihood onethe one with the widest margin
Losslogistic, smooth, never zerohinge, zero for Mi≥1M_i \ge 1
Training points that matterall, each with weight pi−[yi=1]p_i - [y_i = 1] in the gradientonly the support vectors, αi>0\alpha_i \gt 0
Outputa probability P(y=1∣x)P(y = 1 \mid x)a score whose sign is the class; probabilities need calibration
Optimizationunconstrained, smootha quadratic program, usually solved through its dual
Regularization knobλ\lambdaCC, with λ=1/(2Cn)\lambda = 1/(2Cn)
Data enter asfeature vectorsinner products ⟨xi,xj⟩\langle x_i, x_j\rangle in the dual

Limitations#

  • No probabilities. The hinge loss targets only the sign of P(y=1∣x)−1/2P(y = 1 \mid x) - 1/2; any probability has to be fitted afterwards, which needs held-out data.
  • Sensitive to scale and to CC. The margin is a Euclidean distance, so features must be standardized, and CC must be tuned; the table shows test accuracy moving by three points across the grid.
  • Outliers still pull. The hinge grows linearly, so a mislabelled point far on the wrong side becomes a support vector with αi=C\alpha_i = C and shifts the solution; the bound CC limits but does not remove its influence.
  • Linear. As written, the SVM separates with a hyperplane in the original features. Two concentric rings of points defeat it, whatever CC.
  • Two classes. Several classes need a reduction to binary problems or a joint formulation; the multiclass note compares them.

What you should remember#

  • The distance from xx to the hyperplane is ∣⟨w,x⟩+b∣/∥w∥\lvert\langle w, x\rangle + b\rvert / \lVert w\rVert; scaling so that the nearest points have Mi=1M_i = 1 makes the margin 1/∥w∥1/\lVert w\rVert and the band width 2/∥w∥2/\lVert w\rVert.
  • Hard margin: minimize 12∥w∥2\tfrac12\lVert w\rVert^2 subject to Mi≥1M_i \ge 1. Soft margin: add slacks ξi\xi_i at price CC, which is the same as minimizing C∑imax⁡(0,1−Mi)+12∥w∥2C\sum_i \max(0, 1 - M_i) + \tfrac12\lVert w\rVert^2, hinge loss plus an L2 penalty.
  • The dual is max⁡∑iαi−12∑i,jαiαjyiyj⟨xi,xj⟩\max \sum_i \alpha_i - \tfrac12\sum_{i,j}\alpha_i\alpha_j y_i y_j\langle x_i, x_j\rangle subject to 0≤αi≤C0 \le \alpha_i \le C, ∑iαiyi=0\sum_i \alpha_i y_i = 0; strong duality holds, and w=∑iαiyixiw = \sum_i \alpha_i y_i x_i.
  • KKT: αi=0\alpha_i = 0 outside the band, 0<αi<C0 \lt \alpha_i \lt C exactly on its edge (these give bb), αi=C\alpha_i = C inside it or misclassified. Only support vectors, αi>0\alpha_i \gt 0, shape the solution.
  • Large CC means weak regularization. The hinge estimates the Bayes decision, not probabilities. Margin bounds depend on ρ/γ\rho/\gamma, not on the dimension.

Code#

The worked example, the two figures, the dual solved by a generic optimizer, and the effect of CC:

import numpy as np
from scipy.optimize import minimize
from sklearn.svm import SVC
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

def fit(X, y, C):
    m = SVC(kernel='linear', C=C, tol=1e-8).fit(X, y)
    alpha = np.zeros(len(y)); alpha[m.support_] = np.abs(m.dual_coef_[0])   # dual_coef_ holds y_i * alpha_i
    return m.coef_[0], m.intercept_[0], alpha

# the worked example: four points, hard margin (a very large C)
X4 = np.array([[0, 0], [2, 0], [1, 2], [3, 3]]); y4 = np.array([-1, -1, 1, 1])
w, b, alpha = fit(X4, y4, 1e6)
print(w.round(3), round(b, 3), alpha.round(3))          # [0. 1.] -1.0 [0.25 0.25 0.5  0.  ]

# twenty separable points (the first figure); class -1 first
Xh = np.array([[-0.9, -0.7], [-1.7, -0.4], [-1.5, -1.9], [-0.6, -1], [-1.9, -0.2], [-2.6, -0.3], [-1.8, -0.7], [-1.7, -0.1], [-1.6, -0.4], [-1.5, -1.2],
               [2, 1.5], [0.7, 1.3], [0.4, 0.5], [1.5, -0.4], [0.1, 2.1], [2.8, 2], [0.4, 0.8], [1.5, 0.8], [2.5, 0.3], [0.3, 0.4]])
y = np.repeat([-1, 1], 10)
w, b, alpha = fit(Xh, y, 1e6)
sv = np.flatnonzero(alpha > 1e-6)
print(w.round(3), round(b, 3), round(2 / np.linalg.norm(w), 3), sv, alpha[sv].round(3))
# [0.87 0.87] 0.391 1.626 [ 0  3 19] [0.63  0.126 0.756]
print(round(alpha.sum(), 4), round(w @ w, 4))           # 1.5123 1.5123: at the hard-margin optimum, sum(alpha) = ||w||^2
w2, b2, _ = fit(Xh[sv], y[sv], 1e6)
print(w2.round(3), round(b2, 3))                        # [0.87 0.87] 0.391: the support vectors alone give the same line

# twenty overlapping points (the second figure), C = 1
Xs = np.array([[-0.7, -1.3], [-1.6, -0.5], [-0.9, -0.3], [-2.2, -1], [-1.5, 0.1], [-0.4, -0.3], [-0.9, -0.5], [-0.5, -1.3], [0.4, 0.4], [0.7, -0.9],
               [1.6, 0.7], [0.1, 1.5], [0.3, 0.3], [1.1, -0.1], [0.4, 1.4], [1.9, 1.2], [1.8, 0.3], [1.7, 1.8], [1.8, -1.3], [2.3, 0.2]])
w, b, alpha = fit(Xs, y, 1.0)
M = y * (Xs @ w + b); xi = np.maximum(0, 1 - M)
print(w.round(3), round(b, 3), np.flatnonzero((alpha > 1e-6) & (alpha < 1 - 1e-6)), np.flatnonzero(alpha > 1 - 1e-6), xi[alpha > 1 - 1e-6].round(2))
# [1.256 0.762] -0.269 [ 5 11 18] [ 8  9 12] [1.54 0.92 0.66]
print(round(0.5 * w @ w + xi.sum(), 4), round(alpha.sum() - 0.5 * w @ w, 4))   # 4.2044 4.2044: primal = dual

# the same dual solved by a generic optimizer: max sum(a) - 1/2 a^T Q a, 0 <= a <= C, sum(a y) = 0
Q = (y[:, None] * Xs) @ (y[:, None] * Xs).T
res = minimize(lambda a: 0.5 * a @ Q @ a - a.sum(), np.zeros(20), jac=lambda a: Q @ a - 1, method='SLSQP',
               bounds=[(0, 1)] * 20, constraints={'type': 'eq', 'fun': lambda a: a @ y}, options={'ftol': 1e-12, 'maxiter': 500})
a = res.x; w_dual = (a * y) @ Xs
free = (a > 1e-4) & (a < 1 - 1e-4)
print(w_dual.round(3), round(np.mean(y[free] - Xs[free] @ w_dual), 3))   # [1.256 0.762] -0.269

# the effect of C on breast cancer (569 tumours, 30 standardized features)
X, t = load_breast_cancer(return_X_y=True)
Xtr, Xte, ttr, tte = train_test_split(X, 2 * t - 1, test_size=0.3, random_state=0, stratify=t)
sc = StandardScaler().fit(Xtr); Xtr, Xte = sc.transform(Xtr), sc.transform(Xte)
for C in (0.001, 0.01, 0.1, 1, 10, 100):
    m = SVC(kernel='linear', C=C).fit(Xtr, ttr)
    at_C = int((np.abs(m.dual_coef_[0]) > C * (1 - 1e-6)).sum())
    print(C, len(m.support_), at_C, round(2 / np.linalg.norm(m.coef_[0]), 3), round(m.score(Xtr, ttr), 3), round(m.score(Xte, tte), 3))
# C, support vectors, of them with alpha = C, band width, train and test accuracy
# 0.001 195 189 6.381 0.94 0.93 | 0.01 92 83 3.014 0.98 0.959 | 0.1 44 32 1.459 0.992 0.959
# 1 27 11 0.686 0.992 0.953     | 10 25 3 0.321 0.995 0.953    | 100 25 1 0.089 1.0 0.93

Next#

The dual and the classifier touch the data only through inner products ⟨xi,xj⟩\langle x_i, x_j\rangle, and the margin bound does not care about the dimension. Both facts point the same way: replace xx by a richer representation ϕ(x)\phi(x), perhaps of enormous or infinite dimension, and never compute ϕ\phi at all, only ⟨ϕ(xi),ϕ(xj)⟩\langle \phi(x_i), \phi(x_j)\rangle. The next note shows which functions can play the role of that inner product and what they buy: curved decision boundaries from the same quadratic program.

References#