Sixteen microphone clips sit on a plot, voices in blue and engines in pink, and you need the best line between them. You can hunt for the line itself, or hand out weights to the clips. Both hunts end at the same line, and almost every weight is zero.
Learn how a constrained problem hides a second problem behind it, why the two always meet for the SVM, and what the second one tells you about which data matters.
Drag the C slider, or drag any dot. Then press Run the dual solver and watch two numbers squeeze together until the gap between them is zero. Then we build, piece by piece, why they always meet.
You need a line written as wᵀx + b = 0 and what a derivative is. We build the margin, the Lagrangian and the dual from zero.
Based on Lecture 10 of Stanford EE269: Signal Processing for Machine Learning, taught by Mert Pilanci (slide deck Lecture 10, our session 18). The dots and numbers here are our own toy examples.
Toy data: sixteen made-up dots, eight per class, in a fixed layout. The solver is a small pairwise dual solver (SMO) that keeps every weight inside its box. Each frame shows the weights so far, the line they imply, and the two values: the dual value of the weights (a lower bound) and the primal value of that line (an upper bound).
Chapter 0
Write the sorting rule as one score, and measure how far any point sits from the line where the score is zero
You record short clips with a microphone. For each clip you measure two numbers: how loud it is, and how often the waveform crosses zero in a second. A human voice gives one kind of pair. A running engine gives another. You plot every clip as a dot, voice in blue and engine in pink, and the two clouds sit apart.
Now a new clip arrives, and you must say which cloud it belongs to. The simplest rule is a straight line between the clouds. Dots on one side are voices. Dots on the other side are engines. This lecture is about the best way to choose that line, and about a second problem hiding behind the first.
The last lectures took a different road. Linear and quadratic discriminant analysis, and the Bayes optimal classifier, first model the signals. They estimate how clips are spread, p(x), and how labels depend on clips, p(y | x). Only then do they draw a boundary.
The slide answers with a principle from Vladimir Vapnik: when solving a problem of interest, do not solve a more general problem as an intermediate step. The boundary is the problem of interest. A full model of every cloud is a harder, more general problem. So we skip it and estimate the boundary directly.
A straight boundary needs two ingredients. A vector w (the weights) holds one number per feature. A single number b (the bias) shifts the boundary. Together they form the parameters θ = (w, b). For a clip with feature vector x, compute the score:
The vector w has a clear job. It points straight across the line, perpendicular to it, towards the positive side. Change w and the line turns. Change b and the line slides without turning. The score grows as you walk along w and shrinks as you walk against it.
The next question matters for everything after it. Pick a point z. How far is it from H? The slide defines d(z, H) as the smallest distance from z to any point h on H. The shortest path to a flat line is a straight drop that meets it at a right angle.
So split z into two parts. Let z0 be the foot of the drop, the point of H closest to z. Then z is z0 plus a step of signed length r along the unit vector w / ‖w‖:
Now apply the score to both sides. The foot z0 is on H, so its score is zero. And wTw / ‖w‖ equals ‖w‖. This gives wTz + b = wTz0 + b + ‖w‖2 r = 0 + ‖w‖2 r. Solve for r:
d(z, H) = |r| = |wTz + b| / ‖w‖2
Read it in words. The distance is the size of the score, divided by the length of w. A big score does not by itself mean a big distance: a long w inflates every score. You must divide that length out.
Try it. The sliders move the line. Drag the teal dot z anywhere. The third slider multiplies both w and b by a number. Watch what that changes, and what it does not.
The warm line is H. Blue shading is the side with positive score, pink the negative side. The arrow is w. Drag z, then turn the line, then scale (w, b).
Illustrative: the dot and the line are positions in a made-up two-feature plane. The formulas are exact: score = wᵀz + b, distance = |score| / ‖w‖₂, and the drop is drawn from z to the foot z₀.
Scaling (w, b) by 3 makes every score three times bigger, and the arrow three times longer. The line does not move. The distance does not change either, because the length of w grows by the same factor as the score. This small fact comes back in the next chapter and decides how we define the SVM.
This is Lecture 10 of Stanford EE269, taught by Mert Pilanci and dated February 11, 2019. It opens with the recap you just read. It then picks up the support vector machine from the previous lecture, adds a second view of it through duality, and ends with a picture of two shapes whose nearest points decide the answer. The SVM lesson builds the first view in more detail.
Chapter 1
Choose the line that leaves the most empty room on both sides, and turn that wish into a clean optimization problem
Back to the voices and the engines. Many lines separate the two clouds. Some pass a hair's width from a blue dot. They work on the clips you have, but a new clip that lands a little off will fall on the wrong side.
A safer line keeps as much empty space as possible around it. Picture a road laid between two towns. You want the widest road that still fits between the buildings. The buildings nearest the road decide how wide it can be.
The lecture makes this exact. You have data points x1 to xn and labels y1 to yn, each label equal to −1 or +1. The margin ρ of a hyperplane is the distance from the line to the nearest data point:
The maximum margin separating hyperplane is the line that makes ρ as large as possible, while still putting every point on its correct side. A point is on its correct side when its score has the same sign as its label. That means yi(wTxi + b) ≥ 0. So the problem is: maximize ρ(w, b) over w and b, subject to yi(wTxi + b) ≥ 0 for every i.
Feel it first. The device holds eight fixed dots. You choose a direction and an offset. It shows the street your line leaves.
Turn the line and slide it. The shaded street reaches the nearest dots, ringed in purple. A line that misclassifies a dot has no valid margin. Then press the button to see the best one.
Toy data: eight made-up dots, four per class. The widest street is computed live by the same exact dual solver as the hero, with a very large C so no dot may break a rule.
The slide points out a flaw. The solution is not unique. Take any positive number α and replace (w, b) by (αw, αb). The line stays exactly where it was, so the margin stays the same. Every scaled pair is an equally good answer. A solver has nothing to hold on to.
That is the scale freedom you played with in Chapter 0. The fix is to remove it by choosing one scale. Look at the point nearest to the line. Its score is some number m, the smallest |wTxi + b| over all i. Multiply w and b by 1/m. Now the nearest point has a score of exactly 1.
After that rescale, the margin simplifies, because the top of the fraction is 1 at the nearest point:
ρ = 1 / ‖w‖2, and every point obeys yi(wTxi + b) ≥ 1
Why ≥ 1 and not ≥ 0? Because the nearest point has score exactly 1 and every other point is at least as far. Points on their correct side then satisfy score times label ≥ 1. The weak condition of ≥ 0 became the sharper one of ≥ 1 at no cost.
Now maximizing 1/‖w‖ is the same as making ‖w‖ small. Squaring it changes nothing about where the minimum sits, and it removes a square root. The lecture writes the result in two equivalent forms:
This is the hard-margin support vector machine. It is a quadratic program: a squared (bowl-shaped) objective with straight-line constraints. A bowl has one bottom, so there is one best answer. The slide credits the picture of the two dotted lines, wTx + b = −1 and +1, to Alex Smola.
Take two points on a number line: x = −1 with label −1, and x = 2 with label +1. Here w and b are single numbers. The first constraint reads −1 · (−w + b) ≥ 1, so w − b ≥ 1. The second reads 2w + b ≥ 1.
Add the two inequalities. The b terms cancel and you get 3w ≥ 2, so w ≥ 2/3. The objective w2 is smallest at the smallest allowed w, which is w = 2/3. Then b = w − 1 = −1/3. Check the second constraint: 2 · 2/3 − 1/3 = 1. Both constraints hold with equality.
The line is where (2/3)x − 1/3 = 0, which is x = 1/2: exactly halfway between −1 and 2. The margin is 1/w = 3/2, which is half of the gap of 3 between the points. The street fills the gap, as it should.
Chapter 2
Add a small, priced allowance to each constraint, so one noisy clip cannot ruin the whole street
Real recordings are messy. A cough lands in an engine clip. A label is typed wrong. A sensor glitches and one dot appears deep inside the wrong cloud. The hard-margin SVM from Chapter 1 treats every dot as sacred. What happens when one of them misbehaves?
The lecture shows two small pictures. The first is about separability. If the blue and red dots overlap, no straight line puts every dot on its own side. The constraints yi(wTxi + b) ≥ 1 contradict each other. The problem has no answer at all.
The second picture is about sensitivity. Say the clouds are separable, but one red dot sits very close to the blue cluster. The widest street must now squeeze through the small gap that dot leaves. One stray point turns the whole line. The model has been bent by a single clip.
Both failures have the same cause. The rule "every point at least one unit of score from the line, on its side" is absolute. The fix is to make the rule a price instead of a wall.
Give each point i its own non-negative number si, called a slack variable. It is the amount by which that point is allowed to fall short of the rule. The constraint becomes yi(wTxi + b) ≥ 1 − si, with si ≥ 0. A point that obeys the old rule needs no slack, so si = 0.
Slack is not free. The objective pays for it. The lecture's soft-margin SVM is:
C is the tuning parameter. It is the exchange rate between a wide street and obedient points. Each unit of slack on one point costs C/n. A big C makes slack expensive, so the solver obeys. A small C makes slack cheap, so the solver prefers a wide street even if several dots poke in.
Slack costs money, so the solver gives each point the smallest slack that still works. For a point with score f = wTxi + b and label yi, the smallest allowed slack is si = max(0, 1 − yif). This gives three cases:
Suppose n = 10 and C = 1, so one unit of slack costs 0.1. The point in case 2 pays 0.6 × 0.1 = 0.06. The point in case 3 pays 0.15. A misclassified point costs more than one that is merely crowded, but never an infinite amount.
Now use the device. Every dot is draggable. The seg switches between the hard rule and the soft one. Try dragging the odd blue dot that starts inside the pink cloud.
Yellow segments are slack: how far a dot falls short of the margin. The bar underneath shows the two costs. Choose a rule, drag the stray dot out of the pink cloud, and slide C.
Toy data: thirteen made-up dots; the first blue dot is the stray. The hard margin is the soft problem with a huge C. When no line meets every constraint, the solver cannot reach it, and the page says so.
In hard mode the stray dot leaves no street at all. Move it near the edge of its own cloud and a thin street appears, tilted by that one dot. Switch to soft. The line returns to where the two clouds say it should be, and the stray dot just carries some yellow slack.
Count the unknowns. The soft problem has d numbers in w, one b, and n slacks: d + 1 + n variables. It has 2n constraints: one margin rule and one si ≥ 0 for each point. The objective is a bowl in w and a straight line in s. That is still a convex quadratic program, so there is one best answer and fast solvers find it.
It is also exactly the problem the rest of this lecture dissects. The constraints are the fences. The multipliers we meet next are the prices of those fences.
Chapter 3
Fold every fence into the objective by charging a price for crossing it, and let an adversary set the price
A hiker wants to reach the lowest point of a valley. The height of the ground is f(x) = x², so the bottom is at x = 0. But a fence stands at x = 1, and she must stay on the far side: x must be at least 1. The bottom of the valley is now out of reach. The lowest point she may stand on is right at the fence.
That is a constrained optimization problem. You minimize something, but only over choices that obey rules. Almost every training problem in machine learning, including the SVM, is of this kind.
The lecture writes every such problem the same way:
A choice of x is feasible if it obeys every constraint. The best feasible x is the answer, and its objective value is written p*, the primal optimal value. For the hiker, p* = 1.
Solvers prefer problems with no rules attached. So we build one function that carries the rules inside it. Give every inequality gi a number λi ≥ 0, and every equality hj a number μj. Each λi is a price per unit of crossing fence i. The Lagrangian adds the prices to the objective:
Why must each λ be non-negative? The price should punish a violation. If λ could be negative, then crossing the fence would earn money, and the fence would invite you across.
Here is the trick that makes this function useful. Imagine an adversary who sees your choice x, and then picks the prices λ ≥ 0 to make L as large as possible. What can the adversary do?
If you are inside the fence, every gi(x) ≤ 0. Each term λi gi is then zero or negative, so the adversary's best move is λi = 0. The result is L = f(x). The prices vanish.
If you cross a fence, gi(x) > 0. The adversary sets λi as large as it likes, and L climbs without a ceiling. Crossing costs an unlimited price. So the adversary's best result, written max over λ ≥ 0 of L, equals f(x) when x is feasible, and +∞ when it is not.
Now you, the minimizer, choose x to make that worst case small. You will never cross a fence, because the penalty is infinite. The result is the original problem written without any rules:
p* = minx maxλ ≥ 0, μ L(x, λ, μ)
The device works the hiker's example. The fence is g(x) = 1 − x, so L(x, λ) = x² + λ(1 − x). Slide x to the left of 1 and watch what the price does. Then slide λ.
The dashed curve is the ground f(x) = x². The purple curve is L(x, λ). The pink zone is forbidden ground. Move your point x, and the price λ.
The curves are exact for f(x) = x² with the single fence 1 − x ≤ 0. The adversary's reply is computed from the formula, not simulated.
At λ = 2 the purple curve has its lowest point exactly at x = 1, at height 1. That is not an accident, and the next chapter explains why. For now, notice that the fence and the price agree: at x = 1 the fence is just touched, g = 0, and the price is the one that holds the hiker there.
The soft-margin SVM has two families of inequalities. Write each as something ≤ 0. The margin rule becomes 1 − si − yi(wTxi + b) ≤ 0, and the slack rule becomes −si ≤ 0. Give the first a price αi ≥ 0 and the second a price βi ≥ 0. There are 2n prices for 2n fences. Chapter 7 puts them to work.
Chapter 4
Swap the order of the two players, and get a number that can never beat the true answer
In Chapter 3 the minimizer moved first and the adversary replied. The answer p* was the minimizer's best worst case. Now give the adversary a head start instead. First the adversary posts a set of prices. Then the minimizer, who has now seen the prices, picks the best x. Who gains from going second?
Fix the prices λ ≥ 0 and μ. Now minimize the Lagrangian over x, with no fences at all. The fences are gone, because their cost is already inside L. The lowest value you can reach is the dual function:
Work it for the hiker. L(x, λ) = x² + λ(1 − x). This is a bowl in x. Its slope is 2x − λ, which is zero at x = λ/2. Put that back: F(λ) = (λ/2)² + λ(1 − λ/2) = λ − λ²/4. Check a few prices:
F(1) = 0.75 F(2) = 1.00 F(3) = 0.75 F(4) = 0.00
Compare these with p* = 1. Every F(λ) is at or below it. This is not luck. Take any feasible point x. Its constraints hold, so g(x) ≤ 0. With λ ≥ 0, the price term λ g(x) is zero or negative. Therefore L(x, λ) ≤ f(x). The infimum over all x is at most the value at this feasible x, so:
F(λ, μ) ≤ L(x, λ, μ) ≤ f(x) for every feasible x
The left side does not depend on x. The right side can be made as small as p*, by choosing the best feasible x. So F(λ, μ) ≤ p* for every λ ≥ 0 and every μ. Each price list gives a certificate: a proof that no feasible point beats this number. If F(3) = 0.75, you know the hiker cannot get below 0.75, without searching a single point.
Certificates differ in quality. The best one is the highest. So the adversary picks the prices that make F as large as possible. This is the dual problem, and its value is d*:
d* = maxλ ≥ 0, μ F(λ, μ) and d* ≤ p*
The inequality d* ≤ p* is called weak duality. It always holds, for any problem, convex or not. The slides state it in the min-max form. The primal is min over x of max over (λ, μ) of L. The dual is max over (λ, μ) of min over x of L. Weak duality says min-max is at least max-min.
Here is the short proof, in the lecture's own terms. Pick any x′ and any (λ′, μ′). The maximum over prices at x′ is at least L(x′, λ′, μ′). That is at least the minimum over x at the prices (λ′, μ′). Now minimize the left end over x′, and maximize the right end over (λ′, μ′). The order of the inequality stays. Going second never hurts: the player who sees the other's move does at least as well.
The device below draws L(x, λ) for the hiker as a heat map, with x across and λ up. Brighter purple is a higher value. Slice it two ways and see the two players.
The saddle is where the two players meet. Choose who moves first, then drag the slider that sets their move. The side plot shows the other player's best reply.
Exact for f(x) = x² with the fence 1 − x ≤ 0, plotted for x in [0, 3] and λ in [0, 4]. The true optimum is p* = d* = 1, at x = 1 and λ = 2. The λ axis stops at 4; for a broken fence the real maximum is unbounded.
With prices first, the best reply is the bottom of a parabola. Its height F(λ) rises to 1 at λ = 2 and falls again. With the point first, the best reply is the top of a straight line in λ: flat at the left end if the fence is kept, climbing off the top of the plot if it is broken. In both modes the answer lands at or on the same level line, 1, and the dual side never rises above it.
The dual function has one more property, and it is why the dual problem is easy. For each fixed x, L is a straight line in (λ, μ). F is the lowest of a pile of straight lines, so F is concave: it bends like a hill. Maximizing a concave function is a convex problem. This is true even when the primal problem itself is hard. So the dual always gives a clean problem and a usable bound.
Chapter 5
Learn the one condition under which the best certificate equals the true answer, and watch it fail when the condition is missing
Chapter 4 gave you a ladder of lower bounds. Each price list sits somewhere on a rung, and the best rung is d*. The true answer p* sits above. The question is the size of the space between them: the duality gap, p* − d*. If it is zero, the dual problem solves the primal problem exactly. If it is positive, the dual gives only a bound.
When p* = d*, the lecture says that strong duality holds. This is what makes the dual SVM useful, because it means you may solve whichever side is easier and get the same number. The slide gives the sufficient condition as a theorem:
Two words need an explanation. A function is convex if its graph is bowl-shaped: the straight line between any two points on the graph never dips below the graph. The hiker's x² is convex. A function is affine if it is a straight line or flat plane plus a constant, like 3x − 7. Equalities must be affine, because a curved equality would carve out a curved, non-convex set of allowed points.
Convexity alone is not enough. We also need a constraint qualification: a mild condition that stops the feasible region from being pathologically thin. The slide gives the standard example, Slater's condition:
there is a point x with gi(x) < 0 and hj(x) = 0 for all i and j
In words: some point obeys every inequality strictly, with room to spare. The equalities may only be met exactly. For the hiker, x = 2 gives g = 1 − 2 = −1, which is strictly negative. Slater holds.
The lecture's next slide: for the constraints 0 ≤ xi ≤ 1, the middle point xi = 0.5 for every i makes each inequality strict. Slater holds. For the SVM from Chapter 2, take w = 0, b = 0 and every slack equal to 2. Each margin rule reads 1 − 2 − 0 = −1 < 0, and each slack rule reads −2 < 0. Slater holds, so the soft-margin SVM always has strong duality.
Why would room to spare matter? Let v(u) be the best objective value if the fence is moved by u: minimize f subject to g(x) ≤ u. For a convex problem v is a convex curve. A price λ* that closes the gap is the negative slope of a line that touches v at u = 0 and stays below the curve. Such a line exists whenever v has a finite slope at 0. Slater guarantees that: if you have room to spare, a small move of the fence changes v smoothly.
Without room to spare, v can jump at u = 0. A jump has no tangent line, so no finite price works, and a gap opens.
The lecture ends this part with an exercise. Show that strong duality does not hold for:
min e−x1 s.t. x12 / x2 ≤ 0, x1 ∈ R, x2 > 0
Both the objective and the constraint are convex. But look at the fence. Since x2 > 0 and x12 is never negative, the fraction is at most 0 only when it equals 0, which forces x1 = 0. No point makes it strictly negative. Slater fails.
Primal value. The feasible points all have x1 = 0, so f = e0 = 1. Hence p* = 1.
Dual value. L = e−x1 + λ x12 / x2. Both terms are positive, so L > 0 and F(λ) ≥ 0. Now play the minimizer. Take x1 large, so e−x1 is tiny. Then take x2 = x13, so the second term is λ / x1, also tiny. Both sink toward 0 as x1 grows. So F(λ) = 0 for every λ ≥ 0, and d* = 0.
The gap is 1 − 0 = 1. A problem with convex pieces, a clean statement and a finite answer still has a gap. Try the device, and compare it with two problems where Slater holds.
The warm line is p*. The purple mark is the dual function F at your price. Choose a problem, then slide λ. In the third problem, also probe x₁ to see the minimizer sneak toward zero.
Exact formulas. Valley: F(λ) = λ − λ²/4. Ramp: F(λ) = 1 at λ = 1 and minus infinity at every other λ, because the x-term does not cancel. Slater fails: F(λ) = 0 for all λ ≥ 0, with the minimizer taking x₂ = x₁³.
The second problem teaches a lesson the SVM will repeat. The ramp has a dual function that is minus infinity almost everywhere. Only the single price λ = 1 is usable. A dual problem can restrict its own prices to a thin set, and the SVM's condition that the weighted labels sum to zero is exactly such a restriction.
Chapter 6
Learn the four conditions that every optimal point and its prices must meet, and that prove a point optimal
Picture a ball rolling down a bowl, inside a box with walls. It comes to rest at some spot. Ask what must be true there. If it rests in the open, the bowl is flat under it. If it rests against a wall, the wall pushes back exactly as hard as gravity pulls it. If it rests away from a wall, that wall pushes with no force at all. These plain facts are the KKT conditions.
The lecture names them after Karush, Kuhn and Tucker. Suppose p* = d* (strong duality), x* is optimal for the primal, and (λ*, μ*) is optimal for the dual. Then four things hold at (x*, λ*, μ*):
Conditions 2 and 3 are the rules of the two problems, so they are free. For the other two, follow one chain of numbers. Strong duality says f(x*) = F(λ*, μ*). By the definition of F as a lowest value, F(λ*, μ*) ≤ L(x*, λ*, μ*). And L(x*, λ*, μ*) = f(x*) + Σ λi* gi(x*), which is at most f(x*), because each term is a non-negative price times a non-positive g. So:
f(x*) = F(λ*) ≤ L(x*, λ*) = f(x*) + Σ λi* gi(x*) ≤ f(x*)
The two ends are the same number, so every step in between is an equality. In particular Σ λi* gi(x*) = 0. It is a sum of terms that are each zero or negative, so every term is zero. That is complementary slackness. The first step also says x* reaches the lowest value of L(·, λ*, μ*), so the slope of L there is zero. That is stationarity.
Complementary slackness is the condition to remember. For each fence, a seesaw has two ends: the price λ and the slack −g. One end must be down at zero. Either the fence is not touching (g < 0), and then the price must be zero. Or the price is positive, and then the fence must be exactly touching (g = 0).
Check it on the hiker. Her answer is x* = 1 with price λ* = 2. Stationarity: 2x − λ = 2 − 2 = 0. Feasibility: g(1) = 0. Dual feasibility: 2 ≥ 0. Complementary slackness: 2 × 0 = 0. All four pass. Now move the fence to x ≥ −1. The valley's bottom, x* = 0, is now inside. The fence is not touching, g(0) = −1 < 0, so the price must be λ* = 0. Stationarity then reads 2x = 0, which gives x* = 0. All four pass again.
Now the other direction, which is the second theorem on the lecture's next slide. If f and the gi are convex, the hj are affine, and some point (x*, λ*, μ*) satisfies the four conditions, then it is optimal and strong duality holds. The proof is short. For fixed prices, L is convex in x. A convex function with zero slope is at its global minimum, so x* minimizes L(·, λ*, μ*). Hence F(λ*, μ*) = L(x*, λ*, μ*). Complementary slackness makes the price terms vanish, so that equals f(x*). So d* ≥ F(λ*) = f(x*) ≥ p*. Weak duality gives d* ≤ p*, and the chain closes.
This is why KKT is a test you can run. Any point and prices that pass the four checks are proven optimal for a convex problem. No search through other points is needed.
The device is a small convex problem in two features. You pull a target c (the unconstrained best point). The solver finds the closest allowed point x*, with two fences. You then place your own trial point t and see which conditions fail. The prices used are the optimal ones.
Minimise the squared distance to the target c, with two fences: g₁ = x₁ + x₂ − 2 ≤ 0 and g₂ = x₁ − 1.2 ≤ 0. Drag the teal trial point t. Drag the warm target c to change the problem. Arrows at t: the pull toward c (warm) and the pushes of the fences (purple).
Toy problem, exact solution: f(x) = ‖x − c‖², two linear fences, optimum found by trying each set of active fences. Prices at the optimum are used in every check. A light turns green when its value is within 0.1.
Drag t onto the green point x*. All four lights turn green at once. Drag it along a fence and the first light, stationarity, goes red: the forces no longer balance. Drag it inside the allowed region, away from a tight fence, and complementary slackness fails: a fence with a positive price is no longer touching.
Chapter 7
Follow every cancellation from the soft-margin Lagrangian to a problem that only sees inner products
You now own all the tools. The soft-margin SVM has convex pieces, strict room to spare, and strong duality. This chapter turns the crank. We build its Lagrangian, minimize over the primal variables, and watch most of the terms cancel. What is left is the dual SVM, and it is surprisingly small.
Recall the soft-margin problem from Chapter 2. There are n margin rules, each with price αi ≥ 0, and n slack rules si ≥ 0, each with price βi ≥ 0. Each rule is written as "something ≤ 0" and charged at its price. The result is slide 32:
The dual function is F(α, β) = min over (w, b, s) of L, and the dual problem is to maximize F over α ≥ 0 and β ≥ 0. The inner minimum has no fences, so we find it the oldest way: set every slope to zero.
L is a bowl in w and a plain line in b and in each si. Take each partial derivative and set it to zero.
The second and third are different in kind from the first. b and si appear in L only in straight-line form. A straight line with a non-zero slope has no lowest point: it dives to minus infinity. So if either slope condition fails, F = −∞, and that price list is useless. The conditions are the only prices that give a finite bound. This is the same thing the ramp example showed in Chapter 5.
Now substitute. Expand L and group the terms by variable:
L = ½‖w‖² − Σ αi yi wTxi − b Σ αi yi + Σ si (C/n − αi − βi) + Σ αi
Under the second condition the b term is b times zero. Under the third, every si bracket is zero. Both variables drop out. Two terms remain that involve w. The middle one is −wT(Σ αi yi xi), and the first condition says the bracket is w itself. So it equals −‖w‖². Add it to ½‖w‖² and you have −½‖w‖². Now write ‖w‖² as the sum over pairs and get slide 37:
Take a tiny check by hand. Four points: (2, 2) and (3, 1) with label +1, and (−1, −1) and (0, −2) with label −1. Choose α = (0.3, 0.1, 0.25, 0.15). The +1 weights sum to 0.4 and the −1 weights sum to 0.4, so Σ α y = 0. Then w = 0.3(2, 2) + 0.1(3, 1) − 0.25(−1, −1) − 0.15(0, −2) = (1.15, 1.25). Its squared length is 2.885, and Σ α = 0.8. So F = 0.8 − ½ × 2.885 = −0.6425. Now see that this is what L gives no matter what b and s you try.
The left plot sweeps b and the right plot sweeps a common slack s, each with w = Σ α y x. The dashed line is the closed form F. With valid prices, both plots are flat. Break a condition and watch the slope appear.
Toy data from the text: four points, C/n = 0.5, α = (0.3, 0.1, 0.25, 0.15), βi = 0.5 − αi. In the unbalanced case α4 is raised to 0.35. In the wrong-β case every β is set to 0. L is computed from its full definition, term by term.
The last two slides finish the job. Maximize F over the legal prices: Σ αi yi = 0, αi + βi = C/n, αi ≥ 0, βi ≥ 0. The variable β appears in only one place, the equality. Replace it with βi = C/n − αi. For that to be at least 0, αi must be at most C/n. The β's vanish and a box remains (slide 39):
maxα Σi αi − ½ Σij αi αj yi yj xiTxj s.t. Σi αi yi = 0, 0 ≤ αi ≤ C/n
In matrix form, let Q be the n × n table with entries Qij = yi yj xiTxj. The problem is: maximize 1Tα − ½ αTQα. It has n unknowns, one equality and a box, and its curvature matrix Q is built from the data's inner products. That is the entire dual SVM.
Chapter 8
Read the dual answer as a weight for every data point, and see that most of the weights are exactly zero
Suppose you trained a classifier on a million audio clips, then deleted 999,000 of them and trained again. Would the line move? For most methods, yes. For the SVM, it can stay put exactly. The dual explains why, and the explanation is the most useful thing in this lecture.
Slide 40 puts the two problems side by side. The primal searches over a line (w, b) and a slack for every point. The dual searches over one weight αi per point. Strong duality (Chapter 5) says the best values agree. The link between the answers is the first slope condition from Chapter 7:
So w* is a mixture of training points. Solve the dual, get n weights, add up the weighted points, and you hold the primal line's direction. A new clip x is then classified by the sign of w*Tx + b, which is Σ αi* yi xiTx + b. Every training point enters only through its inner product with the new clip.
Complementary slackness (Chapter 6) answers this for the soft-margin SVM. Its conditions are αi( yi(wTxi + b) + si − 1 ) = 0 and βi si = 0, together with αi + βi = C/n. Walk through the three possible sizes of αi:
A point with αi > 0 is called a support vector. The name fits: those are the points that hold up the street. Everything else could be removed and the SVM would not notice, because w* = Σ αi* yi xi has a zero in front of them.
The dual never mentions b. We recover it from a free support vector, where yi(w*Txi + b) = 1. Since yi is ±1, this gives b = yi − w*Txi. In practice you average this over all free support vectors, which reduces rounding error.
Now see the sparsity yourself. The device shows twenty clips, the weights the dual gives them, and a button that deletes everything with zero weight.
Purple rings grow with a point's weight. The bars below sort the weights, with the cap C/n as a dashed line. Slide C, then press the button. Does the line move?
Toy data: twenty random dots, ten per class, from a seeded generator so they are the same on every visit. Bar colours: grey is α = 0, purple is a free support vector, yellow is a bounded one at the cap.
Press the button and the line does not move. The distance between the old and the new w is zero to four decimals. Now lower C. More points become bounded, and the set of support vectors grows. Raise C. The street narrows and fewer points carry weight.
Three gifts come from this view. First, sparsity: prediction needs only the support vectors, not the whole training set. Second, size: the primal has d + 1 + n unknowns, the dual only n. When the number of features d is far larger than n, the dual is smaller. Third, and most important for the next lecture: the data appear only as inner products xiTxj. If you can compute an inner product in some richer feature space cheaply, you can run the same machine there. That is the idea of kernels.
Chapter 9
Rewrite the hard-margin dual until it becomes the shortest distance between two convex shapes
Wrap a rubber band around all the blue dots. Wrap another around all the pink ones. If the two clouds can be separated by a line, the two bands do not touch. Between them lies a gap. The widest street you can lay across that gap seems to depend only on the two bands, and on how close they come.
The last part of the lecture proves exactly this. It starts from the dual SVM and changes the problem, step by step, until a plain geometry question is left.
Slide 41 lets C go to infinity. A huge C makes slack so expensive that every point must obey the rule, which is the hard-margin SVM. The cap C/n on each weight disappears, and the dual becomes:
maxα ≥ 0 − ½ ‖ Σi αi yi xi ‖² + Σi αi s.t. Σi αi yi = 0
Here the squared norm is the same ½‖w‖² as before, written through w = Σ αi yi xi.
Slides 42 and 43 sort the sum by label. Write the + points as one group and the − points as another. The vector Σ αi yi xi becomes Σ+ αi xi − Σ− αi xi: a weighted sum of the + points, minus a weighted sum of the − points. The balance condition Σ αi yi = 0 says the two groups carry equal total weight: Σ+ αi = Σ− αi. The reward Σ αi is the sum of the two group totals.
Slide 44 gives that common group total a name, γ > 0, and adds it as a variable to maximize over. This changes nothing, because for any feasible α the total already has some value. With Σ+ αi = Σ− αi = γ, the reward Σ αi equals 2γ.
Slide 45 scales the weights: replace each αi by γ αi. Now each group's weights sum to 1. A set of non-negative weights that sum to 1 is a recipe for a point inside the group's shape: it is a weighted average. The problem reads:
The shape of all weighted averages of a set of points is its convex hull: the region inside the rubber band. So u ranges over the hull of the + points and v over the hull of the − points.
Let D = ‖u − v‖², the squared distance between the two chosen points. The objective in γ is −(γ²/2)D + 2γ, a downward bowl. Its slope is −γD + 2, which is zero at γ* = 2 / D (slide 46). Plug it back in (slide 47): −(γ*²/2)D + 2γ* = −2/D + 4/D = 2/D.
What is left is to choose u and v to maximize 2/D. A bigger 2/D means a smaller D. So the whole dual SVM is:
min over u in the + hull and v in the − hull of ‖u − v‖²
That is slide 48: the minimum distance between two convex hulls.
The pieces fit together. The weight vector is w = Σ αi yi xi = γ(u − v). Its length is γ‖u − v‖ = (2/D)√D = 2/‖u − v‖. The margin is 1/‖w‖ = ‖u − v‖/2. The street is exactly half the gap between the hulls. The line is the perpendicular bisector of the segment from u to v.
Check with the two-point example from Chapter 1, at x = −1 and x = 2. The hulls are the points themselves, so D = 9 and γ* = 2/9. Then w = (2/9)(2 − (−1)) = 2/3, the same answer we got by solving the constraints by hand. The margin is 3/2, half of the gap of 3.
Support vectors fall out too. The closest points u and v lie on the boundary of their hulls: u on an edge or a corner, v the same. The only data points with non-zero weight are the corners of those touching edges. Interior points of a hull can never matter.
Try it. Drag any dot. The device draws the two hulls, the nearest pair of points, and the street.
Shaded shapes are the convex hulls. The warm segment joins the nearest points u and v. The street is centred on its midpoint. Drag any dot, including ones inside a hull.
Toy data: ten made-up dots. The nearest points are found exactly by comparing every corner against every edge of the other hull. The check value 1/‖w‖ comes from the same dual solver as the hero with a very large C.
Drag a dot in the middle of a hull and nothing changes: interior points do not matter. Drag a corner toward the other shape and the gap shrinks, the street narrows, and the line tilts. Push a dot across so the hulls overlap, and the hard-margin problem has no answer, exactly as Chapter 2 warned.
The lecture works the C → ∞ case. With a finite C, each weight has a cap, so u and v cannot reach all the way to the outer corners as freely. That is the geometric price of the soft margin.
Chapter 10
Review the numbers, the terms and the code, then look ahead to kernels
You can now rebuild Lecture 10 from memory. You can say why the SVM wants a wide street, how a soft margin prices its mistakes, how any constrained problem trades its fences for prices, and why the SVM's prices come out as weights on a few points. Go back to the hero at the top and press the solver once more. This time, name what the two numbers are.
A hyperplane wTx + b = 0 separates two classes, and the margin is the distance to the nearest point. Fixing the scale so the nearest score is 1 turns "widest street" into "minimize ‖w‖²", the hard-margin SVM. Slack variables make it soft. The Lagrangian prices every constraint, the dual function is its lowest value, and weak duality says it never beats the primal. For convex problems with room to spare (Slater), the two meet. The KKT conditions are the four checks at the meeting point. For the SVM they give w* = Σ αi yi xi with 0 ≤ αi ≤ C/n, a dual that sees data only through inner products, weight only on support vectors, and a geometry of nearest points between convex hulls.
| What | Value or form | Why it matters |
|---|---|---|
| Distance to a hyperplane | |wTz + b| / ‖w‖2 | Score divided by the length of w |
| Margin after rescaling | ρ = 1/‖w‖2, with yi(wTxi + b) ≥ 1 | Wide street means short w |
| Hand example | x = −1 (label −1), x = 2 (label +1): w = 2/3, b = −1/3, margin 3/2 | The street fills the gap |
| Soft-margin SVM | min ½‖w‖² + (C/n) Σ si | C prices each unit of slack |
| Variables and constraints | d + 1 + n unknowns; 2n constraints | Primal size grows with features |
| Hiker example | min x² s.t. x ≥ 1: p* = 1, F(λ) = λ − λ²/4, λ* = 2, d* = 1 | A full duality in one line |
| Weak duality | d* ≤ p*, always | Every price list is a certificate |
| Strong duality | p* = d* if convex, affine equalities and Slater | Solve whichever side is easier |
| Slater fails | min e−x1 s.t. x1²/x2 ≤ 0: p* = 1, d* = 0 | A convex problem can still have a gap |
| KKT | stationarity, primal and dual feasibility, λigi = 0 | A test for optimality |
| SVM stationarity | w = Σ αiyixi; Σ αiyi = 0; αi + βi = C/n | Source of the dual |
| Dual SVM | max Σ αi − ½ Σij αiαjyiyjxiTxj, 0 ≤ αi ≤ C/n | Data enter as inner products |
| Three kinds of point | α = 0 outside; 0 < α < C/n on the margin; α = C/n inside or wrong | Only support vectors matter |
| Hull geometry | γ* = 2/D, margin = ‖u − v‖/2 | The SVM is a nearest-points problem |
This sketch solves the dual with a general-purpose solver, then rebuilds the line. X has shape (n, d) and y has shape (n,), with entries ±1. It is fine for a few hundred points. Real libraries use specialised solvers such as SMO.
pythonimport numpy as np from scipy.optimize import minimize def dual_svm(X, y, C): # X: (n, d), y: (n,) in {-1, +1} n = len(y); B = C / n # the box: 0 <= alpha_i <= C/n Q = (y[:, None] * y[None, :]) * (X @ X.T) # (n, n): y_i y_j x_i.x_j, data as inner products obj = lambda a: 0.5 * a @ Q @ a - a.sum() # minimise the NEGATIVE dual grad = lambda a: Q @ a - 1 res = minimize(obj, np.zeros(n), jac=grad, method="SLSQP", bounds=[(0, B)] * n, constraints=[{"type": "eq", "fun": lambda a: a @ y, "jac": lambda a: y}]) a = res.x # (n,) the dual weights; most are ~0 w = (a * y) @ X # (d,) w = sum alpha_i y_i x_i free = (a > 1e-6) & (a < B - 1e-6) # on the margin: y_i (w.x_i + b) = 1 ref = free if free.any() else (a > 1e-6) # fall back to any support vector b = np.mean(y[ref] - X[ref] @ w) # b = y_i - w.x_i, averaged dual = a.sum() - 0.5 * a @ Q @ a slack = np.maximum(0, 1 - y * (X @ w + b)) # s_i, the smallest slack that works primal = 0.5 * w @ w + B * slack.sum() return w, b, a, primal, dual # primal - dual ~ 0: strong duality def predict(x_new, X, y, a, b): # only support vectors (a > 0) matter sv = a > 1e-6 return np.sign((a[sv] * y[sv]) @ (X[sv] @ x_new) + b)
Read it against the chapters. Q is the table from Chapter 7. The bounds and the equality are the box and the balance condition. The weights a are the ones in Chapter 8, and predict touches only support vectors. The last lines compute both values, so you can watch the gap close, as in the hero.
The Bayes and discriminant lessons (Bayes classifiers, Fisher discriminant) model the data and then classify. The SVM, as Vapnik advised, skips straight to the boundary. This lecture added the tool that most of convex machine learning uses: the dual. The SVM lesson is the first half of the story. The dual's key fact, that data appear only through inner products, is the door to kernels. Convex duality returns later in convex neural networks.
Now press Present or Teach and explain the dual SVM out loud, from memory: why the margin is 1/‖w‖, what slack costs, how a Lagrangian prices a fence, why the dual is a lower bound, what KKT checks, and why only support vectors matter. Then go back to the hero and close the gap again.