Support Vector Machine

On this page
Support vector machine graphical representation

Image credit: García-Gonzalo et al. (2016)

Several flat decision boundaries, called hyperplanes, may separate the same training data. A hard-margin support vector machine (SVM) chooses the one farthest from its nearest training point. When perfect separation is unavailable or undesirable, slack variables measure violations of the required margin, and the objective penalizes these violations. We then derive the dual problem to show how kernels allow nonlinear decision boundaries.

The derivation follows four steps:

  • Calculating the distance from a point to a hyperplane and defining the margin.
  • Formulating the hard-margin SVM and understanding how it finds the optimal separating hyperplane.
  • Extending to soft-margin SVM to handle misclassifications and margin violations.
  • Using the kernel trick to work with nonlinear boundaries through inner products, without explicitly computing the transformed feature vectors.

Model Specification

Let {(xi,yi)}i=1n\{(x_i, y_i)\}_{i=1}^n be our training set, with xi∈Rpx_i \in \mathbb{R}^p and yi∈{−1,+1}y_i \in \{-1, +1\}, and assume both classes occur. We define:

  • w∈Rpw \in \mathbb{R}^p as the normal vector,
  • b∈Rb \in \mathbb{R} as the bias,
  • for a separating hyperplane, the geometric margin γ\gamma as its minimum distance to a training point.

The sign of wTx+bw^T x + b determines the predicted class. If wTx+b>0w^T x + b > 0, we predict y=+1y=+1; otherwise, we predict y=−1y=-1. To choose the separating hyperplane, we first need a measure of distance from that hyperplane.

Margin and Perfect Separation

Background: Distance from a Point to a Hyperplane

In three-dimensional space, the perpendicular distance dd from a point (x1,y1,z1)(x_1, y_1, z_1) to a plane

Ax+By+Cz+D=0Ax + By + Cz + D = 0

is given by

d=∣Ax1+By1+Cz1+D∣A2+B2+C2.d = \frac{|Ax_1 + By_1 + Cz_1 + D|}{\sqrt{A^2 + B^2 + C^2}}.

The normal vector (A,B,C)(A,B,C) must be nonzero.

Generalizing this to Rp\mathbb{R}^p, consider the hyperplane

w⊤x+b=0,w^\top x + b = 0,

where w∈Rpw \in \mathbb{R}^p is a nonzero normal vector and b∈Rb \in \mathbb{R} is the bias term. For any point x0x_0, its perpendicular distance to this hyperplane is

∣w⊤x0+b∣∥w∥.\frac{|w^\top x_0 + b|}{\|w\|}.
Margin derivation

For a point xix_i with label yiy_i, its signed distance to the hyperplane is

yi(w⊤xi+b)∥w∥.\frac{y_i\bigl(w^\top x_i + b\bigr)}{\|w\|}.

Multiplying both the normal vector and the bias by a positive constant does not change the hyperplane. For separable data, we use this freedom to normalize the smallest functional margin to one. The constraints are then

yi(w⊤xi+b)≥1,i=1,…,n.y_i\bigl(w^\top x_i + b\bigr) \ge 1,\quad i=1,\dots,n.

with equality for at least one training point. Under this normalization, the geometric margin is

γ=min⁡iyi(w⊤xi+b)∥w∥=1∥w∥.\gamma = \min_{i} \frac{y_i\bigl(w^\top x_i + b\bigr)}{\|w\|} = \frac{1}{\|w\|}.

Thus, maximizing the margin reduces to minimizing ∥w∥\|w\| (or 12∥w∥2\tfrac12\|w\|^2 for mathematical convenience).

Hard-Margin SVM

If the training data are perfectly separable, we can write the constraints as

yi(w⊤xi+b)≥1,i=1,…,n.y_i\bigl(w^\top x_i + b\bigr) \ge 1,\quad i=1,\dots,n.

Then the hard-margin SVM optimization problem is

