Exercise: maximum margin and support vectors

Goal

By the end of this exercise you should be able to:

  • find the maximum-margin line, its street width and its support vectors for a few points by hand,
  • decide whether a given line is the max-margin line,
  • predict what the slack penalty C does to the street, with and without outliers,
  • compute and compare the hinge, 0–1, logistic and squared losses.

Do the pen-and-paper part first, then the two demos.

Background

Idea What it means
Street the region -1 \le f(\mathbf x) \le 1 around the line f = 0; its width is 2/\|\mathbf w\|
Margin constraint y_i f(\mathbf x_i) \ge 1: every training point on its side, outside the street
Max-margin line the widest street, i.e. the smallest \|\mathbf w\| that satisfies all constraints
Support vectors the training points on the edge of the street (soft margin: on, inside, or on the wrong side)
Slack \xi_i = \max(0, 1 - y_i f(\mathbf x_i)): how far a point falls short of the street’s edge
Soft margin minimise \tfrac12\|\mathbf w\|^2 + C\sum_i \xi_i; small C: wide street, many violations; large C: narrow street
Losses of m = y f 0–1: [m \le 0]; hinge: \max(0, 1 - m); logistic: \log_2(1 + e^{-m}); squared: (1 - m)^2

A recipe for a few points in 2D: find the closest pair of points of opposite classes (or the closest point and edge between the two groups). The max-margin line is the perpendicular bisector of that pair; check that no point lies inside the resulting street.

By hand

B1. Class -1: (1, 1), (1, 3). Class +1: (4, 2), (5, 4). (a) Sketch the points. What is the closest distance between the two classes? (Look at the point (4, 2) and the segment between the two blue points.) (b) Give the max-margin line, the street width, and the support vectors. (c) Scale \mathbf w, b so that f = \pm 1 on the edges of the street. Give \mathbf w, b and y f for all four points.

B2. Class -1: (0, 0), (1, 0). Class +1: (0, 2), (2, 3). (a) Is the line x_2 = 1 the max-margin line? Give the distances of the four points to it. (b) Is x_2 = 0.8? What is wrong with it? (c) Could a tilted line give a wider street? (What is the distance between (0, 0) and (0, 2)?)

B3. Fill in the table of losses for these margin values m = y f, and the total of each row:

y f 3 1.5 1 0.2 −0.5 −3
0–1
hinge
squared

Which point dominates the squared loss? Which points does the hinge loss ignore? Which point is correct but still costs something under the hinge?

B4. A soft-margin SVM gives five training points the margin values y f = 2.3,\ 1.0,\ 0.7,\ -0.2,\ 1.5. Which are support vectors? Which are on the edge of the street, which inside it, which on the wrong side? What are their slacks?

B5. Two well-separated clouds and one outlier of the red class deep among the blue points, but a line can still separate all points (only just). Predict what happens to the line, the street width and the number of training errors when C goes from 100 to 1 to 0.01.

B6. A linear SVM has \mathbf w = (3, 4). How wide is its street? If you double the slack penalty C and the street becomes narrower, does \|\mathbf w\| grow or shrink?

Math questions

B7 (math). Show that the distance between the lines \mathbf w\cdot\mathbf x + b = 1 and \mathbf w\cdot\mathbf x + b = -1 is 2/\|\mathbf w\|.

B8 (math). Show that at the optimum of the soft-margin problem, \xi_i = \max(0, 1 - y_i f(\mathbf x_i)), and derive the unconstrained form \tfrac12\|\mathbf w\|^2 + C\sum_i \max(0, 1 - y_i f(\mathbf x_i)).

B9 (math). For the lecture’s example, blue (0, 0), (0, 2), red (2, 1), (3, 0), the next unit will show that the solution has the form \mathbf w = \sum_i \alpha_i y_i\mathbf x_i with \sum_i \alpha_i y_i = 0. Check that \alpha = (0.25,\ 0.25,\ 0.5,\ 0) (in the order of the points) gives \mathbf w = (1, 0) and satisfies \sum_i \alpha_i y_i = 0. Which point has \alpha = 0, and why?

B1. (a) The blue points lie on the segment x_1 = 1, 1 \le x_2 \le 3. The closest point of that segment to (4, 2) is (1, 2), at distance 3 (inside the segment, not an end point). (5, 4) is farther from everything blue. (b) The perpendicular bisector of (1, 2) and (4, 2): the line x_1 = 2.5. The street is 1 \le x_1 \le 4, width 3. No point lies inside it. Support vectors: (1, 1), (1, 3) and (4, 2), on the edges. (c) f = w_1(x_1 - 2.5) with f(4, 2) = 1: w_1 = 1/1.5 = 2/3. So \mathbf w = (2/3, 0), b = -5/3, and the width is 2/\|\mathbf w\| = 3. y f: (1, 1): -1 \cdot (2/3)(1 - 2.5) = 1; (1, 3): 1; (4, 2): 1; (5, 4): (2/3)(2.5) = 5/3 = 1.67.

