The Math Behind the Machine/ Unit 13 · Support Vector Machines Checks 0/20
Unit 13 of 20 · by Prof. Saurabh

Support Vector Machines

Two groups of points, and a straight line to split them. There are many lines that make no mistakes at all, so which one should you draw? This unit's answer is simple to say: draw the widest empty street you can between the two groups, and put the line down its middle. Then we turn that sentence into mathematics. The width becomes 2/∥w∥2/\|\mathbf w\|, the search becomes a bowl with exactly one bottom, and the answer turns out to rest on a few points that touch the street. Last, we bend the street. Lift the points into a bigger space, cut them there with a flat plane, and the cut comes back down as a curve — without ever visiting the big space.

≈ 120 min read + play 16 interactive widgets · 8 in 3D · a live solver you can drag 20 inline checks 🧾 21 proofs, folded away — open "if you want the algebra" when you are ready ✍ 16 solved practice problems

← Unit 12 · Principal Component Analysis

drag the scene to orbit
1

Which line should you draw?

Imagine this

It is sports day. The blue house is warming up on one side of the school field, the orange house on the other. The PT teacher must chalk one straight line between them.

Many lines would do. Any line with every blue child on one side and every orange child on the other makes no mistake today.

But a good teacher chalks it down the middle of the widest empty strip. Then a child who steps a little forward tomorrow is still on her own side.

Here is the same problem with points. Twelve points on a page: six blue ones (class +1+1) and six orange ones (class −1-1). We want a straight line with all the blue points on one side and all the orange points on the other.

Draw one such line. Now tilt it a little, or slide it a little. It still splits the points perfectly. So there are infinitely many lines with zero mistakes. Counting mistakes on the training points cannot choose between them.

What should choose, then? Think about the next point. It will land somewhere near an old point, but not exactly on top of it. A line that passes within a hair of an old point will get that neighbourhood wrong the moment a point wobbles. A line with a wide, empty buffer on both sides can absorb the wobble.

So here is the whole idea of this unit in one line: pave the widest empty street between the two groups, and draw the line down its middle. No point of either group may stand inside the street. The width of that street is called the margin.

For each tilt, there is a best street: push its two edges outwards until each one touches a point. Then change the tilt and do it again. Try it below.

The widest street, one tilt at a timeFor every tilt the street is already pushed out until it touches a point on each side. Only one tilt gives the widest street.

Try: Slide the tilt slowly and watch the ringed points: they change as the tilt changes. Then press ▶ find the widest and watch the dot on the width curve climb to its peak.

drag the picture to orbit

20°
—

Notice two things. First, the width changes a lot with the tilt: about 0.840.84 at 20∘20^\circ, about 1.51.5 at 70∘70^\circ, and 2.832.83 at the best tilt, 45∘45^\circ. Second, the ringed points change as you tilt. Which points touch the street is a result of the tilt, not something you choose.

Why is the widest street a good choice? Four reasons.

  • It forgives wobble. Every point can move by up to half the street width in any direction and still stay on its own side.
  • It picks one answer. Out of infinitely many perfect lines, "widest" picks exactly one.
  • It travels well to new data. There is real theory saying that the error on new points is controlled by the margin compared with how spread out the data is, not by how many features you measured.
  • It is cheap to keep. The answer ends up depending only on the few points that touch the street. The points deep inside each group do not matter at all. (Section 9 proves this.)
The realization

margin=2×dnearest\text{margin}=2\times d_{\text{nearest}}

The margin is the width of the widest empty street: twice the distance dnearestd_{\text{nearest}} from the line to the nearest point. Training error cannot choose between the many perfect lines. The margin can. Among all lines that make no mistake, take the one whose street is widest. Everything else in this unit is turning that sentence into mathematics and solving it.

Pause & predict

Three straight lines all split the training points with zero mistakes. Why can't the number of mistakes pick the best one?

The road ahead. The unit has four acts.

  1. The widest street (§2–§5): measure distances to a line, find the street's width, fix a hidden scale, and write the problem down.
  2. Prices on points (§6–§9): give every point a price, build the boundary out of those prices, solve a second problem that uses only the prices, and meet the few points that hold the street up.
  3. The messy world (§10–§11): let a few points break the rule, pay a fine for it, and watch the hinge loss appear.
  4. The kernel trick (§12–§15): see that the data only ever meets itself through dot products, lift the data so a flat cut becomes a curve, and do it all without visiting the big space.

In one sentence: Many lines make no mistakes, so choose the one with the widest empty street around it — it forgives wobble, it is unique, and it rests on only a few points.

2

The boundary's compass

Imagine this

A straight road runs across a field. You stand somewhere in the grass and want to know how far you are from the road.

You would not walk at a slant. You would walk straight across, at a right angle to the road. That shortest walk is your distance.

So every straight road comes with a hidden arrow: the direction that points straight across it. Know that arrow, and you can measure anything.

A straight line in the plane can be written as w⋅x+b=0\mathbf w\cdot\mathbf x+b=0. Take the line x1+2x2=6x_1+2x_2=6. Move the 6 across: x1+2x2−6=0x_1+2x_2-6=0. So w=(1,2)\mathbf w=(1,2) and b=−6b=-6.

For any point x\mathbf x, the number f(x)=w⋅x+bf(\mathbf x)=\mathbf w\cdot\mathbf x+b is its score. The score is zero on the line, positive on one side and negative on the other. The classifier says "blue" when the score is positive and "orange" when it is negative.

Fact 1: w\mathbf w points straight across the line. Take two points on the line, p=(6,0)\mathbf p=(6,0) and q=(0,3)\mathbf q=(0,3). Both have score 0. Subtract the two scores and bb cancels: w⋅(p−q)=0\mathbf w\cdot(\mathbf p-\mathbf q)=0. Check: p−q=(6,−3)\mathbf p-\mathbf q=(6,-3) and (1,2)⋅(6,−3)=6−6=0(1,2)\cdot(6,-3)=6-6=0. The arrow p−q\mathbf p-\mathbf q runs along the line, and w\mathbf w is at a right angle to it. So w\mathbf w is the "straight across" arrow — the normal.

Fact 2: bb only slides the line. Change bb and the line moves sideways, staying parallel. It never turns. The tilt is set by w\mathbf w alone.

Fact 3: the score divided by the length of w\mathbf w is the distance. Take the point (4.4, 5.2)(4.4,\,5.2). Its score is 4.4+10.4−6=8.84.4+10.4-6=8.8. The length of w\mathbf w is ∥w∥=1+4=5≈2.236\|\mathbf w\|=\sqrt{1+4}=\sqrt5\approx2.236. The true distance to the line is 8.8/2.236≈3.9358.8/2.236\approx3.935. The score alone is not a distance; divide by ∥w∥\|\mathbf w\| and it becomes one.

The distance formula, liveDrag the probe. The dashed drop is the straight-across walk, and its length is |f(x)| / ‖w‖.

Try: Drag the probe onto the line and watch the score hit 0. Then turn the b knob: the line slides but never turns. Turn w's angle: now it turns, and the green arrow w^\hat{\mathbf w} turns with it, always at a right angle.

drag the probe

63.4°
−6
—

Two different "margins" hide in this picture, and people mix them up.

NameFormulaWhat it is
functional marginyi(w⋅xi+b)y_i(\mathbf w\cdot\mathbf x_i+b)the raw score, made positive when the point is on its correct side. Not a distance: double w\mathbf w and bb and it doubles.
geometric marginyi(w⋅xi+b)/∥w∥y_i(\mathbf w\cdot\mathbf x_i+b)/\|\mathbf w\|a real distance, in the units of your data. Double w\mathbf w and bb and it does not change.

Multiplying by the label yi=±1y_i=\pm1 makes both numbers positive exactly when the point is on the correct side. Section 4 is all about the gap between these two.

The realization

d(z)=w⋅z+b∥w∥=f(z)∥w∥d(\mathbf z)=\frac{\mathbf w\cdot\mathbf z+b}{\|\mathbf w\|}=\frac{f(\mathbf z)}{\|\mathbf w\|}

w\mathbf w is the compass: it points straight across the boundary and sets its tilt. bb only slides the boundary. And a score becomes a distance only after you divide by ∥w∥\|\mathbf w\|.

Pause & predict

How far is the point (6,3)(6,3) from the line x1+2x2=6x_1+2x_2=6, and on which side?

Pause & predict

You keep w=(1,2)\mathbf w=(1,2) but change bb from −6-6 to −10-10. What happens to the line?

If you want the algebra · 2 proofs, step by step
Prove it · w points straight across the line

Claim. For the line w⋅x+b=0\mathbf w\cdot\mathbf x+b=0, the vector w\mathbf w is at a right angle to every direction that runs along the line. Changing bb moves the line parallel to itself.

1
Take any two points p,q\mathbf p,\mathbf q on the line. Both have score zero: w⋅p+b=0,w⋅q+b=0.\begin{gathered}\mathbf w\cdot\mathbf p+b=0,\\ \mathbf w\cdot\mathbf q+b=0.\end{gathered} Being "on the line" means exactly this.
2
Subtract the second from the first. The bb cancels: w⋅(p−q)=0.\mathbf w\cdot(\mathbf p-\mathbf q)=0. A zero dot product means a right angle. And p−q\mathbf p-\mathbf q is an arrow from one point of the line to another, so it runs along the line.
3
Every direction along the line is such a difference, so w\mathbf w is at a right angle to all of them: w\mathbf w is the normal. The set {x:w⋅x=−b}\{\mathbf x:\mathbf w\cdot\mathbf x=-b\} for a new bb has the same normal, so it is a parallel line, shifted along w\mathbf w. ∎ Example: on x1+2x2=6x_1+2x_2=6, p=(6,0)\mathbf p=(6,0), q=(0,3)\mathbf q=(0,3) give (1,2)⋅(6,−3)=0(1,2)\cdot(6,-3)=0.
Prove it · the distance formula

Claim. The signed distance from a point z\mathbf z to the line w⋅x+b=0\mathbf w\cdot\mathbf x+b=0 is t=w⋅z+b∥w∥t=\dfrac{\mathbf w\cdot\mathbf z+b}{\|\mathbf w\|} — positive on the side w\mathbf w points to.

1
Walk from z\mathbf z straight across, along the unit normal w^=w/∥w∥\hat{\mathbf w}=\mathbf w/\|\mathbf w\|, a signed distance tt, landing at z−tw^\mathbf z-t\hat{\mathbf w}. The landing point is on the line: w⋅(z−tw^)+b=0.\mathbf w\cdot(\mathbf z-t\hat{\mathbf w})+b=0. The shortest route to a line is the straight-across one, so this tt is the distance.
2
Open the bracket, and use w⋅w^=w⋅w/∥w∥=∥w∥2/∥w∥=∥w∥\mathbf w\cdot\hat{\mathbf w}=\mathbf w\cdot\mathbf w/\|\mathbf w\|=\|\mathbf w\|^2/\|\mathbf w\|=\|\mathbf w\|: w⋅z+b−t ∥w∥=0⇒ t=w⋅z+b∥w∥=f(z)∥w∥.\begin{aligned}&\mathbf w\cdot\mathbf z+b-t\,\|\mathbf w\|=0\\ &\Rightarrow\ t=\frac{\mathbf w\cdot\mathbf z+b}{\|\mathbf w\|}=\frac{f(\mathbf z)}{\|\mathbf w\|}.\end{aligned} ∎ For (4.4,5.2)(4.4,5.2) and x1+2x2=6x_1+2x_2=6: t=8.8/5≈3.935t=8.8/\sqrt5\approx3.935. The raw score and the distance differ by exactly the factor ∥w∥\|\mathbf w\|.

In one sentence: In w⋅x+b=0\mathbf w\cdot\mathbf x+b=0, w\mathbf w points straight across the line and sets its tilt, bb slides it, and the score f(z)f(\mathbf z) divided by ∥w∥\|\mathbf w\| is the true distance.

3

How wide is the street?

Imagine this

Two rows of autos are parked on either side of a road. The road's width is the gap between the two rows.

You could measure from any auto on the left to any auto on the right. Measure at a slant and you get a bigger number. Only the straight-across measurement gives the true width.

That is the whole of this section: the slanted measurements are always at least the true width, and the straight one is exactly it.

Start from the boundary w⋅x+b=0\mathbf w\cdot\mathbf x+b=0. Push two parallel lines outwards, one to each side, until each one touches the nearest point of its group. Parallel lines share the same w\mathbf w and differ only in the constant:

w⋅x+b=+candw⋅x+b=−c.\mathbf w\cdot\mathbf x+b=+c\qquad\text{and}\qquad \mathbf w\cdot\mathbf x+b=-c.

Call the touching points x+\mathbf x^+ (on the +c+c edge) and x−\mathbf x^- (on the −c-c edge). Write both equations and subtract. The bb cancels, which is the whole reason we subtract:

w⋅(x+−x−)=2c.\mathbf w\cdot(\mathbf x^+-\mathbf x^-)=2c.

This is one number, a dot product. We want a distance. The tool is the Cauchy–Schwarz inequality: for any two arrows, ∣u⋅v∣≤∥u∥ ∥v∥|\mathbf u\cdot\mathbf v|\le\|\mathbf u\|\,\|\mathbf v\|, with equality only when they point along the same line. Use it with u=w\mathbf u=\mathbf w:

2c=w⋅(x+−x−)≤∥w∥ ∥x+−x−∥⟹∥x+−x−∥≥2c∥w∥.\begin{aligned}2c&=\mathbf w\cdot(\mathbf x^+-\mathbf x^-)\\ &\le\|\mathbf w\|\,\|\mathbf x^+-\mathbf x^-\|\\ \Longrightarrow\quad\|\mathbf x^+-\mathbf x^-\|&\ge\frac{2c}{\|\mathbf w\|}.\end{aligned}

Read it in words. Any point of one edge is at least 2c/∥w∥2c/\|\mathbf w\| away from any point of the other edge. And the bound is reached — by the pair of points that face each other straight across, along w\mathbf w. The smallest distance between the two edges is the width of the street:

The Cauchy–Schwarz bound, liveEach point slides along its own edge. The straight-across gap stays pinned; the point-to-point distance can only be bigger.

Try: Drag the blue point along the lower edge and the orange point along the upper edge. Make the orange length as short as you can. It reaches the blue gap only when the joining line runs parallel to the green arrow w\mathbf w.

drag either point along its edge

—

Here w=(−12,−12)\mathbf w=(-\tfrac12,-\tfrac12), b=52b=\tfrac52, c=1c=1: the edges are x1+x2=3x_1+x_2=3 (score +1+1) and x1+x2=7x_1+x_2=7 (score −1-1), so ∥w∥=0.5≈0.707\|\mathbf w\|=\sqrt{0.5}\approx0.707 and the width is 2/0.707≈2.8282/0.707\approx2.828.

Watch out — the step people botch

From w⋅(x+−x−)=2c\mathbf w\cdot(\mathbf x^+-\mathbf x^-)=2c it is tempting to write "x+−x−=2c/w\mathbf x^+-\mathbf x^-=2c/\mathbf w". That is not allowed: you cannot divide by a vector. A dot product squashes two arrows into one number, and you cannot un-squash it by dividing.

The conclusion is also wrong in substance: the two touching points can be much further apart than the width. Only the inequality, or a projection onto w/∥w∥\mathbf w/\|\mathbf w\|, turns the number 2c2c into a width.

The realization

street width  =  2c∥w∥\text{street width}\;=\;\frac{2c}{\|\mathbf w\|}

Subtracting the two edge equations kills bb and leaves w⋅(x+−x−)=2c\mathbf w\cdot(\mathbf x^+-\mathbf x^-)=2c. Cauchy–Schwarz turns that single number into a distance, and tells us it is the shortest distance across — which is exactly what the width of a street means.

Pause & predict

A classmate writes: "w⋅(x+−x−)=2c\mathbf w\cdot(\mathbf x^+-\mathbf x^-)=2c, so x+−x−=2c/w\mathbf x^+-\mathbf x^-=2c/\mathbf w, so the width is 2c/∥w∥2c/\|\mathbf w\|." What is wrong?

If you want the algebra · 1 proof, step by step
Prove it · the street is 2c/‖w‖ wide

Claim. If x+\mathbf x^+ lies on w⋅x+b=c\mathbf w\cdot\mathbf x+b=c and x−\mathbf x^- on w⋅x+b=−c\mathbf w\cdot\mathbf x+b=-c, then ∥x+−x−∥≥2c/∥w∥\|\mathbf x^+-\mathbf x^-\|\ge 2c/\|\mathbf w\|, with equality exactly when x+−x−\mathbf x^+-\mathbf x^- points along w\mathbf w. So the smallest distance between the two edges — the width — is 2c/∥w∥2c/\|\mathbf w\|.

1
Subtract the two edge equations; bb cancels: w⋅x++b−(w⋅x−+b)=c−(−c)⟹ w⋅(x+−x−)=2c.\begin{aligned}&\mathbf w\cdot\mathbf x^++b-(\mathbf w\cdot\mathbf x^-+b)\\ &\quad=c-(-c)\\ &\Longrightarrow\ \mathbf w\cdot(\mathbf x^+-\mathbf x^-)=2c.\end{aligned}
2
Cauchy–Schwarz says ∣u⋅v∣≤∥u∥ ∥v∥|\mathbf u\cdot\mathbf v|\le\|\mathbf u\|\,\|\mathbf v\|, with equality only when u\mathbf u and v\mathbf v are parallel. With u=w\mathbf u=\mathbf w, v=x+−x−\mathbf v=\mathbf x^+-\mathbf x^-: 2c=∣w⋅(x+−x−)∣≤∥w∥ ∥x+−x−∥,\begin{aligned}2c&=|\mathbf w\cdot(\mathbf x^+-\mathbf x^-)|\\ &\le\|\mathbf w\|\,\|\mathbf x^+-\mathbf x^-\|,\end{aligned} so ∥x+−x−∥≥2c∥w∥.\|\mathbf x^+-\mathbf x^-\|\ge\frac{2c}{\|\mathbf w\|}. We may divide by ∥w∥\|\mathbf w\|, a positive number. Dividing by the vector w\mathbf w is what is not allowed.
3
The bound is reached. Start at any x+\mathbf x^+ and walk straight across by x−=x+−2c∥w∥2w\mathbf x^-=\mathbf x^+-\dfrac{2c}{\|\mathbf w\|^2}\mathbf w. Its score is c−2c∥w∥2∥w∥2=−cc-\dfrac{2c}{\|\mathbf w\|^2}\|\mathbf w\|^2=-c, so it lies on the other edge, and its distance from x+\mathbf x^+ is 2c∥w∥2∥w∥=2c∥w∥\dfrac{2c}{\|\mathbf w\|^2}\|\mathbf w\|=\dfrac{2c}{\|\mathbf w\|}. ∎ The smallest distance between the two edges is the width of the street, so the width is 2c/∥w∥2c/\|\mathbf w\|. One inequality did two jobs: it gave the number, and it proved nothing is closer.

In one sentence: The street's edges are w⋅x+b=±c\mathbf w\cdot\mathbf x+b=\pm c; subtracting leaves w⋅(x+−x−)=2c\mathbf w\cdot(\mathbf x^+-\mathbf x^-)=2c, and Cauchy–Schwarz turns that into the width 2c/∥w∥2c/\|\mathbf w\|, reached only straight across.

4

The scale is yours to choose

Imagine this

A cricket pitch is 22 yards long. In feet that is 66. In inches, 792. The pitch did not change at all. Only the numbers did.

The street has the same kind of freedom. Its equations can be written with many different sets of numbers, and the street stays exactly where it is. So we may pick the most convenient numbers.

Section 3 left one loose end: the constant cc. Take the three lines

w⋅x+b=+c,w⋅x+b=0,w⋅x+b=−c,\begin{gathered}\mathbf w\cdot\mathbf x+b=+c,\\ \mathbf w\cdot\mathbf x+b=0,\\ \mathbf w\cdot\mathbf x+b=-c,\end{gathered}

and multiply every number by k=3k=3. The first line becomes 3w⋅x+3b=3c3\mathbf w\cdot\mathbf x+3b=3c. Divide by 3 and you are back where you started. It is the same set of points. All three lines stay put. But every number has tripled: ∥w∥\|\mathbf w\|, cc and every score.

QuantityBeforeAfter multiplying by kkReal geometry?
the three lines, as sets of points—identical✔ real
∥w∥\|\mathbf w\|∥w∥\|\mathbf w\|k∥w∥k\|\mathbf w\|✘ your choice
the constant cccckckc✘ your choice
the scores yif(xi)y_if(\mathbf x_i)ziz_ikzikz_i✘ your choice
the width 2c/∥w∥2c/\|\mathbf w\|2c/∥w∥2c/\|\mathbf w\|2kc/(k∥w∥)=2c/∥w∥2kc/(k\|\mathbf w\|)=2c/\|\mathbf w\|✔ real

So cc and ∥w∥\|\mathbf w\| mean nothing on their own; only their ratio is geometry. Since the choice is ours, choose k=1/ck=1/c. That makes the constant exactly 1:

w⋅x+b=+1,w⋅x+b=0,w⋅x+b=−1.\begin{gathered}\mathbf w\cdot\mathbf x+b=+1,\\ \mathbf w\cdot\mathbf x+b=0,\\ \mathbf w\cdot\mathbf x+b=-1.\end{gathered}

This is the canonical form. Two gifts follow. The width is now 2/∥w∥2/\|\mathbf w\|, so "shrink ∥w∥\|\mathbf w\|" finally means "widen the street". And the nearest points score exactly 1, so every point obeys a score of at least 1.

The same thing happens in three dimensions, where the street is a slab between two parallel planes. Slide kk below and watch every number move while the slab stays frozen.

Rescale everything; nothing movesA slab in 3-D between the planes x₁+x₂+x₃ = 2 and = 6. Multiply w, b and c by k: the arrow for w stretches, every score changes, the slab does not.

Try: Drag kk from 1 to 3. The green arrow kwk\mathbf w grows three times longer, the scores read ±3\pm3, and the slab does not move a millimetre. Then press look edge-on: the whole 3-D picture collapses to the familiar 2-D street.

drag the picture to orbit

1
—

In canonical form the rule for a correct point splits by class: blue points need w⋅xi+b≥+1\mathbf w\cdot\mathbf x_i+b\ge+1, orange points need w⋅xi+b≤−1\mathbf w\cdot\mathbf x_i+b\le-1. Multiply the orange rule by −1-1 (which flips the inequality) and it reads −(w⋅xi+b)≥1-(\mathbf w\cdot\mathbf x_i+b)\ge1. But for an orange point yi=−1y_i=-1, so that is yi(w⋅xi+b)≥1y_i(\mathbf w\cdot\mathbf x_i+b)\ge1. The blue rule is the same sentence with yi=+1y_i=+1. One inequality does the work of two:

yi(w⋅xi+b)≥1for every point i.y_i(\mathbf w\cdot\mathbf x_i+b)\ge1\qquad\text{for every point }i.

This is the only reason SVMs label the classes ±1\pm1 and not 0 and 1: the label does the sign-flipping for you.

One more trap. Why not simply make the smallest score as large as possible? Because of this very table. Multiply w\mathbf w and bb by a million and every score is a million times bigger, while the line has not moved. An optimiser would run k→∞k\to\infty and report infinite confidence in a line it never improved. Fixing the scale — score 1 for the nearest points, then make ∥w∥\|\mathbf w\| small — is what makes the question sensible.

The realization

width=2∥w∥yi(w⋅xi+b)≥1\begin{gathered}\text{width}=\frac{2}{\|\mathbf w\|}\\ y_i(\mathbf w\cdot\mathbf x_i+b)\ge1\end{gathered}

Scaling (w,b,c)(\mathbf w,b,c) by kk changes every number but not the street. Only the ratio 2c/∥w∥2c/\|\mathbf w\| is real. Set c=1c=1 and "widest street" becomes "smallest ∥w∥\|\mathbf w\|".

Pause & predict

A street in canonical form has ∥w∥=0.5\|\mathbf w\|=0.5. You multiply w\mathbf w, bb and cc by k=3k=3. Which statement is true?

Pause & predict

Why do SVMs use the labels +1+1 and −1-1 instead of 1 and 0?

If you want the algebra · 2 proofs, step by step
Prove it · rescaling moves nothing

Claim. For any k>0k>0, the triple (kw,kb,kc)(k\mathbf w,kb,kc) describes the same three lines as (w,b,c)(\mathbf w,b,c), and the width 2c/∥w∥2c/\|\mathbf w\| is unchanged. So we may choose c=1c=1.

1
A point x\mathbf x lies on the new ++ edge when kw⋅x+kb=kck\mathbf w\cdot\mathbf x+kb=kc. Divide by k>0k>0: w⋅x+b=c\mathbf w\cdot\mathbf x+b=c. Same condition, same points. The same holds for the other two lines. An equation and a multiple of it describe the same set.
2
The numbers change: ∥kw∥=k∥w∥\|k\mathbf w\|=k\|\mathbf w\|, the constant becomes kckc, each score yif(xi)y_if(\mathbf x_i) becomes k yif(xi)k\,y_if(\mathbf x_i). But the width 2(kc)∥kw∥=2kck∥w∥=2c∥w∥\frac{2(kc)}{\|k\mathbf w\|}=\frac{2kc}{k\|\mathbf w\|}=\frac{2c}{\|\mathbf w\|} does not.
3
Pick k=1/ck=1/c. The new constant is 11, the nearest points score exactly 1, and the width is 2/∥w∥2/\|\mathbf w\|. ∎ In the 3-D slab: w=(12,12,12)\mathbf w=(\tfrac12,\tfrac12,\tfrac12), b=−2b=-2, ∥w∥=32\|\mathbf w\|=\tfrac{\sqrt3}{2}, thickness 2/∥w∥=4/3≈2.3092/\|\mathbf w\|=4/\sqrt3\approx2.309 — which is also the distance between the planes x1+x2+x3=2x_1+x_2+x_3=2 and =6=6, namely 4/34/\sqrt3.
Prove it · one inequality replaces two

Claim. With labels yi=±1y_i=\pm1, "w⋅xi+b≥1\mathbf w\cdot\mathbf x_i+b\ge1 for blue and ≤−1\le-1 for orange" is the same as yi(w⋅xi+b)≥1y_i(\mathbf w\cdot\mathbf x_i+b)\ge1 for every ii.

1
Blue, yi=+1y_i=+1: yi(w⋅xi+b)=w⋅xi+by_i(\mathbf w\cdot\mathbf x_i+b)=\mathbf w\cdot\mathbf x_i+b, so the two statements are identical.
2
Orange, yi=−1y_i=-1: multiply w⋅xi+b≤−1\mathbf w\cdot\mathbf x_i+b\le-1 by −1-1, which flips the inequality: −(w⋅xi+b)≥1-(\mathbf w\cdot\mathbf x_i+b)\ge1, and −(w⋅xi+b)=yi(w⋅xi+b)-(\mathbf w\cdot\mathbf x_i+b)=y_i(\mathbf w\cdot\mathbf x_i+b). ∎ A label of 0 would destroy this: 0×0\times anything is 0, which can never be ≥1\ge1.

In one sentence: Multiplying (w,b,c)(\mathbf w,b,c) by any kk leaves the street untouched, so we pin the nearest scores to 1; then the width is 2/∥w∥2/\|\mathbf w\| and every point obeys yi(w⋅xi+b)≥1y_i(\mathbf w\cdot\mathbf x_i+b)\ge1.

5

The problem, stated — and why we square

Imagine this

You drop a marble into a round steel bowl from Mummy's kitchen. It rolls and comes to rest at the one lowest spot. Drop it again from anywhere, and it ends up at the same spot.

Now put a few straight cardboard walls inside the bowl that the marble may not cross. It rolls until it rests against a wall. Again there is one resting place, and every drop finds it.

That is what the SVM problem looks like: a bowl, straight walls, and one answer.

Everything so far fits in one line. We want the widest street that keeps every point on its correct side and outside the street:

max⁡w,b 2∥w∥subject to  yi(w⋅xi+b)≥1  for every i.\begin{gathered}\max_{\mathbf w,b}\ \frac{2}{\|\mathbf w\|}\\ \text{subject to}\ \ y_i(\mathbf w\cdot\mathbf x_i+b)\ge1\ \text{ for every }i.\end{gathered}

That is correct, but the thing being maximised is awkward. Three rewrites make it pleasant, and none of them changes which w\mathbf w wins:

  1. Making 2/∥w∥2/\|\mathbf w\| big is making ∥w∥\|\mathbf w\| small. A fraction with a fixed top gets bigger as its bottom gets smaller.
  2. Making ∥w∥\|\mathbf w\| small is making ∥w∥2\|\mathbf w\|^2 small. Squaring never reorders positive numbers.
  3. Put a 12\tfrac12 in front. A positive constant cannot move the lowest point, and it makes the slope come out as w\mathbf w instead of 2w2\mathbf w.

The result is the form everyone actually solves, the hard-margin SVM:

min⁡w,b 12∥w∥2subject to  yi(w⋅xi+b)≥1  for every i.\begin{gathered}\min_{\mathbf w,b}\ \tfrac12\|\mathbf w\|^2\\ \text{subject to}\ \ y_i(\mathbf w\cdot\mathbf x_i+b)\ge1\ \text{ for every }i.\end{gathered}

The picture below makes this real for four points: blue (1,2)(1,2) and (2.5,0)(2.5,0), orange (−1,−1)(-1,-1) and (−1,1)(-1,1). Fix bb at its best value −0.6-0.6, so only w=(w1,w2)\mathbf w=(w_1,w_2) is free. The floor is the w\mathbf w-plane. The bowl above it is 12∥w∥2\tfrac12\|\mathbf w\|^2. Each data point's rule yi(w⋅xi+b)≥1y_i(\mathbf w\cdot\mathbf x_i+b)\ge1 is a straight wall on that floor. The allowed region glows.

A bowl, four walls, one resting placeThe floor is the plane of all possible w. Each point cuts it with a straight wall; the ball rolls to the lowest allowed spot — and that spot is the SVM.

Try: Press ▶ drop the ball and watch it slide down to a wall and then along it into the corner at w=(0.8,0.4)\mathbf w=(0.8,0.4). Then press add an overlapping point: a blue point lands between the two orange ones, and the glowing region disappears.

drag the picture to orbit

—

Three objectives, one winner 1-D: ‖w‖ = |w|

0.8

Why is this such good news? The bowl 12∥w∥2\tfrac12\|\mathbf w\|^2 curves up the same amount in every direction, so it has exactly one bottom. Each wall is straight, and a region cut out by straight walls has no dents. A bowl over a dent-free region is called a convex quadratic program, and for these every local lowest point is the global lowest point. There are no false valleys to get stuck in. Train an SVM twice on the same data and you get exactly the same answer, every time.

Watch out — say the right reason for squaring

A common answer is "we square because 2/∥w∥2/\|\mathbf w\| is not convex". Careful. We are maximising that, so the property we would want is not convexity anyway. And ∥w∥\|\mathbf w\| itself is convex — every length is. Minimising ∥w∥\|\mathbf w\| is already a fine convex problem.

The real reason: ∥w∥\|\mathbf w\| has a sharp point at w=0\mathbf w=\mathbf 0, like the tip of a cone, where there is no slope at all. 12∥w∥2\tfrac12\|\mathbf w\|^2 is smooth everywhere, its slope is simply w\mathbf w, and that turns the problem into a quadratic program with fast, reliable solvers.

Two more things to hold on to

Support vectors are an output, never an input. You solve the problem first. Only then do you look at which points ended up exactly on the street's edges. You never pick them in advance — you could not, since you do not know them until the problem is solved.

A hard margin fails hard. If even one point sits among the other group, no line satisfies every wall. The allowed region is empty and the problem has no answer at all — not a bad one, none. Section 10 fixes that.

The realization

min⁡w,b 12∥w∥2s.t.yi(w⋅xi+b)≥1\min_{\mathbf w,b}\ \tfrac12\|\mathbf w\|^2\quad\text{s.t.}\quad y_i(\mathbf w\cdot\mathbf x_i+b)\ge1

A bowl with straight walls. Same winner as "maximise 2/∥w∥2/\|\mathbf w\|", but smooth, with exactly one bottom. The points pressing against the walls at that bottom are the support vectors.

Pause & predict

Why do we minimise 12∥w∥2\tfrac12\|\mathbf w\|^2 instead of ∥w∥\|\mathbf w\|?

If you want the algebra · 1 proof, step by step
Prove it · the three rewrites keep the winner

Claim. Under the same constraints, max⁡2/∥w∥\max 2/\|\mathbf w\|, min⁡∥w∥\min\|\mathbf w\|, min⁡∥w∥2\min\|\mathbf w\|^2 and min⁡12∥w∥2\min\tfrac12\|\mathbf w\|^2 are all won by the same (w,b)(\mathbf w,b).

1
Every allowed w\mathbf w is non-zero (with w=0\mathbf w=\mathbf 0 every score is yiby_ib, and bb cannot be ≥1\ge1 for one class and ≤−1\le-1 for the other). On positive numbers, t↦2/tt\mapsto2/t is strictly decreasing, t↦t2t\mapsto t^2 is strictly increasing, and t↦12tt\mapsto\tfrac12t is strictly increasing. A strictly increasing function never changes which input is smallest; a strictly decreasing one turns the smallest into the largest.
2
So ∥w1∥<∥w2∥  ⟺  12∥w1∥2<12∥w2∥2  ⟺  2/∥w1∥>2/∥w2∥\|\mathbf w_1\|<\|\mathbf w_2\|\iff \tfrac12\|\mathbf w_1\|^2<\tfrac12\|\mathbf w_2\|^2\iff 2/\|\mathbf w_1\|>2/\|\mathbf w_2\|. Whichever w\mathbf w wins one contest wins all four. The values differ; the winner does not.
3
Why the squared form is the one to solve: in one coordinate, ∣w∣|w| has slope −1-1 on the left of 0 and +1+1 on the right — no slope at 0. And 12∥w∥2=12∑jwj2\tfrac12\|\mathbf w\|^2=\tfrac12\sum_jw_j^2 has partial derivatives ∂/∂wk=wk\partial/\partial w_k=w_k, smooth everywhere; its gradient is w\mathbf w itself and its second derivatives form the identity matrix, so it curves up equally in every direction and has exactly one bottom. ∎ In the four-point picture: the corner w=(0.8,0.4)\mathbf w=(0.8,0.4) gives 12∥w∥2=12(0.64+0.16)=0.4\tfrac12\|\mathbf w\|^2=\tfrac12(0.64+0.16)=0.4 and width 2/0.8=5≈2.2362/\sqrt{0.8}=\sqrt5\approx2.236, the distance between the two touching points (1,2)(1,2) and (−1,1)(-1,1).

In one sentence: The widest street is the lowest point of the bowl 12∥w∥2\tfrac12\|\mathbf w\|^2 inside straight walls yi(w⋅xi+b)≥1y_i(\mathbf w\cdot\mathbf x_i+b)\ge1 — a convex quadratic program with exactly one answer, and none at all if the groups overlap.

6

Every point gets a price tag

Imagine this

A crowd leans on a rope barrier at a cricket ground. People standing back from the rope put no force on it at all. Only the ones pressed right against it push.

If you want to know where the rope will settle, you do not need to know about the whole crowd. You need the few who touch it, and how hard each one pushes.

Section 5 left us with a bowl and walls. Walls are awkward to work with directly, so we do what Unit 11 taught (§8–§11): turn each wall into a price. Every training point ii gets its own number αi≥0\alpha_i\ge0 (read "alpha"). Write each wall as "something ≥0\ge0":

gi(w,b)=yi(w⋅xi+b)−1  ≥  0,g_i(\mathbf w,b)=y_i(\mathbf w\cdot\mathbf x_i+b)-1\;\ge\;0,

and fold all the walls into one expression, the Lagrangian:

L(w,b,α)= 12∥w∥2−∑iαi[yi(w⋅xi+b)−1],αi≥0.\begin{aligned}L(\mathbf w,b,\boldsymbol\alpha)=\ &\tfrac12\|\mathbf w\|^2\\ &-\sum_i\alpha_i\big[y_i(\mathbf w\cdot\mathbf x_i+b)-1\big],\\ &\alpha_i\ge0.\end{aligned}

Why is this the same problem? Fix a street (w,b)(\mathbf w,b) and ask the prices to make LL as large as they can. Look at one point:

If the point is…its bracket is…the best price αi\alpha_i is…
inside the street or on the wrong side, yif(xi)<1y_if(\mathbf x_i)<1negative+∞+\infty: LL blows up — this street is ruled out
comfortably outside, yif(xi)>1y_if(\mathbf x_i)>1positive00: any price would only lower LL
exactly on the edge, yif(xi)=1y_if(\mathbf x_i)=1zerofree: any αi≥0\alpha_i\ge0 gives the same LL

So the largest LL equals 12∥w∥2\tfrac12\|\mathbf w\|^2 for a legal street and +∞+\infty for an illegal one. Minimising that over all streets is exactly our problem. The walls did not disappear; they became prices that punish any trespass without limit.

Read αi\alpha_i as how hard point ii pushes on the street. A point standing back pays nothing and pushes nothing. A point pressed against an edge has a real price. Drag the points below and watch the prices.

Who is pushing the street?Twelve points and their solved street. Each point's price αᵢ is its pressure bar. A live solver re-solves the street every time you move a point.

Try: Drag a point from deep inside a group — its price stays 0 and the street does not move, until you drag it into the street. Then drag one of the gold-ringed points: the whole street follows it.

drag any point

—

At the answer, four conditions hold together. They are called the KKT conditions (after Karush, Kuhn and Tucker). For this problem the bowl is convex and the walls are straight, so they are not just necessary: any (w,b,α)(\mathbf w,b,\boldsymbol\alpha) that satisfies all four is the answer.

ConditionIn symbolsIn words
1 · stationarity∂L/∂w=0, ∂L/∂b=0\partial L/\partial\mathbf w=\mathbf 0,\ \partial L/\partial b=0the pushes are in balance; gives w=∑iαiyixi\mathbf w=\sum_i\alpha_iy_i\mathbf x_i and ∑iαiyi=0\sum_i\alpha_iy_i=0 (§7)
2 · primal feasibilityyi(w⋅xi+b)≥1y_i(\mathbf w\cdot\mathbf x_i+b)\ge1the street really is legal
3 · dual feasibilityαi≥0\alpha_i\ge0a point can push the street away, never pull it in
4 · complementary slacknessαi[yi(w⋅xi+b)−1]=0\alpha_i\big[y_i(\mathbf w\cdot\mathbf x_i+b)-1\big]=0for each point, room to spare or a price — never both
Watch out — two sign conventions

You will also see L=12∥w∥2+∑iαi[1−yi(w⋅xi+b)]L=\tfrac12\|\mathbf w\|^2+\sum_i\alpha_i\big[1-y_i(\mathbf w\cdot\mathbf x_i+b)\big]. It is the same function, since +α(1−z)=−α(z−1)+\alpha(1-z)=-\alpha(z-1). Either is fine. Switching between them in the middle of a calculation is where sign mistakes are born. This unit uses the minus form throughout.

The realization

L=12∥w∥2−∑iαi[yif(xi)−1]αi≥0,αi[yif(xi)−1]=0\begin{gathered}L=\tfrac12\|\mathbf w\|^2-\sum_i\alpha_i\big[y_if(\mathbf x_i)-1\big]\\ \alpha_i\ge0,\qquad \alpha_i\big[y_if(\mathbf x_i)-1\big]=0\end{gathered}

Every point gets a price αi\alpha_i: how hard it pushes on the street. Points with room to spare pay nothing. Only points pressed against an edge carry a price.

Pause & predict

At the answer, a point has score yif(xi)=2.4y_if(\mathbf x_i)=2.4. What is its price αi\alpha_i?

If you want the algebra · 1 proof, step by step
Prove it · the prices rebuild the walls

Claim. For a fixed street (w,b)(\mathbf w,b), max⁡α≥0L(w,b,α)\displaystyle\max_{\boldsymbol\alpha\ge\mathbf 0}L(\mathbf w,b,\boldsymbol\alpha) equals 12∥w∥2\tfrac12\|\mathbf w\|^2 if every yi(w⋅xi+b)≥1y_i(\mathbf w\cdot\mathbf x_i+b)\ge1, and +∞+\infty otherwise. So min⁡w,bmax⁡α≥0L\min_{\mathbf w,b}\max_{\boldsymbol\alpha\ge\mathbf 0}L is exactly the hard-margin problem, and at its answer αi[yif(xi)−1]=0\alpha_i\big[y_if(\mathbf x_i)-1\big]=0 for every ii.

1
Write gi=yi(w⋅xi+b)−1g_i=y_i(\mathbf w\cdot\mathbf x_i+b)-1, so L=12∥w∥2−∑iαigiL=\tfrac12\|\mathbf w\|^2-\sum_i\alpha_ig_i. The prices enter separately, one term −αigi-\alpha_ig_i each, so maximise each term on its own. No price appears in any other point's term.
2
If gi<0g_i<0 (a trespasser), −αigi=αi∣gi∣-\alpha_ig_i=\alpha_i|g_i| grows without limit as αi→∞\alpha_i\to\infty. If gi>0g_i>0, −αigi≤0-\alpha_ig_i\le0 and the best choice is αi=0\alpha_i=0. If gi=0g_i=0, the term is 0 for every αi\alpha_i.
3
So the maximum is +∞+\infty as soon as one wall is broken, and otherwise it is 12∥w∥2+0\tfrac12\|\mathbf w\|^2+0. Minimising over streets never picks an infinite value, so it solves the original problem. In every case the best prices satisfy αigi=0\alpha_ig_i=0: either gi=0g_i=0 or αi=0\alpha_i=0. ∎ That last line is complementary slackness, the fourth KKT condition. It came for free from "the prices try to make LL as large as possible".

In one sentence: Turn each wall into a price αi≥0\alpha_i\ge0; the prices punish any trespass without limit, and at the answer only the points pressed against the street carry a price.

7

The boundary is built from the data

Imagine this

