Support Vector Machines: Margins and the Kernel Trick
Published:
Which separating line?
Take two classes that a straight line can separate. There is not one such line; there is a continuum of them, and some are visibly worse than others โ a line that shaves past a training point will misclassify the next sample drawn from near it.
The support vector machine makes the choice precise: pick the hyperplane whose distance to the nearest point of either class is as large as possible.
Write the hyperplane as \(w^\top x + b = 0\). The signed distance from a point \(x_i\) to it is \((w^\top x_i + b)/\lVert w \rVert\). This is over-parameterised โ scaling \(w\) and \(b\) together changes nothing geometrically โ so fix the scale by demanding that the closest points satisfy \(\lvert w^\top x_i + b \rvert = 1\). With that convention the two margin boundaries are \(w^\top x + b = \pm 1\), the distance between them is \(2/\lVert w \rVert\), and the problem becomes:
Maximising the margin and minimising \(\lVert w \rVert^2\) are the same thing. This is a convex quadratic programme, so it has a unique solution and no local minima to worry about.
A worked example you can check
Six points in the plane. Class \(+1\) at \((2,1)\), \((2,3)\), \((3,2)\); class \(-1\) at \((0,1)\), \((0,3)\), \((-1,2)\).
Take \(w = (1,0)\) and \(b = -1\), so the hyperplane is \(x_1 = 1\). Evaluating \(y_i(w^\top x_i + b)\) at each point:
| Point | Class | \(y_i(w^\top x_i + b)\) | ย |
|---|---|---|---|
| \((2,1)\) | \(+1\) | \(+1\) | support vector |
| \((2,3)\) | \(+1\) | \(+1\) | support vector |
| \((3,2)\) | \(+1\) | \(+2\) | ย |
| \((0,1)\) | \(-1\) | \(+1\) | support vector |
| \((0,3)\) | \(-1\) | \(+1\) | support vector |
| \((-1,2)\) | \(-1\) | \(+2\) | ย |
Every constraint holds, four of them with equality, and \(\lVert w \rVert = 1\) gives a margin of \(2/1 = 2\). A brute-force search over separating directions confirms nothing beats it.
Soft margins, for data that is not separable
Real data overlaps, and then no \(w\) satisfies every constraint. Introduce a slack \(\xi_i \ge 0\) per point measuring how far it intrudes:
\(C\) sets the exchange rate between a wide margin and a clean fit. Large \(C\) punishes violations heavily and drives the model towards the separable solution, narrowing the margin and increasing variance. Small \(C\) tolerates intrusions and buys a wider, steadier corridor. It is a regularisation parameter in the sense the biasโvariance chapter describes, and it must be tuned rather than guessed.
The one fact everything else rests on
Solve the problem through its Lagrangian dual and the solution takes the form
with \(\alpha_i \ge 0\) and โ this is the same observation as before, now falling out of the algebra โ \(\alpha_i = 0\) for every point that is not a support vector.
Look at what \(f\) actually touches. Not the coordinates of \(x\). Only the inner product between \(x\) and each support vector. The training problem has the same character: the dual objective involves the data solely through the pairwise products \(x_i^\top x_j\).
The kernel trick, made concrete
Define \(k(x,z) = (x^\top z)^2\) for \(x, z \in \mathbb{R}^2\) and expand it:
The right-hand side is an inner product of two explicit three-dimensional vectors. So with \(\varphi(x) = (x_1^2, \sqrt{2}x_1x_2, x_2^2)\) we have \(k(x,z) = \varphi(x)^\top \varphi(z)\) exactly.
Check it on \(x = (1,2)\) and \(z = (3,-1)\). Directly, \(x^\top z = 3 - 2 = 1\), so \(k = 1\). Through the feature map, \(\varphi(x) = (1,\ 2\sqrt{2},\ 4)\) and \(\varphi(z) = (9,\ -3\sqrt{2},\ 1)\), giving \(9 - 12 + 4 = 1\). The same.
That is the trick in full. Computing \(k\) costs one two-dimensional dot product and a squaring. Computing \(\varphi(x)^\top\varphi(z)\) the honest way costs building two three-dimensional vectors first. Here the saving is trivial; for a degree-\(d\) polynomial kernel in \(n\) dimensions the feature space has \(\binom{n+d-1}{d}\) coordinates and the saving is the difference between possible and impossible.
The decision function becomes
Common kernels. The linear kernel \(k(x,z) = x^\top z\) recovers the plain SVM. The polynomial kernel \((x^\top z + c)^d\) gives all monomials up to degree \(d\). The RBF kernel \(k(x,z) = \exp(-\gamma \lVert x - z \rVert^2)\) corresponds to an infinite-dimensional feature space โ which is only usable because you never construct it. Its \(\gamma\) controls locality: large \(\gamma\) makes each support vectorโs influence decay quickly, giving a wiggly boundary that can overfit; small \(\gamma\) approaches a linear fit.
What SVMs do not buy you
They do not scale. Training involves the \(n \times n\) Gram matrix, so cost grows roughly quadratically to cubically in the number of training points and memory quadratically. Past the order of \(10^5\) samples this is the binding constraint, and it is the main reason SVMs receded as datasets grew.
They do not give probabilities. The output \(f(x)\) is a signed distance, not a likelihood. Converting it to a probability requires a separate calibration step fitted on held-out data, and the result is a retrofit rather than something the model estimated.
Kernel choice is a real decision. With an RBF kernel you are tuning \(C\) and \(\gamma\) together over a 2-D grid, and the result is genuinely sensitive to both. There is no principled default.
They stop being interpretable once kernelised. A linear SVM has a weight vector you can read. An RBF SVM has a set of support vectors and coefficients, and no accessible statement about which features mattered.
They need scaled features. The margin is measured with a Euclidean norm, so a feature in millimetres will dominate one in metres for no good reason. Standardise first.
โ Key Takeaways
- Among the infinitely many separating hyperplanes, the SVM takes the one maximising the corridor width \(2/\lVert w \rVert\) โ which makes it the minimiser of \(\lVert w \rVert^2\) under margin constraints.
- Only the points touching the margin matter. Deleting the rest changes nothing, which is what "support vector" means and why the solution is sparse.
- The soft margin adds slack variables and the parameter \(C\), trading margin width against violations โ a regularisation knob, not a convenience.
- Both the training problem and the prediction touch the data only through inner products. That is the fact the kernel trick exploits.
- \((x^\top z)^2\) in 2-D is exactly an inner product of explicit 3-D feature vectors โ expand it and see. RBF does the same thing into an infinite-dimensional space you never build.
- The costs are real: quadratic-to-cubic training, no native probabilities, two coupled hyperparameters, and no interpretability once kernelised.
