Skip to content
BIASED

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 λi≥0\lambda_i \ge 0 and νj\nu_j, and the conditions a constrained minimum must satisfy. There the multipliers were helpers for finding x∗x^*. 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 5.36035.3603 after 100 iterations, 4.37424.3742 after 1000 and 4.36724.3672 after 5000. The numbers still fall, only more slowly. Is 4.36724.3672 the minimum, or would another 50 000 iterations reach 4.24.2?

We work with the general problem from the previous topic, which we call the primal problem: minimize ff subject to mm inequality and pp equality constraints,

min⁡x∈Rnf(x),gi(x)≤0, i=1,…,m,hj(x)=0, j=1,…,p.\min_{x \in \mathbb R^n} f(x), \qquad g_i(x) \le 0,\ i = 1, \dots, m, \qquad h_j(x) = 0,\ j = 1, \dots, p.

Its optimal value is p∗p^*. A point that satisfies every constraint is feasible.

One side of p∗p^* is free. Every feasible xx gives an upper bound, p∗≤f(x)p^* \le f(x), because p∗p^* is the smallest such value. The other side is missing. A lower bound would pin p∗p^* 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:

L(x,λ,ν)=f(x)+∑i=1mλigi(x)+∑j=1pνjhj(x).\mathcal L(x, \lambda, \nu) = f(x) + \sum_{i=1}^m \lambda_i g_i(x) + \sum_{j=1}^p \nu_j h_j(x).

Now fix the multipliers and minimize over xx. The result is the dual function:

g(λ,ν)=inf⁡xL(x,λ,ν).g(\lambda, \nu) = \inf_{x} \mathcal L(x, \lambda, \nu).

Do not confuse gg with the constraints gig_i: the dual function has no index, and its arguments are multipliers, not points.

Two details matter. First, the infimum runs over all xx, feasible or not. Computing gg is an unconstrained problem, the kind Unconstrained optimization taught us to solve. Second, gg can be −∞-\infty: if the Lagrangian is unbounded below in xx, for instance linear in xx with a nonzero slope, there is no finite infimum.

The dual function is always concave. For each fixed xx the Lagrangian is an affine function of (λ,ν)(\lambda, \nu). The graph of gg is the lower envelope of all these planes, and an infimum of affine functions is concave. Nothing here asks ff or gig_i to be convex.

Our running example comes from the previous topic: minimize (x−2)2(x - 2)^2 subject to x≤1x \le 1. There we found x∗=1x^* = 1, p∗=1p^* = 1 and λ=2\lambda = 2. The constraint is g1(x)=x−1≤0g_1(x) = x - 1 \le 0, so the Lagrangian is

L(x,λ)=(x−2)2+λ(x−1).\mathcal L(x, \lambda) = (x - 2)^2 + \lambda (x - 1).

For a fixed λ\lambda this is a parabola in xx with its vertex where 2(x−2)+λ=02(x - 2) + \lambda = 0, at x=2−λ/2x = 2 - \lambda/2. Substituting the vertex gives

g(λ)=λ24+λ(1−λ2)=λ−λ24.g(\lambda) = \frac{\lambda^2}{4} + \lambda\Big(1 - \frac{\lambda}{2}\Big) = \lambda - \frac{\lambda^2}{4}.

A downward parabola, concave as promised.

Weak duality: every multiplier gives a bound

Here is the payoff. For any λ≥0\lambda \ge 0 and any ν\nu,

g(λ,ν)≤p∗.g(\lambda, \nu) \le p^*.

The proof takes three lines. Pick any feasible point x~\tilde x. The infimum is no larger than the value at one particular point:

g(λ,ν)≤L(x~,λ,ν)=f(x~)+∑iλigi(x~)+∑jνjhj(x~).g(\lambda, \nu) \le \mathcal L(\tilde x, \lambda, \nu) = f(\tilde x) + \sum_i \lambda_i g_i(\tilde x) + \sum_j \nu_j h_j(\tilde x).

At a feasible point hj(x~)=0h_j(\tilde x) = 0, and gi(x~)≤0g_i(\tilde x) \le 0 together with λi≥0\lambda_i \ge 0 makes each λigi(x~)≤0\lambda_i g_i(\tilde x) \le 0. So the right-hand side is at most f(x~)f(\tilde x). This holds for every feasible x~\tilde x, so g(λ,ν)g(\lambda, \nu) is at most their infimum, p∗p^*.

This is weak duality. It needs no convexity: any problem, any multipliers with λ≥0\lambda \ge 0.

In the running example every λ≥0\lambda \ge 0 gives a bound on p∗=1p^* = 1:

