Two lines can both classify every training point correctly while one passes far from the nearest point and the other almost touches it. A small change in that point, or a new point close to it, can flip the decision of the second line. A support vector machine chooses the boundary with the widest gap to the nearest points, and its soft-margin version lets some points sit inside the gap or on the wrong side at a price set by a parameter . This lesson separates the two kinds of margin, fits a small soft-margin model exactly and identifies the training points that determine it.
Key terms
Functional margin
Definition As in the previous lesson, labels are , the score is , and the prediction is when and otherwise. The functional margin of a labeled example is
and the functional margin of a training set is the smallest of these values, . A positive value means the example lies strictly on its correct side and a negative value means it lies on the wrong side. Multiplying and by a constant multiplies every functional margin by and leaves the boundary where it was.
In plain words The functional margin says whether a point is on its correct side and how large its score is there. Its size alone is not a distance, because rescaling the parameters changes it while the boundary stays put. A requirement such as a functional margin of at least gets a geometric meaning only together with the length of .
Example Take the constructed inputs , , and on a number line, with labels , , and . With and the functional margins are , , and , so the training set has functional margin . With and the boundary is still the point , yet the functional margins become , , and . A point with label would have functional margin under the first parameters, because it lies on the wrong side.
Geometric margin
Definition For the geometric margin of example is
the signed Euclidean distance from to the boundary , positive on the correct side. It does not change when and are multiplied by the same . The geometric margin of a training set is , which is the distance from the boundary to the nearest training point when every point is on its correct side. If the parameters are scaled so that the functional margin of the training set equals , the geometric margin equals . The hyperplanes and , called the margin hyperplanes, then lie at that distance on either side of the boundary and enclose a slab of width .
In plain words The geometric margin is a real distance in the units of the features. It says how far the nearest point could move before it reaches the boundary. Because it is a distance, it depends on how the features are measured, and rescaling one feature can change which boundary leaves the widest gap.
Example For , and the point with label , the functional margin is and , so the geometric margin is . The parameters and describe the same line and give functional margin but the same geometric margin . On the number line above, a threshold at separates the classes with geometric margin , set by the input , while a threshold at separates them with geometric margin , the largest possible.
Support vector machine
Definition A support vector machine (SVM) is a linear classifier whose weights and intercept are chosen to make the geometric margin of the training set large, either exactly, as in the hard-margin problem below, or in balance with margin violations, as in the soft-margin problem. For a linearly separable training set that contains both classes, the hard-margin SVM solves
The constraints fix the scale so that the functional margin is at least , and then the geometric margin is at least , so minimizing maximizes the geometric margin. The problem has a solution exactly when the set is linearly separable, the objective is convex, and the optimal is unique. New points are classified by the sign rule of the score.
In plain words Among all separating hyperplanes, the SVM picks the one in the middle of the widest empty slab between the classes. Squaring the length of and minimizing it, instead of maximizing a minimum distance directly, gives a convex problem that standard solvers handle well. A wide margin on the training data is a sensible preference, not a guarantee of lower error on new data. The SVM score is also not a probability, and turning it into one requires a separate calibration model fitted on held-out or cross-validated scores.
Example For the inputs , , and with labels , , and , the constraints of the two inner points are and . Adding them gives whatever is, so the smallest possible is , and with both constraints force . The outer points then have functional margin , so all constraints hold. The hard-margin SVM is with objective , its margin hyperplanes are and , and its geometric margin is .
Hinge loss
Definition For a label and a real score , the hinge loss is
With the functional margin, the loss is when , strictly between and when , exactly when and greater than when . An example misclassified by the rule that predicts when has and therefore loss at least , so the hinge loss is an upper bound on the zero-one loss of that rule. For a linear score it is a convex function of , being the larger of two affine functions, and it has a corner at where it is not differentiable.
In plain words The hinge charges nothing for a point that is on its correct side by at least the unit margin, and it charges linearly for anything less, including correctly classified points that sit too close to the boundary. Unlike the log loss of logistic regression, it is exactly zero beyond the margin, so points far on their correct side have no effect on the fit.
Example Functional margins of , , , and give hinge losses of , , , and . A point with label and score exactly is classified correctly by the sign rule and still costs , the same as a point with label and score , which is misclassified.
Soft margin
Definition For a penalty parameter , the soft-margin SVM solves
The slack variable measures how far example falls short of the unit functional margin. At the optimum , so the problem is the same as minimizing over and
a convex problem in which the intercept is not penalized. It is always feasible, for example with , and every . The optimal is unique, but the intercept need not be. Increasing never widens the slab of the solution and never raises its total hinge loss, and on separable data a large enough gives exactly the hard-margin solution. Some texts and software write the objective as plus the mean hinge loss, and that version has the same minimizers when . A value of or therefore means nothing without its convention and the sample size. Because margins are distances, features are usually standardized first, with the scaler fitted on the training data only, and inside each training fold when is chosen by cross-validation.
In plain words The parameter is the price of each unit of violation. A small accepts points inside the margin, or even on the wrong side, in exchange for a wide margin. A large narrows the margin to cut violations, which also lets a single mislabeled point pull hard on the boundary. Because violations are allowed, a soft-margin SVM still has a solution when the classes overlap, and it can leave training mistakes even when the classes are separable.
Example Fit the four inputs , , and again. The data are symmetric, since reflecting every input and label gives the same set. So if is optimal then so is , and by convexity their average is optimal too, which lets us set . The functional margins are then , , and , and negative weights only raise every loss above its value at . The total hinge loss is for , then for , and for . With the derivative of the objective is on the first piece and on the second, so the minimum is at the corner , where
less than the value of the hard-margin weight . At the total hinge loss is for , so is the only optimal intercept. The margin hyperplanes move out to , a slab of width . The inner points sit inside it with functional margin and hinge loss each, and all four points are still classified correctly. With the derivative is negative on both pieces, so the minimum moves to and the fit is the hard-margin SVM.
Support vector
Definition The soft-margin problem has a dual problem in variables with and , and at the optimum
The training examples with are the support vectors. The optimality conditions tie each to the functional margin . An example with has , an example with has , and an example exactly on a margin hyperplane, with , can have any from to . Every support vector therefore lies on a margin hyperplane, inside the margin or on the wrong side, while an example on a margin hyperplane with is not a support vector. Deleting an example with and refitting leaves the same solution optimal. The hard-margin problem has the same description without the upper bound .
In plain words The support vectors are the training points that hold the boundary in place. A point outside the margin on its correct side can be moved or deleted without changing the fit, as long as it stays outside the margin. Influence is not trustworthiness, and a mislabeled point inside the margin is a support vector with the largest weight the problem allows.
Example In the hard-margin fit of the four inputs, and in the soft-margin fit with , the dual weights for the inputs , , and are , , and . They satisfy and give . The support vectors are the inputs and , and moving the input to changes nothing. With the inner points have , so their weights are . The outer points lie exactly on the margin hyperplanes, and the conditions and give . All four points are support vectors at this smaller .
Check yourself
- A candidate soft-margin SVM has functional margins , and on three training examples, and . Find each hinge loss, say which examples are classified correctly and which satisfy the margin, and compute the objective .
Show the answer
The hinge losses are , and , with total . The first example is correct and outside the margin. The second is correct but inside the margin, a margin violation without a classification error. The third has a negative functional margin, so it is misclassified. The objective is
Under the mean-loss convention the same model would be scored with a different formula and parameter, so the number belongs to the sum convention with .
- Two linear classifiers are compared at the point with label . Model A has and , and model B has and . A classmate says that model B keeps this point four times as far from its boundary. Compute both kinds of margin and assess the claim.
Show the answer
Model A gives the score , so the functional margin is , and with the geometric margin is . Model B gives the score , so the functional margin is , and with the geometric margin is .
The claim is wrong. Model B is model A with every parameter multiplied by , so both describe the same line and the point is at the same distance from it. Only the functional margin grew, and it grows with any rescaling. This is why the SVM fixes the scale with the constraints before it measures the margin by .
- Refit the four inputs , , and , with labels , , and , using and the intercept , which is optimal by the symmetry argument above. Find the optimal weight, the width of the slab, the violations and the support vectors with their dual weights, and compare with and .
Show the answer
On the piece the objective is with derivative , so it decreases there. On the piece it is with derivative , which is zero at , inside the piece. For the objective increases. So , with objective , and the slab has width .
The functional margins are , , and . The inner points violate the margin, each with hinge loss , and are still classified correctly, while the outer points lie outside the margin. So the inner points have , the outer points have , and the support vectors are the inputs and . As a check, and . Here the intercept is not unique, since every from to gives the same objective, but the weight is.
Across the three fits, gives and a slab of width , gives and width about , and gives and width with no violations. Raising makes violations more expensive, so the margin narrows. In practice is chosen on validation data, not by the training objective.