Duality
Lagrange multipliers as the variables of a second problem: the dual function bounds the minimum from below, Slater's condition makes the bound exact, least squares in a ball turns into ridge regression, and the duality gap tells an SVM solver when to stop.
A minimum without a certificate
The topic Lagrange multipliers and KKT conditions gave us multipliers and , and the conditions a constrained minimum must satisfy. There the multipliers were helpers for finding . Here they become the variables of a problem of their own, and that problem answers a question KKT leaves open: how do we know a candidate is optimal?
Take a soft-margin SVM on 40 points in the plane, two overlapping classes. An iterative solver reports the objective after 100 iterations, after 1000 and after 5000. The numbers still fall, only more slowly. Is the minimum, or would another 50 000 iterations reach ?
We work with the general problem from the previous topic, which we call the primal problem: minimize subject to inequality and equality constraints,
Its optimal value is . A point that satisfies every constraint is feasible.
One side of is free. Every feasible gives an upper bound, , because is the smallest such value. The other side is missing. A lower bound would pin between two numbers, and a pair like that is a certificate: anyone can check it without trusting the solver. Duality builds lower bounds out of multipliers.
The dual function
Recall the Lagrangian, with one multiplier per constraint:
Now fix the multipliers and minimize over . The result is the dual function:
Do not confuse with the constraints : the dual function has no index, and its arguments are multipliers, not points.
Two details matter. First, the infimum runs over all , feasible or not. Computing is an unconstrained problem, the kind Unconstrained optimization taught us to solve. Second, can be : if the Lagrangian is unbounded below in , for instance linear in with a nonzero slope, there is no finite infimum.
The dual function is always concave. For each fixed the Lagrangian is an affine function of . The graph of is the lower envelope of all these planes, and an infimum of affine functions is concave. Nothing here asks or to be convex.
Our running example comes from the previous topic: minimize subject to . There we found , and . The constraint is , so the Lagrangian is
For a fixed this is a parabola in with its vertex where , at . Substituting the vertex gives
A downward parabola, concave as promised.
Weak duality: every multiplier gives a bound
Here is the payoff. For any and any ,
The proof takes three lines. Pick any feasible point . The infimum is no larger than the value at one particular point:
At a feasible point , and together with makes each . So the right-hand side is at most . This holds for every feasible , so is at most their infimum, .
This is weak duality. It needs no convexity: any problem, any multipliers with .
In the running example every gives a bound on :
The best bound is the dual problem:
It maximizes a concave function over a convex set, so it is a convex problem even when the primal is not. Weak duality says , and the difference is the duality gap.
The same inequality has a second reading. Fix and maximize the Lagrangian over the multipliers instead. If is feasible, the best the multipliers can do is make the added terms zero, so the maximum is . If some constraint is violated, its multiplier pushes the Lagrangian up without limit, and the maximum is . So the two problems are one function optimized in opposite orders:
Weak duality is the max–min inequality , which holds for any function of two arguments: the player who moves second does at least as well. The three-line proof above is that inequality in disguise, since its last step, , compares a value with the supremum over multipliers. Strong duality says that the order does not matter.
In the example the best bound sits at and equals exactly. And is the KKT multiplier from the previous topic. That is no accident.
Strong duality and Slater's condition
When we have strong duality. It does not always hold, but for convex problems a mild condition guarantees it.
Slater's condition. The problem is convex ( and all convex, all affine), and some point is strictly feasible: for every and . Then , and when is finite some attains it. Affine inequalities need not hold strictly: for them plain feasibility is enough. So linear programs, least squares with linear constraints and the SVM all have strong duality as long as they are feasible.
The running example is convex, and gives . That is why its gap is zero. Without such a point a gap can open: for under the constraint with , the problem is convex, yet and .
Strong duality explains the coincidence with KKT. Let be primal optimal and dual optimal. Then
Both inequalities are equalities, and each says something:
- the first: minimizes , so the gradient of the Lagrangian vanishes there (stationarity);
- the second: , and since every term is , each (complementary slackness).
So under strong duality the KKT multipliers are exactly the solution of the dual problem. They also keep their meaning as prices: says how fast falls when constraint is relaxed. In the example, relaxing to gives , which falls at rate .
Least squares in a ball is ridge regression
Now an ML problem. Fit least squares, but only over weights in a ball of radius :
The problem is convex, and for the point is strictly feasible. Slater's condition holds, so there is no gap.
The Lagrangian is the ridge loss minus a constant:
Setting its gradient (see Matrix calculus) to zero gives the ridge regression solution:
The dual is a search over one number . Its slope is simple. Differentiate by the chain rule:
The first term vanishes because minimizes , so its own change does not count. While is too long, increase .
Complementary slackness splits the cases. If the plain least-squares solution fits in the ball, then . Otherwise and the solution sits on the sphere, .
Example: , , . Plain least squares gives , with squared norm and residual . The constraint binds. The dual function climbs and falls:
The peak is at , with of squared norm and . A constrained solver run on the primal finds the same .
So "ridge with penalty " and "least squares in a ball of radius " are one family of solutions in two parametrizations. The penalty is the price of the radius.
The SVM dual
The most famous dual in ML belongs to the support vector machine. Points have labels , and the hard-margin SVM looks for the widest strip that puts every point on its own side with room to spare:
Here is the offset of the hyperplane, and the margin is . With multipliers , minimizing the Lagrangian over gives . Minimizing over forces , or else the Lagrangian is linear in and . What remains is the dual, a maximization over with one linear constraint:
The dual reveals three things.
- Inner products only. The data enter through . Replace them with a kernel and the same solver fits a nonlinear boundary.
- Support vectors. By complementary slackness only for points exactly on the margin. Only these support vectors enter .
- Geometry. The optimal strip is the perpendicular bisector of the shortest segment between the convex hulls of the two classes. With the length of that segment, the margin is and .
The geometry takes a few lines. The constraint gives both classes the same total weight . Write , with summing to on each class. Then and , where are points of the two convex hulls. The dual objective becomes a parabola in ,
with maximum at . That maximum is largest when and are as close as possible, so . The vector points along the shortest segment, and , so the margin is .
Example: negatives and , positives and . The nearest hull points are , the middle of , and , so . The dual solution is , , . It gives , , margin and .
The price is one dual variable per training point and an matrix of inner products. For millions of points the primal with stochastic gradients wins. Duality also shows up far from SVMs: the WGAN in the course Generative Models rests on the Kantorovich–Rubinstein duality, which swaps transport plans for 1-Lipschitz critics.
The duality gap as a stopping rule
Back to the question from the start. For any feasible and any multipliers with ,
So the computable number bounds how far is from optimal, and also how far the multipliers are from the dual optimum. We do not need to know it. A solver that stops once this gap is below is guaranteed to be within . The coordinate-descent Lasso in scikit-learn checks exactly such a gap.
The SVM from the first block is a soft-margin one with , with the offset folded into through a constant feature. Write for with the constant appended. The primal and dual objectives are
and the dual keeps the multipliers in a box, . Projected gradient ascent on the dual gives a dual-feasible at every step and a primal candidate . We track both objectives and their gap:
| iteration | gap | true error | ||
|---|---|---|---|---|
| 1 | ||||
| 10 | ||||
| 1000 | ||||
| 5000 |
The gap is pessimistic early on and tight at the end. The rule "stop when the gap is below " fires at iteration 2096, where the true error is . So was the minimum after all. Seven of the 40 points end up as support vectors, six of them at the bound . The seventh lies exactly on the margin, as in the previous block; the six at the bound lie inside the margin, and one of them is on the wrong side of the boundary.
Look at the last row once more. The gap does not reach : it stalls at and stays there however long we run. That is the size of rounding error in 64-bit floating point. The ridge solution hides a similar question: we invert , and how accurately can a computer do that? The next topic, Conditioning and numerical stability, deals with what floating point does to all of this mathematics.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.