Skip to content
Kudos AI

Separating Hyperplanes and the Margin

A hyperplane as a decision rule, the margin as the width of the widest slab between the classes, the handful of observations that fix it, and the two ways the idea fails.

IntermediateModule 125 min · 100 XP
Many separating lines drawn through the same two clouds, then all but the widest slab fading away, with the four points that touch its edges lighting up.

Logistic regression fits a boundary by making the observed labels probable. This lesson takes a different starting point: among all boundaries that separate the classes, prefer the one that is furthest from every observation. That single idea produces a classifier with an unusual property, which is that almost all of the data turn out to be irrelevant to it.

A hyperplane is a decision rule

In pp dimensions a hyperplane is the flat, (p−1)(p-1)-dimensional set

β0+β1x1+β2x2+⋯+βpxp=0.\beta_0 + \beta_1 x_1 + \beta_2 x_2 + \cdots + \beta_p x_p = 0 .

In two dimensions that is a line, in three a plane. A point not on it makes the left-hand side either positive or negative, so a hyperplane cuts the space in two and gives a classifier for free: write

f(x)=β0+β1x1+⋯+βpxpf(x) = \beta_0 + \beta_1 x_1 + \cdots + \beta_p x_p

and predict class +1+1 when f(x)>0f(x) > 0 and class −1-1 when f(x)<0f(x) < 0. Coding the labels as ±1\pm 1 makes "correctly classified" compact: the prediction is right exactly when

yif(xi)>0.y_i f(x_i) > 0 .

The magnitude of f(x)f(x) is informative too. A point far from the boundary has f(x)f(x) far from zero, and we can be confident about it; a point close to the boundary is close to a coin flip.

Which separating hyperplane?

If the classes can be separated at all, they can usually be separated in infinitely many ways: nudge or tilt a separating line slightly and it still separates. So "find a separating hyperplane" is not yet a well-posed problem.

The maximal margin classifier resolves it. Compute the distance from every training observation to a candidate hyperplane; the smallest of those distances is the margin. Then choose the hyperplane whose margin is largest. In words, it is the mid-line of the widest slab you can push between the two classes.

Worked example

Take six observations in the plane:

class +1:(3,3), (4,4), (3,5)class −1:(1,1), (0,2), (2,0)\begin{array}{ll} \text{class } +1: & (3,3),\ (4,4),\ (3,5) \\ \text{class } -1: & (1,1),\ (0,2),\ (2,0) \end{array}

The maximal margin hyperplane is

x1+x2−4=0,x_1 + x_2 - 4 = 0 ,

and the perpendicular distance from a point to it is ∣x1+x2−4∣/2|x_1 + x_2 - 4| / \sqrt{2}. Evaluating that at each observation:

(3,3)+12≈1.4142(4,4)+122≈2.8284(3,5)+122≈2.8284(1,1)−12≈1.4142(0,2)−12≈1.4142(2,0)−12≈1.4142\begin{array}{lll} (3,3) & +1 & \sqrt{2} \approx 1.4142 \\ (4,4) & +1 & 2\sqrt{2} \approx 2.8284 \\ (3,5) & +1 & 2\sqrt{2} \approx 2.8284 \\ (1,1) & -1 & \sqrt{2} \approx 1.4142 \\ (0,2) & -1 & \sqrt{2} \approx 1.4142 \\ (2,0) & -1 & \sqrt{2} \approx 1.4142 \end{array}

The margin is 2\sqrt{2}, and four observations achieve it: (3,3)(3,3), (1,1)(1,1), (0,2)(0,2) and (2,0)(2,0). These are the support vectors. They lie on the edges of the slab and they hold it in place - move one and the hyperplane moves.

The other two, (4,4)(4,4) and (3,5)(3,5), sit at 222\sqrt{2} and are irrelevant. You may move them anywhere on their side of the slab and the fitted classifier does not change at all. Note also that the support vectors are not evenly split between the classes: one positive, three negative.

Python

Runs in your browser. The first run downloads the Python runtime (~10 MB), then it is cached.

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.

Stating it as an optimisation

The problem is written

max⁡β0,…,βp, MMsubject to∑j=1pβj2=1,yi(β0+β1xi1+⋯+βpxip)≥M  ∀i.\max_{\beta_0,\dots,\beta_p,\,M} M \quad\text{subject to}\quad \sum_{j=1}^{p}\beta_j^2 = 1, \qquad y_i\big(\beta_0 + \beta_1 x_{i1} + \cdots + \beta_p x_{ip}\big) \ge M \ \ \forall i .

The second constraint says every observation is on its correct side and at least MM away. The first looks like a technicality and is not. Multiplying every coefficient by any k≠0k \neq 0 describes the same hyperplane, so without a scale convention the parameters are not determined. Fixing the coefficient vector to unit length pins them down, and it makes yi(β0+β⋅xi)y_i(\beta_0 + \beta \cdot x_i) equal the actual perpendicular distance - which is what makes "maximise MM" mean "maximise the margin".

The canonical form. An equivalent convention scales so the closest points satisfy yif(xi)=1y_i f(x_i) = 1. Our hyperplane becomes β=(0.5,0.5)\beta = (0.5, 0.5) with β0=−2\beta_0 = -2, giving ∥β∥=0.7071\lVert\beta\rVert = 0.7071 and a margin of 1/∥β∥=1.41421/\lVert\beta\rVert = 1.4142, the same 2\sqrt{2}. Maximising the margin is then minimising ∥β∥\lVert\beta\rVert, which is the form most software solves.

Two ways it fails

The classes may not be separable. Then no hyperplane satisfies the constraints with M>0M > 0 and the problem simply has no solution. Real data are frequently like this, and a method that returns nothing at all is not much use.

Even when it works, it is fragile. The solution is determined entirely by the few points nearest the boundary, so it inherits their instability. Add a single +1+1 observation at (1.6,1.6)(1.6, 1.6), tucked in near the negative cloud, and the best achievable margin falls from 1.41421.4142 to 0.42430.4243 - a factor of more than three, from one point. Since a narrow margin is precisely what generalises poorly, demanding perfect separation is self-defeating.

Both failures point the same way: the classifier should be allowed to get a few observations wrong. That is the next lesson.

Before the quiz

Be able to classify by the sign of f(x)f(x), define the margin, pick out the support vectors and say why the others do not matter, explain what the normalisation constraint is for, and name the two failure modes.

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.

Unlock the full path

This first lesson is free. Enrol to take the mastery quiz, earn XP, and unlock every module, with more interactive, runnable examples throughout.