min⁡w,b  12∥w∥2subject toyi(w⊤xi+b)≥1,i=1,…,n.\min_{w,b} \; \frac12 \|w\|^2 \quad \text{subject to} \quad y_i\bigl(w^\top x_i + b\bigr) \ge 1,\quad i=1,\dots,n.

Derivation of the Hinge Loss

In practice, data are rarely perfectly separable. To accommodate misclassifications or margin violations, we introduce slack variables ξi≥0\xi_i \geq 0. The modified constraint is

yi(w⊤xi+b)≥1−ξi,i=1,…,n.y_i\bigl(w^\top x_i + b\bigr) \ge 1 - \xi_i,\quad i=1,\dots,n.

For fixed model parameters, the smallest feasible slack variable ξi\xi_i is zero when the unit-margin constraint is met and equals its shortfall otherwise. Because the soft-margin objective below penalizes slack with a positive coefficient, it chooses this smallest value:

ξi=max⁡{0, 1−yi(w⊤xi+b)}.\xi_i = \max\Bigl\{0,\,1 - y_i\bigl(w^\top x_i + b\bigr)\Bigr\}.

This expression is known as the hinge loss:

Lhinge(yi,f(xi))=max⁡{0, 1−yi(w⊤xi+b)}.L_{\text{hinge}}\bigl(y_i, f(x_i)\bigr) = \max\Bigl\{0,\,1 - y_i\bigl(w^\top x_i + b\bigr)\Bigr\}.

It is zero when yi(w⊤xi+b)≥1y_i(w^\top x_i + b) \geq 1 (i.e., when the data point is correctly classified with a sufficient margin) and increases linearly otherwise.

Soft-Margin SVM (Convex Optimization)

We penalize the slack variables to balance margin width against margin violations. This gives the constrained soft-margin SVM problem:

min⁡w,b,{ξi}12∥w∥2+C∑i=1nξisubject toξi≥0,yi(w⊤xi+b)≥1−ξi,i=1,…,n.\min_{w,b,\{\xi_i\}} \quad \frac12 \|w\|^2 + C \sum_{i=1}^n \xi_i \quad \text{subject to} \quad \xi_i \ge 0,\quad y_i\bigl(w^\top x_i + b\bigr) \ge 1 - \xi_i,\quad i=1,\dots,n.

where C>0C > 0 balances the squared-norm penalty against total margin violations. The slack penalty is not the number of misclassified points: correctly classified points inside the unit margin also incur a penalty.

The hinge-loss expression lets us eliminate the slack variables and write the penalty as C∑i=1nmax⁡{0, 1−yi(w⊤xi+b)}C \sum_{i=1}^{n} \max\{0,\, 1 - y_i(w^\top x_i + b)\}.
The equivalent unconstrained problem is therefore

min⁡w,b12∥w∥2+C∑i=1nmax⁡{0, 1−yi(w⊤xi+b)}.\min_{w,b} \quad \frac12 \|w\|^2 + C \sum_{i=1}^n \max\Bigl\{0,\,1 - y_i\bigl(w^\top x_i + b\bigr)\Bigr\}.

The constrained formulation is a convex quadratic program. The hinge-loss formulation is also convex, but its corners make it nondifferentiable at some points. Direct optimization therefore requires subgradients or another method that allows these corners.

The Kernel Trick: Handling Nonlinear Boundaries

Some datasets cannot be well separated by a hyperplane in the original feature space. To address this, we map data into a (possibly high-dimensional) feature space using a transformation ϕ\phi:

ϕ:Rp→H,x↦ϕ(x).\phi: \mathbb{R}^p \to \mathcal{H}, \quad x \mapsto \phi(x).

The map ϕ\phi takes an input xx from the original pp-dimensional space to a representation ϕ(x)\phi(x) in a Hilbert space H\mathcal{H}. In this notation:

  • Rp\mathbb{R}^p is the original input space.
  • H\mathcal{H} is a Hilbert space (often high-dimensional or even infinite-dimensional) in which the data may become linearly separable.
  • The arrow x↦ϕ(x)x \mapsto \phi(x) indicates that each data point xx is transformed into ϕ(x)\phi(x).