B2. (a) Distances to x_2 = 1: (0, 0): 1, (1, 0): 1, (0, 2): 1, (2, 3): 2. The street 0 \le x_2 \le 2 is empty, width 2. Yes (see (c)). (b) No: it is 0.8 from the blue points and 1.2 from (0, 2). Its street is only 2 \cdot 0.8 = 1.6 wide; shifting it to the middle, x_2 = 1, widens it to 2. (c) No. Any street that separates (0, 0) from (0, 2) lies between them, so its width is at most their distance, 2. The line x_2 = 1 reaches that, so it is the max-margin line (\mathbf w = (0, 1), b = -1).

B3.

y f 3 1.5 1 0.2 −0.5 −3 total
0–1 0 0 0 0 1 1 2
hinge 0 0 0 0.8 1.5 4 6.3
squared 4 0.25 0 0.64 2.25 16 23.14

(Logistic, for comparison: 0.07, 0.29, 0.45, 0.86, 1.41, 4.40.) The point at y f = -3 dominates the squared loss (16 of 23.14); note that the safe point at y f = 3 also costs 4 there. The hinge ignores the three points with y f \ge 1. The point at y f = 0.2 is correct but inside the street: hinge 0.8, 0–1 loss 0.

B4. Support vectors are the points with y f \le 1: 1.0 (on the edge, slack 0), 0.7 (inside the street, on the correct side, slack 0.3) and -0.2 (wrong side, slack 1.2). The points at 2.3 and 1.5 are not support vectors (slack 0).

B5. At C = 100 violations are expensive, and since a line can separate everything, the SVM squeezes the street past the outlier: a narrow street, tilted towards the outlier, 0 training errors. At C = 1 (or somewhere on the way) it becomes cheaper to give up the outlier: 1 training error, and the street jumps to a much wider one between the two clouds. At C = 0.01 the street widens further, many points of both clouds lie inside it and become support vectors; the line is set by the bulk of the data. (The demo’s “one outlier” preset shows exactly this.)

B6. Width 2/\|\mathbf w\| = 2/5 = 0.4. A narrower street means a larger \|\mathbf w\|: with a larger C the SVM accepts a larger \|\mathbf w\| to reduce the slack.

B7. Take \mathbf x_+ on f = 1 and move along the unit normal \mathbf n = \mathbf w/\|\mathbf w\|: f(\mathbf x_+ - t\mathbf n) = 1 - t\|\mathbf w\|, which reaches -1 at t = 2/\|\mathbf w\|. Moving along the normal is the shortest way between two parallel lines, so the distance is 2/\|\mathbf w\|. (Or: each line is at distance 1/\|\mathbf w\| from f = 0, by unit 1’s distance formula.)

B8. For fixed \mathbf w, b, each \xi_i appears only in C\xi_i (to be minimised, C > 0) and in the constraints \xi_i \ge 1 - y_i f(\mathbf x_i) and \xi_i \ge 0. The smallest allowed value is \xi_i = \max(0, 1 - y_i f(\mathbf x_i)). Substituting it gives the objective \tfrac12\|\mathbf w\|^2 + C\sum_i \max(0, 1 - y_i f(\mathbf x_i)) with no constraints left.

B9. \sum_i \alpha_i y_i \mathbf x_i = 0.25 \cdot (-1)(0, 0) + 0.25 \cdot (-1)(0, 2) + 0.5 \cdot (+1)(2, 1) + 0 = (0, -0.5) + (1, 0.5) = (1, 0). \sum_i \alpha_i y_i = -0.25 - 0.25 + 0.5 = 0. The point (3, 0) has \alpha = 0: it lies outside the street (y f = 2), and only points on or inside the street can have \alpha > 0.

Explore: the widest street

Click to add points of the chosen class (blue = -1, red = +1), drag points to move them, remove points to delete. The SVM is retrained after every change. The solid line is f = 0, the dashed lines the edges f = \pm 1, the grey band the street. Support vectors are ringed: a solid ring for a point on the edge of the street, a dashed ring for a point inside the street or on the wrong side; the yellow segment shows its slack (from the point to its edge of the street). The slider sets C (log scale); the readout gives f, the street width 2/\|\mathbf w\|, the support vectors and the training errors.