λ\lambda 00 11 22 33 44
g(λ)g(\lambda) 00 0.750.75 11 0.750.75 00

The best bound is the dual problem:

d∗=max⁡λ≥0, νg(λ,ν).d^* = \max_{\lambda \ge 0,\ \nu} g(\lambda, \nu).

It maximizes a concave function over a convex set, so it is a convex problem even when the primal is not. Weak duality says d∗≤p∗d^* \le p^*, and the difference p∗−d∗≥0p^* - d^* \ge 0 is the duality gap.

The same inequality has a second reading. Fix xx and maximize the Lagrangian over the multipliers instead. If xx is feasible, the best the multipliers can do is make the added terms zero, so the maximum is f(x)f(x). If some constraint is violated, its multiplier pushes the Lagrangian up without limit, and the maximum is +∞+\infty. So the two problems are one function optimized in opposite orders:

p∗=inf⁡xsup⁡λ≥0, νL(x,λ,ν),d∗=sup⁡λ≥0, νinf⁡xL(x,λ,ν).p^* = \inf_x \sup_{\lambda \ge 0,\ \nu} \mathcal L(x, \lambda, \nu), \qquad d^* = \sup_{\lambda \ge 0,\ \nu} \inf_x \mathcal L(x, \lambda, \nu).

Weak duality is the max–min inequality sup⁡inf⁡≤inf⁡sup⁡\sup \inf \le \inf \sup, 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, L(x~,λ,ν)≤f(x~)\mathcal L(\tilde x, \lambda, \nu) \le f(\tilde x), 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 λ=2\lambda = 2 and equals p∗=1p^* = 1 exactly. And λ=2\lambda = 2 is the KKT multiplier from the previous topic. That is no accident.

Strong duality and Slater's condition

When d∗=p∗d^* = p^* 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 (ff and all gig_i convex, all hjh_j affine), and some point xˉ\bar x is strictly feasible: gi(xˉ)<0g_i(\bar x) < 0 for every ii and hj(xˉ)=0h_j(\bar x) = 0. Then d∗=p∗d^* = p^*, and when p∗p^* is finite some (λ∗,ν∗)(\lambda^*, \nu^*) 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 xˉ=0\bar x = 0 gives g1(0)=−1<0g_1(0) = -1 < 0. That is why its gap is zero. Without such a point a gap can open: for e−xe^{-x} under the constraint x2/y≤0x^2/y \le 0 with y>0y > 0, the problem is convex, yet p∗=1p^* = 1 and d∗=0d^* = 0.

Strong duality explains the coincidence with KKT. Let x∗x^* be primal optimal and (λ∗,ν∗)(\lambda^*, \nu^*) dual optimal. Then

f(x∗)=g(λ∗,ν∗)≤L(x∗,λ∗,ν∗)≤f(x∗).f(x^*) = g(\lambda^*, \nu^*) \le \mathcal L(x^*, \lambda^*, \nu^*) \le f(x^*).

Both inequalities are equalities, and each says something:

  • the first: x∗x^* minimizes L(⋅,λ∗,ν∗)\mathcal L(\cdot, \lambda^*, \nu^*), so the gradient of the Lagrangian vanishes there (stationarity);
  • the second: ∑iλi∗gi(x∗)=0\sum_i \lambda_i^* g_i(x^*) = 0, and since every term is ≤0\le 0, each λi∗gi(x∗)=0\lambda_i^* g_i(x^*) = 0 (complementary slackness).

So under strong duality the KKT multipliers are exactly the solution of the dual problem. They also keep their meaning as prices: λi∗\lambda_i^* says how fast p∗p^* falls when constraint ii is relaxed. In the example, relaxing x≤1x \le 1 to x≤1+ux \le 1 + u gives p∗(u)=(1−u)2p^*(u) = (1 - u)^2, which falls at rate 2=λ∗2 = \lambda^*.

Least squares in a ball is ridge regression

Now an ML problem. Fit least squares, but only over weights in a ball of radius t\sqrt t:

min⁡x∥Ax−b∥2,∥x∥2≤t.\min_x \lVert Ax - b \rVert^2, \qquad \lVert x \rVert^2 \le t.

The problem is convex, and for t>0t > 0 the point x=0x = 0 is strictly feasible. Slater's condition holds, so there is no gap.

The Lagrangian is the ridge loss minus a constant:

L(x,λ)=∥Ax−b∥2+λ∥x∥2−λt.\mathcal L(x, \lambda) = \lVert Ax - b \rVert^2 + \lambda \lVert x \rVert^2 - \lambda t.

Setting its gradient (see Matrix calculus) 2A⊤(Ax−b)+2λx2A^\top(Ax - b) + 2\lambda x to zero gives the ridge regression solution:

xλ=(A⊤A+λI)−1A⊤b,g(λ)=∥Axλ−b∥2+λ(∥xλ∥2−t).x_\lambda = (A^\top A + \lambda I)^{-1} A^\top b, \qquad g(\lambda) = \lVert A x_\lambda - b \rVert^2 + \lambda\big(\lVert x_\lambda \rVert^2 - t\big).

The dual is a search over one number λ≥0\lambda \ge 0. Its slope is simple. Differentiate g(λ)=L(xλ,λ)g(\lambda) = \mathcal L(x_\lambda, \lambda) by the chain rule:

g′(λ)=∇xL(xλ,λ)⊤dxλdλ+∂L∂λ(xλ,λ)=0+∥xλ∥2−t.\begin{aligned} g'(\lambda) &= \nabla_x \mathcal L(x_\lambda, \lambda)^\top \frac{d x_\lambda}{d\lambda} + \frac{\partial \mathcal L}{\partial \lambda}(x_\lambda, \lambda) \\ &= 0 + \lVert x_\lambda \rVert^2 - t. \end{aligned}

The first term vanishes because xλx_\lambda minimizes L\mathcal L, so its own change does not count. While xλx_\lambda is too long, increase λ\lambda.

Complementary slackness splits the cases. If the plain least-squares solution fits in the ball, then λ∗=0\lambda^* = 0. Otherwise λ∗>0\lambda^* > 0 and the solution sits on the sphere, ∥xλ∗∥2=t\lVert x_{\lambda^*} \rVert^2 = t.

Example: A=(100111)A = \begin{pmatrix} 1 & 0 \\ 0 & 1 \\ 1 & 1 \end{pmatrix}, b=(1,2,2)b = (1, 2, 2), t=1t = 1. Plain least squares gives (2/3,5/3)(2/3, 5/3), with squared norm 3.22>13.22 > 1 and residual 1/31/3. The constraint binds. The dual function climbs and falls:

λ\lambda 00 0.50.5 11 22 44
g(λ)g(\lambda) 0.3330.333 1.1671.167 1.6251.625 1.9331.933 1.4001.400

The peak is at λ∗≈2.085\lambda^* \approx 2.085, with xλ∗≈(0.526,0.850)x_{\lambda^*} \approx (0.526, 0.850) of squared norm 11 and d∗≈1.9348d^* \approx 1.9348. A constrained solver run on the primal finds the same 1.93481.9348.

So "ridge with penalty λ\lambda" and "least squares in a ball of radius t\sqrt t" are one family of solutions in two parametrizations. The penalty λ\lambda is the price of the radius.

The SVM dual

The most famous dual in ML belongs to the support vector machine. Points xi∈Rnx_i \in \mathbb R^n have labels yi∈{−1,+1}y_i \in \{-1, +1\}, and the hard-margin SVM looks for the widest strip that puts every point on its own side with room to spare:

min⁡w,b 12∥w∥2,yi(w⊤xi+b)≥1, i=1,…,N.\min_{w, b}\ \tfrac12 \lVert w \rVert^2, \qquad y_i (w^\top x_i + b) \ge 1,\ i = 1, \dots, N.

Here bb is the offset of the hyperplane, and the margin is 1/∥w∥1/\lVert w \rVert. With multipliers αi≥0\alpha_i \ge 0, minimizing the Lagrangian over ww gives w=∑iαiyixiw = \sum_i \alpha_i y_i x_i. Minimizing over bb forces ∑iαiyi=0\sum_i \alpha_i y_i = 0, or else the Lagrangian is linear in bb and g=−∞g = -\infty. What remains is the dual, a maximization over α≥0\alpha \ge 0 with one linear constraint:

max⁡α≥0 ∑iαi−12∑i,kαiαkyiyk xi⊤xk,∑iαiyi=0.\max_{\alpha \ge 0}\ \sum_i \alpha_i - \frac12 \sum_{i, k} \alpha_i \alpha_k y_i y_k\, x_i^\top x_k, \qquad \sum_i \alpha_i y_i = 0.

The dual reveals three things.

  • Inner products only. The data enter through xi⊤xkx_i^\top x_k. Replace them with a kernel k(xi,xk)k(x_i, x_k) and the same solver fits a nonlinear boundary.
  • Support vectors. By complementary slackness αi>0\alpha_i > 0 only for points exactly on the margin. Only these support vectors enter w∗w^*.
  • Geometry. The optimal strip is the perpendicular bisector of the shortest segment between the convex hulls of the two classes. With δ\delta the length of that segment, the margin is δ/2\delta/2 and d∗=2/δ2d^* = 2/\delta^2.