A tug-of-war at the village fair. The rope does not move when the two teams pull equally hard. If one side pulled harder, the rope would slide, and the match would not be over.

The street works the same way. Each price is a pull. At the answer the pulls from the two classes cancel, and the direction of the street is made of those very pulls.

Stationarity means: the slope of LL is zero in every direction of w\mathbf w and bb. Two facts do all the work, both proved in the drawer below. The slope of 12∥w∥2\tfrac12\|\mathbf w\|^2 is w\mathbf w, and the slope of w⋅x\mathbf w\cdot\mathbf x is x\mathbf x. So:

∂L∂w=w−∑iαiyixi=0⟹w=∑iαiyixi,\begin{aligned}\frac{\partial L}{\partial\mathbf w}&=\mathbf w-\sum_i\alpha_iy_i\mathbf x_i=\mathbf 0\\ \Longrightarrow\quad\mathbf w&=\sum_i\alpha_iy_i\mathbf x_i,\end{aligned} ∂L∂b=−∑iαiyi=0⟹∑iαiyi=0.\frac{\partial L}{\partial b}=-\sum_i\alpha_iy_i=0\quad\Longrightarrow\quad \sum_i\alpha_iy_i=0.

Read the first one slowly. The answer w\mathbf w is not some new direction invented from nothing. It is the training points themselves, added up with weights. Blue points (yi=+1y_i=+1) pull w\mathbf w towards themselves; orange points (yi=−1y_i=-1) push it away; αi\alpha_i says how hard.

For our twelve points only three prices are not zero: α=18\alpha=\tfrac18 for (1,3)(1,3) and (3,5)(3,5), and α=14\alpha=\tfrac14 for (4,2)(4,2). Build w\mathbf w:

w=18(1,3)+18(3,5)−14(4,2)=(0.125+0.375−1,0.375+0.625−0.5)=(−0.5, 0.5).\begin{aligned}\mathbf w&=\tfrac18(1,3)+\tfrac18(3,5)-\tfrac14(4,2)\\ &=(0.125+0.375-1,\\ &\qquad 0.375+0.625-0.5)\\ &=(-0.5,\ 0.5).\end{aligned}

The second fact is the tug-of-war: the blue prices add to 18+18=14\tfrac18+\tfrac18=\tfrac14, the orange price is 14\tfrac14. Balanced. If they were not, you could slide the street sideways (change bb) and do better, so it would not be the answer.

w, assembled from its support vectorsEach surviving point adds the arrow αᵢyᵢxᵢ. Laid tip to tail they land on w. The beam weighs the two classes' prices.

Try: Press ▶ build w. Then drag the orange price α3\alpha_3 up to 0.35: the beam tips (the pulls no longer balance) and w\mathbf w swings away from the true answer.

0.125
0.125
0.25
—
Why this line matters most

Sparsity. Every zero price deletes its point from the sum. Most prices are zero (§9).

Calm about far-away points. A point far on its correct side has αi=0\alpha_i=0. Move it further away and w\mathbf w does not change at all.

Kernels. If w\mathbf w is built from data points, then w⋅x\mathbf w\cdot\mathbf x is built from dot products between data points — and a dot product is something you can swap out. That is §12–§15, hiding in this one line.

The realization

w=∑iαiyixi,∑iαiyi=0\mathbf w=\sum_i\alpha_iy_i\mathbf x_i,\qquad\sum_i\alpha_iy_i=0

The direction of the street is a weighted sum of the training points, and the two classes' prices balance like a tug-of-war that has come to rest.

Pause & predict

A solver reports α=0.25\alpha=0.25 for (2,2)(2,2) with y=+1y=+1, α=0\alpha=0 for (4,1)(4,1) with y=+1y=+1, and α=0.25\alpha=0.25 for (0,0)(0,0) with y=−1y=-1. What is w\mathbf w?

If you want the algebra · 4 proofs, step by step
Prove it · Lemma A: the slope of ½‖w‖² is w

Claim. ∇w12∥w∥2=w\nabla_{\mathbf w}\tfrac12\|\mathbf w\|^2=\mathbf w.

1
Write it as a sum: 12∥w∥2=12∑j=1dwj2\tfrac12\|\mathbf w\|^2=\tfrac12\sum_{j=1}^dw_j^2. Differentiate with respect to one coordinate wkw_k: every term with j≠kj\ne k is a constant, and ∂(wk2)/∂wk=2wk\partial(w_k^2)/\partial w_k=2w_k: ∂∂wk12∑jwj2=12⋅2wk=wk.\frac{\partial}{\partial w_k}\tfrac12\sum_jw_j^2=\tfrac12\cdot2w_k=w_k. ∎ Stack the dd answers: the gradient is (w1,…,wd)=w(w_1,\dots,w_d)=\mathbf w. This is where the 12\tfrac12 pays for itself. Picture: on the bowl, the uphill arrow at any point is the position arrow itself.
Prove it · Lemma B: the slope of w·x is x

Claim. For a fixed x\mathbf x, ∇w(w⋅x)=x\nabla_{\mathbf w}(\mathbf w\cdot\mathbf x)=\mathbf x.

1
w⋅x=∑jwjxj\mathbf w\cdot\mathbf x=\sum_jw_jx_j. Only the j=kj=k term contains wkw_k, and it is wkxkw_kx_k, whose slope in wkw_k is xkx_k. Stacking gives x\mathbf x. ∎
Prove it · stationarity in w

Claim. ∂L/∂w=0\partial L/\partial\mathbf w=\mathbf 0 gives w=∑iαiyixi\mathbf w=\sum_i\alpha_iy_i\mathbf x_i.

1
Open the bracket in LL: L= 12∥w∥2−∑iαiyi w⋅xi−b∑iαiyi+∑iαi.\begin{aligned}L=\ &\tfrac12\|\mathbf w\|^2-\sum_i\alpha_iy_i\,\mathbf w\cdot\mathbf x_i\\ &-b\sum_i\alpha_iy_i+\sum_i\alpha_i.\end{aligned} The last two terms contain no w\mathbf w, so their slope in w\mathbf w is zero.
2
Lemma A on the first term, Lemma B on each term of the second (αiyi\alpha_iy_i is just a number): ∂L∂w=w−∑iαiyixi=0⇒ w=∑iαiyixi.\begin{aligned}\frac{\partial L}{\partial\mathbf w}&=\mathbf w-\sum_i\alpha_iy_i\mathbf x_i=\mathbf 0\\ \Rightarrow\ \mathbf w&=\sum_i\alpha_iy_i\mathbf x_i.\end{aligned} ∎
Prove it · stationarity in b

Claim. ∂L/∂b=0\partial L/\partial b=0 gives ∑iαiyi=0\sum_i\alpha_iy_i=0.

1
In the opened-up LL, exactly one term contains bb: −b∑iαiyi-b\sum_i\alpha_iy_i. It is a straight line in bb, so ∂L∂b=−∑iαiyi=0⇒ ∑iαiyi=0.\begin{gathered}\frac{\partial L}{\partial b}=-\sum_i\alpha_iy_i=0\\ \Rightarrow\ \sum_i\alpha_iy_i=0.\end{gathered} ∎ Physically: sliding bb moves the whole street sideways. If the two classes' pushes did not cancel, sliding would still help, so it could not be the answer. In a problem with unknowns α\alpha and bb, this is often the equation you are missing.

In one sentence: Setting the slopes of LL to zero gives w=∑iαiyixi\mathbf w=\sum_i\alpha_iy_i\mathbf x_i — the street's direction is built from the points themselves — and ∑iαiyi=0\sum_i\alpha_iy_i=0, the balance of the two classes' pushes.

8

The dual — a problem only in the prices

Imagine this

You can plan a wedding by choosing the hall, the food and the decorations. Or you can hand the whole job to a caterer and only argue about prices.

The second way can be easier — and if the deal is fair, you end up with the same wedding. The SVM has such a "prices only" version too, and it is the version everybody solves.

Take the two facts from §7 and put them back into LL. Every trace of w\mathbf w and bb disappears (the steps are in the drawer), and what is left is a problem in the prices alone — the dual:

max⁡α ∑iαi−12∑i,jαiαjyiyj xi⋅xjs.t.αi≥0,∑iαiyi=0.\begin{gathered}\max_{\boldsymbol\alpha}\ \sum_i\alpha_i-\tfrac12\sum_{i,j}\alpha_i\alpha_jy_iy_j\,\mathbf x_i\cdot\mathbf x_j\\ \text{s.t.}\quad\alpha_i\ge0,\quad\sum_i\alpha_iy_i=0.\end{gathered}

A tiny example. Two points: (1,1)(1,1) blue and (−1,−1)(-1,-1) orange. The dot products are x1⋅x1=2\mathbf x_1\cdot\mathbf x_1=2, x2⋅x2=2\mathbf x_2\cdot\mathbf x_2=2, x1⋅x2=−2\mathbf x_1\cdot\mathbf x_2=-2. The balance condition α1−α2=0\alpha_1-\alpha_2=0 makes both prices equal, call it α\alpha. Then

D(α)=2α−12[α2(2)+α2(2)+2α2(−1)(−2)]=2α−4α2.\begin{aligned}D(\alpha)&=2\alpha-\tfrac12\big[\alpha^2(2)+\alpha^2(2)\\ &\qquad+2\alpha^2(-1)(-2)\big]\\ &=2\alpha-4\alpha^2.\end{aligned}

The top of this hill is where the slope 2−8α2-8\alpha is zero: α=14\alpha=\tfrac14, with D=12−14=14D=\tfrac12-\tfrac14=\tfrac14. Rebuild the street: w=14(1,1)−14(−1,−1)=(12,12)\mathbf w=\tfrac14(1,1)-\tfrac14(-1,-1)=(\tfrac12,\tfrac12). A point on the edge scores exactly 1: (12,12)⋅(1,1)+b=1(\tfrac12,\tfrac12)\cdot(1,1)+b=1 gives b=0b=0. The width is 2/∥w∥=2/0.707=22≈2.832/\|\mathbf w\|=2/0.707=2\sqrt2\approx2.83 — exactly the distance between the two points, as it must be.

And a free check: the primal value is 12∥w∥2=12⋅12=14\tfrac12\|\mathbf w\|^2=\tfrac12\cdot\tfrac12=\tfrac14. The dual peak is also 14\tfrac14. For a convex problem like this, the two always agree (strong duality). Compute both, and if they differ, you made a slip.

The dual objective, and the line that binds itThe height is D(α). Only the prices on the glowing balance line are allowed; the peak along it is the answer.

Try: On two points, slide α\alpha along the balance line and watch the bead climb to the peak at 14\tfrac14. Notice the whole ridge is equally high — only the balance line picks one point on it. Then open three points: the free peak asks for a negative price, so the answer sits on the wall α3=0\alpha_3=0.

drag the picture to orbit

0.1
—

Sit with what changed between the two versions:

Primal (street)Dual (prices)
unknownsw\mathbf w and bb: one per feature, plus oneα1,…,αn\alpha_1,\dots,\alpha_n: one per training point
data enters asthe coordinates of each xi\mathbf x_ionly the dot products xi⋅xj\mathbf x_i\cdot\mathbf x_j
zerosnone — w\mathbf w is densemost αi\alpha_i come out exactly 0

That middle row is the most important line in the subject. Hand a solver the table of numbers xi⋅xj\mathbf x_i\cdot\mathbf x_j, throw the original points away, and it still finds the answer. Section 12 opens that door.

The realization

max⁡α ∑iαi−12∑i,jαiαjyiyj(xi⋅xj)s.t.αi≥0,  ∑iαiyi=0\begin{gathered}\max_{\boldsymbol\alpha}\ \sum_i\alpha_i-\tfrac12\sum_{i,j}\alpha_i\alpha_jy_iy_j(\mathbf x_i\cdot\mathbf x_j)\\ \text{s.t.}\quad\alpha_i\ge0,\ \ \sum_i\alpha_iy_i=0\end{gathered}

A hill in the prices alone. Its peak equals the primal minimum (a free check), and the data appears only through dot products.

Pause & predict

For the two points (1,1)(1,1) blue and (−1,−1)(-1,-1) orange, the dual along the balance line is D(α)=2α−4α2D(\alpha)=2\alpha-4\alpha^2. What are α\alpha and the primal value 12∥w∥2\tfrac12\|\mathbf w\|^2?

If you want the algebra · 2 proofs, step by step
Prove it · from L to the dual

Claim. Substituting w=∑iαiyixi\mathbf w=\sum_i\alpha_iy_i\mathbf x_i and ∑iαiyi=0\sum_i\alpha_iy_i=0 into LL gives D(α)=∑iαi−12∑i,jαiαjyiyj(xi⋅xj)D(\boldsymbol\alpha)=\sum_i\alpha_i-\tfrac12\sum_{i,j}\alpha_i\alpha_jy_iy_j(\mathbf x_i\cdot\mathbf x_j).

1
Start from L=12w⋅w−∑iαiyi(w⋅xi)−b∑iαiyi+∑iαiL=\tfrac12\mathbf w\cdot\mathbf w-\sum_i\alpha_iy_i(\mathbf w\cdot\mathbf x_i)-b\sum_i\alpha_iy_i+\sum_i\alpha_i. The bb term is b⋅0b\cdot0 by the balance condition. Without the balance condition bb would still be sitting here and the substitution would fail.
2
Pull w\mathbf w out of the middle sum: ∑iαiyi(w⋅xi)=w⋅∑iαiyixi=w⋅w\sum_i\alpha_iy_i(\mathbf w\cdot\mathbf x_i)=\mathbf w\cdot\sum_i\alpha_iy_i\mathbf x_i=\mathbf w\cdot\mathbf w. So L=12w⋅w−w⋅w+∑iαi=∑iαi−12 w⋅w.\begin{aligned}L&=\tfrac12\mathbf w\cdot\mathbf w-\mathbf w\cdot\mathbf w+\textstyle\sum_i\alpha_i\\ &=\textstyle\sum_i\alpha_i-\tfrac12\,\mathbf w\cdot\mathbf w.\end{aligned} Half minus one is minus a half: that sign flip is why the dual is a maximisation.
3
Expand w⋅w\mathbf w\cdot\mathbf w with two separate counters, because squaring a sum makes every pair meet: w⋅w=(∑iαiyixi)⋅(∑jαjyjxj)=∑i,jαiαjyiyj xi⋅xj.\begin{aligned}\mathbf w\cdot\mathbf w&=\Big(\sum_i\alpha_iy_i\mathbf x_i\Big)\\ &\quad\cdot\Big(\sum_j\alpha_jy_j\mathbf x_j\Big)\\ &=\sum_{i,j}\alpha_i\alpha_jy_iy_j\,\mathbf x_i\cdot\mathbf x_j.\end{aligned} ∎ The constraints on the prices remain: αi≥0\alpha_i\ge0 and ∑iαiyi=0\sum_i\alpha_iy_i=0. The dual value at any allowed α\boldsymbol\alpha is a lower bound on the primal; at the peak the two are equal.
Prove it · the two-point shortcut

Claim. For one blue point x1\mathbf x_1 and one orange point x2\mathbf x_2, α1=α2=2∥x1−x2∥2\alpha_1=\alpha_2=\dfrac{2}{\|\mathbf x_1-\mathbf x_2\|^2}, and the width equals ∥x1−x2∥\|\mathbf x_1-\mathbf x_2\|.

1
Balance gives α1=α2=α\alpha_1=\alpha_2=\alpha. The double sum has four terms: α2∥x1∥2+α2∥x2∥2−2α2 x1⋅x2=α2∥x1−x2∥2\alpha^2\|\mathbf x_1\|^2+\alpha^2\|\mathbf x_2\|^2-2\alpha^2\,\mathbf x_1\cdot\mathbf x_2=\alpha^2\|\mathbf x_1-\mathbf x_2\|^2. So D(α)=2α−12∥x1−x2∥2α2.D(\alpha)=2\alpha-\tfrac12\|\mathbf x_1-\mathbf x_2\|^2\alpha^2.
2
D′(α)=2−∥x1−x2∥2α=0D'(\alpha)=2-\|\mathbf x_1-\mathbf x_2\|^2\alpha=0 gives α=2/∥x1−x2∥2\alpha=2/\|\mathbf x_1-\mathbf x_2\|^2. Then w=α(x1−x2)\mathbf w=\alpha(\mathbf x_1-\mathbf x_2), so ∥w∥=2/∥x1−x2∥\|\mathbf w\|=2/\|\mathbf x_1-\mathbf x_2\| and the width 2/∥w∥=∥x1−x2∥2/\|\mathbf w\|=\|\mathbf x_1-\mathbf x_2\|. ∎ For (1,1)(1,1) and (−1,−1)(-1,-1): ∥x1−x2∥2=8\|\mathbf x_1-\mathbf x_2\|^2=8, so α=14\alpha=\tfrac14 and the width is 8=22\sqrt8=2\sqrt2. Any two-point problem becomes a five-second problem.

In one sentence: Substituting w=∑αiyixi\mathbf w=\sum\alpha_iy_i\mathbf x_i and ∑αiyi=0\sum\alpha_iy_i=0 back into LL leaves a hill in the prices alone, whose peak equals the primal minimum and which sees the data only through dot products.

9

Support vectors — the few points holding up the street

Imagine this

A shamiana is held up by a handful of bamboo poles. The rest of the ground under it could be cleared of every chair and table, and the tent would stand exactly as before.

Pull out even one pole, though, and the roof sags into a new shape. The poles are the only things that matter.

One condition does all the interesting work — complementary slackness:

αi[yi(w⋅xi+b)−1]=0.\alpha_i\big[y_i(\mathbf w\cdot\mathbf x_i+b)-1\big]=0.

A product is zero only if one of its factors is. So every training point is in one of three places:

Where the point sitsscore yif(xi)y_if(\mathbf x_i)price αi\alpha_ieffect on the street
outside the street>1>1=0=0none at all — delete it and nothing moves
exactly on an edge=1=1≥0\ge0a support vector when αi>0\alpha_i>0: it holds the street up
inside the street<1<1—impossible with a hard margin (§10 allows it)

A support vector is a training point with αi>0\alpha_i>0. It must sit on an edge, and it is one of the points w\mathbf w is built from. Everything else could be deleted. To classify a new point you only need the support vectors:

f(x)=∑i∈SVαiyi (xi⋅x)+b.f(\mathbf x)=\sum_{i\in\text{SV}}\alpha_iy_i\,(\mathbf x_i\cdot\mathbf x)+b.

This is why kernel SVMs are practical at all: a million training points, a few hundred support vectors, and prediction only looks at those.

The answer depends on a few pointsA live SVM on twenty points. Add, drag or delete points; the street re-solves as you move. Gold rings mark the support vectors, with their prices.

Try: Press delete every non-support point: seventeen points vanish and the street does not move by a hair. Then choose delete and click a gold-ringed point: now the street jumps.

click to

tap to add or delete · drag to move

—
class +1class −1support vectorstreet edgeboundary
Watch out — "on the edge" is not the same as "support vector"

Complementary slackness says: a price above zero forces the point onto an edge. It does not say the reverse. A point can sit exactly on an edge and still have αi=0\alpha_i=0. It touches the street but does not push; delete it and nothing changes.

Example: (1,1)(1,1) and (2,0)(2,0) blue, (0,0)(0,0) orange. The answer is w=(1,1)\mathbf w=(1,1), b=−1b=-1, and all three points score exactly 1. But the prices come out α=(1,0,1)\alpha=(1,0,1): (2,0)(2,0) is on the edge with a zero price. The safe sentence: support vectors are the points with αi>0\alpha_i>0; they always lie on an edge, but not every point on an edge is one.

The realization

αi>0 ⇒ yif(xi)=1yif(xi)>1 ⇒ αi=0f(x)=∑i∈SVαiyi(xi⋅x)+b\begin{gathered}\alpha_i>0\ \Rightarrow\ y_if(\mathbf x_i)=1\\ y_if(\mathbf x_i)>1\ \Rightarrow\ \alpha_i=0\\ f(\mathbf x)=\sum_{i\in\text{SV}}\alpha_iy_i(\mathbf x_i\cdot\mathbf x)+b\end{gathered}

Find the widest street → it collides with a few points → those are the support vectors → the whole model can be rebuilt from them alone. Every arrow runs forward. You never choose them; the solution hands them to you.

Pause & predict

After training, a point sits exactly on the +1+1 edge but its price is αi=0\alpha_i=0. Is it a support vector, and what happens if you delete it?

Pause & predict

A trained SVM has 10 000 training points and 30 support vectors. You delete the other 9 970 points and train again. What do you get?

If you want the algebra · 1 proof, step by step
Prove it · far-away points can be deleted

Claim. Let (w,b,α)(\mathbf w,b,\boldsymbol\alpha) be the answer on a data set. Delete any points with αi=0\alpha_i=0. The same (w,b)(\mathbf w,b) with the remaining prices is the answer on the smaller set.