S1. Preset the by-hand example. Check the lecture’s numbers: f, the width, the support vectors. Drag the point (3, 0) around without entering the street: does anything change? Drag it into the street. Then drag the support vector (2, 1).

S2. Same preset. Lower C step by step from 1000. Below which value does the street start to widen? Read the width at C = 0.32 and C = 0.1. How many support vectors are there at C = 0.1, and where are they?

S3. Preset one outlier: hard vs soft. Read the width, the number of support vectors and the training errors at C = 1000, C = 1 and C = 0.1. Compare with your prediction in B5.

S4. Preset overlapping classes: sweep C. How do the number of support vectors and the street width change from C = 0.01 to C = 1000? Does the number of training errors change much?

S5. Preset C large = narrow street. Two points near the middle force a narrow street at large C. At which C does the street open up, and what happens to the support vectors?

S1. f(\mathbf x) = 1.00\,x_1 + 0.00\,x_2 - 1.00, street width 2.00, 3 support vectors, all on the edge, 0 errors. Moving (3, 0) anywhere with x_1 > 2 changes nothing: it is not a support vector. Once it enters the street (x_1 < 2) it becomes a support vector and the street narrows. Dragging (2, 1) turns and shifts the line: it is one of the three points that fix it.

S2. The street stays at width 2 down to C = 0.5; at C = 0.40 it is already 2.51. At C = 0.32 the width is 3.00 (\mathbf w = (0.67, 0)); at C = 0.1 it is 3.92, and all 4 points are support vectors inside the street (dashed rings, each with \alpha = C). Making violations cheap trades a few points inside the street for a wider one.

S3. C = 1000: width 0.61, 3 support vectors, 0 errors: the street squeezes between the outlier and the blue cloud. C = 1: width 1.94, 5 support vectors (3 on the edge, 2 inside or wrong), 1 error: the outlier is given up. C = 0.1: width 2.89, 8 support vectors, still 1 error. As predicted in B5: one point decides the hard-margin line; the soft margin follows the bulk of the data.

S4. C = 0.01: 46 support vectors, width 4.09, 4 errors. C = 1: 10 support vectors, width 1.22, 3 errors. C = 1000: 7 support vectors, width 0.86, 3 errors. The street narrows and fewer points lie in it as C grows, but the training error hardly changes: the classes overlap, and no line does better than about 3 errors.

S5. For C \ge 10: width 1.39 and 3 support vectors. At C = 1 the width is 1.44 (one point already inside the street); at C = 0.1 the street opens to 3.05 with 8 support vectors, and at C = 0.01 to 4.78 with 26. There are no training errors at any C: the wider street simply lets the two middle points (and others) inside it.

Explore: four losses

The top-left plot shows the four losses against the margin value y f (tick boxes to show or hide them); the dots are the data points. The bottom plot shows a 1D data set (blue = -1, red = +1) and the classifier f(x) = a(x - t): drag in the plot to move the threshold t; the slider sets the slope a; the grey band is the street |f| < 1. Each point carries a bar: its loss under the loss chosen below “bars under each point”. The top-right plot shows the average loss against t; the buttons “move t to the minimum of” find the best t for each loss (slope fixed).

H1. Preset by hand: y f = 2, 1, 0.5, 0, −1. Check the lecture’s table: the total of each loss in the readout.

H2. Preset an outlier on the wrong side. What is the outlier’s squared loss, and the total squared loss? Its hinge loss? Now move t to the minimum of each loss in turn. Which loss lets the outlier move the threshold the least, and which the most?

H3. Preset points far on the right side. What is the total hinge loss at t = 5? The total logistic loss? Move t to the minimum of the squared loss: in which direction does it move, and why?

H4. Preset 0–1 has flat steps. Look at the right plot. Why can you not find the minimum of the 0–1 loss by walking downhill from t = 1? Where are the minima of the 0–1 and the hinge losses?

H1. Totals: 0–1 2.00, hinge 3.50, logistic 4.21, squared 6.25, as in the lecture.

H2. At t = 5 the outlier has y f = -4.5: squared loss 5.5^2 = 30.25 of a total 40.75 (more than all other points together), hinge loss 5.5 of a total 5.5 (the other points are outside the street). Minima: 0–1 at t = 5 (it does not care how far the outlier is), hinge 4.75, squared 4.39, logistic 4.03. The 0–1 loss moves least (not at all), then the hinge. The logistic loss moves most here: the outlier pulls with a constant slope, as for the hinge, but the other points push back only weakly, because the logistic loss is small and flat for points that are already correct; the hinge pushes back fully as soon as a blue point enters the street.

