CV Courseversity

Support Vector Machines and Kernel Methods

Covers the maximum-margin principle behind support vector machines, the kernel trick that lets them draw nonlinear decision boundaries without ever computing a high-dimensional feature mapping explicitly, and support vector regression.

“Two classes of points on a page can be separated by infinitely many different straight lines — so which one should a classifier trust when a brand-new point lands close to the boundary? And what happens when no straight line can separate the classes at all, yet you can still draw a clean circle around one of them?”

Maximum-Margin Classification and the Support Vector Algorithm · 15 min

Among the many hyperplanes that might separate two linearly separable classes, Corinna Cortes and Vladimir Vapnik's 1995 paper "Support-Vector Networks" argues that one particular choice is principled: the hyperplane with maximal margin, defined as "the linear decision function with maximal margin between the vectors of the two classes." Concretely, the optimal hyperplane "separates the training data with a maximal margin: it determines the direction w/|w| where the distance between the projections of the training vectors of two different classes is maximal." Stanford CS229's lecture notes formalize this margin concept in two related but distinct ways: the functional margin of a training example (x⁽ⁱ⁾, y⁽ⁱ⁾) is defined as γ̂⁽ⁱ⁾ = y⁽ⁱ⁾(wᵀx⁽ⁱ⁾ + b), while the geometric margin, γ⁽ⁱ⁾ = y⁽ⁱ⁾((w/‖w‖)ᵀx⁽ⁱ⁾ + b/‖w‖), rescales this by the norm of w so that it corresponds to an actual, rescaling-invariant Euclidean distance from the point to the decision boundary; because the functional margin can be made arbitrarily large simply by scaling w and b up, it is the geometric margin that captures what maximal margin really means. CS229's notes show that maximizing this geometric margin over a linearly separable dataset reduces to the optimization problem min_{w,b} (1/2)‖w‖² subject to y⁽ⁱ⁾(wᵀx⁽ⁱ⁾ + b) ≥ 1 for every training example, a quadratic objective with linear constraints, which Cortes and Vapnik's original paper describes as a "quadratic programming problem."

Because this optimization problem's constraints are only tight, meaning satisfied with equality, for the training points closest to the boundary, the solution w depends only on a small subset of the training data: the points lying exactly on the margin, which give the support vector method its name. Every other training point, safely on the correct side of its margin, could in principle be removed from the dataset entirely without changing the fitted hyperplane at all, a sharp contrast to methods like linear regression's normal equations, where every single training point contributes to the final solution. Cortes and Vapnik's paper further shows that this quadratic program can be reformulated into an equivalent dual problem in which the objective depends on the training data only through pairwise dot products between input vectors, never through the vectors' individual coordinates; CS229's notes make this dual explicit as maximizing W(α) = Σᵢαᵢ − (1/2)Σᵢ,ⱼ y⁽ⁱ⁾y⁽ʲ⁾αᵢαⱼ⟨x⁽ⁱ⁾, x⁽ʲ⁾⟩ over the dual variables α, with the fitted classifier's prediction for a new point x likewise expressible using only inner products, wᵀx + b = Σᵢαᵢy⁽ⁱ⁾⟨x⁽ⁱ⁾, x⟩ + b. This dependence on inner products alone, rather than on the raw feature vectors themselves, is what makes the kernel trick possible, since any expression built entirely out of inner products can be computed in a different, implicit feature space simply by substituting a different function for the inner product.

The Kernel Trick and Nonlinear Classification · 14 min

Because the SVM optimization problem and its final prediction rule depend on the training data only through inner products ⟨x⁽ⁱ⁾, x⁽ʲ⁾⟩, Stanford CS229's lecture notes describe a substitution that lets support vector machines draw nonlinear decision boundaries without ever leaving the linear-classifier machinery already derived: replace every inner product with a kernel function K(x, z), defined so that K(x, z) = ⟨φ(x), φ(z)⟩ for some feature mapping φ that sends the original input into a new, often much higher-dimensional, feature space. Wherever the algorithm previously computed ⟨x, z⟩ directly, it now computes K(x, z) instead, which implicitly performs the entire computation as if the inputs had first been mapped through φ into the higher-dimensional space, without ever actually constructing φ(x) or φ(z) as explicit vectors. CS229's notes work through the Gaussian, or radial basis function, kernel as a concrete example, K(x, z) = exp(−‖x − z‖²/(2σ²)), describing it as "a reasonable measure of x and z's similarity" that in fact "corresponds to an infinite dimensional feature mapping φ," meaning the implicit feature space this kernel corresponds to has infinitely many dimensions, a space no algorithm could ever construct explicitly but which the kernel trick makes fully usable anyway, since only K(x, z) itself, never φ(x), ever needs to be computed.