1
Check the four KKT conditions on the smaller set. Stationarity: w=∑αiyixi\mathbf w=\sum\alpha_iy_i\mathbf x_i and ∑αiyi=0\sum\alpha_iy_i=0 did not use the deleted points, since their terms were 0⋅yixi0\cdot y_i\mathbf x_i. Primal feasibility: the remaining points still score at least 1 — nothing about them changed. Dual feasibility and complementary slackness: unchanged for the points that remain. Fewer points means fewer conditions to satisfy, and none of the remaining ones changed.
2
For a convex problem, satisfying all four KKT conditions means being the answer. So the street is the same. ∎ The reverse fails: delete a point with αi>0\alpha_i>0 and stationarity breaks, so the old street is no longer the answer — it moves.

In one sentence: Complementary slackness splits the points into those with room (price 0, deletable) and those pressed on an edge; the few with a positive price — the support vectors — hold the street up on their own.

10

Soft margin — allowing a few trespassers

Imagine this

A traffic constable keeps a lane clear. In a perfect world nobody ever stops in it. In real Bengaluru, a few autos always do.

A sensible rule is not "the lane must be perfectly empty, or there is no lane". It is: stop inside if you must, but pay a fine for every metre you intrude. Now the constable can keep a wide lane and still deal with the odd rule-breaker.

The hard margin fails hard. Real data is noisy: one blue point lands among the orange ones, and suddenly no line satisfies every wall — the problem has no answer at all (§5). The fix keeps the same rule but gives every point a small allowance ξi\xi_i (the Greek letter xi), its slack:

yi(w⋅xi+b)≥1−ξi,ξi≥0.y_i(\mathbf w\cdot\mathbf x_i+b)\ge1-\xi_i,\qquad \xi_i\ge0.

Think of ξi\xi_i as a record of how far point ii breaks the old rule, measured in half-street widths. Read its value like this:

ξi\xi_ithe rule becomeswhere the point is
00yif(xi)≥1y_if(\mathbf x_i)\ge1the old rule: outside the street, or exactly on its edge
between 0 and 1yif(xi)≥1−ξi>0y_if(\mathbf x_i)\ge1-\xi_i>0inside the street, but still on its own side — still classified correctly
11yif(xi)≥0y_if(\mathbf x_i)\ge0exactly on the centre line
more than 1the right side is negativecrossed to the wrong side — misclassified; the bigger ξi\xi_i, the deeper

If slack were free, the optimiser would cheat: make every ξi\xi_i huge and pave a street as wide as the world. So every unit of slack costs a fine C>0C>0:

min⁡w,b,ξ 12∥w∥2⏟widen the street+C∑iξi⏟total fines.t.yi(w⋅xi+b)≥1−ξi,  ξi≥0.\begin{gathered}\min_{\mathbf w,b,\boldsymbol\xi}\ \underbrace{\tfrac12\|\mathbf w\|^2}_{\text{widen the street}}+\underbrace{C\sum_i\xi_i}_{\text{total fine}}\\ \text{s.t.}\quad y_i(\mathbf w\cdot\mathbf x_i+b)\ge1-\xi_i,\ \ \xi_i\ge0.\end{gathered}

Two wishes now pull against each other. The first term wants a wide street. The second wants nobody inside it. CC is the exchange rate between them.

Now the surprise. Run the same Lagrangian-and-dual walk as §6–§8 with one extra family of prices μi\mu_i for the rule ξi≥0\xi_i\ge0. Setting the slope in ξi\xi_i to zero gives αi+μi=C\alpha_i+\mu_i=C. Since μi≥0\mu_i\ge0, that means αi≤C\alpha_i\le C. Everything else is letter for letter the hard-margin dual:

max⁡α ∑iαi−12∑i,jαiαjyiyj(xi⋅xj)s.t.0≤αi≤C,∑iαiyi=0.\begin{gathered}\max_{\boldsymbol\alpha}\ \sum_i\alpha_i-\tfrac12\sum_{i,j}\alpha_i\alpha_jy_iy_j(\mathbf x_i\cdot\mathbf x_j)\\ \text{s.t.}\quad \boxed{0\le\alpha_i\le C},\quad\sum_i\alpha_iy_i=0.\end{gathered}

The slack variables have vanished. The only change is a ceiling on every price. In a hard margin one badly placed point can push infinitely hard — that is why one outlier wrecks everything. With a soft margin nobody can push harder than CC. That ceiling is the robustness.

The fine dialSix blue and six orange points, plus one rogue blue point near the orange group. Each red pillar is a point's slack ξᵢ — how far it trespasses. Turn the fine C.

Try: Sweep CC from 0.01 up to 1000. At small CC the street is wide and the rogue point simply stands inside it (a tall red pillar). As CC grows the fine bites, the pillars shrink, and the street narrows until it honours the rogue point exactly — the hard margin.

drag the picture to orbit

0.02
—
free price, 0 < α < Cat the ceiling, α = C
small fine
large fine

Reading CC. A small fine makes cheating cheap: the optimiser buys a wide street and shrugs when a few points step inside or even cross. Wide street, more support vectors, a calm boundary that ignores single points. A large fine makes every unit of slack expensive: the street narrows until nobody trespasses. As C→∞C\to\infty you get the hard margin back exactly. So CC runs opposite to the λ\lambda of ridge regression: big CC means less smoothing, not more. In practice CC is chosen by trying several values on held-out data.

Reading the prices. With the ceiling, complementary slackness gives three kinds of points:

priceslack and scorewhere the point is
αi=0\alpha_i=0ξi=0\xi_i=0, yif(xi)≥1y_if(\mathbf x_i)\ge1outside the street — not a support vector
0<αi<C0<\alpha_i<Cξi=0\xi_i=0, yif(xi)=1y_if(\mathbf x_i)=1exactly on an edge — a free support vector
αi=C\alpha_i=Cξi≥0\xi_i\ge0, yif(xi)≤1y_if(\mathbf x_i)\le1on the edge, inside the street or across the line — a bounded support vector

One practical rule follows. bb is found exactly as before, b=ys−∑iαiyi(xi⋅xs)b=y_s-\sum_i\alpha_iy_i(\mathbf x_i\cdot\mathbf x_s) — but only from a free support vector, where the score is known to be exactly 1. A bounded one may be inside the street, and would give the wrong bb.

The realization

min⁡ 12∥w∥2+C∑iξi⟺ the same dual with 0≤αi≤C\begin{gathered}\min\ \tfrac12\|\mathbf w\|^2+C\sum_i\xi_i\\ \Longleftrightarrow\ \text{the same dual with }0\le\alpha_i\le C\end{gathered}

Let points trespass, but fine them CC per unit. In the dual the fine becomes a ceiling on how hard any one point can push. Small CC: wide and forgiving. Large CC: narrow and strict. C→∞C\to\infty: the hard margin.

Pause & predict

After training, a point has slack ξi=1.4\xi_i=1.4. Where is it?

Pause & predict

You lower the fine CC from 100 to 0.1. What usually happens?

If you want the algebra · 1 proof, step by step
Prove it · the soft-margin dual, in outline

Claim. The dual of min⁡12∥w∥2+C∑iξi\min\tfrac12\|\mathbf w\|^2+C\sum_i\xi_i s.t. yi(w⋅xi+b)≥1−ξiy_i(\mathbf w\cdot\mathbf x_i+b)\ge1-\xi_i, ξi≥0\xi_i\ge0 is the hard-margin dual with the extra condition αi≤C\alpha_i\le C; and the three price regimes follow.

1
Two families of walls, two families of prices: αi≥0\alpha_i\ge0 for the margin rule, μi≥0\mu_i\ge0 for ξi≥0\xi_i\ge0: L= 12∥w∥2+C∑iξi−∑iμiξi−∑iαi(zi−1+ξi),\begin{aligned}L=\ &\tfrac12\|\mathbf w\|^2+C\sum_i\xi_i\\ &-\sum_i\mu_i\xi_i\\ &-\sum_i\alpha_i\big(z_i-1+\xi_i\big),\end{aligned} where zi=yi(w⋅xi+b)z_i=y_i(\mathbf w\cdot\mathbf x_i+b) is point ii's score.
2
The slopes in w\mathbf w and bb are exactly as before: w=∑αiyixi\mathbf w=\sum\alpha_iy_i\mathbf x_i, ∑αiyi=0\sum\alpha_iy_i=0. The only terms with ξi\xi_i are Cξi−αiξi−μiξiC\xi_i-\alpha_i\xi_i-\mu_i\xi_i, so ∂L∂ξi=C−αi−μi=0⇒ αi+μi=C.\begin{gathered}\frac{\partial L}{\partial\xi_i}=C-\alpha_i-\mu_i=0\\ \Rightarrow\ \alpha_i+\mu_i=C.\end{gathered} Since μi≥0\mu_i\ge0, this says αi≤C\alpha_i\le C.
3
Substitute. All the ξi\xi_i terms collect into ∑iξi(C−αi−μi)=0\sum_i\xi_i(C-\alpha_i-\mu_i)=0, every bb term dies by the balance condition, and the rest is the hard-margin algebra of §8. The dual is the same hill with the box 0≤αi≤C0\le\alpha_i\le C.
4
The regimes. Complementary slackness now has two parts: αi[yif(xi)−1+ξi]=0\alpha_i\big[y_if(\mathbf x_i)-1+\xi_i\big]=0 and μiξi=0\mu_i\xi_i=0. If αi=0\alpha_i=0 then μi=C>0\mu_i=C>0, so ξi=0\xi_i=0 and yif≥1y_if\ge1. If 0<αi<C0<\alpha_i<C then μi>0\mu_i>0, so ξi=0\xi_i=0, and the bracket is zero: yif=1y_if=1. If αi=C\alpha_i=C then μi=0\mu_i=0, ξi≥0\xi_i\ge0 and yif=1−ξi≤1y_if=1-\xi_i\le1. ∎ Only the middle case pins the score at exactly 1 — which is why bb must be read off a free support vector.

In one sentence: Give each point a slack ξi\xi_i, fine it CC per unit, and the dual stays the same except for a ceiling 0≤αi≤C0\le\alpha_i\le C — the knob that trades a wide street against trespassers.

11

The hinge loss falls out of the slack

Imagine this

A phone plan: the first 2 GB are free. After that you pay ₹10 for every extra GB. The bill is flat at zero, then rises in a straight line.

Nobody sat down and chose that shape for the SVM. It falls out of the slack by itself — and it has exactly this "free, then a straight line" look.

Write zi=yi(w⋅xi+b)z_i=y_i(\mathbf w\cdot\mathbf x_i+b) for the score of point ii. The soft rule zi≥1−ξiz_i\ge1-\xi_i says ξi≥1−zi\xi_i\ge1-z_i. We also asked ξi≥0\xi_i\ge0. So the slack has two floors, and since every unit of slack costs CC, the optimiser never pays more than the higher floor:

ξi=max⁡(0, 1−zi).\xi_i=\max\big(0,\ 1-z_i\big).

This is the hinge loss. Put in a few scores:

score ziz_i22110.50.500−0.5-0.5−2-2
hinge loss ξi\xi_i000.511.53
where it iswell outsideon the edgeinside, correct sideon the centre linewrong sidewrong side, far

Put the hinge back into the problem and the constraints disappear altogether. What is left is the form most machine-learning books start from — a regulariser plus a loss, like every other classifier:

min⁡w,b 12∥w∥2+C∑imax⁡(0, 1−yi(w⋅xi+b)).\begin{aligned}\min_{\mathbf w,b}\ &\tfrac12\|\mathbf w\|^2\\ &+C\sum_i\max\big(0,\ 1-y_i(\mathbf w\cdot\mathbf x_i+b)\big).\end{aligned}
Drag a point across the streetLeft: a street and one point. Right: its score z on the hinge (solid) and on the "count the mistakes" loss (dashed).

Try: Drag the point from far outside, through the street, across the centre line and beyond. The hinge stays flat at 0 until the edge, then climbs one-for-one. The counting loss does nothing, then jumps.

put the score at
—

Why not simply count mistakes? Because counting is flat everywhere with a cliff at zero. A slope-following method stands on flat ground and learns nothing about which way to go. The hinge has a slope exactly where it matters — inside the street and beyond — and is exactly zero beyond the edge. That flat zero is what makes most prices vanish, and so it is what gives an SVM its few support vectors.

The realization

ξi=max⁡(0, 1−zi)zi=yi(w⋅xi+b)\begin{gathered}\xi_i=\max\big(0,\,1-z_i\big)\\ z_i=y_i(\mathbf w\cdot\mathbf x_i+b)\end{gathered}

The hinge loss is not a design choice. It is the cheapest slack that obeys both floors: zero when a point is safely outside the street, growing one-for-one as it walks in and across.

Pause & predict

A point has score z=0.5z=0.5. What is its hinge loss, and what does the 0/1 "count the mistakes" loss say?

If you want the algebra · 1 proof, step by step
Prove it · the slack is the hinge

Claim. At the soft-margin answer, ξi=max⁡(0, 1−yi(w⋅xi+b))\xi_i=\max\big(0,\,1-y_i(\mathbf w\cdot\mathbf x_i+b)\big) for every ii. So the problem equals the unconstrained min⁡12∥w∥2+C∑imax⁡(0,1−yif(xi))\min\tfrac12\|\mathbf w\|^2+C\sum_i\max(0,1-y_if(\mathbf x_i)).

1
Fix (w,b)(\mathbf w,b) and write zi=yi(w⋅xi+b)z_i=y_i(\mathbf w\cdot\mathbf x_i+b). The constraints on ξi\xi_i are ξi≥1−zi\xi_i\ge1-z_i and ξi≥0\xi_i\ge0, that is ξi≥max⁡(0,1−zi)\xi_i\ge\max(0,1-z_i). Rearranging zi≥1−ξiz_i\ge1-\xi_i moves ξi\xi_i to the left; the inequality keeps its direction.
2
The objective charges CξiC\xi_i with C>0C>0, and each ξi\xi_i appears in nothing else. So the cheapest allowed value is the floor: ξi=max⁡(0,1−zi)\xi_i=\max(0,1-z_i). Put it in and the constraints are gone. ∎ Check the table: z=2→0z=2\to0, z=1→0z=1\to0, z=0.5→0.5z=0.5\to0.5, z=0→1z=0\to1, z=−0.5→1.5z=-0.5\to1.5, z=−2→3z=-2\to3.

In one sentence: The cheapest slack that obeys both floors is ξi=max⁡(0,1−zi)\xi_i=\max(0,1-z_i) — the hinge loss, flat beyond the edge and a straight slope inside, which turns the SVM into "regulariser plus loss".

12

Dot products are everywhere — and the classifier is a vote

Imagine this

A new student joins the class, and the teacher must guess which house she will fit into. The teacher asks a few senior students to vote.

Each voter's say depends on three things: how important that voter is, which house the voter is in, and how similar the new student is to the voter. Add up the votes, and the sign tells you the house.

Look back at the dual (§8). The data appears only as dot products xi⋅xj\mathbf x_i\cdot\mathbf x_j — never a single coordinate on its own. Now look at how the trained machine classifies a new point x\mathbf x. Put w=∑iαiyixi\mathbf w=\sum_i\alpha_iy_i\mathbf x_i into f(x)=w⋅x+bf(\mathbf x)=\mathbf w\cdot\mathbf x+b:

f(x)=∑i αi⏟importance⋅yi⏟which side⋅(xi⋅x)⏟similarity+b⏟offset.\begin{aligned}f(\mathbf x)=\sum_i\ &\underbrace{\alpha_i}_{\text{importance}}\cdot\underbrace{y_i}_{\text{which side}}\\ &\cdot\underbrace{(\mathbf x_i\cdot\mathbf x)}_{\text{similarity}}+\underbrace{b}_{\text{offset}}.\end{aligned}

Again, only dot products. And read it as a vote. Each support vector votes for its own side (yi=±1y_i=\pm1), with a strength set by its importance αi\alpha_i and by how similar the new point is to it. A dot product is a similarity score — Unit 3's "agreement" between two lists. Points with αi=0\alpha_i=0 do not vote at all.

A tiny example. The twelve points of §6 have three voters: (1,3)(1,3) and (3,5)(3,5) with α=18\alpha=\tfrac18, and (4,2)(4,2) with α=14\alpha=\tfrac14, and b=0b=0. For the new point x=(2,4)\mathbf x=(2,4):

f(2,4)=18(1⋅2+3⋅4)+18(3⋅2+5⋅4)−14(4⋅2+2⋅4)=1.75+3.25−4=1.\begin{aligned}f(2,4)&=\tfrac18(1\cdot2+3\cdot4)\\ &\quad+\tfrac18(3\cdot2+5\cdot4)\\ &\quad-\tfrac14(4\cdot2+2\cdot4)\\ &=1.75+3.25-4=1.\end{aligned}

Positive, so blue — and exactly 1, so (2,4)(2,4) sits right on the blue edge of the street.

The classifier as a voteDrag the gold star. Each support vector casts a vote αᵢ · yᵢ · similarity; the votes plus b add up to f, and its sign is the answer.

Try: Drag the star towards the orange voter and watch its bar grow while the blue bars shrink. Then open a kernel machine: the same vote, but the similarity is (xi⋅x)2(\mathbf x_i\cdot\mathbf x)^2 — and the regions are no longer split by a straight line.

drag the star

—

The observation that makes the trick possible

Building the street (the dual) and using it (the vote) both touch the data only through dot products. So if we replace "dot product" by some other similarity score, the whole machine keeps running with no other change. The next two sections show which replacements are allowed, and what they buy.

The realization

f(x)=∑i∈SVαi yi (xi⋅x)+bf(\mathbf x)=\sum_{i\in\text{SV}}\alpha_i\,y_i\,(\mathbf x_i\cdot\mathbf x)+b

Classifying is a vote: each support vector says how similar the new point is to it, weighted by its importance, pulling towards its own side. Keep this picture — the kernel trick changes only the similarity.

Pause & predict

With the three voters above (α=18,18\alpha=\tfrac18,\tfrac18 blue at (1,3)(1,3), (3,5)(3,5); α=14\alpha=\tfrac14 orange at (4,2)(4,2); b=0b=0), what is f(4,1)f(4,1)?

In one sentence: The data enters both the training and the prediction only through dot products, and f(x)=∑αiyi(xi⋅x)+bf(\mathbf x)=\sum\alpha_iy_i(\mathbf x_i\cdot\mathbf x)+b is a vote: importance × side × similarity, plus an offset.

13

When no straight line will do — lift the data

Imagine this

Blue and orange marbles lie mixed on a table, the blue ones in the middle and the orange ones around them. No straight ruler laid on the table can separate them.

Now slap the middle of the table from below. The blue marbles jump higher than the orange ones. For that instant, a flat sheet of glass slid in sideways could separate them perfectly.

An SVM can only draw a straight line (a flat plane, in more dimensions). Plenty of data refuses. Three small examples show the way out.

One dimension. Blue points at x=±1x=\pm1, orange points at x=±2x=\pm2. The blue ones sit between the orange ones, so no single threshold separates them. Add one new feature, x2x^2. The blue points go to (±1,1)(\pm1,1) and the orange to (±2,4)(\pm2,4): the orange points have been lifted higher, and the flat line x2=2.5x^2=2.5 separates them. Push that line back down to the original line and it becomes two thresholds, x=±2.5≈±1.58x=\pm\sqrt{2.5}\approx\pm1.58.

XOR. Blue at (1,1)(1,1) and (−1,−1)(-1,-1), orange at (1,−1)(1,-1) and (−1,1)(-1,1). Every straight line leaves one colour on both sides. Add the feature x1x2x_1x_2: for the blue points it is +1+1, for the orange ones −1-1. One colour rises, the other sinks, and the flat plane "height = 0" separates them. Back on the floor that plane is x1x2=0x_1x_2=0 — the pair of axes.

Rings. Blue points in an inner ring, orange points in an outer ring. Add x12+x22x_1^2+x_2^2, the squared distance from the centre. Every point rises onto a bowl, the outer ring higher than the inner one, and a flat plane slices between them. Its shadow on the floor is a circle.

Lift, cut, look downAdd one feature, lift every point by it, slice the lifted points with a flat plane — and see what that plane looks like from the floor.

Try: Press ▶ lift and cut on each tab. Then drag the lift slider back to 0 and up again slowly: the separation only exists up there, but its shadow stays on the floor.

drag the picture to orbit

0
—
The realization

flat cut in the lifted space⇓curved rule on the floor\begin{gathered}\text{flat cut in the lifted space}\\ \Big\Downarrow\\ \text{curved rule on the floor}\end{gathered}

Take the data to a higher-dimensional space by adding new features. Up there, fit an ordinary linear SVM — a flat boundary. Its shadow back home is curved. The SVM never stopped being linear; it is linear in the higher space.

Pause & predict

In the 1-D example the cut up there is x2=2.5x^2=2.5. What is the rule back on the original line?

In one sentence: Add features so the data rises into a higher space, cut it there with a flat plane, and the cut comes back down as a curve — the machine stays linear, just not in the space you started in.

14

The trick — similarity without visiting the big space

Imagine this

You want to know how alike two cities are across every product in their markets — lakhs of prices each. You could fly to both, write down every price, and compare them one by one.

Or someone hands you a formula that takes a two-line summary of each city and gives the same answer. You get the full comparison without ever making the trip.