In the feature space H\mathcal{H}, the SVM decision boundary is

⟨w,ϕ(x)⟩H+b=0.\langle w,\phi(x)\rangle_{\mathcal H} + b = 0.

Directly computing ϕ(x)\phi(x) might be impractical if H\mathcal{H} is very large. Instead, we define a kernel function KK such that

K(x,x′)=⟨ϕ(x),ϕ(x′)⟩H.K(x, x') = \langle\phi(x),\phi(x')\rangle_{\mathcal H}.

This identity is useful only once the optimization depends on features through inner products. The dual derivation below gives that form, allowing kernel evaluations to replace explicit calculations with ϕ(x)\phi(x). In finite-dimensional feature spaces, the inner product can be written as K(x,x′)=ϕ(x)Tϕ(x′)K(x,x') = \phi(x)^T \phi(x'). More generally, a symmetric kernel is valid when every finite matrix of pairwise kernel values, called a Gram matrix, is positive semidefinite. This means that its quadratic form is nonnegative for every vector. Such a kernel has an associated reproducing kernel Hilbert space (RKHS): a Hilbert space of functions in which evaluating a function at a fixed point is a continuous operation.

Recap: The lower bound supplied by a Lagrangian

For a minimization problem with inequalities gi(u)≤0g_i(u)\le0, the duality note uses

q(λ)=inf⁡u{f(u)+∑iλigi(u)}≤f(ufeas),λi≥0.q(\lambda)=\inf_u\left\{f(u)+\sum_i\lambda_i g_i(u)\right\} \le f(u_{\mathrm{feas}}),\qquad \lambda_i\ge0.

The bound holds for every feasible ufeasu_{\mathrm{feas}}. Maximizing it gives the dual problem; below, any equality constraint on the multipliers arises from minimizing over a primal variable.

Primal and Dual Formulations of SVM

The primal problem optimizes the original decision variables subject to constraints. A general constrained minimization problem has the form

minimize f(x)subject to gi(x)≤0,i=1,…,m,hj(x)=0,j=1,…,p.\begin{aligned} \text{minimize } & f(x) \\ \text{subject to } & g_i(x) \le 0,\quad i = 1, \dots, m, \\ & h_j(x) = 0,\quad j = 1, \dots, p. \end{aligned}

To form the dual problem, we introduce Lagrange multipliers and take the infimum of the Lagrangian over the primal variables. The resulting dual objective gives a lower bound on the primal optimum. Under conditions such as convexity and Slater’s condition, strong duality holds: the primal and dual optimal values are equal. For the soft-margin SVM, the primal variables are the weight vector ww, bias bb, and slack variables ξ\xi, with objective

min⁡w,b,ξ12∥w∥2+C∑i=1nξi,\min_{w,b,\xi}\quad \frac{1}{2}\|w\|^2 + C\sum_{i=1}^{n}\xi_i,

subject to

1−ξi−yi(wTxi+b)≤0and−ξi≤0,i=1,…,n.1 - \xi_i - y_i(w^T x_i + b) \le 0 \quad \text{and} \quad -\xi_i \le 0,\quad i=1,\dots,n.

We assign Lagrange multipliers αi≥0\alpha_i \ge 0 to the margin constraints and βi≥0\beta_i \ge 0 to the nonnegativity constraints on the slack variables. The Lagrangian is

L(w,b,ξ,α,β)=12∥w∥2+C∑i=1nξi+∑i=1nαi(1−ξi−yi(wTxi+b))−∑i=1nβi ξi.\begin{aligned} L(w,b,\xi,\alpha,\beta) = & \frac{1}{2}\|w\|^2 + C\sum_{i=1}^{n}\xi_i \\ & + \sum_{i=1}^n \alpha_i \Bigl(1 - \xi_i - y_i(w^T x_i + b)\Bigr) - \sum_{i=1}^n \beta_i\,\xi_i. \end{aligned}