Not every function of two inputs qualifies as a valid kernel, since the substitution only makes mathematical sense if K(x, z) really does correspond to an inner product in some feature space. CS229's notes state the precise condition, attributed to Mercer's theorem: for K to be a valid kernel, it is necessary and sufficient that, for any finite set of points {x⁽¹⁾, ..., x⁽ⁿ⁾}, the corresponding n-by-n kernel matrix, whose (i, j) entry is K(x⁽ⁱ⁾, x⁽ʲ⁾), is symmetric and positive semi-definite. Cortes and Vapnik's original 1995 paper anticipates exactly this generalization, noting that the classification function "only depends on the dot-products" in feature space and that substituting "any symmetric function K(u, v)" satisfying this same Mercer condition for the ordinary inner product lets the support vector method construct "arbitrary types of decision surfaces," not just straight hyperplanes, entirely by choosing a different kernel rather than by redesigning the optimization algorithm itself. This is the sense in which the kernel trick separates the geometry of the problem, whatever curved or twisted decision surface the data actually requires, from the algorithm, which continues to solve the exact same quadratic program regardless of which kernel is plugged in.

Support Vector Regression · 12 min

The maximum-margin idea underlying support vector classification can be adapted to regression as well, producing what Alex Smola and Bernhard Scholkopf's 2004 tutorial on the subject calls Support Vector Regression, or SVR. Rather than trying to make every prediction match its target as closely as possible, the way ordinary least-squares regression does, SVR's stated goal is different: "to find a function f(x) that has at most ε deviation from the actually obtained targets yi for all the training data, and at the same time is as flat as possible," where ε is a small tolerance chosen in advance. This is formalized using the ε-insensitive loss function, defined so that a deviation ξ incurs zero loss when |ξ| ≤ ε and a loss of |ξ| − ε otherwise, meaning any prediction that falls within ε of its true target incurs exactly zero loss, no matter how close or far from perfect it is within that tolerance, while predictions that miss by more than ε are penalized only for the excess beyond ε, and that excess is penalized linearly rather than quadratically. Because not every training point can necessarily be fit within an ε-wide tube around the true function while also keeping the function as flat as possible, the tutorial introduces slack variables ξᵢ, ξᵢ* "to cope with otherwise infeasible constraints," allowing some points to fall outside the tube at a controlled cost.

This framing produces an optimization problem with the same overall shape as the classification SVM: minimize (1/2)‖w‖², the same flatness-maximizing objective used to find the maximum-margin hyperplane, subject now to constraints that keep predictions within the ε-tube wherever possible and penalize the slack variables when they cannot be. The tutorial is explicit that "only the points outside the shaded region contribute to the cost," which mirrors the classification case's dependence on only the support vectors, points near or violating the margin, rather than on every single training example; in SVR, the analogous support vectors are precisely the training points that lie on or outside the boundary of the ε-tube, while points comfortably inside it contribute nothing at all to the fitted function. Because this optimization problem, like the classification SVM's, can be expressed entirely in terms of inner products between training inputs, the same kernel substitution used for nonlinear classification carries over directly to SVR: replacing each inner product with a kernel function such as the Gaussian RBF kernel lets support vector regression fit a curved, nonlinear function through the ε-tube rather than only a flat line or hyperplane, while still solving the same fundamentally convex, sparsely-supported optimization problem underneath.

Practice

Maximum Margin, Then a Kernel Twist

Class A Class B circled = support vectors Not linear in (x, y) r² = x² + y² (linearly separable)

Left: the maximum-margin hyperplane is fixed entirely by the few points that touch the margin (circled) — the support vectors. Right: mapping (x, y) to r² = x² + y² turns a circular boundary into a simple threshold, the same trick a kernel function performs implicitly and in far higher dimensions.

  • The SVM's fitted decision boundary depends only on the support vectors — the training points that lie exactly on the margin — because the optimization's constraints are tight only for those points; every other point could be deleted from the training set without changing the solution at all.
  • The kernel trick works because the SVM's optimization and prediction rule depend on the training data only through pairwise inner products ⟨x,z⟩ — substituting a kernel function K(x,z) = ⟨φ(x),φ(z)⟩ implicitly computes those inner products in a different, often much higher-dimensional feature space, without ever constructing φ(x) explicitly.
  • Support Vector Regression replaces ordinary least-squares' penalize-every-deviation objective with the ε-insensitive loss: any prediction within ε of its target costs nothing at all, and only points outside that tolerance tube contribute to the fit — and only linearly, not quadratically.