Lifting works, but adding features costs time and memory. Here is the trick that avoids the cost. Take 2-D points and the quadratic lift, with one odd-looking 2\sqrt2:

φ(x)=(x12, 2 x1x2, x22).\varphi(\mathbf x)=\big(x_1^2,\ \sqrt2\,x_1x_2,\ x_2^2\big).

Compute the dot product up there, coordinate by coordinate:

φ(x)⋅φ(z)=x12z12+x22z22+(2x1x2)(2z1z2)=(x1z1)2+2(x1z1)(x2z2)+(x2z2)2=(x1z1+x2z2)2=(x⋅z)2.\begin{aligned}\varphi(\mathbf x)\cdot\varphi(\mathbf z)&=x_1^2z_1^2+x_2^2z_2^2\\ &\quad+(\sqrt2x_1x_2)(\sqrt2z_1z_2)\\ &=(x_1z_1)^2+2(x_1z_1)(x_2z_2)\\ &\quad+(x_2z_2)^2\\ &=(x_1z_1+x_2z_2)^2=(\mathbf x\cdot\mathbf z)^2.\end{aligned}

Read the last line slowly. The similarity in the 3-D space is the ordinary 2-D dot product, squared. You never have to build φ\varphi at all. And now the 2\sqrt2 makes sense: the square (a+b)2(a+b)^2 has a middle term 2ab2ab, and the 2\sqrt2 splits that 2 evenly between the x\mathbf x half and the z\mathbf z half, 2⋅2=2\sqrt2\cdot\sqrt2=2.

A tiny example. x=(1,2)\mathbf x=(1,2), z=(3,1)\mathbf z=(3,1). The short route: x⋅z=3+2=5\mathbf x\cdot\mathbf z=3+2=5, squared, 2525. The long route: φ(x)=(1,22,4)\varphi(\mathbf x)=(1,2\sqrt2,4), φ(z)=(9,32,1)\varphi(\mathbf z)=(9,3\sqrt2,1), and 9+12+4=259+12+4=25. Same number.

So the kernel trick is one sentence: choose a function K(x,z)K(\mathbf x,\mathbf z) that equals φ(x)⋅φ(z)\varphi(\mathbf x)\cdot\varphi(\mathbf z) for some lift φ\varphi, and replace every dot product by it —

xi⋅xj ⟶ K(xi,xj),xi⋅x ⟶ K(xi,x).\begin{gathered}\mathbf x_i\cdot\mathbf x_j\ \longrightarrow\ K(\mathbf x_i,\mathbf x_j),\\ \mathbf x_i\cdot\mathbf x\ \longrightarrow\ K(\mathbf x_i,\mathbf x).\end{gathered}

The SVM then draws a flat boundary in the lifted space and a curved one down here, while all the arithmetic stays in the small space.

kernelK(x,z)K(\mathbf x,\mathbf z)what it means
linearx⋅z\mathbf x\cdot\mathbf zno lift at all: the SVM of §1–§11
polynomial, homogeneous(x⋅z)d(\mathbf x\cdot\mathbf z)^dall products of exactly dd coordinates; d=2d=2 is the quadratic lift above
polynomial, non-homogeneous(x⋅z+1)d(\mathbf x\cdot\mathbf z+1)^dthe same plus every lower degree and a constant; for d=2d=2 in 2-D, six features: (x12, x22, 2x1x2, 2x1, 2x2, 1)(x_1^2,\,x_2^2,\,\sqrt2x_1x_2,\,\sqrt2x_1,\,\sqrt2x_2,\,1)
RBF (Gaussian)e−γ∥x−z∥2e^{-\gamma\|\mathbf x-\mathbf z\|^2}similarity that fades with distance; its lift has infinitely many coordinates — yet the kernel costs three operations

Why may we use a kernel whose φ\varphi nobody can write down? A result called Mercer's theorem says: if KK is symmetric and every kernel table it produces is positive semi-definite (no "negative lengths"), then KK is a dot product in some lifted space, even an infinite one. That is what makes the RBF kernel legal. It is one of the quiet engines of applied mathematics; you only need to know it exists.

How big is the space we never visit? With nn original features and degree dd, the homogeneous kernel uses every product of exactly dd coordinates: (n+d−1d)\binom{n+d-1}{d} features. The non-homogeneous one uses every degree up to dd: (n+dd)\binom{n+d}{d}. For n=2,d=2n=2,d=2 that is 33 and 66 — the two lifts above. For n=10,d=4n=10,d=4 it is 715715 and 10011001. The kernel still costs one 10-term dot product and one power.

Two routes to one numberRoute A builds the lift and takes a long dot product. Route B takes the short dot product and applies the kernel. Change x and z and watch them agree.

Try: Keep x=(1,2)\mathbf x=(1,2), z=(3,1)\mathbf z=(3,1) and switch between the kernels: both routes print 5, 25, 36. Then slide nn to 100 and dd to 5 in the feature counter and compare the two bars.

(, ) (, )

Route A · build φ, then dot

Route B · dot first, then the kernel

How many features are we secretly using? n original features, degree d

10
4
The realization

φ(x)⋅φ(z)=(x⋅z)2⟹xi⋅xj → K(xi,xj)\begin{gathered}\varphi(\mathbf x)\cdot\varphi(\mathbf z)=(\mathbf x\cdot\mathbf z)^2\\ \Longrightarrow\quad \mathbf x_i\cdot\mathbf x_j\ \to\ K(\mathbf x_i,\mathbf x_j)\end{gathered}

A kernel is a dot product in a bigger space, computed without going there. Swap every dot product in the dual and in the vote for KK, and the linear machine draws curves — at the cost of the small space, whatever the size of the big one.

Pause & predict

For x=(1,2)\mathbf x=(1,2) and z=(3,1)\mathbf z=(3,1), what is K(x,z)=(x⋅z)2K(\mathbf x,\mathbf z)=(\mathbf x\cdot\mathbf z)^2? And what goes wrong if you square the coordinates first?

Pause & predict

With n=10n=10 features and degree d=4d=4, how many features do (x⋅z)4(\mathbf x\cdot\mathbf z)^4 and (x⋅z+1)4(\mathbf x\cdot\mathbf z+1)^4 secretly use?

If you want the algebra · 3 proofs, step by step
Prove it · the quadratic kernel is a dot product upstairs

Claim. With φ(x)=(x12,2x1x2,x22)\varphi(\mathbf x)=(x_1^2,\sqrt2x_1x_2,x_2^2), φ(x)⋅φ(z)=(x⋅z)2\varphi(\mathbf x)\cdot\varphi(\mathbf z)=(\mathbf x\cdot\mathbf z)^2 for all x,z∈R2\mathbf x,\mathbf z\in\mathbb R^2.

1
Multiply matching coordinates and add: φ(x)⋅φ(z)=x12z12+x22z22+(2x1x2)(2z1z2)=x12z12+x22z22+2x1z1x2z2.\begin{aligned}&\varphi(\mathbf x)\cdot\varphi(\mathbf z)\\ &=x_1^2z_1^2+x_2^2z_2^2\\ &\quad+(\sqrt2x_1x_2)(\sqrt2z_1z_2)\\ &=x_1^2z_1^2+x_2^2z_2^2+2x_1z_1x_2z_2.\end{aligned} 2⋅2=2\sqrt2\cdot\sqrt2=2: that is the only job of the 2\sqrt2.
2
Regroup as a2+2ab+b2a^2+2ab+b^2 with a=x1z1a=x_1z_1, b=x2z2b=x_2z_2: the sum is (x1z1+x2z2)2=(x⋅z)2(x_1z_1+x_2z_2)^2=(\mathbf x\cdot\mathbf z)^2. ∎ Without the 2\sqrt2 the middle term would be x1z1x2z2x_1z_1x_2z_2, and the sum would not be a perfect square of anything simple.
Prove it · (x·z + 1)² uses six features

Claim. In 2-D, (x⋅z+1)2=φ(x)⋅φ(z)(\mathbf x\cdot\mathbf z+1)^2=\varphi(\mathbf x)\cdot\varphi(\mathbf z) with φ(v)=(v12, v22, 2v1v2, 2v1, 2v2, 1)\varphi(\mathbf v)=(v_1^2,\,v_2^2,\,\sqrt2v_1v_2,\,\sqrt2v_1,\,\sqrt2v_2,\,1).

1
Expand the square of three terms: (x1z1+x2z2+1)2=x12z12+x22z22+1+2x1z1x2z2+2x1z1+2x2z2.\begin{aligned}&(x_1z_1+x_2z_2+1)^2\\ &=x_1^2z_1^2+x_2^2z_2^2+1\\ &\quad+2x_1z_1x_2z_2\\ &\quad+2x_1z_1+2x_2z_2.\end{aligned}
2
Write each term as "something in x\mathbf x" times "the same thing in z\mathbf z": (x12)(z12)(x_1^2)(z_1^2), (x22)(z22)(x_2^2)(z_2^2), (2x1x2)(2z1z2)(\sqrt2x_1x_2)(\sqrt2z_1z_2), (2x1)(2z1)(\sqrt2x_1)(\sqrt2z_1), (2x2)(2z2)(\sqrt2x_2)(\sqrt2z_2), (1)(1)(1)(1). The x\mathbf x-halves are φ(x)\varphi(\mathbf x). ∎ So the "+1" quietly adds the two linear features and a constant to the three quadratic ones. Check with x=(2,1)\mathbf x=(2,1), z=(1,3)\mathbf z=(1,3): 4+9+12+4+6+1=36=(5+1)24+9+12+4+6+1=36=(5+1)^2.
Prove it · counting the hidden features

Claim. With nn variables there are (n+d−1d)\binom{n+d-1}{d} products of degree exactly dd, and (n+dd)\binom{n+d}{d} of degree at most dd.

1
A product of degree dd is a choice of how many times each variable appears: k1+⋯+kn=dk_1+\dots+k_n=d with each ki≥0k_i\ge0. Picture dd stars split into nn groups by n−1n-1 bars: an arrangement of d+n−1d+n-1 symbols, of which dd are stars. There are (n+d−1d)\binom{n+d-1}{d} of them. Example: n=2,d=2n=2,d=2: x12,x1x2,x22x_1^2,x_1x_2,x_2^2, and (32)=3\binom{3}{2}=3.
2
Degree at most dd: add a dummy variable x0=1x_0=1. Every product of degree ≤d\le d in x1..xnx_1..x_n is exactly one product of degree dd in x0..xnx_0..x_n. That gives ((n+1)+d−1d)=(n+dd)\binom{(n+1)+d-1}{d}=\binom{n+d}{d}. ∎ n=10,d=4n=10,d=4: (134)=715\binom{13}{4}=715 and (144)=1001\binom{14}{4}=1001. n=3,d=4n=3,d=4: 1515 and 3535.

In one sentence: Because φ(x)⋅φ(z)=(x⋅z)2\varphi(\mathbf x)\cdot\varphi(\mathbf z)=(\mathbf x\cdot\mathbf z)^2, a kernel gives the similarity of the lifted points from the small space alone — so we swap every dot product for KK and never build the huge (even infinite) lift.

15

A kernel SVM, start to finish

Imagine this

A good recipe for dal is a fixed order of steps: wash, soak, boil, temper. Follow the order and it comes out right every time, whoever is cooking.

A kernel SVM on paper has the same kind of recipe. Eight steps, always in the same order.

The recipe.

  1. Kernel table. Dot products first, then apply the kernel to each number. Use the symmetry Kij=KjiK_{ij}=K_{ji} and check the diagonal.
  2. Look for symmetry. If two rows of the table look alike (or are mirror images) with the same label, take their prices equal. Four unknowns often become one.
  3. Use ∑iαiyi=0\sum_i\alpha_iy_i=0 to tie the remaining unknowns together.
  4. Write the dual with numbers, block by block: same-class entries carry yiyj=+1y_iy_j=+1, cross-class entries −1-1. Reduce to one unknown, differentiate, solve.
  5. Check αi≥0\alpha_i\ge0 and note the support vectors.
  6. Find bb from a support vector: ys(∑iαiyiKis+b)=1y_s\big(\sum_i\alpha_iy_iK_{is}+b\big)=1. Confirm with a second one. Never assume b=0b=0.
  7. Classify a new point: f(x)=∑iαiyiK(xi,x)+bf(\mathbf x)=\sum_i\alpha_iy_iK(\mathbf x_i,\mathbf x)+b, read the sign.
  8. Optional finish: simplify ff in x1,x2x_1,x_2 to see the curve. It doubles as a check: every support vector must score exactly ±1\pm1.

Worked through once. Four points: x1=(1,2)\mathbf x_1=(1,2) and x2=(−1,−2)\mathbf x_2=(-1,-2) blue; x3=(1,−2)\mathbf x_3=(1,-2) and x4=(−1,2)\mathbf x_4=(-1,2) orange. Kernel K=(x⋅z)2K=(\mathbf x\cdot\mathbf z)^2.

Step 1. Row 1 of dot products: x1⋅x1=5\mathbf x_1\cdot\mathbf x_1=5, x1⋅x2=−5\mathbf x_1\cdot\mathbf x_2=-5, x1⋅x3=−3\mathbf x_1\cdot\mathbf x_3=-3, x1⋅x4=3\mathbf x_1\cdot\mathbf x_4=3. Square each: 25,25,9,925,25,9,9. The whole table:

K=[252599252599992525992525].K=\begin{bmatrix}25&25&9&9\\25&25&9&9\\9&9&25&25\\9&9&25&25\end{bmatrix}.

Steps 2–3. Rows 1–2 are identical and share a label; so are rows 3–4. Take α1=α2=a\alpha_1=\alpha_2=a and α3=α4=c\alpha_3=\alpha_4=c. Balance: 2a−2c=02a-2c=0, so c=ac=a: all four prices equal aa.

Step 4. Each row of yiyjKijy_iy_jK_{ij} is 25+25−9−9=3225+25-9-9=32, so the double sum is 4⋅32 a2=128a24\cdot32\,a^2=128a^2 and

D(a)=4a−12⋅128a2=4a−64a2,D′(a)=4−128a=0⇒ a=132.\begin{aligned}D(a)&=4a-\tfrac12\cdot128a^2\\ &=4a-64a^2,\\ D'(a)&=4-128a=0\\ \Rightarrow\ a&=\tfrac1{32}.\end{aligned}

Step 5. All four prices are 132>0\tfrac1{32}>0: every point is a support vector.

Step 6. From x1\mathbf x_1: 132(25+25−9−9)+b=1\tfrac1{32}(25+25-9-9)+b=1, so 1+b=11+b=1 and b=0b=0.

Step 7. For the new point (2,1)(2,1): the dot products with the four points are 4,−4,0,04,-4,0,0; squared, 16,16,0,016,16,0,0. So f=132(16+16)=1f=\tfrac1{32}(16+16)=1: class +1+1, right on the edge.

Step 8. Put the kernel into ff and simplify:

f(x)=132[2(x1+2x2)2−2(x1−2x2)2]=x1x22.\begin{aligned}f(\mathbf x)&=\tfrac1{32}\big[2(x_1+2x_2)^2-2(x_1-2x_2)^2\big]\\ &=\frac{x_1x_2}{2}.\end{aligned}

The boundary x1x2=0x_1x_2=0 is the pair of axes, and f=±1f=\pm1 are the curves x1x2=±2x_1x_2=\pm2, passing through all four points.

And upstairs? The lift φ(x)=(x12,2x1x2,x22)\varphi(\mathbf x)=(x_1^2,\sqrt2x_1x_2,x_2^2) sends both blue points to the same place, (1,22,4)(1,2\sqrt2,4), and both orange points to (1,−22,4)(1,-2\sqrt2,4). Four points became two, and a two-point SVM is the easiest there is.

Four points become twoThe four points on the floor, then lifted by φ(x) = (x₁², √2·x₁x₂, x₂²). Up there the problem is a two-point street; down here its shadow is the pair of axes.

Try: Press ▶ lift and watch each pair of same-coloured points fly to one spot. The street between the two spots is 42≈5.664\sqrt2\approx5.66 wide — exactly the distance between them.

drag the picture to orbit

0

Now try everything at once. Pick a data set, pick a kernel, set the fine, and a live solver draws the boundary f=0f=0 (white) and the street edges f=±1f=\pm1 (violet). For the four-point set with (x⋅z)2(\mathbf x\cdot\mathbf z)^2, the recipe panel walks the eight steps with live numbers.

The kernel playgroundFive data sets, four kernels, a fine C, and a live solver. Click to add points; drag to move them.

Try: On rings, start with linear — no street exists. Switch to (x⋅z+1)2(\mathbf x\cdot\mathbf z+1)^2: a circle appears. Then try RBF and raise γ\gamma until the boundary starts hugging single points. Open the landscape tab to see ff as hills and valleys with the boundary as the shoreline.

data
kernel

tap to add a point · drag to move

∞
1
tap adds
—
The realization

max⁡α∑iαi−12∑i,jαiαjyiyjKijb=ys−∑iαiyiKisf(x)=∑iαiyiK(xi,x)+b\begin{gathered}\max_{\boldsymbol\alpha}\sum_i\alpha_i-\tfrac12\sum_{i,j}\alpha_i\alpha_jy_iy_jK_{ij}\\ b=y_s-\sum_i\alpha_iy_iK_{is}\\ f(\mathbf x)=\sum_i\alpha_iy_iK(\mathbf x_i,\mathbf x)+b\end{gathered}

The kernel table is always N×NN\times N — four points give a 4×44\times4 table whatever the degree. The degree only changes the size of the lifted space, which we never build. w\mathbf w still exists, w=∑αiyiφ(xi)\mathbf w=\sum\alpha_iy_i\varphi(\mathbf x_i), but it lives up there; we keep the prices and the support vectors instead.

Pause & predict

In the worked example (αi=132\alpha_i=\tfrac1{32}, b=0b=0, K=(x⋅z)2K=(\mathbf x\cdot\mathbf z)^2), classify the new point (−1,3)(-1,3).

If you want the algebra · 2 proofs, step by step
Prove it · the four-point machine is f = x₁x₂/2

Claim. For x1=(1,2)+\mathbf x_1=(1,2)^+, x2=(−1,−2)+\mathbf x_2=(-1,-2)^+, x3=(1,−2)−\mathbf x_3=(1,-2)^-, x4=(−1,2)−\mathbf x_4=(-1,2)^- with K=(x⋅z)2K=(\mathbf x\cdot\mathbf z)^2, αi=132\alpha_i=\tfrac1{32} and b=0b=0, the decision function is f(x)=x1x2/2f(\mathbf x)=x_1x_2/2.

1
K(x1,x)=(x1+2x2)2=K(x2,x)K(\mathbf x_1,\mathbf x)=(x_1+2x_2)^2=K(\mathbf x_2,\mathbf x), and K(x3,x)=(x1−2x2)2=K(x4,x)K(\mathbf x_3,\mathbf x)=(x_1-2x_2)^2=K(\mathbf x_4,\mathbf x). So f(x)=132[2(x1+2x2)2−2(x1−2x2)2].\begin{aligned}f(\mathbf x)=\tfrac1{32}\big[&2(x_1+2x_2)^2\\ &-2(x_1-2x_2)^2\big].\end{aligned}
2
Use (p+q)2−(p−q)2=4pq(p+q)^2-(p-q)^2=4pq with p=x1p=x_1, q=2x2q=2x_2: the bracket is 2⋅4⋅2x1x2=16x1x22\cdot4\cdot2x_1x_2=16x_1x_2, so f=1632x1x2=x1x22f=\tfrac{16}{32}x_1x_2=\tfrac{x_1x_2}2. ∎ Checks: each training point has x1x2=±2x_1x_2=\pm2, so f=±1f=\pm1 — all four sit on the street's edges. Upstairs, w=∑αiyiφ(xi)=(0,24,0)\mathbf w=\sum\alpha_iy_i\varphi(\mathbf x_i)=(0,\tfrac{\sqrt2}{4},0) and the width is 2/∥w∥=422/\|\mathbf w\|=4\sqrt2, the distance between the two lifted points.
Prove it · why the symmetric prices are the natural answer

Claim. In the four-point problem the prices are not unique — any α1+α2=116=α3+α4\alpha_1+\alpha_2=\tfrac1{16}=\alpha_3+\alpha_4 with αi≥0\alpha_i\ge0 gives the same machine — and the equal split 132\tfrac1{32} each is the one of smallest size.

1
x1\mathbf x_1 and x2\mathbf x_2 have the same kernel row (the kernel sees them as the same point: φ(x1)=φ(x2)\varphi(\mathbf x_1)=\varphi(\mathbf x_2)), and so do x3\mathbf x_3 and x4\mathbf x_4. In DD and in ff, α1\alpha_1 and α2\alpha_2 only ever appear as the sum α1+α2\alpha_1+\alpha_2; likewise α3+α4\alpha_3+\alpha_4.
2
So only the two sums are fixed, at 116\tfrac1{16} each. Among all splits of a fixed sum, α12+α22\alpha_1^2+\alpha_2^2 is smallest when the two are equal. That is the answer "by symmetry", and the one this page's solver reports. ∎ w\mathbf w, bb and ff are the same for every split — the prediction never depends on the choice.

