Module IV·Article II·~5 min read

SVM and Kernel Methods

Applications in Machine Learning

Reading tools · save, download, print, audio

Turn this article into a podcast

Pick voices, format, length — AI generates the audio

Idea: Maximize the Margin

Classification problem: given N points (xi,yi)(x_i, y_i), where xi∈Rnx_i \in \mathbb{R}^n and yi∈{−1,+1}y_i \in \{-1, +1\} are class labels. The goal is to find a hyperplane separating the two classes. There are infinitely many such hyperplanes—any separating one will do. SVM (Support Vector Machine) chooses the "best"—the one maximally distant from both classes. Maximum margin means maximum robustness to new data. The elegance of SVM lies in the fact that finding the optimal hyperplane is a convex quadratic programming problem.

Primal SVM Problem

Hyperplane: w⊤x+b=0w^\top x + b = 0. Class of a point xx: sign⁡(w⊤x+b)\operatorname{sign}(w^\top x + b).

Distance from point x0x_0 to the hyperplane: ∣w⊤x0+b∣/∥w∥|w^\top x_0 + b| / \|w\|.

Margin = 2/∥w∥2/\|w\| (distance between the two parallel hyperplanes w⊤x+b=±1w^\top x + b = \pm 1).

SVM Problem (hard margin): maximize the margin under correct classification:

max⁡2∥w∥subject to yi(w⊤xi+b)≥1 for all i\max \frac{2}{\|w\|} \quad \text{subject to } y_i(w^\top x_i + b) \ge 1 \text{ for all } i

Equivalently:

min⁡w,b12∥w∥2subject to yi(w⊤xi+b)≥1\min_{w,b} \frac{1}{2}\|w\|^2 \quad \text{subject to } y_i(w^\top x_i + b) \ge 1

This is a convex quadratic programming problem! Unique solution (strictly convex objective).

Dual Problem and the Kernel Trick

Lagrangian: L(w,b,α)=12∥w∥2−∑iαi[yi(w⊤xi+b)−1], αi≥0.L(w, b, \alpha) = \frac{1}{2}\|w\|^2 - \sum_i \alpha_i [y_i(w^\top x_i + b) - 1],\ \alpha_i \ge 0.

KKT conditions:

  • ∂L/∂w=0\partial L / \partial w = 0: w=∑iαiyixiw = \sum_i \alpha_i y_i x_i
  • ∂L/∂b=0\partial L / \partial b = 0: ∑iαiyi=0\sum_i \alpha_i y_i = 0

Substituting into the Lagrangian, we obtain the dual problem:

max⁡∑iαi−12∑i∑jαiαjyiyjxi⊤xjsubject to ∑iαiyi=0, αi≥0\max \sum_i \alpha_i - \frac{1}{2} \sum_i \sum_j \alpha_i \alpha_j y_i y_j x_i^\top x_j \quad \text{subject to } \sum_i \alpha_i y_i = 0, \ \alpha_i \ge 0

Key observation: the input data xix_i enter only via inner products xi⊤xjx_i^\top x_j!

Kernel trick: replace xi⊤xjx_i^\top x_j with K(xi,xj)K(x_i, x_j)—a kernel function. Then, the SVM implicitly operates in a high-dimensional (possibly infinite-dimensional) feature space, without computing it explicitly.

Support Vectors

From the KKT conditions (complementary slackness): αi[yi(w⊤xi+b)−1]=0\alpha_i [y_i(w^\top x_i + b) - 1] = 0.

Therefore, αi>0\alpha_i > 0 only for points "on the margin"—yi(w⊤xi+b)=1y_i(w^\top x_i + b) = 1. These are the support vectors—the points "closest" to the separating hyperplane. There are usually few of them: in a problem with 1000 points—dozens. Only they determine the solution!

Classification of a new point xx: sign⁡(∑iαiyiK(xi,x)+b)\operatorname{sign}(\sum_i \alpha_i y_i K(x_i, x) + b)—depends only on kernels with support vectors.

Soft Margin SVM

When classes are not separable, add "slack variables" ξi≥0\xi_i \ge 0:

min⁡12∥w∥2+C∑iξisubject to yi(w⊤xi+b)≥1−ξi, ξi≥0\min \frac{1}{2}\|w\|^2 + C \sum_i \xi_i \quad \text{subject to } y_i(w^\top x_i + b) \ge 1 - \xi_i, \ \xi_i \ge 0

The parameter CC controls the trade-off: large CC → strict margin (few violations), small CC → wide margin (many violations allowed). In the dual problem: αi∈[0,C]\alpha_i \in [0, C].