Recall Practice

Support vectorsClick to reveal
Why does the SVM's fitted hyperplane depend only on a small subset of the training points, the support vectors?
Because in the maximum-margin optimization problem, the separating constraints are only tight (satisfied with equality) for the points closest to the boundary. Every other point, safely on the correct side of its margin, could be removed from the dataset entirely without changing the fitted hyperplane.
Valid kernelsClick to reveal
What condition, per Mercer's theorem, must a function K(x,z) satisfy to be usable as a valid SVM kernel?
For any finite set of points, the resulting kernel matrix — whose (i,j) entry is K(x⁽ⁱ⁾, x⁽ʲ⁾) — must be symmetric and positive semi-definite. Only then does K(x,z) actually correspond to an inner product ⟨φ(x),φ(z)⟩ in some real feature space.
RBF kernel's implicit spaceClick to reveal
What does CS229's notes say the Gaussian (RBF) kernel K(x,z) = exp(−‖x−z‖²/(2σ²)) corresponds to?
An infinite-dimensional feature mapping φ — a feature space with infinitely many dimensions that no algorithm could ever construct or store explicitly, but which the kernel trick makes fully usable because only K(x,z) itself, never φ(x), needs to be computed.
SVR's epsilon tubeClick to reveal
In Support Vector Regression, what happens to the loss for a prediction that falls within ε of its true target, and what happens beyond that?
It costs exactly zero — the ε-insensitive loss ignores any deviation up to ε, no matter how close to perfect it is within that tolerance. Beyond ε, the loss grows linearly with the size of the excess deviation, not quadratically as in ordinary least squares.

Glossary

Maximum-margin hyperplane
The linear decision boundary that maximizes the geometric margin, the distance to the nearest training points of either class, among all hyperplanes that separate the two classes.
Support vector
A training point that lies exactly on the margin of a fitted SVM and therefore has a tight (equality) constraint in the optimization problem; the fitted hyperplane depends only on these points.
Kernel function
A function K(x,z) = ⟨φ(x),φ(z)⟩ that implicitly computes an inner product in some (possibly high- or infinite-dimensional) feature space defined by a mapping φ, without ever computing φ(x) explicitly.
Mercer's theorem (valid kernel condition)
The condition that a function K(x,z) is a valid kernel if and only if, for any finite set of points, its corresponding kernel matrix is symmetric and positive semi-definite.
Radial basis function (RBF) kernel
The Gaussian kernel K(x,z) = exp(−‖x−z‖²/(2σ²)), a similarity measure that corresponds to an infinite-dimensional implicit feature mapping.
ε-insensitive loss
The loss function used in Support Vector Regression that assigns zero cost to any prediction within ε of its true target and penalizes larger deviations linearly in the excess beyond ε.
Practical Activity

Trace a Margin, Then Break It With a Kernel

A fully paper-based worksheet, no software. Part A: learners are given four labeled 2D points on graph paper — two of class +1 and two of class −1, arranged so a hand-drawable straight line can separate them — and asked to sketch by eye the maximum-margin separating line, mark which of the four points touch (are closest to) the margin on each side, and explain in one sentence why those touching points, and only those, are the support vectors for this toy dataset. Part B: learners are given a second, non-linearly-separable dataset (points arranged in two concentric rings, an inner ring of radius 1 labeled class +1 and an outer ring of radius 2 labeled class −1) and asked to compute, by hand for each point, the single new feature r² = x² + y² (the squared distance from the origin) — giving r²=1 for every inner-ring point and r²=4 for every outer-ring point — then confirm on paper that in this new one-dimensional feature space a simple threshold such as r²=2.5 perfectly separates the two classes, a concrete, hand-computable illustration of what an implicit kernel mapping accomplishes, in miniature and in only one derived dimension rather than the RBF kernel's infinite ones.

Ready to test yourself?

5 questions on this module.

Start Quiz