In one sentence: Build the kernel table (dots first, kernel second), shrink the dual with symmetry and balance, find the prices, get bb from a support vector, and classify by the vote — the boundary is flat upstairs and curved down here.

16

What to carry forward

One idea, built up in four acts: the widest street, prices on points, a fine for trespassers, and a similarity that measures a bigger space without going there.

The problem

min⁡ 12∥w∥2  s.t.  yi(w⋅xi+b)≥1\min\ \tfrac12\|\mathbf w\|^2\ \ \text{s.t.}\ \ y_i(\mathbf w\cdot\mathbf x_i+b)\ge1

The widest street is 2/∥w∥2/\|\mathbf w\| wide once the nearest points score 1. A bowl with straight walls: one answer.

The dual

max⁡∑αi−12∑αiαjyiyj xi⋅xj\max\sum\alpha_i-\tfrac12\sum\alpha_i\alpha_jy_iy_j\,\mathbf x_i\cdot\mathbf x_j

with αi≥0\alpha_i\ge0, ∑αiyi=0\sum\alpha_iy_i=0. Its peak equals the primal minimum.

The two identities

w=∑iαiyixi,∑iαiyi=0\mathbf w=\sum_i\alpha_iy_i\mathbf x_i,\qquad\sum_i\alpha_iy_i=0

The street is built from the points; the two classes' pushes balance.

Three kinds of points

Outside the street: α=0\alpha=0, deletable. On an edge: α≥0\alpha\ge0, support vectors when α>0\alpha>0. Inside: impossible with a hard margin, fined with a soft one.

Soft margin's one change

0≤αi≤C,ξi=max⁡(0,1−zi)0\le\alpha_i\le C,\qquad \xi_i=\max(0,1-z_i)

A fine per unit of trespass; in the dual, a ceiling on every price. Big CC = strict and narrow.

The kernel swap

xi⋅xj→K(xi,xj)=φ(xi)⋅φ(xj)\mathbf x_i\cdot\mathbf x_j\to K(\mathbf x_i,\mathbf x_j)=\varphi(\mathbf x_i)\cdot\varphi(\mathbf x_j)

Flat upstairs, curved down here, and the table stays N×NN\times N.

The recipe

Dots first, kernel second · symmetry · balance · dual with numbers · check α≥0\alpha\ge0 · bb from a support vector · classify by the vote · simplify ff to see the curve.

The thread to Units 16 and 18

Look at the vote once more: score every stored item by its similarity to the new one, then take a weighted sum. Hold on to that sentence. When machines start reading sentences (Unit 16) and then paying attention to words (Unit 18), the core operation will be exactly this: similarity by a dot product, then a weighted vote.

The one thing to remember

Draw the widest street, and let the few points that touch it decide. Everything else — the dual, the fine, the kernel — is how you find that street when the world is large, noisy and curved.

In one sentence: An SVM is the widest street between two groups, held up by a few support vectors, softened by a fine, and bent by a kernel — and it only ever meets the data through dot products.

17

Practice arena — sixteen problems, solved in full

Sixteen problems, easy to hard: a margin read off a given line, a two-point street from scratch, five points on a line, the kernel identity checked by hand, counting hidden features, rebuilding a model from its prices, adding a point in one dimension, verifying a claimed answer in two, the "on the edge but no price" trap, reading a soft-margin model, auditing a proposed soft-margin answer — and then five full kernel problems, including one whose boundary is a hyperbola and one where a point's price is zero. Every number here was checked by machine.

Three habits do most of the work. Write the two identities first: ∑iαiyi=0\sum_i\alpha_iy_i=0 and w=∑iαiyixi\mathbf w=\sum_i\alpha_iy_i\mathbf x_i (or the kernel vote f(x)=∑iαiyiK(xi,x)+bf(\mathbf x)=\sum_i\alpha_iy_iK(\mathbf x_i,\mathbf x)+b). Dots first, kernel second: finish every dot product before you square it. And find bb from a support vector, then check it with a second one — never assume b=0b=0.

Problem 1easygeometry

A classifier has w=(3,4)\mathbf w=(3,4) and b=−10b=-10, in canonical form (the nearest points score exactly ±1\pm1). (a) Write the boundary and both street edges. (b) What is the width of the street? (c) How far is the point (1,1)(1,1) from the boundary, and on which side? (d) You rescale to w=(6,8)\mathbf w=(6,8), b=−20b=-20. What changes and what does not?

What this tests. That geometry lives in ratios like ∣f∣/∥w∥|f|/\|\mathbf w\|, and that "width =2/∥w∥=2/\|\mathbf w\|" is only true in canonical form. Plan. ∥w∥\|\mathbf w\| first; then the three parallel lines; then the distance formula; for (d), list what moves and what does not.

Show the full solution
Step 1 — the length of w. ∥w∥=9+16=5\|\mathbf w\|=\sqrt{9+16}=5 (the 3-4-5 triangle).
Step 2 — the three lines (a). Set f=3x1+4x2−10f=3x_1+4x_2-10 to 00 and ±1\pm1: boundary: 3x1+4x2=10,f=+1: 3x1+4x2=11,f=−1: 3x1+4x2=9.\begin{aligned}\text{boundary: }&3x_1+4x_2=10,\\ f=+1:\ &3x_1+4x_2=11,\\ f=-1:\ &3x_1+4x_2=9.\end{aligned}
Step 3 — the width (b). 2/∥w∥=2/5=0.42/\|\mathbf w\|=2/5=0.4. Check: each edge is 1/∥w∥=0.21/\|\mathbf w\|=0.2 from the boundary, and 0.2+0.2=0.40.2+0.2=0.4.
Step 4 — the distance of (1, 1) (c). f(1,1)=3+4−10=−3,∣f∣∥w∥=35=0.6.\begin{gathered}f(1,1)=3+4-10=-3,\\ \frac{|f|}{\|\mathbf w\|}=\frac35=0.6.\end{gathered} It is on the negative side. Since ∣f∣=3>1|f|=3>1, it is outside the street; as a training point it would have α=0\alpha=0.
Step 5 — the rescaling (d).
quantitybeforeafter ×2changed?
the boundary itself3x1+4x2=103x_1+4x_2=106x1+8x2=206x_1+8x_2=20no — the same line
score f(1,1)f(1,1)−3-3−6-6yes, doubles
∥w∥\|\mathbf w\|551010yes, doubles
distance ∣f∣/∥w∥|f|/\|\mathbf w\|0.60.60.60.6no
where f=±1f=\pm1 sits0.20.2 away0.10.1 awayyes — no longer canonical
The rescaled version is not canonical: its nearest points now score 2, not 1. Reading "2/∥w∥=0.22/\|\mathbf w\|=0.2" off it would be wrong.

answers at a glance: (a) boundary 3x1+4x2=103x_1+4x_2=10, edges =11=11 and =9=9. (b) width 0.40.4. (c) 0.60.6 away, negative side, outside the street. (d) the line and every distance stay; the scores and ∥w∥\|\mathbf w\| double, and the form is no longer canonical.

Remember

A score is not a distance until you divide by ∥w∥\|\mathbf w\|, and 2/∥w∥2/\|\mathbf w\| is the width only when the nearest points score exactly 1. Renormalise before you quote a margin.

Problem 2easyhard margin

Two training points: x1=(1,1)\mathbf x_1=(1,1) with y1=+1y_1=+1, x2=(4,5)\mathbf x_2=(4,5) with y2=−1y_2=-1. (a) Find α1,α2\alpha_1,\alpha_2. (b) Find w\mathbf w and bb. (c) Give the boundary, both edges and the width. (d) Check that the primal and dual values agree.

What this tests. The whole machinery on the smallest problem there is: balance, the edge conditions, w=∑αiyixi\mathbf w=\sum\alpha_iy_i\mathbf x_i — then two independent checks. Plan. With two points both must be support vectors. Balance makes the prices equal; the two edge conditions give two equations in α\alpha and bb.

Show the full solution
Step 1 — both points must push. If α1=0\alpha_1=0, balance α1−α2=0\alpha_1-\alpha_2=0 forces α2=0\alpha_2=0, then w=0\mathbf w=\mathbf 0, which cannot give any point a score of 1. So both prices are positive and both points sit on an edge. Balance: α1=α2=α\alpha_1=\alpha_2=\alpha.
Step 2 — the dot products. x1⋅x1=2\mathbf x_1\cdot\mathbf x_1=2, x1⋅x2=4+5=9\mathbf x_1\cdot\mathbf x_2=4+5=9, x2⋅x2=16+25=41\mathbf x_2\cdot\mathbf x_2=16+25=41.
Step 3 — the two edge conditions. With f(x)=α(x1⋅x)−α(x2⋅x)+bf(\mathbf x)=\alpha(\mathbf x_1\cdot\mathbf x)-\alpha(\mathbf x_2\cdot\mathbf x)+b: (+1)[2α−9α+b]=1⇒ −7α+b=1,(−1)[9α−41α+b]=1⇒ 32α−b=1.\begin{aligned}&(+1)\big[2\alpha-9\alpha+b\big]=1\\ &\qquad\Rightarrow\ -7\alpha+b=1,\\ &(-1)\big[9\alpha-41\alpha+b\big]=1\\ &\qquad\Rightarrow\ 32\alpha-b=1.\end{aligned}
Step 4 — solve (a). Add the two equations; bb cancels: 25α=225\alpha=2, so α=225=0.08\alpha=\tfrac{2}{25}=0.08. Check with the two-point shortcut: 2/∥x1−x2∥2=2/(9+16)=0.082/\|\mathbf x_1-\mathbf x_2\|^2=2/(9+16)=0.08 ✓.
Step 5 — w and b (b). b=1+7(0.08)=1.56b=1+7(0.08)=1.56, and w=0.08[(1,1)−(4,5)]=0.08(−3,−4)=(−0.24, −0.32).\begin{aligned}\mathbf w&=0.08\big[(1,1)-(4,5)\big]\\ &=0.08(-3,-4)\\ &=(-0.24,\,-0.32).\end{aligned} w\mathbf w points along x1−x2\mathbf x_1-\mathbf x_2: the boundary is at right angles to the segment joining the two points.
Step 6 — the lines and the width (c). Multiply −0.24x1−0.32x2+1.56=0-0.24x_1-0.32x_2+1.56=0 by −25-25: the boundary is 6x1+8x2=396x_1+8x_2=39, and the edges f=±1f=\pm1 are 6x1+8x2=39∓256x_1+8x_2=39\mp25, i.e. 1414 and 6464. Check: (1,1)(1,1) gives 1414 ✓, (4,5)(4,5) gives 6464 ✓. ∥w∥=0.08⋅5=0.4\|\mathbf w\|=0.08\cdot5=0.4, so the width is 2/0.4=52/0.4=5 — the distance between the two points, 9+16=5\sqrt{9+16}=5 ✓.
Step 7 — strong duality (d). Primal: 12∥w∥2=12(0.16)=0.08\tfrac12\|\mathbf w\|^2=\tfrac12(0.16)=0.08. Dual: ∑αi−12∥w∥2=0.16−0.08=0.08\sum\alpha_i-\tfrac12\|\mathbf w\|^2=0.16-0.08=0.08. Equal ✓.

answers at a glance: α1=α2=0.08\alpha_1=\alpha_2=0.08; w=(−0.24,−0.32)\mathbf w=(-0.24,-0.32), b=1.56b=1.56; boundary 6x1+8x2=396x_1+8x_2=39, edges =14=14 and =64=64, width 55; primal == dual =0.08=0.08.

Remember

With exactly two points, α=2/∥x1−x2∥2\alpha=2/\|\mathbf x_1-\mathbf x_2\|^2, w\mathbf w is parallel to x1−x2\mathbf x_1-\mathbf x_2, and the width is the distance between them. Use it as a five-second check.

Problem 3easyhard margin · 1-D

One-dimensional data: x=1,2,4x=1,2,4 have y=+1y=+1; x=−1,−3x=-1,-3 have y=−1y=-1. (a) Which points are support vectors? (b) Find ww, bb and the width. (c) Find every αi\alpha_i. (d) Which points could be deleted without changing the answer?

What this tests. Guessing the active points from a sketch, then proving the guess; and complementary slackness in its most useful form. Plan. In 1-D the street is an interval, so the classes touch at the closest opposite pair. Solve their two edge equations, then check every other point.

Show the full solution
Step 1 — the collision. The classes come closest at x=1x=1 and x=−1x=-1. These are the candidates.
Step 2 — two edge equations. (+1)(w+b)=1(+1)(w+b)=1 and (−1)(−w+b)=1(-1)(-w+b)=1, i.e. w+b=1w+b=1 and w−b=1w-b=1. Adding: w=1w=1; subtracting: b=0b=0. So f(x)=xf(x)=x, boundary x=0x=0, width 2/∣w∣=22/|w|=2: the street runs from −1-1 to 11.
Step 3 — check every point.
xxyyscore yf(x)yf(x)α\alpha
1+1112\tfrac12
2+120
4+140
−1−1112\tfrac12
−3−130
All scores are at least 1, so the street is legal; exactly two are tight.
Step 4 — the prices (c). Balance makes the two live prices equal, and the two-point shortcut gives α=2/(1−(−1))2=12\alpha=2/(1-(-1))^2=\tfrac12. Check: w=12(1)(1)+12(−1)(−1)=1w=\tfrac12(1)(1)+\tfrac12(-1)(-1)=1 ✓. The others score more than 1, so their prices are 0.
Step 5 — what can go (d). x=2x=2, 44 and −3-3 — any of them, or all three at once. Delete x=1x=1 or x=−1x=-1 and the street widens immediately.

answers at a glance: support vectors x=1x=1 and x=−1x=-1; w=1w=1, b=0b=0, boundary x=0x=0, width 22; α=12\alpha=\tfrac12 for x=±1x=\pm1, 00 for the rest; x=2,4,−3x=2,4,-3 are deletable.

Problem 4easykernel

x=(2,1)\mathbf x=(2,1), z=(1,3)\mathbf z=(1,3), K(x,z)=(x⋅z+1)2K(\mathbf x,\mathbf z)=(\mathbf x\cdot\mathbf z+1)^2. (a) Evaluate the kernel. (b) Write the six-feature map φ\varphi, compute φ(x)\varphi(\mathbf x) and φ(z)\varphi(\mathbf z), and confirm φ(x)⋅φ(z)\varphi(\mathbf x)\cdot\varphi(\mathbf z) matches. (c) Count the arithmetic each route costs.

What this tests. That K=φ⋅φK=\varphi\cdot\varphi is an identity, not an analogy — and that you can write φ\varphi when the degree is small. Plan. One line for the kernel; then the map from §14 at both points, multiplied coordinate by coordinate.

Show the full solution
Step 1 — the kernel route (a). x⋅z=2(1)+1(3)=5\mathbf x\cdot\mathbf z=2(1)+1(3)=5, so K=(5+1)2=36K=(5+1)^2=36.
Step 2 — the map (b). φ(v)=(v12, v22, 2v1v2, 2v1, 2v2, 1)\varphi(\mathbf v)=(v_1^2,\,v_2^2,\,\sqrt2v_1v_2,\,\sqrt2v_1,\,\sqrt2v_2,\,1).
coordinateφ(x)\varphi(\mathbf x)φ(z)\varphi(\mathbf z)product
v12v_1^2414
v22v_2^2199
2v1v2\sqrt2v_1v_2222\sqrt2323\sqrt212
2v1\sqrt2v_1222\sqrt22\sqrt24
2v2\sqrt2v_22\sqrt2323\sqrt26
11111
Total 4+9+12+4+6+1=364+9+12+4+6+1=36 ✓. Every 2\sqrt2 meets another 2\sqrt2 and becomes a plain 2.
Step 3 — the cost (c). Kernel route: two multiplications and one addition for the dot product, one addition for the +1+1, one squaring — five operations. Explicit route: build two six-long vectors (with square roots), then six multiplications and five additions. At degree 2 in 2-D it is close; at degree 5 in 100-D the explicit vectors have about 96.6 million coordinates, and the kernel is still a handful of operations.

answers at a glance: K=36K=36; φ(v)=(v12,v22,2v1v2,2v1,2v2,1)\varphi(\mathbf v)=(v_1^2,v_2^2,\sqrt2v_1v_2,\sqrt2v_1,\sqrt2v_2,1), φ(x)⋅φ(z)=4+9+12+4+6+1=36\varphi(\mathbf x)\cdot\varphi(\mathbf z)=4+9+12+4+6+1=36 ✓; five operations against a six-dimensional construction.

Problem 5easykernel theory

Input dimension n=3n=3, polynomial degree d=4d=4. (a) How many features does (x⋅z+1)4(\mathbf x\cdot\mathbf z+1)^4 secretly use? (b) And (x⋅z)4(\mathbf x\cdot\mathbf z)^4? (c) With 500 training points, how many kernel evaluations does the kernel table need, and how does that compare with building the lift?

What this tests. The two counting formulas — and that they describe two different models, not two ways of counting one. Plan. With the +1+1: every degree up to dd, (n+dd)\binom{n+d}{d}. Without: degree exactly dd, (n+d−1d)\binom{n+d-1}{d}. Then count the entries of a symmetric 500×500500\times500 table.

Show the full solution
Step 1 — non-homogeneous (a). (3+44)=(74)=7⋅6⋅53⋅2⋅1=35\binom{3+4}{4}=\binom74=\dfrac{7\cdot6\cdot5}{3\cdot2\cdot1}=35. Check degree by degree in 3 variables: 1+3+6+10+15=351+3+6+10+15=35 ✓.
Step 2 — homogeneous (b). (3+4−14)=(64)=6⋅52⋅1=15\binom{3+4-1}{4}=\binom64=\dfrac{6\cdot5}{2\cdot1}=15. This is a genuinely different model: it has only the 15 quartic products — no constant, no linear, no quadratic, no cubic terms.
Step 3 — the table (c). A symmetric 500×500500\times500 table needs 500⋅5012=125 250\dfrac{500\cdot501}{2}=125\,250 distinct kernel values, each a 3-term dot product, one addition and one fourth power — about six operations.
Step 4 — against the lift. The explicit lift stores 500×35=17 500500\times35=17\,500 numbers and takes 35-term dot products. At n=3,d=4n=3,d=4 the trick barely pays. At n=100,d=5n=100,d=5 the lift has (1055)=96 560 646\binom{105}{5}=96\,560\,646 coordinates per point, and the kernel cost does not move. Past some size the kernel is not a speed-up; it is the only thing that runs at all.

answers at a glance: (a) (74)=35\binom74=35. (b) (64)=15\binom64=15. (c) 125 250125\,250 evaluations at about six operations each, against 17 50017\,500 stored coordinates and 35-term dot products for the lift — a gap that explodes with nn and dd.

Problem 6mediumhard margin

A trained hard-margin SVM reports: (2,2)(2,2), y=+1y=+1, α=0.25\alpha=0.25; (4,1)(4,1), y=+1y=+1, α=0\alpha=0; (0,0)(0,0), y=−1y=-1, α=0.25\alpha=0.25. (a) Recover w\mathbf w and bb. (b) Give the boundary and the width. (c) Verify that (4,1)(4,1) deserves α=0\alpha=0. (d) Classify (1,0)(1,0).

What this tests. Using w=∑αiyixi\mathbf w=\sum\alpha_iy_i\mathbf x_i the way you do in practice, recovering bb (which the dual never hands you), and knowing that α=0\alpha=0 is a claim you can check. Plan. w\mathbf w straight from the sum; balance as a free error check; bb from one support vector, confirmed by the other.

Show the full solution
Step 1 — w (a). w=0.25(+1)(2,2)+0⋅(4,1)+0.25(−1)(0,0)=(0.5, 0.5).\begin{aligned}\mathbf w&=0.25(+1)(2,2)+0\cdot(4,1)\\ &\quad+0.25(-1)(0,0)\\ &=(0.5,\ 0.5).\end{aligned} Balance: 0.25+0−0.25=00.25+0-0.25=0 ✓.
Step 2 — b. From (0,0)(0,0): b=ys−w⋅xs=−1−0=−1b=y_s-\mathbf w\cdot\mathbf x_s=-1-0=-1. Confirm from (2,2)(2,2): b=1−(0.5,0.5)⋅(2,2)=1−2=−1b=1-(0.5,0.5)\cdot(2,2)=1-2=-1 ✓. Two support vectors must agree; if they do not, the prices are wrong.
Step 3 — boundary and width (b). 0.5x1+0.5x2−1=0  ⟺  x1+x2=20.5x_1+0.5x_2-1=0\iff x_1+x_2=2. ∥w∥=1/2\|\mathbf w\|=1/\sqrt2, so the width is 22≈2.832\sqrt2\approx2.83.
Step 4 — does (4, 1) deserve α = 0? (c) f(4,1)=2+0.5−1=1.5f(4,1)=2+0.5-1=1.5, score 1.5>11.5>1: strictly outside the street, so complementary slackness forces α=0\alpha=0 ✓.
Step 5 — classify (1, 0) (d). f(1,0)=0.5−1=−0.5<0f(1,0)=0.5-1=-0.5<0: class −1-1. But ∣f∣<1|f|<1, so the point is inside the street — a correct but unsure call. Its distance to the boundary is 0.52≈0.710.5\sqrt2\approx0.71, less than the half-width 2≈1.41\sqrt2\approx1.41.