Popular Kernels

Linear: K(x,y)=x⊤yK(x, y) = x^\top y. SVM becomes a linear classifier.

Polynomial: K(x,y)=(x⊤y+1)dK(x, y) = (x^\top y + 1)^d. Operates implicitly in the space of degree dd polynomial features.

RBF (radial): K(x,y)=exp⁡(−∥x−y∥2/(2σ2))K(x, y) = \exp\left(-\|x - y\|^2 / (2\sigma^2)\right). Corresponds to an infinite-dimensional space (!) of Gaussian functions. σ\sigma—the "width" parameter. As σ→0\sigma \to 0: memorization; as σ→∞\sigma \to \infty: linear classifier.

Full Example Analysis

Data (2D, 4 points):

  • Class +1: x1=(1,2)x_1 = (1, 2), x2=(2,1)x_2 = (2, 1)
  • Class −1: x3=(−1,−2)x_3 = (−1, −2), x4=(−2,−1)x_4 = (−2, −1)

Symmetry: w=α1y1x1+α2y2x2+α3y3x3+α4y4x4w = \alpha_1 y_1 x_1 + \alpha_2 y_2 x_2 + \alpha_3 y_3 x_3 + \alpha_4 y_4 x_4.

By symmetry of the problem: α1=α2\alpha_1 = \alpha_2, α3=α4\alpha_3 = \alpha_4, and αiyixi\alpha_i y_i x_i for each pair are opposite → w=2α[(1,2)+(2,1)]=2α(3,3)w = 2\alpha[(1,2) + (2,1)] = 2\alpha(3,3).

Dual Problem: max⁡4α−(1/2)⋅(4α)2⋅(5+5+5+5)/(some norm)\max 4\alpha - (1/2) \cdot (4\alpha)^2 \cdot (5+5+5+5)/(\text{some norm}). After computation: w=(0.2,0.2)w = (0.2, 0.2), b=0b = 0. Margin =2/∥w∥=2/0.283≈7.07= 2/\|w\| = 2/0.283 \approx 7.07.

Generalization Guarantees

Theorem: with probability ≥1−δ\ge 1-\delta (over random training sample), the SVM error is bounded:

L(h)≤number of training errorsN+O(ρ2/γ2+log⁡(1/δ)N)L(h) \le \frac{\text{number of training errors}}{N} + O\left(\sqrt{\frac{\rho^2 / \gamma^2 + \log(1/\delta)}{N}}\right)

Here ρ\rho is the data norm, γ\gamma is the margin. Feature space dimensionality doesn't appear! The margin, not the model complexity, determines generalization. This is the theoretical basis: large margin = good generalization.

Support Vector Method: Mathematical Core

SVM solves a classification problem by finding a hyperplane with maximum margin between two classes. This is naturally formulated as a QP. The dual form is especially important: it depends only on inner products xi⊤xjx_i^\top x_j between training samples, which paves the way for the kernel trick.

Popular Kernels and Their Properties

  • Linear: K(x,y)=x⊤yK(x,y) = x^\top y—for linearly separable data
  • Polynomial: K(x,y)=(x⊤y+c)dK(x,y) = (x^\top y + c)^d—for polynomial boundary of degree dd
  • RBF (Gaussian): K(x,y)=exp⁡(−γ∥x−y∥2)K(x,y) = \exp(-\gamma\|x - y\|^2)—infinite-dimensional kernel, universal approximator
  • Sigmoid: K(x,y)=tanh⁡(αx⊤y+c)K(x,y) = \tanh(\alpha x^\top y + c)—related to neural networks
  • String kernels, graph kernels—for non-geometric data

Key requirement: Gram matrix Kij=K(xi,xj)K_{ij} = K(x_i, x_j) must be positive semi-definite (Mercer's theorem). This guarantees that the kernel corresponds to an inner product in some Hilbert space.

Implementation and Scaling

For large data, standard QP does not scale. Specialized algorithms are used:

  • SMO (Sequential Minimal Optimization, Platt, 1998): iteratively optimizes pairs of Lagrange multipliers
  • Pegasos (Shalev-Shwartz): stochastic gradient descent for linear SVMs
  • LIBLINEAR, LIBSVM—standard libraries

Applications

For many years, SVMs were state-of-the-art in text, image, and bioinformatics classification tasks. Before the deep learning era, it was SVM with RBF kernel that won competitions for handwritten digit recognition (MNIST), protein classification, and spam filtering. Today, SVMs remain relevant for small datasets, where deep networks overfit, and in industrial applications requiring model interpretability.

§ Act · what next