Support Vector Machines: Margins and Kernels
Why the widest slab between two classes is a good boundary, why insisting on a perfect one is self-defeating, how a budget for violations buys back stability, and how a kernel bends the boundary by working in a space it never has to build.
Prerequisites: Logistic Regression and Classification
Most classifiers are fitted by writing down a loss and minimising it. Support vector machines start somewhere more geometric: among all the boundaries that separate two classes, prefer the one that is furthest from every observation. Following that idea honestly leads to a classifier that ignores most of its own training data, and then to a technique for curving the boundary without paying for the space it curves in.
A. The widest slab
In dimensions a hyperplane is the set where . It splits the space in two, so writing for that left-hand side gives a classifier: predict when and otherwise. With labels coded , an observation is correctly classified exactly when .
If the classes separate at all they usually separate in infinitely many ways, so the question is which hyperplane to take. The maximal margin classifier computes the distance from every observation to a candidate hyperplane, calls the smallest of these the margin, and picks the hyperplane with the largest one. It is the mid-line of the widest slab that fits between the classes.
Six points make it concrete. Class at , , and class at , , . The answer is , with distances :
Four observations touch the edge of the slab and hold it in place; these are the support vectors. The other two can be moved anywhere on their own side of the slab, as long as they stay outside it (), without changing the fitted classifier at all. Move one inside and the slab narrows. Note the support vectors are not split evenly between classes.
The optimisation is stated as maximising subject to and for every . That normalisation looks like bookkeeping but is essential: scaling all the coefficients by any describes the same hyperplane, so without a scale convention the parameters are undetermined. Fixing the length to one also makes the constrained quantity the true perpendicular distance.
Interactive: the widest slab that fits
Ringed points are the support vectors.
- Margin
- 1.414214
- Support vectors
- 4
- Separable
- yes
The widest slab has half-width 1.414214, and exactly 4 points touch it. Those are the support vectors, and they are the whole solution: the two positives further out sit at 2.8284 and could be moved anywhere on their own side, outside the slab, without the boundary twitching. A classifier that depends on four points out of six is a strange object, and it is the reason margins generalise well and are fragile at the same time.
B. Why perfection is the wrong goal
The maximal margin classifier fails in two ways. It has no solution at all when the classes are not separable, which is common. And when it does work, it is determined entirely by the points nearest the boundary, so it inherits their instability: add a single observation at to the six above and the best achievable margin drops from to . One point, a factor of more than three. Since a narrow margin is exactly what fails to generalise, demanding perfect separation defeats itself.
The support vector classifier allows violations and charges for them. Each observation gets a slack variable , the constraint becomes , and the total is capped at . The slack says how badly a point behaves: zero is on the right side of the margin, up to one is inside the margin but still correctly classified, and above one is on the wrong side of the hyperplane altogether.
is a budget for violation. At nothing is affordable and the problem reverts to the maximal margin classifier. As grows the margin widens and more points sit inside it. Because each misclassification costs more than one unit, also caps the number of training errors. It is not estimated by the optimiser; it is tuned by cross-validation.
The soft-margin problem has a property worth isolating: an observation strictly on the correct side of the margin has no effect on the classifier. Move it and nothing changes. Only points on the margin or violating it - the support vectors - enter the solution with non-zero coefficients. That is the precise sense in which the method is robust to distant points, and where it differs from linear discriminant analysis, which uses every observation through the class means and covariance.
is therefore the bias-variance dial in another costume. Small gives a narrow margin held up by few points: low bias, high variance. Large gives a wide margin resting on many: more bias, less variance.
C. Bending the boundary for free
Some data are not separated by anything flat - a class in a ring around another, say. The standard remedy is to enlarge the feature space: fit a linear boundary in and it comes back down to the plane as a conic. The obstacle is cost. The monomials of degree at most 2 on predictors, constant included, number :
What rescues it is that the solution can be written , and fitting the needs only the inner products between pairs of training observations. Neither step ever touches the coordinates. So replace every inner product with a kernel , a similarity function, and the algorithm still works - now implicitly in whatever space that kernel corresponds to. Since vanishes off the support vectors, the sum is short as well.
The polynomial kernel turns the support vector classifier into a support vector machine. It is not sleight of hand, and it is worth checking once. For , :
Runs in your browser. The first run downloads the Python runtime (~10 MB), then it is cached.
The kernel returns a six-dimensional inner product from two dimensions of input. At the explicit map needs half a million coordinates while the kernel still costs a thousand multiplications.
The other common choice is the radial kernel , which depends only on distance and decays exponentially in its square. With , a squared distance of gives and a squared distance of gives . It is local: a prediction is governed by the training points near it, and distant ones enter with weights indistinguishable from zero. That locality is the source of its flexibility, and it corresponds to an infinite-dimensional feature space you could not write down at any price - which is the strongest argument for working through kernels rather than coordinates.
Check it for yourself below rather than on three fixed pairs. Move the second point anywhere: the kernel, computed from two coordinates, and the inner product of the two six-dimensional maps stay the same number, and the six coordinates the right-hand side had to build are printed underneath. Then switch to the radial kernel and push the points apart. Two units is already enough for a training observation to contribute nothing that matters.
Interactive: the same number, computed two ways
Move the second point. The two columns never disagree.
- K(x, z)
- 49.0000
- Inner product of the maps
- 49.0000
- Squared distance
- 8.00
- Features at p = 1000
- 501,501
The six coordinates the right-hand side had to build
1.000 1.414 1.414 1.000 1.000 1.414
The kernel gives 49.0000 from two coordinates; the inner product of the two six-dimensional maps gives 49.0000. They are the same number, and they will be for any points you choose - the kernel is that inner product, not an approximation of it. Now look at the cost. Degree-2 features on a thousand predictors need 501,501 coordinates written down; the kernel still costs 1,000 multiplications. That gap is the entire point, and it is why nobody enlarges the feature space by hand.
Where this leaves you
A margin is a defensible reason to prefer one boundary over another, and the support vectors are the only data that matter to it. Insisting on perfect separation is fragile, so a budget for violations buys stability, and that budget is the familiar bias-variance dial. Kernels then curve the boundary by changing what "similarity" means rather than by building a larger space. The training path Support Vector Machines works each of these by hand and in code.
References & further reading
- Gareth James, Daniela Witten, Trevor Hastie, Robert Tibshirani, An Introduction to Statistical Learning, with Applications in R, Springer (Springer Texts in Statistics 103), 2013source ↗
Copyrighted works are cited for reference only and are not hosted here; please consult the publisher for access.