Differentiating the Lagrangian LL with respect to the primal variables gives the stationarity part of the Karush–Kuhn–Tucker (KKT) conditions. The full conditions also require primal feasibility, dual feasibility, and complementary slackness. We begin with stationarity:

Differentiating with respect to ww:

∂L∂w=w−∑i=1nαi yi xi=0⟹w=∑i=1nαiyixi.\frac{\partial L}{\partial w} = w - \sum_{i=1}^n \alpha_i\,y_i\,x_i = 0 \quad \Longrightarrow \quad w = \sum_{i=1}^{n} \alpha_i y_i x_i.

Differentiating with respect to bb:

∂L∂b=−∑i=1nαi yi=0⟹∑i=1nαiyi=0.\frac{\partial L}{\partial b} = - \sum_{i=1}^n \alpha_i\,y_i = 0 \quad \Longrightarrow \quad \sum_{i=1}^{n} \alpha_i y_i = 0.

Differentiating with respect to each ξi\xi_i:

∂L∂ξi=C−αi−βi=0⟹βi=C−αi.\frac{\partial L}{\partial \xi_i} = C - \alpha_i - \beta_i = 0 \quad \Longrightarrow \quad \beta_i = C - \alpha_i.

Since βi≥0\beta_i \ge 0, it follows that 0≤αi≤C0 \le \alpha_i \le C. The remaining KKT conditions, for every training point, are

ξi≥0,yi(wTxi+b)≥1−ξi,αi≥0,βi≥0,αi(1−ξi−yi(wTxi+b))=0,βiξi=0.\begin{aligned} \xi_i&\ge0, & y_i(w^T x_i+b)&\ge1-\xi_i,\\ \alpha_i&\ge0, & \beta_i&\ge0,\\ \alpha_i\bigl(1-\xi_i-y_i(w^T x_i+b)\bigr)&=0, & \beta_i\xi_i&=0. \end{aligned}

Convexity and strict feasibility make these KKT conditions an optimality characterization, rather than only a set of derivative equations. Substituting the stationarity relations into the Lagrangian removes the primal variables. The remaining feature dependence is through inner products, which can be replaced by kernel evaluations. Andrew Ng’s CS229 notes, Sections 5–8, give the duality and kernel arguments in detail.


Objective Function with a Kernel

Using the feature map ϕ\phi and its kernel K(x,x′)=⟨ϕ(x),ϕ(x′)⟩HK(x,x') = \langle\phi(x),\phi(x')\rangle_{\mathcal H}, the soft-margin primal problem becomes

min⁡w∈H, b∈R12∥w∥H2+C∑i=1nmax⁡{0, 1−yi(⟨w,ϕ(xi)⟩H+b)}.\min_{w\in\mathcal H,\,b\in\mathbb R}\quad \frac{1}{2}\|w\|_{\mathcal H}^2 + C\sum_{i=1}^{n}\max\{0,\,1-y_i(\langle w,\phi(x_i)\rangle_{\mathcal H}+b)\}.

The term ∥w∥H2\|w\|_{\mathcal H}^2 controls the margin width in feature space. Applying the same stationarity argument gives

w=∑i=1nαiyiϕ(xi).w = \sum_{i=1}^{n} \alpha_i y_i \phi(x_i).

Substituting this expression back into the norm term yields:

∥w∥H2=∑i,j=1nαiαjyiyj⟨ϕ(xi),ϕ(xj)⟩H=∑i,j=1nαiαjyiyjK(xi,xj).\begin{aligned} \|w\|_{\mathcal H}^2 &=\sum_{i,j=1}^{n}\alpha_i\alpha_j y_i y_j \langle\phi(x_i),\phi(x_j)\rangle_{\mathcal H}\\ &=\sum_{i,j=1}^{n}\alpha_i\alpha_j y_i y_j K(x_i,x_j). \end{aligned}