answers at a glance: w=(0.5,0.5)\mathbf w=(0.5,0.5), b=−1b=-1; boundary x1+x2=2x_1+x_2=2, width 222\sqrt2; (4,1)(4,1) scores 1.5>11.5>1, so α=0\alpha=0 is right; (1,0)→−1(1,0)\to-1 with f=−0.5f=-0.5, inside the street.

Problem 7mediumhard margin · KKT

A hard-margin SVM, min⁡12w2\min\tfrac12w^2 s.t. yi(wxi+b)≥1y_i(wx_i+b)\ge1, is trained on x1=4,y1=+1x_1=4,y_1=+1; x2=7,y2=+1x_2=7,y_2=+1; x3=1,y3=−1x_3=1,y_3=-1; x4=−3,y4=−1x_4=-3,y_4=-1. (a) Write the KKT conditions, find the active points and solve for w,bw,b and all αi\alpha_i. (b) Give the width and check strong duality. (c) A fifth point x5=3x_5=3, y5=+1y_5=+1, is added. Is the old answer still optimal? If not, find the new one.

What this tests. Writing all four KKT conditions, using a sketch to spot the active points, and reasoning about a new point without starting over. Plan. Draw the number line: the closest opposite pair is 11 and 44. For (c), test the new point against the old ff.

Show the full solution
Step 1 — KKT. With L=12w2−∑iαi[yi(wxi+b)−1]L=\tfrac12w^2-\sum_i\alpha_i[y_i(wx_i+b)-1]: stationarity w=∑αiyixiw=\sum\alpha_iy_ix_i and ∑αiyi=0\sum\alpha_iy_i=0; primal feasibility yi(wxi+b)≥1y_i(wx_i+b)\ge1; dual feasibility αi≥0\alpha_i\ge0; complementary slackness αi[yi(wxi+b)−1]=0\alpha_i[y_i(wx_i+b)-1]=0.
Step 2 — the active pair. (+1)(4w+b)=1(+1)(4w+b)=1 and (−1)(w+b)=1(-1)(w+b)=1. Subtract: 3w=23w=2, so w=23w=\tfrac23 and b=−1−23=−53b=-1-\tfrac23=-\tfrac53. Boundary: f=0f=0 at x=2.5x=2.5.
Step 3 — check the rest. f(7)=143−53=3≥1f(7)=\tfrac{14}3-\tfrac53=3\ge1 ✓; f(−3)=−2−53=−113f(-3)=-2-\tfrac53=-\tfrac{11}3, score 113≥1\tfrac{11}3\ge1 ✓. So α2=α4=0\alpha_2=\alpha_4=0.
Step 4 — the prices. Balance: α1=α3\alpha_1=\alpha_3. Stationarity: w=4α1−1⋅α3=3α1=23w=4\alpha_1-1\cdot\alpha_3=3\alpha_1=\tfrac23, so α1=α3=29\alpha_1=\alpha_3=\tfrac29. Shortcut check: 2/(4−1)2=292/(4-1)^2=\tfrac29 ✓. All four KKT items hold, so (w,b)=(23,−53)(w,b)=(\tfrac23,-\tfrac53) is optimal, with α=(29,0,29,0)\boldsymbol\alpha=(\tfrac29,0,\tfrac29,0).
Step 5 — width and duality (b). Width 2/∣w∣=32/|w|=3: the street runs from 1 to 4. Primal 12w2=29\tfrac12w^2=\tfrac29. Dual: ∑αi−12w2=49−29=29\sum\alpha_i-\tfrac12w^2=\tfrac49-\tfrac29=\tfrac29. Equal ✓.
Step 6 — the new point (c). f(3)=2−53=13f(3)=2-\tfrac53=\tfrac13, score 13<1\tfrac13<1: the new point sits inside the old street, so the old answer is no longer legal. The new closest pair is 11 and 33: 3w+b=13w+b=1, w+b=−1w+b=-1, so w=1w=1, b=−2b=-2. Check: f(4)=2f(4)=2, f(7)=5f(7)=5, f(−3)=−5f(-3)=-5 — all fine. Prices: α3=α5\alpha_3=\alpha_5 and w=3α5−α3=2α5=1w=3\alpha_5-\alpha_3=2\alpha_5=1, so α3=α5=12\alpha_3=\alpha_5=\tfrac12, the rest 0. The point x=4x=4 has been released: it now scores 2.

answers at a glance: (a) w=23w=\tfrac23, b=−53b=-\tfrac53, α=(29,0,29,0)\boldsymbol\alpha=(\tfrac29,0,\tfrac29,0). (b) width 3; primal == dual =29=\tfrac29. (c) f(3)=13<1f(3)=\tfrac13<1, so the answer changes: w=1w=1, b=−2b=-2, α3=α5=12\alpha_3=\alpha_5=\tfrac12, others 0; width 2.

Problem 8mediumhard margin · KKT

A hard-margin SVM is trained on x1=(0,3)\mathbf x_1=(0,3), y=+1y=+1; x2=(−2,2)\mathbf x_2=(-2,2), y=+1y=+1; x3=(2,1)\mathbf x_3=(2,1), y=−1y=-1; x4=(3,0)\mathbf x_4=(3,0), y=−1y=-1. The answer is claimed to be w=(−12,12)\mathbf w=(-\tfrac12,\tfrac12), b=−12b=-\tfrac12. (a) Use KKT to find all αi\alpha_i and so verify the claim. (b) Give the width, the support vectors, the boundary and both edges. Does removing x2\mathbf x_2 change the answer? (c) Classify (1,1)(1,1). Is it inside the street?

What this tests. Verification rather than discovery: score every point, zero the prices of the ones with room, solve for the rest, then tick every KKT item. Plan. f(x)=−12x1+12x2−12f(\mathbf x)=-\tfrac12x_1+\tfrac12x_2-\tfrac12; score all four; solve the two-component equation w=∑αiyixi\mathbf w=\sum\alpha_iy_i\mathbf x_i.

Show the full solution
Step 1 — score everyone.
pointyyffscorestatus
(0,3)(0,3)+10+32−12=10+\tfrac32-\tfrac12=11on the + edge
(−2,2)(-2,2)+11+1−12=321+1-\tfrac12=\tfrac3232\tfrac32outside ⇒ α2=0\alpha_2=0
(2,1)(2,1)−1−1+12−12=−1-1+\tfrac12-\tfrac12=-11on the − edge
(3,0)(3,0)−1−32−12=−2-\tfrac32-\tfrac12=-22outside ⇒ α4=0\alpha_4=0
Step 2 — stationarity, two components. w=α1(0,3)−α3(2,1)\mathbf w=\alpha_1(0,3)-\alpha_3(2,1): first: −2α3=−12⇒ α3=14,second: 3α1−α3=12⇒ α1=14.\begin{aligned}\text{first: }&-2\alpha_3=-\tfrac12\\ &\Rightarrow\ \alpha_3=\tfrac14,\\ \text{second: }&3\alpha_1-\alpha_3=\tfrac12\\ &\Rightarrow\ \alpha_1=\tfrac14.\end{aligned}
Step 3 — tick the list. Balance 14+0−14−0=0\tfrac14+0-\tfrac14-0=0 ✓; all α≥0\alpha\ge0 ✓; all scores ≥1\ge1 ✓; the two priced points are exactly the tight ones ✓. The problem is convex, so the claim is the answer: α=(14,0,14,0)\boldsymbol\alpha=(\tfrac14,0,\tfrac14,0). Shortcut check: 2/∥(0,3)−(2,1)∥2=2/8=142/\|(0,3)-(2,1)\|^2=2/8=\tfrac14 ✓.
Step 4 — geometry (b). ∥w∥=12\|\mathbf w\|=\tfrac1{\sqrt2}, width 22≈2.832\sqrt2\approx2.83 — the distance between the two support vectors (0,3)(0,3) and (2,1)(2,1). Multiply f=0f=0 by −2-2: boundary x2=x1+1x_2=x_1+1; edge f=+1f=+1: x2=x1+3x_2=x_1+3 (through (0,3)(0,3)); edge f=−1f=-1: x2=x1−1x_2=x_1-1 (through (2,1)(2,1)). Removing x2\mathbf x_2 changes nothing: its price is 0 and its rule has room.
Step 5 — classify (1, 1) (c). f(1,1)=−12+12−12=−12f(1,1)=-\tfrac12+\tfrac12-\tfrac12=-\tfrac12: class −1-1, and ∣f∣<1|f|<1, so it is inside the street — 122≈0.71\tfrac12\sqrt2\approx0.71 from the boundary, less than the half-width 2≈1.41\sqrt2\approx1.41.

answers at a glance: (a) α=(14,0,14,0)\boldsymbol\alpha=(\tfrac14,0,\tfrac14,0); every KKT item holds, so the claim is optimal. (b) width 222\sqrt2; support vectors (0,3)(0,3), (2,1)(2,1); boundary x2=x1+1x_2=x_1+1, edges x2=x1+3x_2=x_1+3 and x2=x1−1x_2=x_1-1; removing x2\mathbf x_2 changes nothing. (c) (1,1)→−1(1,1)\to-1, f=−12f=-\tfrac12, inside the street.

Problem 9mediumhard margin · a trap

Three points: (1,1)→+1(1,1)\to+1, (2,0)→+1(2,0)\to+1, (0,0)→−1(0,0)\to-1. (a) Find w\mathbf w, bb and the width. (b) Find every αi\alpha_i. (c) Which points are support vectors? Answer carefully.

What this tests. The one definitional trap in the subject: a point can sit exactly on an edge and still have α=0\alpha=0. Plan. Try "all three on an edge": three linear equations for w1,w2,bw_1,w_2,b. Then get the prices from w=∑αiyixi\mathbf w=\sum\alpha_iy_i\mathbf x_i and see what is forced.

Show the full solution
Step 1 — all three tight. w1+w2+b=1w_1+w_2+b=1, 2w1+b=12w_1+b=1, −(0+b)=1-(0+b)=1.
Step 2 — solve back to front. b=−1b=-1; then 2w1−1=1⇒w1=12w_1-1=1\Rightarrow w_1=1; then 1+w2−1=1⇒w2=11+w_2-1=1\Rightarrow w_2=1. So w=(1,1)\mathbf w=(1,1), b=−1b=-1, boundary x1+x2=1x_1+x_2=1, width 2/2=22/\sqrt2=\sqrt2.
Step 3 — the prices (b). w=α1(1,1)+α2(2,0)−α3(0,0)=(α1+2α2, α1)=(1,1).\begin{aligned}\mathbf w&=\alpha_1(1,1)+\alpha_2(2,0)\\ &\quad-\alpha_3(0,0)\\ &=(\alpha_1+2\alpha_2,\ \alpha_1)=(1,1).\end{aligned} Second coordinate: α1=1\alpha_1=1. First: 1+2α2=1⇒α2=01+2\alpha_2=1\Rightarrow\alpha_2=0 — forced. Balance: α1+α2−α3=0⇒α3=1\alpha_1+\alpha_2-\alpha_3=0\Rightarrow\alpha_3=1.
Step 4 — check. All α≥0\alpha\ge0 ✓; all scores equal 1 ✓; complementary slackness holds since every rule is tight ✓; duality: 12∥w∥2=1\tfrac12\|\mathbf w\|^2=1 and ∑αi−12∥w∥2=2−1=1\sum\alpha_i-\tfrac12\|\mathbf w\|^2=2-1=1 ✓.
Step 5 — the trap (c). All three points lie exactly on an edge, but (2,0)(2,0) has α=0\alpha=0: it touches the street without pushing on it. Delete it and nothing changes; delete either of the others and the boundary moves. So the support vectors are (1,1)(1,1) and (0,0)(0,0) only — the points with α>0\alpha>0.

answers at a glance: w=(1,1)\mathbf w=(1,1), b=−1b=-1, boundary x1+x2=1x_1+x_2=1, width 2\sqrt2; α=(1,0,1)\boldsymbol\alpha=(1,0,1); support vectors (1,1)(1,1) and (0,0)(0,0) — (2,0)(2,0) is on an edge with α=0\alpha=0.

Remember

Say it this way: support vectors are the points with αi>0\alpha_i>0; they always lie on an edge, but not every point on an edge is one.

Problem 10mediumsoft margin

A soft-margin SVM trained with C=1C=1 returns these prices: A: α=0\alpha=0; B: 0.40.4; C: 1.01.0; D: 1.01.0; E: 0.150.15. (a) Classify each as a non-support vector, a free support vector or a bounded one. (b) What do you know about each point's slack and score? (c) Which may be used to compute bb? (d) Which are definitely misclassified?

What this tests. The three price regimes of §10, read from the prices to the geometry. Plan. Compare each α\alpha with 0 and with C=1C=1; that alone fixes the regime, and the regime fixes ξ\xi and the score.

Show the full solution
Step 1 — the three regimes (with z=y f(x)z=y\,f(\mathbf x)): α=0⇒z≥1\alpha=0\Rightarrow z\ge1; 0<α<C⇒z=10<\alpha<C\Rightarrow z=1; α=C⇒z≤1\alpha=C\Rightarrow z\le1.
Step 2 — apply them (a), (b), (c).
pointα\alphatypeξ\xiscore zzfor bb?
A0not a support vector0≥1\ge1no
B0.4free0=1=1yes
C1.0bounded≥0\ge0≤1\le1no
D1.0bounded≥0\ge0≤1\le1no
E0.15free0=1=1yes
Only B and E are known to score exactly 1, so only they give b=ys−∑iαiyiK(xi,xs)b=y_s-\sum_i\alpha_iy_iK(\mathbf x_i,\mathbf x_s). In practice, average over all free support vectors.
Step 3 — misclassified? (d) None, necessarily. α=C\alpha=C says only z≤1z\le1: at best on the edge, at worst deep on the wrong side. Misclassified needs z<0z<0 (ξ>1\xi>1), which the price alone cannot tell you.

answers at a glance: A non-support; B and E free (ξ=0\xi=0, z=1z=1); C and D bounded (ξ≥0\xi\ge0, z≤1z\le1); only B and E give bb; none is definitely misclassified.

Problem 11hardsoft margin

A colleague proposes w=(1,1)\mathbf w=(1,1), b=−3b=-3 for: (1,1)→+1(1,1)\to+1, (3,3)→+1(3,3)\to+1, (2,0)→−1(2,0)\to-1, (0,2)→−1(0,2)\to-1, (2,2)→−1(2,2)\to-1. (a) For each point compute the score zz and slack ξ\xi, and say where it is. (b) Give the total objective 12∥w∥2+C∑ξi\tfrac12\|\mathbf w\|^2+C\sum\xi_i at C=1C=1 and C=10C=10. (c) Could this (w,b)(\mathbf w,b) be the soft-margin answer for any C>0C>0?

What this tests. The hinge formula and the two halves of the objective; then running complementary slackness backwards to audit someone else's answer. Plan. Tabulate score and slack; add the halves; in (c), give each point its price regime and test the balance condition.

