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 be our training set, with xi∈Rp and yi∈{−1,+1}, and assume both classes occur. We define:
w∈Rp as the normal vector,
b∈R as the bias,
for a separating hyperplane, the geometric marginγ as its minimum distance to a training point.
The sign of wTx+b determines the predicted class. If wTx+b>0, we predict y=+1; otherwise, we predict y=−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 d from a point (x1,y1,z1) to a plane
Ax+By+Cz+D=0
Scroll to see the full equation →
is given by
d=A2+B2+C2∣Ax1+By1+Cz1+D∣.
Scroll to see the full equation →
The normal vector (A,B,C) must be nonzero.
Generalizing this to Rp, consider the hyperplane
w⊤x+b=0,
Scroll to see the full equation →
where w∈Rp is a nonzero normal vector and b∈R is the bias term. For any point x0, its perpendicular distance to this hyperplane is
∥w∥∣w⊤x0+b∣.
Scroll to see the full equation →
Margin derivation
For a point xi with label yi, its signed distance to the hyperplane is
∥w∥yi(w⊤xi+b).
Scroll to see the full equation →
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.
Scroll to see the full equation →
with equality for at least one training point. Under this normalization, the geometric margin is
γ=imin∥w∥yi(w⊤xi+b)=∥w∥1.
Scroll to see the full equation →
Thus, maximizing the margin reduces to minimizing ∥w∥ (or 21∥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.
Scroll to see the full equation →
Then the hard-margin SVM optimization problem is
w,bmin21∥w∥2subject toyi(w⊤xi+b)≥1,i=1,…,n.
Scroll to see the full equation →
Derivation of the Hinge Loss
In practice, data are rarely perfectly separable. To accommodate misclassifications or margin violations, we introduce slack variables ξi≥0. The modified constraint is
yi(w⊤xi+b)≥1−ξi,i=1,…,n.
Scroll to see the full equation →
For fixed model parameters, the smallest feasible slack variable ξ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)}.
Scroll to see the full equation →
This expression is known as the hinge loss:
Lhinge(yi,f(xi))=max{0,1−yi(w⊤xi+b)}.
Scroll to see the full equation →
It is zero when yi(w⊤xi+b)≥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:
where C>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)}.
The equivalent unconstrained problem is therefore
w,bmin21∥w∥2+Ci=1∑nmax{0,1−yi(w⊤xi+b)}.
Scroll to see the full equation →
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 ϕ:
ϕ:Rp→H,x↦ϕ(x).
Scroll to see the full equation →
The map ϕ takes an input x from the original p-dimensional space to a representation ϕ(x) in a Hilbert space H. In this notation:
Rp is the original input space.
H is a Hilbert space (often high-dimensional or even infinite-dimensional) in which the data may become linearly separable.
The arrow x↦ϕ(x) indicates that each data point x is transformed into ϕ(x).
In the feature space H, the SVM decision boundary is
⟨w,ϕ(x)⟩H+b=0.
Scroll to see the full equation →
Directly computing ϕ(x) might be impractical if H is very large. Instead, we define a kernel functionK such that
K(x,x′)=⟨ϕ(x),ϕ(x′)⟩H.
Scroll to see the full equation →
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).
In finite-dimensional feature spaces, the inner product can be written as K(x,x′)=ϕ(x)Tϕ(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)≤0, the duality note uses
q(λ)=uinf{f(u)+i∑λigi(u)}≤f(ufeas),λi≥0.
Scroll to see the full equation →
The bound holds for every feasible ufeas. 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 subject to f(x)gi(x)≤0,i=1,…,m,hj(x)=0,j=1,…,p.
Scroll to see the full equation →
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 w, bias b, and slack variables ξ, with objective
w,b,ξmin21∥w∥2+Ci=1∑nξi,
Scroll to see the full equation →
subject to
1−ξi−yi(wTxi+b)≤0and−ξi≤0,i=1,…,n.
Scroll to see the full equation →
We assign Lagrange multipliers αi≥0 to the margin constraints and βi≥0 to the nonnegativity constraints on the slack variables. The Lagrangian is
Differentiating the Lagrangian L 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 w:
∂w∂L=w−i=1∑nαiyixi=0⟹w=i=1∑nαiyixi.
Scroll to see the full equation →
Differentiating with respect to b:
∂b∂L=−i=1∑nαiyi=0⟹i=1∑nαiyi=0.
Scroll to see the full equation →
Differentiating with respect to each ξi:
∂ξi∂L=C−αi−βi=0⟹βi=C−αi.
Scroll to see the full equation →
Since βi≥0, it follows that 0≤αi≤C. The remaining KKT conditions, for every training point, are
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 ϕ and its kernel K(x,x′)=⟨ϕ(x),ϕ(x′)⟩H, the soft-margin primal problem becomes
subject to 0≤αi≤C and ∑i=1nαiyi=0. The L2 penalty now depends only on kernel evaluations. We can therefore fit a nonlinear decision boundary without explicitly computing ϕ(x).
Examples of Common Kernels
Polynomial Kernel:
K(x,x′)=(αx⊤x′+c)d,
Scroll to see the full equation →
where α≥0, c≥0, and d is a positive integer. Here α is a kernel parameter, not one of the dual multipliers.
Radial Basis Function (RBF) Kernel:
K(x,x′)=exp(−γ∥x−x′∥2),
Scroll to see the full equation →
with γ>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).
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).
We do not directly compute large dot products ϕ(xi)⋅ϕ(xj) in the transformed space.
Recap
Distance to Hyperplane:
d=∥w∥∣w⊤x+b∣.
Scroll to see the full equation →
Margin:
With yi(w⊤xi+b)≥1 and equality for at least one point, the geometric margin is γ=∥w∥1.
Hard-Margin SVM:
w,bmin21∥w∥2s.t.yi(w⊤xi+b)≥1.
Scroll to see the full equation →
Hinge Loss:
max{0,1−yi(w⊤xi+b)}.
Scroll to see the full equation →
Soft-Margin SVM:
w,bmin21∥w∥2+Ci=1∑nmax{0,1−yi(w⊤xi+b)}.
Scroll to see the full equation →
Kernel Trick:
K(x,x′)=⟨ϕ(x),ϕ(x′)⟩H,
Scroll to see the full equation →
which allows us to implicitly map data to a high-dimensional space without explicitly computing ϕ(x).