Sub topic 2
Why the margin matters
This chapter assumes you know what a linear classifier is, how a hyperplane is written as w·x + b = 0, and how to read Python and NumPy code. The previous chapter showed that many hyperplanes can separate the same data. Here you will see how the margin ranks them, why the highest-ranked one is a sound choice, and how to compute it.
Many separators, one choice
Suppose two classes are linearly separable. Then infinitely many hyperplanes classify every training point correctly. The perceptron stops at the first one it finds, and which one that is depends on the starting weights and the order of the data. Two runs can give two different boundaries, and both have zero training error.
Training error cannot choose between them. We need a second criterion that measures how safely a boundary separates the classes. The margin is that criterion.
Functional and geometric margin
Use labels y in {-1, +1} and the score f(x) = w·x + b. A point is classified correctly when y·f(x) > 0.
- The functional margin of a point is y·(w·x + b). It is positive when the point is correct and larger when the point is more confident.
- The geometric margin of a point is y·(w·x + b) / ||w||. It is the signed Euclidean distance from the point to the hyperplane.
| Quantity | Formula | Changes if you rescale (w, b) by 10? | Meaning |
|---|---|---|---|
| Functional margin | y(w·x + b) | Yes, multiplied by 10 | Confidence in the score's own units |
| Geometric margin | y(w·x + b) / ‖w‖ | No | Real distance in feature space |
The functional margin is easy to inflate. Replacing (w, b) with (10w, 10b) describes the same hyperplane but gives every point ten times the functional margin. Comparing hyperplanes therefore requires the geometric margin, which does not change under rescaling.
The margin of a whole classifier is the smallest geometric margin over the training set. It is the distance from the hyperplane to the nearest training point. The maximum-margin classifier is the separating hyperplane for which this smallest distance is as large as possible.
Fixing the scale: the canonical form
Because (w, b) can be rescaled freely, we can choose a scale that simplifies the algebra. We require the nearest points to have functional margin exactly 1:
min over i of yi (w·xi + b) = 1
Under this convention the nearest points lie on the two hyperplanes w·x + b = +1 and w·x + b = -1. The distance from the decision boundary to each of them is 1/‖w‖, so the full width of the gap between the classes is 2/‖w‖.
Maximizing the margin 2/‖w‖ is therefore the same as minimizing ‖w‖, or equivalently ½‖w‖². The next chapter turns this observation into an optimization problem.
Computing a margin in code
The script below fits a linear SVM on a small separable set. It uses a very large C so the fit behaves like a hard margin, a case the next chapter covers. It then checks that the smallest geometric margin matches 1/‖w‖.
import numpy as np
from sklearn.svm import SVC
X = np.array([[1, 1], [2, 1], [1, 2], [4, 4], [5, 4], [4, 5]], dtype=float)
y = np.array([-1, -1, -1, 1, 1, 1])
clf = SVC(kernel="linear", C=1e6).fit(X, y)
w = clf.coef_[0]
b = clf.intercept_[0]
functional = y * (X @ w + b)
geometric = functional / np.linalg.norm(w)
print("functional margins:", np.round(functional, 3))
print("smallest geometric margin:", round(geometric.min(), 3))
print("1 / ||w||:", round(1.0 / np.linalg.norm(w), 3))
print("margin width 2 / ||w||:", round(2.0 / np.linalg.norm(w), 3))
print("support vectors:\n", clf.support_vectors_)You should see the smallest functional margin come out at about 1.0, which is the canonical scaling. The smallest geometric margin and 1/‖w‖ should agree, at about 1.77. Three points sit on the margin: (2, 1), (1, 2) and (4, 4). Those are the support vectors. The other three points have larger margins and do not affect the result.
Why a larger margin is better
There are three reasons to prefer the widest gap. They support one another, but they are not equally rigorous.
- Robustness to noise. Test points are drawn from the same distribution as training points but are not identical to them. A boundary that passes close to a training point misclassifies a new point that is only slightly displaced toward it. A boundary with a wide gap tolerates displacements up to the margin distance.
- Capacity control. For a fixed margin, a bounded data radius limits how many ways the classifier can arrange the points. Among hyperplanes that fit the data, a large margin picks a simple member of the family. This matches the minimization of ‖w‖, which acts as regularization.
- Generalization bounds. Classical results bound the test error of a margin classifier in terms of R²/γ², where R is the radius of a ball containing the data and γ is the margin. This ratio is independent of the number of features. That is why SVMs can work in very high dimensions, as the kernel chapters will show.
The bounds are often loose in practice, so treat them as justification for the principle and not as a way to predict error.
Edge cases and common errors
- Non-separable data. If no hyperplane separates the classes, no positive margin exists and the problem as stated has no solution. The soft-margin formulation, covered after the hard-margin chapter, relaxes the requirement.
- Outliers. The margin is defined by the nearest points, so one mislabeled or extreme point can shrink it sharply or move the hyperplane. This is the main weakness of the hard-margin version.
- Feature scale. The geometric margin is measured with Euclidean distance. If one feature has a range of thousands and another has a range of one, the distance is dominated by the large feature, and the "maximum margin" mostly reflects that feature's units. Scale your features first.
- Confusing the two margins. Reporting a large functional margin without normalizing by ‖w‖ says nothing about the boundary. Always check which margin a formula or library output refers to.
- Reading
decision_function. In scikit-learn,decision_function(X)returns w·x + b, a functional-style score. Dividing by ‖w‖ gives the signed distance.
Exercise
A linear SVM trained on separable data has ‖w‖ = 0.5 in the canonical scaling (nearest points have y(w·x + b) = 1). What is the distance from the decision boundary to the nearest point, and what is the total width of the gap? If you doubled w and b without moving the boundary, would the geometric margin change?
Show answer
The distance from the boundary to the nearest point is 1/‖w‖ = 1/0.5 = 2. The total gap width is 2/‖w‖ = 4. Doubling w and b describes the same hyperplane, and the geometric margin is unchanged because the factor of 2 cancels in y(w·x + b)/‖w‖. The functional margin would double to 2, which is why it cannot be used to compare hyperplanes.
Exercise
In the code above, you add the point (3, 3) with label +1 and refit with C=1e6. Is the data still separable, and which of the printed quantities would you expect to change?
Show answer
The point (3, 3) has coordinate sum 6, so it lies beyond the old margin line near x + y = 5.5 only partly: with the old hyperplane x + y = 5.5 it would fall on the negative side. The old boundary therefore misclassifies it. A different hyperplane may still separate all seven points, because (3, 3) lies farther from the negative points than (2, 1) and (1, 2) do. The margin shrinks, so 1/‖w‖ and 2/‖w‖ decrease, the support vectors change to include (3, 3), and the smallest functional margin stays near 1.