The geometry takes a few lines. The constraint ∑iαiyi=0\sum_i \alpha_i y_i = 0 gives both classes the same total weight ss. Write αi=sβi\alpha_i = s \beta_i, with β\beta summing to 11 on each class. Then ∑iαi=2s\sum_i \alpha_i = 2s and ∑iαiyixi=s(c+−c−)\sum_i \alpha_i y_i x_i = s(c_+ - c_-), where c±=∑yi=±1βixic_\pm = \sum_{y_i = \pm 1} \beta_i x_i are points of the two convex hulls. The dual objective becomes a parabola in ss,

2s−12s2∥c+−c−∥2,2s - \tfrac12 s^2 \lVert c_+ - c_- \rVert^2,

with maximum 2/∥c+−c−∥22/\lVert c_+ - c_- \rVert^2 at s=2/∥c+−c−∥2s = 2/\lVert c_+ - c_- \rVert^2. That maximum is largest when c+c_+ and c−c_- are as close as possible, so d∗=2/δ2d^* = 2/\delta^2. The vector w∗=s(c+−c−)w^* = s(c_+ - c_-) points along the shortest segment, and ∥w∗∥=sδ=2/δ\lVert w^* \rVert = s\delta = 2/\delta, so the margin 1/∥w∗∥1/\lVert w^* \rVert is δ/2\delta/2.

Example: negatives A=(0,0)A = (0, 0) and B=(2,0)B = (2, 0), positives C=(1,2)C = (1, 2) and D=(3,3)D = (3, 3). The nearest hull points are (1,0)(1, 0), the middle of ABAB, and CC, so δ=2\delta = 2. The dual solution is αA=αB=14\alpha_A = \alpha_B = \tfrac14, αC=12\alpha_C = \tfrac12, αD=0\alpha_D = 0. It gives w∗=(0,1)w^* = (0, 1), b∗=−1b^* = -1, margin 11 and d∗=∑iαi−12∥w∗∥2=12=p∗d^* = \sum_i \alpha_i - \tfrac12 \lVert w^* \rVert^2 = \tfrac12 = p^*.

The price is one dual variable per training point and an N×NN \times N 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 xx and any multipliers with λ≥0\lambda \ge 0,

g(λ,ν)≤d∗≤p∗≤f(x).g(\lambda, \nu) \le d^* \le p^* \le f(x).

So the computable number f(x)−g(λ,ν)f(x) - g(\lambda, \nu) bounds how far xx is from optimal, and also how far the multipliers are from the dual optimum. We do not need p∗p^* to know it. A solver that stops once this gap is below ε\varepsilon is guaranteed to be within ε\varepsilon. The coordinate-descent Lasso in scikit-learn checks exactly such a gap.

The SVM from the first block is a soft-margin one with C=1C = 1, with the offset folded into ww through a constant feature. Write ziz_i for xix_i with the constant 11 appended. The primal and dual objectives are

P(w)=12∥w∥2+C∑imax⁡(0, 1−yiw⊤zi),D(α)=∑iαi−12∥∑iαiyizi∥2,P(w) = \tfrac12 \lVert w \rVert^2 + C \sum_i \max\big(0,\ 1 - y_i w^\top z_i\big), \qquad D(\alpha) = \sum_i \alpha_i - \tfrac12 \Big\lVert \sum_i \alpha_i y_i z_i \Big\rVert^2,

and the dual keeps the multipliers in a box, 0≤αi≤C0 \le \alpha_i \le C. Projected gradient ascent on the dual gives a dual-feasible α\alpha at every step and a primal candidate w(α)=∑iαiyiziw(\alpha) = \sum_i \alpha_i y_i z_i. We track both objectives and their gap:

iteration P(w(α))P(w(\alpha)) D(α)D(\alpha) gap true error P−p∗P - p^*
1 12.51612.516 0.1340.134 12.3812.38 8.158.15
10 8.4558.455 0.4970.497 7.967.96 4.094.09
1000 4.3744.374 4.3474.347 0.0270.027 0.00690.0069
5000 4.3672164.367216 4.3672164.367216 3.6⋅10−153.6 \cdot 10^{-15} 00

The gap is pessimistic early on and tight at the end. The rule "stop when the gap is below 10−310^{-3}" fires at iteration 2096, where the true error is 9.9⋅10−49.9 \cdot 10^{-4}. So 4.36724.3672 was the minimum after all. Seven of the 40 points end up as support vectors, six of them at the bound αi=C\alpha_i = C. 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 00: it stalls at 3.6⋅10−153.6 \cdot 10^{-15} 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 A⊤A+λIA^\top A + \lambda I, 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.

Full notes

Answers are saved on the server and tied to this browser until you sign in.