Show the full solution
Step 1 — scores and slacks (a). f=x1+x2−3f=x_1+x_2-3, ξ=max⁡(0,1−z)\xi=\max(0,1-z):
pointyyffzzξ\xiwhere
(1,1)(1,1)+1−1−12misclassified
(3,3)(3,3)+1+330outside the street
(2,0)(2,0)−1−110on an edge
(0,2)(0,2)−1−110on an edge
(2,2)(2,2)−1+1−12misclassified
Step 2 — the objective (b). 12∥w∥2=12(1+1)=1\tfrac12\|\mathbf w\|^2=\tfrac12(1+1)=1 and ∑ξi=4\sum\xi_i=4. At C=1C=1: 1+4=51+4=5. At C=10C=10: 1+40=411+40=41.
Step 3 — regimes (c). (3,3)(3,3): z>1⇒α=0z>1\Rightarrow\alpha=0. (1,1)(1,1) and (2,2)(2,2): ξ>0⇒α=C\xi>0\Rightarrow\alpha=C. (2,0)(2,0) and (0,2)(0,2): z=1⇒α=p,q∈[0,C]z=1\Rightarrow\alpha=p,q\in[0,C].
Step 4 — balance fails. ∑iαiyi=C+0−p−q−C=−(p+q)=0⇒ p=q=0.\begin{aligned}\sum_i\alpha_iy_i&=C+0-p-q-C\\ &=-(p+q)=0\\ &\Rightarrow\ p=q=0.\end{aligned} Then w=∑αiyixi=C(1,1)−C(2,2)=(−C,−C)\mathbf w=\sum\alpha_iy_i\mathbf x_i=C(1,1)-C(2,2)=(-C,-C). To equal (1,1)(1,1) we would need C=−1C=-1, impossible. So the proposal is not optimal for any C>0C>0.
Step 5 — for comparison. At C=1C=1 the true answer is w=(12,12)\mathbf w=(\tfrac12,\tfrac12), b=−2b=-2: slacks (2,0,0,0,1)(2,0,0,0,1), objective 12⋅12+3=3.25\tfrac12\cdot\tfrac12+3=3.25 — better than 5, as it must be. (The points (1,1)(1,1) and (2,2)(2,2) sit on each other's wrong side, so no hard-margin answer exists at all.)

answers at a glance: ξ=(2,0,0,0,2)\boldsymbol\xi=(2,0,0,0,2) — two misclassified, two on an edge, one outside; objective 5 at C=1C=1, 41 at C=10C=10; (c) no — balance forces the edge prices to 0 and then w=(−C,−C)\mathbf w=(-C,-C). The true C=1C=1 answer is w=(12,12)\mathbf w=(\tfrac12,\tfrac12), b=−2b=-2, objective 3.25.

Problem 12mediumkernel SVM

Four points form an XNOR pattern (same signs ⇒ class +1+1): x1=(1,2)\mathbf x_1=(1,2), y1=+1y_1=+1; x2=(−1,−2)\mathbf x_2=(-1,-2), y2=+1y_2=+1; x3=(1,−2)\mathbf x_3=(1,-2), y3=−1y_3=-1; x4=(−1,2)\mathbf x_4=(-1,2), y4=−1y_4=-1. Use K(x,z)=(x⋅z)2K(\mathbf x,\mathbf z)=(\mathbf x\cdot\mathbf z)^2. (a) Compute the 4×44\times4 kernel table. (b) Solve the hard-margin dual and name the support vectors. (c) Find bb, then classify (2,1)(2,1) and (−1,3)(-1,3).

What this tests. A kernel table without slips, symmetry to shrink the dual to one unknown, the balance condition, bb from a support vector, and classification by kernel values. Plan. Dots first, square second. Rows 1–2 and 3–4 are identical ⇒ equal prices. One-variable dual, then bb from x1\mathbf x_1, then the two classifications.

Show the full solution
Step 1 — dot products. Row 1: 5,−5,−3,35,-5,-3,3. By the same arithmetic the full table of xi⋅xj\mathbf x_i\cdot\mathbf x_j is [5−5−33−553−3−335−53−3−55].\begin{bmatrix}5&-5&-3&3\\-5&5&3&-3\\-3&3&5&-5\\3&-3&-5&5\end{bmatrix}.
Step 2 — square every entry (a). K=[252599252599992525992525].K=\begin{bmatrix}25&25&9&9\\25&25&9&9\\9&9&25&25\\9&9&25&25\end{bmatrix}. The diagonal is ∥xi∥4=25\|\mathbf x_i\|^4=25 and the table is symmetric.
Step 3 — symmetry and balance. Rows 1–2 are identical with the same label, as are rows 3–4, so take α1=α2=a\alpha_1=\alpha_2=a, α3=α4=c\alpha_3=\alpha_4=c. Balance: 2a−2c=0⇒c=a2a-2c=0\Rightarrow c=a.
Step 4 — the dual with numbers. ∑αi=4a\sum\alpha_i=4a. The double sum, block by block: same-class blocks 2×42\times4 entries of 25 give +200a2+200a^2; cross blocks, 8 entries of 9 with yiyj=−1y_iy_j=-1, give −72a2-72a^2. Total 128a2128a^2: D(a)=4a−12⋅128a2=4a−64a2,D′(a)=4−128a=0⇒ a=132.\begin{aligned}D(a)&=4a-\tfrac12\cdot128a^2\\ &=4a-64a^2,\\ D'(a)&=4-128a=0\\ \Rightarrow\ a&=\tfrac1{32}.\end{aligned} All four prices are 132>0\tfrac1{32}>0: every point is a support vector (b).
Step 5 — b (c). From x1\mathbf x_1 (column 1 of KK): 132(25+25−9−9)+b=1⇒1+b=1⇒b=0\tfrac1{32}(25+25-9-9)+b=1\Rightarrow1+b=1\Rightarrow b=0. Confirm with x3\mathbf x_3: −[132(9+9−25−25)+0]=−[−1]=1-\big[\tfrac1{32}(9+9-25-25)+0\big]=-[-1]=1 ✓.
Step 6 — classify. (2,1)(2,1): dots 4,−4,0,04,-4,0,0, squares 16,16,0,016,16,0,0, f=3232=1f=\tfrac{32}{32}=1: class +1+1, exactly on the edge. (−1,3)(-1,3): dots 5,−5,−7,75,-5,-7,7, squares 25,25,49,4925,25,49,49, f=50−9832=−1.5f=\tfrac{50-98}{32}=-1.5: class −1-1.
Step 7 — the curve (optional). f(x)=132[2(x1+2x2)2−2(x1−2x2)2]=x1x22f(\mathbf x)=\tfrac1{32}\big[2(x_1+2x_2)^2-2(x_1-2x_2)^2\big]=\tfrac{x_1x_2}{2}: the boundary is the pair of axes, and f=±1f=\pm1 are x1x2=±2x_1x_2=\pm2, through all four points. Upstairs, w=(0,24,0)\mathbf w=(0,\tfrac{\sqrt2}4,0) and the width is 424\sqrt2.

answers at a glance: KK as above; αi=132\alpha_i=\tfrac1{32} for all four (all support vectors); b=0b=0; (2,1)→+1(2,1)\to+1 with f=1f=1, (−1,3)→−1(-1,3)\to-1 with f=−1.5f=-1.5; f=x1x2/2f=x_1x_2/2.

Remember

Here x1\mathbf x_1 and x2\mathbf x_2 look identical to the kernel, so only α1+α2\alpha_1+\alpha_2 is really fixed; the equal split is the natural (smallest) choice, and w\mathbf w, bb, ff are the same for any split. The slip to avoid: squaring the coordinates before finishing the dot product.

Problem 13hardkernel SVM

An inner point and two outer ones: x1=(0,0)\mathbf x_1=(0,0), y1=+1y_1=+1; x2=(2,0)\mathbf x_2=(2,0), y2=−1y_2=-1; x3=(0,−2)\mathbf x_3=(0,-2), y3=−1y_3=-1. Use K(x,z)=(x⋅z+1)2K(\mathbf x,\mathbf z)=(\mathbf x\cdot\mathbf z+1)^2. (a) Compute the 3×33\times3 kernel table. (b) Solve the dual for the αi\alpha_i and find bb. (c) Classify (1,1)(1,1) and (1,−1)(1,-1).

What this tests. The "add 1, then square" habit, a dual whose classes are not balanced (one point against two), and reading a curved boundary out of ff. Plan. Rows 2 and 3 are mirror images, so α2=α3=c\alpha_2=\alpha_3=c; balance then gives α1=2c\alpha_1=2c. One-variable dual; bb from x1\mathbf x_1, whose column is all ones.

Show the full solution
Step 1 — dots, add 1, square (a). x1\mathbf x_1 is the origin, so every dot product with it is 0; x2⋅x2=4\mathbf x_2\cdot\mathbf x_2=4, x2⋅x3=0\mathbf x_2\cdot\mathbf x_3=0, x3⋅x3=4\mathbf x_3\cdot\mathbf x_3=4. Then (0+1)2=1(0+1)^2=1 and (4+1)2=25(4+1)^2=25: K=[11112511125].K=\begin{bmatrix}1&1&1\\1&25&1\\1&1&25\end{bmatrix}. Without the +1+1, the origin's row would be all zeros and it would carry no information at all.
Step 2 — balance and symmetry. α1−α2−α3=0\alpha_1-\alpha_2-\alpha_3=0, and α2=α3=c\alpha_2=\alpha_3=c, so α1=2c\alpha_1=2c.
Step 3 — the dual. ∑αi=4c\sum\alpha_i=4c. The double sum, term by term: (1,1)(1,1): 4c24c^2; (2,2)(2,2), (3,3)(3,3): 25c225c^2 each; (1,2)(1,2)+(2,1)(2,1): 2(2c)(c)(−1)(1)=−4c22(2c)(c)(-1)(1)=-4c^2; (1,3)(1,3)+(3,1)(3,1): −4c2-4c^2; (2,3)(2,3)+(3,2)(3,2): 2c22c^2. Total 48c248c^2: D(c)=4c−24c2,D′(c)=4−48c=0⇒ c=112.\begin{aligned}D(c)&=4c-24c^2,\\ D'(c)&=4-48c=0\\ \Rightarrow\ c&=\tfrac1{12}.\end{aligned} So α1=16\alpha_1=\tfrac16, α2=α3=112\alpha_2=\alpha_3=\tfrac1{12} — all support vectors.
Step 4 — b (b). From x1\mathbf x_1: 16−112−112+b=1⇒b=1\tfrac16-\tfrac1{12}-\tfrac1{12}+b=1\Rightarrow b=1. Confirm with x2\mathbf x_2: −[16−2512−112+1]=−[−1]=1-\big[\tfrac16-\tfrac{25}{12}-\tfrac1{12}+1\big]=-[-1]=1 ✓.
Step 5 — classify (c). (1,1)(1,1): dots 0,2,−20,2,-2 → kernel 1,9,11,9,1: f=16−912−112+1=13f=\tfrac16-\tfrac9{12}-\tfrac1{12}+1=\tfrac13 → class +1+1. (1,−1)(1,-1): dots 0,2,20,2,2 → 1,9,91,9,9: f=16−912−912+1=−13f=\tfrac16-\tfrac9{12}-\tfrac9{12}+1=-\tfrac13 → class −1-1. Both are inside the street.
Step 6 — the circle (optional). f=76−112[(2x1+1)2+(2x2−1)2]f=\tfrac76-\tfrac1{12}\big[(2x_1+1)^2+(2x_2-1)^2\big], so f=0  ⟺  (x1+12)2+(x2−12)2=3.5f=0\iff(x_1+\tfrac12)^2+(x_2-\tfrac12)^2=3.5: a circle centred at (−12,12)(-\tfrac12,\tfrac12), radius 3.5≈1.87\sqrt{3.5}\approx1.87.

answers at a glance: K=[11112511125]K=\begin{bmatrix}1&1&1\\1&25&1\\1&1&25\end{bmatrix}; α=(16,112,112)\boldsymbol\alpha=(\tfrac16,\tfrac1{12},\tfrac1{12}), all support vectors; b=1b=1; (1,1)→+1(1,1)\to+1 (f=13f=\tfrac13), (1,−1)→−1(1,-1)\to-1 (f=−13f=-\tfrac13); boundary: the circle (x1+12)2+(x2−12)2=3.5(x_1+\tfrac12)^2+(x_2-\tfrac12)^2=3.5.

Problem 14hardkernel SVM · b ≠ 0

x1=(1,1)\mathbf x_1=(1,1), y1=+1y_1=+1; x2=(−1,−1)\mathbf x_2=(-1,-1), y2=+1y_2=+1; x3=(2,0)\mathbf x_3=(2,0), y3=−1y_3=-1; x4=(−2,0)\mathbf x_4=(-2,0), y4=−1y_4=-1. Use K=(x⋅z)2K=(\mathbf x\cdot\mathbf z)^2. (a) Compute the kernel table. (b) Solve the dual and name the support vectors. (c) Find bb and classify (0,2)(0,2) and (3,0)(3,0).

What this tests. The same skills as Problem 12 — but the bias is not zero, so anyone who "remembers b=0b=0" loses the classifications. It also shows a kernel SVM drawing a hyperbola. Plan. Dots, square; rows 1–2 and 3–4 identical ⇒ equal prices; balance; one-variable dual; bb from x1\mathbf x_1, confirmed from x3\mathbf x_3.

Show the full solution
Step 1 — dots (a). x1⋅x1=2\mathbf x_1\cdot\mathbf x_1=2, x1⋅x2=−2\mathbf x_1\cdot\mathbf x_2=-2, x1⋅x3=2\mathbf x_1\cdot\mathbf x_3=2, x1⋅x4=−2\mathbf x_1\cdot\mathbf x_4=-2; x3⋅x3=4\mathbf x_3\cdot\mathbf x_3=4, x3⋅x4=−4\mathbf x_3\cdot\mathbf x_4=-4. Square: K=[44444444441616441616].K=\begin{bmatrix}4&4&4&4\\4&4&4&4\\4&4&16&16\\4&4&16&16\end{bmatrix}.
Step 2 — symmetry, balance, dual (b). α1=α2=a\alpha_1=\alpha_2=a, α3=α4=c\alpha_3=\alpha_4=c, balance c=ac=a. Blocks: 4×44\times4 gives 16a216a^2, 4×164\times16 gives 64a264a^2, cross blocks 8×48\times4 with a minus: −32a2-32a^2. Total 48a248a^2: D(a)=4a−24a2 ⇒ a=112.D(a)=4a-24a^2\ \Rightarrow\ a=\tfrac1{12}. All four prices are 112\tfrac1{12}: all support vectors.
Step 3 — b (c). From x1\mathbf x_1: 112(4+4−4−4)+b=1⇒b=1\tfrac1{12}(4+4-4-4)+b=1\Rightarrow b=1. Confirm from x3\mathbf x_3: −[112(4+4−16−16)+1]=−[−2+1]=1-\big[\tfrac1{12}(4+4-16-16)+1\big]=-[-2+1]=1 ✓. Had you assumed b=0b=0, x1\mathbf x_1 would score 0 — on the boundary, which is absurd for a support vector.
Step 4 — classify. (0,2)(0,2): dots 2,−2,0,02,-2,0,0 → 4,4,0,04,4,0,0: f=812+1=53f=\tfrac8{12}+1=\tfrac53 → +1+1. (3,0)(3,0): dots 3,−3,6,−63,-3,6,-6 → 9,9,36,369,9,36,36: f=18−7212+1=−3.5f=\tfrac{18-72}{12}+1=-3.5 → −1-1.
Step 5 — the curve (optional). f=(x1+x2)2−4x126+1f=\dfrac{(x_1+x_2)^2-4x_1^2}{6}+1. The boundary is (x1+x2)2−4x12=−6(x_1+x_2)^2-4x_1^2=-6, i.e. (x2−x1)(x2+3x1)=−6(x_2-x_1)(x_2+3x_1)=-6 — a hyperbola. Upstairs the four points collapse to two, (1,2,1)(1,\sqrt2,1) and (4,0,0)(4,0,0); w=16(−3,2,1)\mathbf w=\tfrac16(-3,\sqrt2,1), ∥w∥=13\|\mathbf w\|=\tfrac1{\sqrt3}, width 232\sqrt3 — the distance between those two points.

answers at a glance: KK as above; αi=112\alpha_i=\tfrac1{12}, all support vectors; b=1b=1; (0,2)→+1(0,2)\to+1 (f=53f=\tfrac53), (3,0)→−1(3,0)\to-1 (f=−3.5f=-3.5); boundary a hyperbola (x2−x1)(x2+3x1)=−6(x_2-x_1)(x_2+3x_1)=-6.

Problem 15hardkernel SVM · given prices

x1=(0,0)\mathbf x_1=(0,0), y1=+1y_1=+1; x2=(1,1)\mathbf x_2=(1,1), y2=−1y_2=-1; x3=(1,−1)\mathbf x_3=(1,-1), y3=−1y_3=-1; x4=(3,0)\mathbf x_4=(3,0), y4=−1y_4=-1. A hard-margin SVM with K=(x⋅z+1)2K=(\mathbf x\cdot\mathbf z+1)^2 reports α1=12\alpha_1=\tfrac12, α2=14\alpha_2=\tfrac14, α3=14\alpha_3=\tfrac14, α4=0\alpha_4=0. (a) Compute the kernel table. (b) Check the balance, name the support vectors and explain α4=0\alpha_4=0. (c) Find bb from one support vector and confirm it from another. (d) Classify (1,0)(1,0) and (−2,1)(-2,1).

What this tests. The table again; what a zero price means; bb when the prices are handed to you; classifying with support vectors only. Plan. Dots, add 1, square. Balance in one line. bb from x1\mathbf x_1 (its column is all ones), confirm with x2\mathbf x_2, then check x4\mathbf x_4 really is outside the street.

Show the full solution
Step 1 — the table (a). Dots: everything with x1\mathbf x_1 is 0; x2⋅x2=2\mathbf x_2\cdot\mathbf x_2=2, x2⋅x3=0\mathbf x_2\cdot\mathbf x_3=0, x2⋅x4=3\mathbf x_2\cdot\mathbf x_4=3, x3⋅x3=2\mathbf x_3\cdot\mathbf x_3=2, x3⋅x4=3\mathbf x_3\cdot\mathbf x_4=3, x4⋅x4=9\mathbf x_4\cdot\mathbf x_4=9. Add 1 and square: K=[1111191161191611616100].K=\begin{bmatrix}1&1&1&1\\1&9&1&16\\1&1&9&16\\1&16&16&100\end{bmatrix}.
Step 2 — balance and support vectors (b). 12−14−14−0=0\tfrac12-\tfrac14-\tfrac14-0=0 ✓. Support vectors: x1,x2,x3\mathbf x_1,\mathbf x_2,\mathbf x_3. α4=0\alpha_4=0 means x4\mathbf x_4's rule has room: it lies strictly outside the street and plays no part in ff (checked in Step 4).
Step 3 — b (c). From x1\mathbf x_1: 12−14−14−0+b=1⇒b=1\tfrac12-\tfrac14-\tfrac14-0+b=1\Rightarrow b=1. Confirm with x2\mathbf x_2 (column 1,9,1,161,9,1,16): 12−94−14=−2\tfrac12-\tfrac94-\tfrac14=-2, and y2(−2+1)=(−1)(−1)=1y_2(-2+1)=(-1)(-1)=1 ✓.
Step 4 — is x₄ really outside? Column 4 is 1,16,16,1001,16,16,100: f(x4)=12−4−4+1=−6.5f(\mathbf x_4)=\tfrac12-4-4+1=-6.5, score 6.5>16.5>1 ✓. Consistent with α4=0\alpha_4=0. (If a given price ever fails this check, the numbers are not an answer — say so.)
Step 5 — classify (d). (1,0)(1,0): dots with the support vectors 0,1,10,1,1 → 1,4,41,4,4: f=12−1−1+1=−12f=\tfrac12-1-1+1=-\tfrac12 → −1-1. (−2,1)(-2,1): dots 0,−1,−30,-1,-3 → 1,0,41,0,4: f=12−0−1+1=12f=\tfrac12-0-1+1=\tfrac12 → +1+1.
Step 6 — the circle (optional). f=32−12[(x1+1)2+x22]f=\tfrac32-\tfrac12\big[(x_1+1)^2+x_2^2\big], so f=0  ⟺  (x1+1)2+x22=3f=0\iff(x_1+1)^2+x_2^2=3: a circle centred at (−1,0)(-1,0) with radius 3≈1.73\sqrt3\approx1.73. The origin gives f=1f=1, (1,±1)(1,\pm1) give f=−1f=-1, (3,0)(3,0) gives −6.5-6.5 ✓.

answers at a glance: KK as above; balance holds; support vectors x1,x2,x3\mathbf x_1,\mathbf x_2,\mathbf x_3, and x4\mathbf x_4 is outside the street (score 6.5); b=1b=1; (1,0)→−1(1,0)\to-1 (f=−12f=-\tfrac12), (−2,1)→+1(-2,1)\to+1 (f=12f=\tfrac12); boundary (x1+1)2+x22=3(x_1+1)^2+x_2^2=3.

Problem 16hardkernel SVM · 1-D

One-dimensional data: x1=1x_1=1, y1=+1y_1=+1; x2=−1x_2=-1, y2=+1y_2=+1; x3=3x_3=3, y3=−1y_3=-1; x4=−3x_4=-3, y4=−1y_4=-1. No single threshold separates them. Use K(x,z)=(xz+1)2K(x,z)=(xz+1)^2. (a) Compute the kernel table. (b) Solve the hard-margin dual. (c) Find bb, write f(x)f(x) in closed form, give the boundary on the line, and classify x=2x=2 and x=−2.5x=-2.5. (d) Write the feature map φ(x)\varphi(x) and use it to check K(1,3)K(1,3).

What this tests. Whether the recipe survives with scalars instead of vectors; mirror-image symmetry; turning ff into a formula; the meaning of φ\varphi. Plan. Products, add 1, square. Reflecting x→−xx\to-x swaps the points in pairs, so α1=α2\alpha_1=\alpha_2, α3=α4\alpha_3=\alpha_4; balance equates them.

Show the full solution
Step 1 — the table (a). Products xixjx_ix_j, add 1, square: (1+1)2=4(1+1)^2=4, (−1+1)2=0(-1+1)^2=0, (3+1)2=16(3+1)^2=16, (−3+1)2=4(-3+1)^2=4, (9+1)2=100(9+1)^2=100, (−9+1)2=64(-9+1)^2=64: K=[40164044161641006441664100].K=\begin{bmatrix}4&0&16&4\\0&4&4&16\\16&4&100&64\\4&16&64&100\end{bmatrix}. K12=0K_{12}=0: the points 11 and −1-1 are at right angles upstairs — kernel values may be zero.
Step 2 — symmetry and balance. α1=α2=a\alpha_1=\alpha_2=a, α3=α4=c\alpha_3=\alpha_4=c; balance 2a−2c=0⇒c=a2a-2c=0\Rightarrow c=a.
Step 3 — the dual (b). Same-class blocks: (4+0+0+4)a2=8a2(4+0+0+4)a^2=8a^2 and (100+64+64+100)a2=328a2(100+64+64+100)a^2=328a^2; cross blocks: −2(16+4+4+16)a2=−80a2-2(16+4+4+16)a^2=-80a^2. Total 256a2256a^2: D(a)=4a−128a2 ⇒ a=164.D(a)=4a-128a^2\ \Rightarrow\ a=\tfrac1{64}. All four prices 164\tfrac1{64}: all support vectors.
Step 4 — b and f (c). From x1=1x_1=1 (column 4,0,16,44,0,16,4): 164(4+0−16−4)+b=1⇒b=1+14=54\tfrac1{64}(4+0-16-4)+b=1\Rightarrow b=1+\tfrac14=\tfrac54. Confirm from x3x_3: −[164(16+4−100−64)+54]=−[−14464+8064]=1-\big[\tfrac1{64}(16+4-100-64)+\tfrac54\big]=-\big[-\tfrac{144}{64}+\tfrac{80}{64}\big]=1 ✓. Closed form, using (u+1)2+(1−u)2=2u2+2(u+1)^2+(1-u)^2=2u^2+2: f(x)=164[(2x2+2)−(18x2+2)]+54=5−x24.\begin{aligned}f(x)&=\tfrac1{64}\big[(2x^2+2)\\ &\qquad-(18x^2+2)\big]+\tfrac54\\ &=\frac{5-x^2}{4}.\end{aligned}
Step 5 — boundary and classification. f=0  ⟺  x=±5≈±2.24f=0\iff x=\pm\sqrt5\approx\pm2.24: two thresholds, "class +1+1 if ∣x∣<5|x|<\sqrt5". Checks: f(±1)=1f(\pm1)=1, f(±3)=−1f(\pm3)=-1 ✓. f(2)=5−44=14f(2)=\tfrac{5-4}4=\tfrac14 → +1+1 (inside the street); f(−2.5)=5−6.254=−0.3125f(-2.5)=\tfrac{5-6.25}4=-0.3125 → −1-1.
Step 6 — the feature map (d). (xz+1)2=x2z2+2xz+1=(x2)(z2)+(2x)(2z)+(1)(1)(xz+1)^2=x^2z^2+2xz+1=(x^2)(z^2)+(\sqrt2x)(\sqrt2z)+(1)(1), so φ(x)=(x2,2x,1)\varphi(x)=(x^2,\sqrt2x,1). Check: φ(1)=(1,2,1)\varphi(1)=(1,\sqrt2,1), φ(3)=(9,32,1)\varphi(3)=(9,3\sqrt2,1), dot product 9+6+1=16=(3+1)29+6+1=16=(3+1)^2 ✓. Upstairs w=(−14,0,0)\mathbf w=(-\tfrac14,0,0): the cut is x2=5x^2=5, with edges x2=1x^2=1 and x2=9x^2=9 — a flat street 8 wide in the lifted space, two thresholds on the line.

answers at a glance: KK as above; αi=164\alpha_i=\tfrac1{64} for all four; b=54b=\tfrac54; f(x)=(5−x2)/4f(x)=(5-x^2)/4, boundary x=±5x=\pm\sqrt5; x=2→+1x=2\to+1, x=−2.5→−1x=-2.5\to-1; φ(x)=(x2,2x,1)\varphi(x)=(x^2,\sqrt2x,1), K(1,3)=16K(1,3)=16.

Remember

As in Problem 12, symmetry picks the natural prices, but here other price sets give the very same machine too (for example α=(0,132,196,148)\boldsymbol\alpha=(0,\tfrac1{32},\tfrac1{96},\tfrac1{48})); bb and ff never change. And a kernel SVM in one dimension can still draw two thresholds: flat upstairs, curved down here.

Next up

Unit 14 · Thinking in Probabilities — upcoming

Every unit so far has been geometry — lines, projections, eigenvectors, margins. Everything that comes next runs on uncertainty. Unit 14 teaches the language: how likely, how surprised, how far apart two beliefs are — and it shows that the covariance ellipse of Unit 12 was a Gaussian all along, and that "minimise the squared error" was secretly "make the data most likely".

← Unit 12 · Principal Component Analysis · All units