H3. Hinge total 0 (every point has y f \ge 1), logistic 1.41 (never exactly 0). The squared-loss minimum moves right, to t = 5.37: the red points far on the right have y f up to 4.5, and the squared loss punishes them for being “too correct”, pulling the threshold towards them. The hinge and logistic losses do not have this flaw.

H4. The average 0–1 loss is constant between data points and jumps at them: its slope is 0 almost everywhere, so a downhill walk has no direction to go. Its minimum (1 error of 10) is a flat stretch; the button picks the middle, t = 5.5. The hinge loss slopes smoothly; its minimum is at t = 5.1.

Code

Write the algorithms of this unit yourself, in Rust. Setup, and how to run and check your code: coding exercises: getting started. Your code goes in svm/src/maxmargin/exercise.rs. A point is [x1, x2]; the labels are +1 and -1.

C1. losses(m): the 0–1, hinge, logistic (in bits) and squared loss of a point with margin value m = y\,f(\mathbf x). The checks are the lecture’s table and your table of B3; the slacks of B4 are hinge losses.

C2. margins(points, labels, w, b): the margin value y_i\,f(\mathbf x_i) of every training point. The check is B1(c).

C3. objective(points, labels, w, b, c): the soft-margin objective \tfrac12\|\mathbf w\|^2 + C\sum_i \max(0,\ 1 - y_i f(\mathbf x_i)).

C4. train(points, labels, c, steps): the linear SVM, by walking downhill on that objective (the subgradient of the slide “The math: the soft-margin problem”). The hinge has a kink, so the objective does not fall at every step; keep the best (\mathbf w, b) seen:

w = (0, 0);  b = 0
for t = 1, 2, ..., steps:
    g_w = w;  g_b = 0
    for every training point (x, y) with y · f(x) < 1:     # inside the street or wrong
        g_w = g_w − C · y · x
        g_b = g_b − C · y
    move (w, b) a distance 1/√t against (g_w, g_b)          # divide by its length first
    remember (w, b) if its objective is the smallest so far
return the remembered (w, b)

The checks are the lecture’s example (the street 0 \le x_1 \le 2, \mathbf w = (1, 0), b = -1) and B1 (the line x_1 = 2.5, width 3).

Then cargo run -p svm -- maxmargin and open the pictures in out/maxmargin/: the street in grey, the support vectors ringed (all points with y f \le 1, with a little tolerance, because walking downhill is never exact).

C5. The lecture’s example for C = 1000, 1, 0.32, 0.1 (lecture-c*.png). At which C does the street start to widen? Compare the widths with the demo (question S2).

C6. Two clouds and one red point close to the blue cloud, for C = 100, 1, 0.1, 0.01 (outlier-c*.png). Read the width, the number of support vectors and the training errors. Compare with your prediction in B5.

C7. Count the cost: one step of C4 looks at every training point once. How does the cost of training grow with the number of points n? Why does it hardly depend on C?

The model solution is svm/src/maxmargin/solution.rs in the download.

C5. For C = 1000 and C = 1 the street is the hard-margin one: \mathbf w = (1, 0), b = -1, width 2.00, 3 support vectors. At C = 0.32 it has widened to 2.99 (\mathbf w = (0.67, 0)) and all 4 points are support vectors; at C = 0.1 the width is 3.92, with \mathbf w = (0.50, -0.10). These are the demo’s numbers (S2): 3.00 and 3.92.

C6.

C width support vectors errors
100 0.92 3 0
1 1.64 5 0
0.1 4.16 6 1
0.01 5.09 20 1

At C = 100 the street squeezes between the red outlier and the blue cloud and is tilted towards it. At C = 1 it is wider (1.64), the outlier now lies inside it, and the red cloud touches its other edge. At C = 0.1 the outlier is given up (1 error): the street lies in the middle between the two clouds and is set by the bulk of the data. At C = 0.01, 20 of the 51 points are support vectors. As predicted in B5.

C7. Proportional to n per step, so n \times steps in all. Because each step has the same length 1/\sqrt t whatever the size of the gradient, C changes the direction of the steps, not their size. (Plain gradient steps, proportional to the gradient, would be about C times too large for large C, and too small for small C.)

Where this leads

The max-margin line is still a line. The next unit keeps all of this, but measures similarity with a kernel function instead of a dot product: the same street, in a feature space we never write down, gives curved boundaries in the original one.