Support Vector Machines
Source: Unit 2 §11
SVM is one of the most sophisticated yet simple-to-implement algorithms in machine learning. It is largely thought of as a linear classifier, and it is far more than that.
Myth-busting
| Myth | Reality |
|---|---|
| "Only a linear classifier" | No - it handles non-linearity too, via kernels |
| "Only single or binary classification" | No - it can do multi-class classification |
| "Only a classifier" | No - it can do regression as well (SVR) |
| "Computationally expensive" | No - with the right C and γ it works like a charm |
| "Math heavy and provable" | Not a myth. It is math-heavy and fundamentally provable |
A data set is linearly separable if a hyperplane can separate the classes. In 2-D that hyperplane is a line, in 3-D a plane. Separable points typically form a convex hull.
The key insight: maximum margin
Many hyperplanes can separate the same data, so the question is which one is best. The SVM answer: the one that is farthest from the closest points of both classes.
SVM is a maximum-margin classifier, described in the course as "one of the most elegant ideas in computer science". The support vectors are the closest data points to the boundary, and they alone define the hyperplane.
The three hyperplanes
| Plane | Equation | Meaning |
|---|---|---|
| H0 | w·x + b = 0 | Decision boundary, the "median" of the street; separates maximally |
| H1 | w·x + b = +1 | Upper margin - the closest positive points lie on it |
| H2 | w·x + b = −1 | Lower margin - the closest negative points lie on it |
- is perpendicular to the street, exactly as it was perpendicular to the perceptron's boundary.
- The distance between H1 and H2 is , the street width, and the whole optimisation exists to make it as large as possible.
Building the decision rule
To classify an unknown point , project onto with a dot product . The farther sits on the positive side of , the more likely it is a positive sample.
Without loss of generality the rule can be hardened, insisting that no training point sits inside the street:
The mathematical convenience
Introduce a label variable for every instance: for positive samples and for negative ones. Multiplying each of the two constraints by its own flips the negative one round, and the pair collapses into a single inequality:
Rearranged, . For points on the gutter - the support vectors - it is exactly zero:
The quantity to maximise
Take a point on the upper margin and on the lower margin. The street width is the projection of onto the unit vector :
- lies on H1, so , giving .
- lies on H2, so , giving .
- Subtract: .
- Divide by to turn the projection into a distance: .
Primal form
Maximising is the same as minimising , which is the same as minimising :
That is the primal form. The shape is a mathematical convenience for differentiation - the same trick as squaring the error in gradient descent.
Constrained optimisation and the Lagrangian
The Lagrange-multiplier detour. To maximise subject to , walk along the constraint curve. The optimum is the point where and align; any further move along the constraint decreases .
- Construct the Lagrangian, introducing one Lagrange multiplier per constraint - which here means one per training instance.
- Compute , set the partial derivatives to 0, and solve.
The dual form
It is easier to solve the dual than the primal: instead of minimising over and , we maximise over the dual variables .
Take the partial derivatives of with respect to and and set both to zero:
The weight vector is a linear combination of the training inputs , their labels and their multipliers . The model is literally built out of the training data, not merely fitted to it.
Substituting both back into eliminates and and leaves the Wolfe dual:
The dual depends on the data only through the dot product . Nothing else about appears anywhere. That single fact is what makes the kernel trick possible.
KKT conditions and the support vectors
- The KKT slack condition implies that for every point except the support vectors, .
- Only the support vectors have , so only they contribute anything to .
- That is why an SVM is defined by a handful of points: deleting any non-support vector from the training set changes nothing at all.