Thus, the kernelized dual formulation is:

max⁡α∑i=1nαi−12∑i,j=1nαiαjyiyjK(xi,xj),\max_{\alpha}\quad \sum_{i=1}^{n} \alpha_i - \frac{1}{2}\sum_{i,j=1}^{n} \alpha_i \alpha_j y_i y_j K(x_i,x_j),

subject to 0≤αi≤C0 \le \alpha_i \le C and ∑i=1nαiyi=0\sum_{i=1}^{n}\alpha_i y_i = 0. The L2L_2 penalty now depends only on kernel evaluations. We can therefore fit a nonlinear decision boundary without explicitly computing ϕ(x)\phi(x).

Examples of Common Kernels

  • Polynomial Kernel:
    K(x,x′)=(α x⊤x′+c)d,K(x, x') = \bigl(\alpha\, x^\top x' + c\bigr)^d,
    where α≥0\alpha\ge0, c≥0c\ge0, and dd is a positive integer. Here α\alpha is a kernel parameter, not one of the dual multipliers.
  • Radial Basis Function (RBF) Kernel:
    K(x,x′)=exp⁡(−γ ∥x−x′∥2),K(x, x') = \exp\Bigl(-\gamma\,\|x - x'\|^2\Bigr),
    with γ>0\gamma > 0 controlling the spread.
  • What happens:
    • In training, the dual formulation of the SVM uses only dot products, which are computed as K(xi,xj)K(x_i, x_j).
    • The final classifier depends solely on these kernel evaluations rather than on explicit high-dimensional feature vectors.
  • What does not happen:
    • We do not build or store the high-dimensional vectors ϕ(x)\phi(x).
    • We do not directly compute large dot products ϕ(xi)⋅ϕ(xj)\phi(x_i) \cdot \phi(x_j) in the transformed space.

Recap

  • Distance to Hyperplane:
    d=∣w⊤x+b∣∥w∥.d = \frac{|w^\top x + b|}{\|w\|}.
  • Margin:
    With yi(w⊤xi+b)≥1y_i\bigl(w^\top x_i + b\bigr) \ge 1 and equality for at least one point, the geometric margin is γ=1∥w∥\gamma = \frac{1}{\|w\|}.
  • Hard-Margin SVM:
    min⁡w,b  12∥w∥2s.t.yi(w⊤xi+b)≥1.\min_{w,b} \; \frac12 \|w\|^2 \quad \text{s.t.} \quad y_i\bigl(w^\top x_i + b\bigr) \ge 1.
  • Hinge Loss:
    max⁡{0, 1−yi(w⊤xi+b)}.\max\{0,\,1 - y_i\bigl(w^\top x_i + b\bigr)\}.
  • Soft-Margin SVM:
    min⁡w,b  12∥w∥2+C∑i=1nmax⁡{0, 1−yi(w⊤xi+b)}.\min_{w,b} \; \frac12 \|w\|^2 + C \sum_{i=1}^n \max\{0,\,1 - y_i\bigl(w^\top x_i + b\bigr)\}.
  • Kernel Trick:
    K(x,x′)=⟨ϕ(x),ϕ(x′)⟩H,K(x,x') = \langle\phi(x),\phi(x')\rangle_{\mathcal H},
    which allows us to implicitly map data to a high-dimensional space without explicitly computing ϕ(x)\phi(x).
  • Kernelized Dual Form:
    max⁡α∑i=1nαi−12∑i,j=1nαiαjyiyjK(xi,xj),\max_{\alpha}\quad \sum_{i=1}^{n} \alpha_i - \frac{1}{2}\sum_{i,j=1}^{n} \alpha_i \alpha_j y_i y_j K(x_i,x_j),
    subject to 0≤αi≤C0 \le \alpha_i \le C and ∑i=1nαiyi=0\sum_{i=1}^{n}\alpha_i y_i = 0.