In this blog post, I will give a geometric interpretation of hard-margin SVM and solve the corresponding optimization problem using quadratic programming. See also the paper  for some proofs.
Let and be nonempty disjoint and linearly separable convex sets. Then according to the hyperplane separation theorem, there exist and such that
Our task is to find the separating hyperplane such that the distance to each set is as large as possible. In other words, the hyperplane must be in the middle between the two sets and .
There are two ways to define the objective for the optimization problem and then use the output:
- Method 1
- Optimization: Find the points and that are closest to each other.
- Output: Calculate the normal vector and the bias (offset) in order to construct a hyperplane.
- Method 2:
- Optimization: Find the hyperplanes and that are furthest to the right (i.e. set ) and left (i.e. set ), while maximizing the distance to each other.
- Output: Calculate in order to construct a new hyperplane. The optimization step gave us the normal vector , one bias for $C$ and one bias for $D$.
The second approach is how the hard-margin primal SVM is normally taught (see Ng’s lecture notes on SVM). The two approaches differ in how they obtain the final separating hyperplane, but are otherwise equivalent (i.e. primal/dual).
To see the equivalence note that due to the constraint “furthest to the right and left” (method 2) the hyperplanes are supporting hyperplanes. This means both hyperplanes have at least one point on the boundary and , such that for all , and for all , .
Thus, by finding supporting hyperplanes we are actually looking for the closest point in each convex set and .
An example will make this all a bit clearer. Let us plot two convex sets with Python.
We can see in the following plot that the two sets are disjoint and nonempty. Hence, it is possible to find a separating hyperplane.
Method 1 will give us the following blue separating hyperplane.
Method 2 returns two red supporting hyperplanes and a blue separating hyperplane.
Now, let us try out both methods.
The sets and in the Python code can be written in matrix notation as follows
Points like were removed, because they can be represented as a convex combination of other points.
If we look at the plot, we can already see that and are the closest two points. However, if we didn’t know the answer yet, we could find it by solving the following optimization problem:
In words: find and (i.e. inside the convex hulls) such that the squared distance between the two points is minimized. The corresponding Python code looks like this:
In order to have only one vector to optimize, I added zeros to both matrices. The program returns the same and we found before.
The next step is to construct a normal vector between the points and .
Then find the distance from the origin to the point halfway between the two closest points along the normal.
We have finally found our separating hyperplane. This means
To make this more concrete, let us write the hyperplane in slope-intercept form.
The hyperplane can now be easily graphed.
And the graph is
To understand the red and blue “line”: the red line gives us the slope, any point on the blue line gives us the offset. All the points that are orthogonal to the red line lie on the blue line.
Another way to see this: draw a position vector (any point on the blue line), then place the tail of at the head of the arrow . By considering all points orthogonal to , we obtain again the blue line.
In the first method, we had to find the two closest points. This time we have to figure out which two points give us the optimal normal vector and then find two offsets , .
To find we could choose , but the second vector is harder to find. We can even choose a vector outside the sets and . For example, together with would not give us the maximal distance between the two hyperplanes.
Let us choose instead the two points and . The first point will be located on the first supporting hyperplane. Next, calculate .
Thus, and .
is the leftmost point in the set , therefore it will be on the second supporting hyperplane. Since the two hyperplanes are parallel, does not change. Then the second offset is .
The separating hyperplane is given by . Then multiply by . We obtain the same result as in method 1.
Let us plot the three hyperplanes.
Now, we will set up an optimization problem.
In words, maximize the distance between two parallel supporting hyperplanes. Hence, the constraints are and .
Since the distance between two planes is given by , maximizing this fraction is equivalent to minimizing and .
In the last section, we will solve the problem using standard QP.
The program returns
which means the distance is
Standard hard-margin SVM
If you look on the internet (e. g. Wikipedia), you will see the hard-margin optimization problem often written like this
We can show that this optimization problem is equivalent to the one stated above. First, transform the problem into vector form, rename the variables and do minor rearrangements.
Then set and .
Create a set and give each a value . The two optimization problems are now the same.
Quadratic Programming (QP)
Let us now solve the standard hard-margin problem with QP. We will use this time cvxopt and a more complicated example.
First, we have to write the optimization problem in standard form.
where is a weight vector, , , and .
Then the code is:
The solution is and .
Remember we set before and . Hence, the bias for the separating hyperplane is given by . This formulation of the optimization problem finds directly the separating hyperplane.
If you use method 1, you will find the same solution in terms of points and . The following plot shows both method 1 and method 2 (with QP).
 K. Bennett and E. Bredensteiner. Duality and Geometry in SVM Classifiers. In P. Langley, eds., Proc. of Seventeenth Intl. Conf. on Machine Learning, p. 57-64, Morgan Kaufmann, San Francisco, 2000.