Sub topic 3
The hard-margin SVM
This chapter assumes you know the equation of a hyperplane, dot products, and what a constrained minimization problem is. You should also be comfortable with NumPy and scikit-learn. Here we turn the maximum-margin idea into an optimization problem you can write down and check in code.
Setup and notation
We have training pairs (xi, yi), where xi is a feature vector and yi is a label. For the SVM we encode the labels as -1 and +1, not 0 and 1. This lets one expression, yi(w·xi + b), say whether a point is on the correct side.
The decision boundary is the hyperplane w·x + b = 0. The classifier predicts sign(w·x + b).
- If yi(w·xi + b) > 0, point i is classified correctly.
- If it is negative, point i is on the wrong side.
A dataset is linearly separable if some (w, b) makes this value positive for every point. The hard-margin SVM only exists for such data.
From distance to margin
The distance from a point x to the hyperplane is |w·x + b| / ||w||. For a correctly classified point this equals y(w·x + b) / ||w||. The margin of a classifier is the smallest such distance over the training set.
There is a scaling freedom to deal with. Replacing (w, b) with (2w, 2b) describes the same hyperplane but doubles w·x + b. We remove the freedom by a convention: scale (w, b) so that the closest point satisfies
mini yi(w·x_i + b) = 1.
With this convention, the closest points on each side lie exactly on the hyperplanes w·x + b = +1 and w·x + b = -1. Each is 1/||w|| from the boundary, so the full gap between them has width 2/||w||.
The optimization problem
Maximizing 2/||w|| is the same as minimizing ||w||. We minimize ½||w||² instead, because it is smooth and the ½ cancels the 2 when we differentiate. The hard-margin SVM is:
minimize over w, b: ½ ||w||²
subject to: yi(w·xi + b) ≥ 1 for i = 1, …, n
Each constraint says that point i is correctly classified and at least 1/||w|| from the boundary. No point may sit inside the margin.
This is a convex quadratic program. The objective is a convex quadratic in w, and the constraints are linear in (w, b). Two consequences matter in practice:
- There are no local minima. Any solver that converges finds the global optimum.
- The optimal w is unique. (The intercept b is also unique when both classes are present.)
| Quantity | Meaning | Value at the optimum |
|---|---|---|
| y_i(w·x_i + b) | Functional margin of point i | At least 1 for every point |
| 1 / ‖w‖ | Distance from boundary to the nearest point | Maximized |
| 2 / ‖w‖ | Full margin width | Maximized |
| Points with y_i(w·x_i + b) = 1 | Points touching the margin | These are the support vectors |
The last row previews a later chapter. Only the points that touch the margin determine the solution.
Checking the solution in code
scikit-learn has no pure hard-margin solver. SVC always solves the soft-margin problem, which a later chapter introduces. Setting C very large penalizes any margin violation so heavily that, on separable data, the solution matches the hard-margin one. The code below uses this to verify the geometry above.
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.svm import SVC
X, y01 = make_blobs(
n_samples=60,
centers=[[-3.0, -3.0], [3.0, 3.0]],
cluster_std=0.8,
random_state=0,
)
y = 2 * y01 - 1 # convert labels from {0, 1} to {-1, +1}
clf = SVC(kernel="linear", C=1e6).fit(X, y)
w = clf.coef_[0]
b = clf.intercept_[0]
functional = y * (X @ w + b)
print("training accuracy:", clf.score(X, y))
print("smallest y(w.x + b):", functional.min())
print("margin width 2/||w||:", 2.0 / np.linalg.norm(w))
print("support vectors per class:", clf.n_support_)
print("values at support vectors:", np.round(functional[clf.support_], 4))You should see three things:
- Training accuracy is 1.0, so the data is separable.
- The smallest value of y(w·x + b) is about 1, which is the scaling convention at work.
- The values at the support vectors are all close to 1, because those points lie on the margin hyperplanes.
All other points have values above 1, so they are strictly outside the margin.
Why maximizing the margin helps
Many hyperplanes separate the same data, and all of them have zero training error. The hard-margin SVM picks the one farthest from every training point. The usual argument has two parts:
- A boundary far from the data tolerates small perturbations. A test point that is a slightly noisy copy of a training point is still on the correct side.
- A larger margin limits the capacity of the classifier. Statistical learning theory bounds the generalization error in terms of the ratio of the data radius to the margin, not just the number of features.
This is why the SVM can work well in high-dimensional spaces: the margin, not the dimension, controls complexity.
Failure modes
The hard-margin formulation is clean but fragile.
- No feasible solution. If the classes overlap, no (w, b) satisfies every constraint. The problem is infeasible, and a solver either fails or never converges. With
SVCand a huge C, you will see very slow training and aConvergenceWarningrather than a clean error. - Sensitivity to single points. One mislabeled or outlying point can force a tiny margin or tilt the boundary sharply, because every constraint must hold exactly.
- Numerical trouble with huge C. Very large C makes the solver's job harder. It converges slowly, and results can differ across versions.
- Unscaled features. The margin is measured with the Euclidean norm, so features on larger scales dominate the distance. A later chapter covers scaling.
Edge cases worth knowing
- One support vector per class is the minimum. In two dimensions with well-separated data, the optimum is often defined by as few as two or three points.
- Adding a far-away point does not change the solution. A correctly classified point with y(w·x + b) > 1 has an inactive constraint. Removing it leaves the optimum unchanged.
- Moving a support vector changes everything. Shifting a point on the margin changes the active constraints, so w and b change.
Exercise
A hard-margin SVM trained on separable data returns w = (3, 4). What is the full margin width, assuming the scaling convention where the closest points satisfy y(w·x + b) = 1? What happens to the width if you train again after deleting a point that has y(w·x + b) = 2.5?
Show answer
The norm is ||w|| = sqrt(3² + 4²) = 5, so the margin width is 2/5 = 0.4. The point with value 2.5 has an inactive constraint, because it is strictly outside the margin. Removing it does not change the optimum, so the width stays 0.4.
Exercise
Why do we minimize 0.5 * ||w||^2 instead of maximizing 2/||w|| directly, and why is the problem free of local minima?
Show answer
Maximizing 2/||w|| is equivalent to minimizing ||w||, and minimizing ½||w||² has the same minimizer while being smooth and quadratic. The objective is convex and all constraints are linear, so the problem is a convex quadratic program, and any local minimum is the global one.