Week 10 · Support vector machines
Read the words of the video
- Imagine kids on a playground, boys on one side, girls on the other. You need to draw a line to separate them. Many lines could work! But which line is the very best? Think of this line as the middle of a road. We want the widest possible road that still keeps all the kids safely on their own sides. This idea is called a Support Vector Machine, aiming for the 'maximum margin'.
- This special road has two clear edges. Only the few kids standing right on these edges truly matter for defining the road! These important 'edge-standers' are called 'support vectors.' If you moved any other kid, the road would not change. The best separating line is found by making this road as wide as possible, or 'maximizing the margin'.
- Let's try a small example. Imagine two points on a number line: one at zero, labelled negative; another at two, labelled positive. The best separating line will be exactly in the middle, at one. The 'road' would have a width of two, with its edges at zero and two. Both original points become our support vectors, defining this perfect road.
- SVMs are super important for your exams! Remember, the margin is two divided by the weight vector's length. Support vectors are the points on the road's edge. A big 'C' means a narrow road, while a small 'C' means a wide road. And with cool tricks, SVMs can even find curved roads! Keep practicing, you've got this!
The one-minute story
Two groups of kids stand on a playground, boys on the left, girls on the right, and you want to draw a line between them. Many lines work. Which one is best?
Think of the line as the centre of a road. A thin road might be squeezed against some child on either side; any wobble and that child is on the wrong side. The safest line is the one at the centre of the widest possible road that still has nobody standing on it. That is the support vector machine (SVM): maximum margin.
Here is the beautiful surprise. Only the kids standing right at the edges of the road matter. If you moved any child further back, or removed them, the road would not change. Those edge-standers are the support vectors, and they alone decide where the road goes. And thanks to the kernel trick from week 2, the same machine can draw curved roads.
By the end of this week you can:
- state the hard-margin SVM problem and compute the margin;
- explain the dual problem and the role of support vectors (via complementary slackness);
- extend to the soft margin with slack variables and the parameter ;
- use the kernel trick to get non-linear boundaries and read hinge loss.
Words you need
- Margin - the width of the empty road between the two classes. A wider margin is more robust.
- Support vectors - the training points that lie exactly on the edge of the road (or inside it, in the soft-margin case).
- Hard margin - insist that nobody is inside the road: needs separable data.
- Soft margin - allow a few points inside or across, at a price.
- Slack variable - how far point violates the road.
- - the price of violations. Large : strict. Small : relaxed.
- Hinge loss - .
Idea 1: Maximising the margin
Labels and a linear classifier . We scale so that the closest points of each class satisfy . Then:
- every training point obeys (correct side, outside the road);
- the two road edges are , and the width of the road is
Making the road as wide as possible means making as small as possible:
Hard-margin SVM (primal)
This is a convex problem (quadratic objective, linear constraints) with a unique solution.
Question. One point with and one point with . Find the maximum-margin classifier and its margin.
Solution. In 1-D, . We need , i.e. , and . To minimise take both constraints tight: and . So , the boundary is at (the midpoint, as you'd expect), and the margin is , the distance between the points. Both points are support vectors.
Idea 2: The dual problem and support vectors
Using Lagrange multipliers , one for each constraint (MLF week 9), the dual of the hard-margin SVM is
and the weight vector is recovered as
KKT complementary slackness says . So for any point strictly outside the road () we get : it plays no part in . Only points on the road edges () can have . These are the support vectors.
Two consequences:
- The classifier depends on only a few points. Delete any non-support vector and nothing changes.
- The data enters the dual only through dot products . Replace them by a kernel and you have a kernel SVM with a curved boundary, at no extra conceptual cost. Prediction is , a sum over support vectors only.
Question. For the two points in Example 1 write and solve the dual.
Solution. The points: (), (). The constraint gives , so . The double sum has only one non-zero term, : (every term involving vanishes). The dual objective is . Its maximum: derivative .
Then ✓. And from a support vector: ✓. It matches the primal.
Idea 3: The soft margin
Real data is rarely perfectly separable, or it is separable only by a skinny, fragile road. Allow violations with slack :
Soft-margin SVM
- : the point is correctly outside the road. : inside the road but on the right side. : on the wrong side (misclassified).
- is the price of a violation. Large : violations are expensive, so the margin is narrow and few points violate it (low bias, risk of overfitting). Small : violations are cheap, so the margin is wide and many points may violate (more bias, underfitting risk).
- In the dual the only change is the box constraint . Support vectors are the points with : on the edge, inside the road, or misclassified.
Hinge loss view. Eliminating gives the unconstrained problem
The hinge loss is for points beyond the road edge and grows linearly as a point enters the road or crosses over. Compared with logistic loss (week 9), hinge loss goes exactly to zero for "safe" points, which is why those points drop out.
Idea 4: Non-linear SVMs
Use the kernel trick: the dual and the prediction only need . With an RBF kernel the boundary can wrap around clusters. Choices: kernel type (linear, polynomial, RBF) and its parameters (degree, ), plus , all chosen by cross-validation. A very small (very local RBF) lets the SVM memorise: overfits.
Your turn
A trained SVM has and . What is the margin width? On which lines do the road edges lie?
Need a hint?
Margin .
Show the full solution
, so the margin is . Edges: and , i.e. and .
A hard-margin SVM gives, for five points, the values . Which points can be support vectors?
Need a hint?
Support vectors lie exactly on the margin, .
Show the full solution
Points 1 and 3. The others are strictly outside the road, so their .
What happens to the margin width and to the number of training violations as increases?
Need a hint?
Large makes mistakes expensive.
Show the full solution
The margin gets narrower and the number of violations decreases (the model fits the training data more tightly and risks overfitting).
For and , compute the hinge loss. And for , ? For , ?
Need a hint?
.
Show the full solution
(inside the road). (wrong side). (safe).
(a) Removing a non-support vector changes the SVM solution. (b) The SVM dual depends on the data only through dot products. (c) The SVM objective is convex. (d) A larger always gives better test accuracy.
Need a hint?
Use complementary slackness.
Show the full solution
(a) False. (b) True. (c) True. (d) False, it can overfit.
Exam radar
In the MLT End Term, summaries mention Soft-Margin SVM in 36 of 49 papers, Hard-Margin SVM in 25, Support Vectors in 20, Dual Problem in 15, Support Vector Machines in 13, Kernel Trick in 16. SVMs are the single most mentioned topic in the End Term.
Typical items: margin ; "which points are support vectors?"; "what does a large do?"; "the dual constraint "; ""; hinge-loss values; the primal vs dual form; hard vs soft margin differences.
- Margin is , not (that is the distance from the boundary to one edge).
- Large narrow margin, small wide margin. Easy to flip.
- Support vectors have ; in the hard-margin case they lie exactly on .
- Hard margin needs linearly separable data; soft margin does not.
- Don't forget the dual constraint .
Quick quiz
The margin of a hard-margin SVM with weight vector w is...
- ||w||
- 1 / ||w||
- 2 / ||w||
- ||w|| squared
A support vector is a training point...
- far from the boundary
- on (or inside) the margin, with a non-zero dual weight
- that is misclassified only
- at the origin
In the soft-margin SVM, increasing C...
- widens the margin
- penalises margin violations more, narrowing the margin
- removes the slack variables
- has no effect
Select all TRUE statements about the SVM. (select all that apply)
- The solution depends only on the support vectors.
- The dual involves only dot products between data points.
- It needs the data to be linearly separable in the soft-margin case.
- The primal problem is non-convex.
Recap
- Hard margin: s.t. . Margin .
- Dual: s.t. , ; . Complementary slackness support vectors only.
- Soft margin: add ; ; large = narrow margin, small = wide margin. Hinge loss .
- Kernel SVM: replace dot products with .
- Primal: s.t. ,
- Margin
- Dual: ; ;
- ;
- Hinge loss:
- MLF week 9 - Optimising with rules: Lagrange and KKT: The SVM dual comes straight out of Lagrange multipliers and the KKT conditions.
- MLP week 8 - SVMs in sklearn: SVMs: the theory, then the code.
Practise with real exam questions
These come straight from past papers (with the real figures and the official answer key). Open 33 real questions on this week All End Term papers