Course contents 14 / 16

14 / 16 · Optimization

Duality

Lower bounds from Lagrange multipliers: weak and strong duality, Slater's condition, the duals of a linear program, of norm-constrained least squares and of the SVM, and the duality gap as a stopping rule.

On this page 9

A soft-margin SVM on 40 points is trained by an iterative solver. After 100 iterations its objective is 13.47813.478, after 1,000 iterations 13.23113.231, after 5,000 iterations 13.222613.2226. Is that the minimum? The values keep decreasing, and the gradient tests of the unconstrained optimization note do not apply, because the hinge loss has kinks. The optimal value p∗p^* is exactly what is unknown.

An upper bound on p∗p^* is easy: any feasible point gives one. What is missing is a lower bound, a number that p∗p^* provably cannot go below. With both, the optimum is bracketed and the solver can stop with a guarantee. The KKT conditions of the previous note already contained one: on the feasible set, adding the constraints with non-negative multipliers can only lower the objective. Every choice of multipliers gives such a bound, and the best of them is often the optimum itself.

The Lagrange dual function#

The primal problem is the one of the previous note: minimize f(x)f(x) subject to gi(x)≤0g_i(x) \le 0 for i=1,…,mi = 1, \dots, m and hj(x)=0h_j(x) = 0 for j=1,…,pj = 1, \dots, p. Its optimal value is p∗p^*, which is +∞+\infty if no point is feasible and −∞-\infty if the objective is unbounded below. The problem is convex if ff and the gig_i are convex and the hjh_j affine. With multipliers λ∈Rm\lambda \in \mathbb R^m and ν∈Rp\nu \in \mathbb R^p the Lagrangian is

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) .

The dual function is its minimum over xx, for fixed multipliers:

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

The infimum is over all xx, feasible or not, so computing gg is an unconstrained problem, and the result may be −∞-\infty. For each fixed xx the Lagrangian is an affine function of (λ,ν)(\lambda, \nu), and a pointwise infimum of affine functions is concave, by the pointwise maximum rule applied to −g-g. So the dual function is concave whatever ff, gig_i and hjh_j are, convex or not.

Weak duality#

Take any feasible x~\tilde x and any λ≥0\lambda \ge 0, ν\nu. Inequalities between vectors, such as λ≥0\lambda \ge 0, hold entry by entry. Then

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

The first line holds because an infimum is at most any of its values, the last because λi≥0\lambda_i \ge 0, gi(x~)≤0g_i(\tilde x) \le 0 and hj(x~)=0h_j(\tilde x) = 0. The bound holds for every feasible x~\tilde x, hence for the infimum of ff over them, which is p∗p^*. Nothing in the argument uses convexity.

The dual problem asks for the best lower bound:

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

It maximizes a concave function subject to λ≥0\lambda \ge 0, so it is a convex problem even when the primal is not. The difference p∗−d∗≥0p^* - d^* \ge 0 is the duality gap.

Take the problem: minimize x2x^2 subject to x≥1x \ge 1, written as g1(x)=1−x≤0g_1(x) = 1 - x \le 0. On the feasible set x≥1x \ge 1 the objective x2x^2 increases, so p∗=1p^* = 1 at x∗=1x^* = 1. The Lagrangian x2+λ(1−x)x^2 + \lambda(1 - x) is a parabola in xx with its minimum at x=λ/2x = \lambda/2, so

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

At λ=0,1,2,3,4\lambda = 0, 1, 2, 3, 4 it takes the values 00, 0.750.75, 11, 0.750.75 and 00. Every one is a lower bound on p∗=1p^* = 1, and the best one, at λ∗=2\lambda^* = 2, equals p∗p^*. The minimizer of the Lagrangian at λ∗=2\lambda^* = 2 is x=1=x∗x = 1 = x^*, and 2x∗−λ∗=02x^* - \lambda^* = 0 is the stationarity condition of KKT.

Figure

Weak duality: every multiplier gives a lower bound

Lagrangians below the objective and the dual function below the optimumLeft, the parabola x², a shaded band x ≥ 1 and three Lagrangian parabolas whose minima are marked by dots at heights 0.75, 1 and 0.75, none above the dashed line p* = 1. Right, an upside-down parabola g(λ) that touches the dashed line p* = 1 at λ = 2.primal: x² over x ≥ 112123xf(x) = x²L(x, λ)x ≥ 1p* = 1dual function g(λ)1234-11λg(λ) = λ − λ²/4p* = 1d* = p* at λ = 2Lagrangians below the objective and the dual function below the optimumLeft, the parabola x², a shaded band x ≥ 1 and three Lagrangian parabolas whose minima are marked by dots at heights 0.75, 1 and 0.75, none above the dashed line p* = 1. Right, an upside-down parabola g(λ) that touches the dashed line p* = 1 at λ = 2.primal: x² over x ≥ 112123xf(x) = x²L(x, λ)x ≥ 1p* = 1dual function g(λ)1234-11λg(λ) = λ − λ²/4p* = 1d* = p* at λ = 2
Minimize x² subject to x ≥ 1, with p* = 1. Left, x² (blue), the feasible region (green) and the Lagrangians x² + λ(1 − x) for λ = 1, 2, 3 (amber), whose minima (dots) are 0.75, 1 and 0.75. Right, the dual function g(λ) = λ − λ²/4, below p* everywhere and touching it at λ = 2.

The two problems are the same function optimized in opposite orders (Boyd and Vandenberghe, 2004, §5.4.1; Deisenroth, Faisal and Ong, 2020, Section 7.2). For a fixed xx, the supremum of L\mathcal L over λ≥0\lambda \ge 0 and ν\nu is f(x)f(x) if xx is feasible and +∞+\infty if some constraint is violated, since its multiplier can then grow without limit. So

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

Weak duality is the max–min inequality, which holds for any function ϕ(x,y)\phi(x, y) of two arguments. For any x′x' and y′y',

inf⁡xϕ(x,y′)≤ϕ(x′,y′)≤sup⁡yϕ(x′,y).\inf_x \phi(x, y') \le \phi(x', y') \le \sup_y \phi(x', y) .

The left end does not depend on x′x' and the right end does not depend on y′y', so taking the supremum over y′y' on the left and the infimum over x′x' on the right keeps the inequality: sup⁡yinf⁡xϕ≤inf⁡xsup⁡yϕ\sup_y \inf_x \phi \le \inf_x \sup_y \phi. The player who moves second does at least as well. Strong duality says that the order does not matter; the section below shows that a primal and a dual solution then form a saddle point of L\mathcal L.

Strong duality and Slater’s condition#

When d∗=p∗d^* = p^*, strong duality holds. It can fail, even for convex problems, as an example below shows. The simplest sufficient condition is Slater’s condition: the problem is convex and some point xˉ\bar x is strictly feasible, with gi(xˉ)<0g_i(\bar x) \lt 0 for every ii and hj(xˉ)=0h_j(\bar x) = 0 for every jj. Under it, d∗=p∗d^* = p^*, and the dual optimum is attained whenever p∗p^* is finite (Boyd and Vandenberghe, 2004, §5.2.3). The folded proof below covers one inequality; the general proof is in §5.3.2.

Inequalities that are affine need not hold strictly: a feasible point is enough for them. In particular every feasible problem with a convex objective defined on all of Rn\mathbb R^n and only affine constraints, such as least squares with linear constraints or the SVM, has strong duality (§5.2.3, the refined Slater condition).

Proof sketch, for one inequality constraint

Assume p∗p^* is finite. Consider the set of pairs that the problem can achieve, A={(u,t):g1(x)≤u and f(x)≤t for some x}\mathcal A = \{(u, t) : g_1(x) \le u \text{ and } f(x) \le t \text{ for some } x\}. It contains everything above and to the right of each of its points. It is convex: if xx achieves (u,t)(u, t) and x′x' achieves (u′,t′)(u', t'), then by convexity of g1g_1 and ff the point θx+(1−θ)x′\theta x + (1 - \theta)x' achieves θ(u,t)+(1−θ)(u′,t′)\theta(u, t) + (1 - \theta)(u', t'). The point (0,p∗)(0, p^*) lies on its boundary: points of A\mathcal A come arbitrarily close to it, but (0,p∗−ε)(0, p^* - \varepsilon) is not in A\mathcal A for any ε>0\varepsilon \gt 0, since that would need a feasible xx with f(x)<p∗f(x) \lt p^*.

A nonempty convex set has a supporting line at each boundary point, by the supporting hyperplane theorem (Boyd and Vandenberghe, 2004, §2.5.2): there are (λ,μ)≠(0,0)(\lambda, \mu) \ne (0, 0) with λu+μt≥μp∗\lambda u + \mu t \ge \mu p^* for every (u,t)∈A(u, t) \in \mathcal A. If λ\lambda were negative, letting uu grow along A\mathcal A would break the inequality, and the same holds for μ\mu and tt; so λ≥0\lambda \ge 0 and μ≥0\mu \ge 0. If μ=0\mu = 0, then λ>0\lambda \gt 0 and λu≥0\lambda u \ge 0 on A\mathcal A; but the Slater point gives a pair with u=g1(xˉ)<0u = g_1(\bar x) \lt 0, a contradiction.

So μ>0\mu \gt 0. Every xx achieves the pair (g1(x),f(x))(g_1(x), f(x)), and dividing by μ\mu gives f(x)+(λ/μ)g1(x)≥p∗f(x) + (\lambda/\mu) g_1(x) \ge p^* for every xx, that is g(λ/μ)≥p∗g(\lambda/\mu) \ge p^*. With weak duality, d∗=p∗d^* = p^*, attained at λ/μ\lambda/\mu. The full proof, with equality constraints, is in Boyd and Vandenberghe (2004), §5.3.2.

Without a Slater point, even a convex problem can have a gap. Minimize e−xe^{-x} subject to x2/y≤0x^2/y \le 0, over y>0y \gt 0. Only x=0x = 0 is feasible, so p∗=1p^* = 1. No point satisfies the constraint strictly, since x2/yx^2/y is never negative. The dual function is

g(λ)=inf⁡x, y>0(e−x+λx2y)=0g(\lambda) = \inf_{x,\, y \gt 0}\Big(e^{-x} + \frac{\lambda x^2}{y}\Big) = 0

for every λ≥0\lambda \ge 0. Both terms are non-negative, so the infimum is at least 0. Along y=x3y = x^3 the Lagrangian is e−x+λ/xe^{-x} + \lambda/x, which for λ=1\lambda = 1 takes the values 1.3681.368, 0.2070.207, 0.0500.050 and 0.0100.010 at x=1,5,20,100x = 1, 5, 20, 100 and tends to 0. So d∗=0<1=p∗d^* = 0 \lt 1 = p^* (Boyd and Vandenberghe, 2004, Exercise 5.21).

Strong duality forces the KKT conditions. If x∗x^* is primal optimal, (λ∗,ν∗)(\lambda^*, \nu^*) dual optimal and d∗=p∗d^* = p^*, then

f(x∗)=g(λ∗,ν∗)≤L(x∗,λ∗,ν∗)=f(x∗)+∑iλi∗gi(x∗)+∑jνj∗hj(x∗)≤f(x∗),\begin{aligned} f(x^*) &= g(\lambda^*, \nu^*) \\ &\le \mathcal L(x^*, \lambda^*, \nu^*) \\ &= f(x^*) + \textstyle\sum_i \lambda_i^* g_i(x^*) + \sum_j \nu_j^* h_j(x^*) \\ &\le f(x^*) , \end{aligned}

so both inequalities are equalities. The first says that x∗x^* minimizes L(⋅,λ∗,ν∗)\mathcal L(\cdot, \lambda^*, \nu^*) over all xx; for differentiable functions the gradient at an unconstrained minimum is zero, which gives stationarity. The second says that ∑iλi∗gi(x∗)=0\sum_i \lambda_i^* g_i(x^*) = 0, a sum of non-positive terms, so every term is zero: complementary slackness. The same chain shows the saddle point: for every xx and every λ≥0\lambda \ge 0, ν\nu,

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

The left inequality is weak duality’s last step at the feasible x∗x^*; the right one holds because x∗x^* minimizes L(⋅,λ∗,ν∗)\mathcal L(\cdot, \lambda^*, \nu^*). This is how the SVM note derives its KKT conditions.

The dual of a linear program#

For a linear objective the Lagrangian is linear in xx, and its infimum is finite only when the coefficient of xx vanishes. That condition becomes the dual’s constraint. Take the linear program

min⁡x c⊤xsubject toAx≥b, x≥0.\min_x\ c^\top x \quad \text{subject to} \quad Ax \ge b,\ x \ge 0 .

Give the constraints b−Ax≤0b - Ax \le 0 the multipliers y≥0y \ge 0 and the constraints −x≤0-x \le 0 the multipliers s≥0s \ge 0, and collect the terms in xx:

L(x,y,s)=c⊤x+y⊤(b−Ax)−s⊤x=b⊤y+(c−A⊤y−s)⊤x.\begin{aligned} \mathcal L(x, y, s) &= c^\top x + y^\top(b - Ax) - s^\top x \\ &= b^\top y + (c - A^\top y - s)^\top x . \end{aligned}

A linear function d⊤xd^\top x is bounded below only if d=0d = 0: otherwise x=−sdx = -sd gives −s∥d∥2→−∞-s\lVert d\rVert^2 \to -\infty. So g(y,s)=b⊤yg(y, s) = b^\top y when c−A⊤y−s=0c - A^\top y - s = 0 and −∞-\infty otherwise. The condition s=c−A⊤y≥0s = c - A^\top y \ge 0 is the dual constraint, and the dual is again a linear program:

max⁡y b⊤ysubject toA⊤y≤c, y≥0.\max_y\ b^\top y \quad \text{subject to} \quad A^\top y \le c,\ y \ge 0 .

The constraints of an LP are affine, so by the refined Slater condition above strong duality holds whenever the primal is feasible.

For example, minimize 3x1+2x23x_1 + 2x_2 subject to x1+x2≥2x_1 + x_2 \ge 2, x1+3x2≥3x_1 + 3x_2 \ge 3 and x≥0x \ge 0. The feasible set is a polyhedron with vertices (0,2)(0, 2), (1.5,0.5)(1.5, 0.5) and (3,0)(3, 0), with costs 4, 5.5 and 9. The cheapest vertex is x∗=(0,2)x^* = (0, 2); the dual will certify that p∗=4p^* = 4. The dual is: maximize 2y1+3y22y_1 + 3y_2 subject to y1+y2≤3y_1 + y_2 \le 3, y1+3y2≤2y_1 + 3y_2 \le 2 and y≥0y \ge 0. The point y∗=(2,0)y^* = (2, 0) is feasible with value 4. Every dual value is at most p∗p^* and every primal value at least d∗d^*, so the two points certify each other: both are optimal, with no search. Complementary slackness holds pair by pair:

ConstraintSlack at the optimumPaired variable
primal x1+x2≥2x_1 + x_2 \ge 200 (tight)y1∗=2y_1^* = 2
primal x1+3x2≥3x_1 + 3x_2 \ge 333y2∗=0y_2^* = 0
dual y1+y2≤3y_1 + y_2 \le 311x1∗=0x_1^* = 0
dual y1+3y2≤2y_1 + 3y_2 \le 200 (tight)x2∗=2x_2^* = 2

Every row has a zero in it. Read yy as prices of the requirements bb: raising the first requirement from 2 to 2+u2 + u, for small uu, raises the optimal cost by 2u2u, the multiplier. The same pair certifies it: x=(0,2+u)x = (0, 2 + u) is feasible with cost 4+2u4 + 2u, and y∗=(2,0)y^* = (2, 0) stays dual feasible with value 2(2+u)2(2 + u).

Least squares under a norm constraint#

The constraint ∥x∥2≤t\lVert x\rVert^2 \le t enters the Lagrangian as a penalty λ∥x∥2\lambda\lVert x\rVert^2, so minimizing the Lagrangian is ridge regression. Take

min⁡x ∥Ax−b∥2subject to∥x∥2≤t,\min_x\ \lVert Ax - b\rVert^2 \quad \text{subject to} \quad \lVert x\rVert^2 \le t ,

with t>0t \gt 0 and one multiplier λ≥0\lambda \ge 0. The Lagrangian is convex in xx, and setting its gradient to zero gives the ridge solution:

L(x,λ)=∥Ax−b∥2+λ(∥x∥2−t),∇xL=2A⊤(Ax−b)+2λx=0,xλ=(A⊤A+λI)−1A⊤b.\begin{aligned} \mathcal L(x, \lambda) &= \lVert Ax - b\rVert^2 + \lambda\big(\lVert x\rVert^2 - t\big) , \\ \nabla_x\mathcal L &= 2A^\top(Ax - b) + 2\lambda x = 0 , \\ x_\lambda &= (A^\top A + \lambda I)^{-1}A^\top b . \end{aligned}

The dual function is g(λ)=L(xλ,λ)g(\lambda) = \mathcal L(x_\lambda, \lambda), and its derivative is simple:

g′(λ)=∥xλ∥2−t.g'(\lambda) = \lVert x_\lambda\rVert^2 - t .

The chain rule through xλx_\lambda, which is differentiable in λ\lambda as a product of a matrix inverse and a fixed vector, gives it:

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

because ∇xL=0\nabla_x\mathcal L = 0 at xλx_\lambda. This is the envelope theorem: only the explicit λ\lambda counts. The point x=0x = 0 is strictly feasible, so Slater holds and d∗=p∗d^* = p^*. The dual function is concave, so g′g' never increases. If the least-squares solution already has ∥x0∥2≤t\lVert x_0\rVert^2 \le t, then g′(0)≤0g'(0) \le 0 and λ∗=0\lambda^* = 0. Otherwise g′(0)>0g'(0) \gt 0, while g′(λ)→−tg'(\lambda) \to -t as λ→∞\lambda \to \infty because xλ→0x_\lambda \to 0. So g′(λ∗)=0g'(\lambda^*) = 0 somewhere, which says ∥xλ∗∥2=t\lVert x_{\lambda^*}\rVert^2 = t: complementary slackness. The dual picks the penalty whose ridge solution exactly meets the constraint.

This is the duality that the regularization note invokes: the constrained and the penalized forms of ridge regression trace the same family of solutions, with λ\lambda the multiplier of the radius tt.

Take AA with rows (1,0)(1, 0), (0,1)(0, 1) and (1,1)(1, 1), b=(1,2,2)b = (1, 2, 2) and t=1t = 1. The least-squares solution (0.667,1.667)(0.667, 1.667) has squared norm 3.22>13.22 \gt 1, so the constraint binds. The dual function gives a lower bound for every λ\lambda:

λ\lambda00.5122.0854
g(λ)g(\lambda)0.3331.1671.6251.9331.93481.400

The first value is the unconstrained minimum. The maximum is at λ∗=2.085\lambda^* = 2.085, where xλ∗=(0.526,0.850)x_{\lambda^*} = (0.526, 0.850) has squared norm exactly 1. A constrained solver applied to the primal finds the same point and the same value, 1.93481.9348.

The SVM dual: nearest points of two convex hulls#

The SVM note derives the dual of the soft-margin SVM step by step, by the scheme above, and reads off the support vectors from it. Duality adds a geometric reading of the hard-margin dual (Bennett and Bredensteiner, 2000). Start from

maximize∑iαi−12∥∑iαiyixi∥2subject toα≥0,∑iαiyi=0.\begin{aligned} \text{maximize}\quad & \sum_i \alpha_i - \frac12\Big\lVert\sum_i \alpha_i y_i x_i\Big\rVert^2 \\ \text{subject to}\quad & \alpha \ge 0, \quad \sum_i \alpha_i y_i = 0 . \end{aligned}

The equality constraint gives the two classes the same total weight s=∑yi=+1αi=∑yi=−1αis = \sum_{y_i = +1}\alpha_i = \sum_{y_i = -1}\alpha_i. Write αi=s βi\alpha_i = s\,\beta_i, so that β\beta sums to 1 on each class. Then ∑iαi=2s\sum_i \alpha_i = 2s, and

∑iαiyixi=s (c+−c−),c±=∑yi=±1βixi,\begin{aligned} \sum_i \alpha_i y_i x_i &= s\,(c_+ - c_-) , \\ c_\pm &= \sum_{y_i = \pm1}\beta_i x_i , \end{aligned}

where c+c_+ and c−c_- are convex combinations of the points of each class: points of the two convex hulls. The dual objective becomes a parabola in ss:

D(s)=2s−12s2∥c+−c−∥2,max⁡s≥0D=2∥c+−c−∥2.\begin{aligned} D(s) &= 2s - \tfrac12 s^2\lVert c_+ - c_-\rVert^2 , \\ \max_{s \ge 0} D &= \frac{2}{\lVert c_+ - c_-\rVert^2} . \end{aligned}

The maximum is at s=2/∥c+−c−∥2s = 2/\lVert c_+ - c_-\rVert^2, where D′(s)=2−s∥c+−c−∥2=0D'(s) = 2 - s\lVert c_+ - c_-\rVert^2 = 0. This maximum decreases with the distance, so maximizing over the remaining weights β\beta is minimizing ∥c+−c−∥\lVert c_+ - c_-\rVert over the two hulls. With δ\delta the distance between the hulls, ∥w∗∥=s δ=2/δ\lVert w^*\rVert = s\,\delta = 2/\delta, and

d∗=2/δ2,w∗=s (c+−c−),γ=1/∥w∗∥=δ/2.\begin{aligned} d^* &= 2/\delta^2 , \\ w^* &= s\,(c_+ - c_-) , \\ \gamma &= 1/\lVert w^*\rVert = \delta/2 . \end{aligned}

The weight vector w∗w^* points along c+−c−c_+ - c_-, so the hyperplane is perpendicular to the shortest segment between the hulls. It also bisects it. The points with αi>0\alpha_i \gt 0 lie on the margin, w∗⊤xi+b=yiw^{*\top}x_i + b = y_i, by complementary slackness, and c±c_\pm are averages of such points, so w∗⊤c±+b=±1w^{*\top}c_\pm + b = \pm1. Adding the two gives w∗⊤12(c++c−)+b=0w^{*\top}\tfrac12(c_+ + c_-) + b = 0: the midpoint lies on the hyperplane. The margin is half the length of the segment. For the soft margin, the bound αi≤C\alpha_i \le C caps each βi\beta_i at C/sC/s, and the hulls shrink to “reduced” hulls, which can be separated even when the full hulls overlap (Bennett and Bredensteiner, 2000).

The four points of the SVM note make this concrete: 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 hulls are the segments ABAB and CDCD. Their nearest points are c−=(1,0)c_- = (1, 0), the midpoint of ABAB, and c+=C=(1,2)c_+ = C = (1, 2), at distance δ=2\delta = 2. Then s=12s = \frac12, so αC=12\alpha_C = \frac12, αA=αB=14\alpha_A = \alpha_B = \frac14 and αD=0\alpha_D = 0; w∗=12(0,2)=(0,1)w^* = \frac12(0, 2) = (0, 1), γ=1\gamma = 1 and d∗=12d^* = \frac12. These are the numbers the SVM note finds from the KKT conditions.

The duality gap as a stopping criterion#

A primal feasible point gives an upper bound on p∗p^*, a dual feasible point a lower bound, and the optimum lies between them. For any feasible xx and any λ≥0\lambda \ge 0, ν\nu,

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

So the computable difference f(x)−g(λ,ν)f(x) - g(\lambda, \nu) bounds both f(x)−p∗f(x) - p^* and d∗−g(λ,ν)d^* - g(\lambda, \nu): it certifies how far the current point is from optimal without knowing p∗p^* (Boyd and Vandenberghe, 2004, §5.5.1). A solver that stops when the gap is below ε\varepsilon returns a point within ε\varepsilon of the optimum. Scikit-learn’s coordinate-descent Lasso, for example, checks a duality gap against its tolerance (scikit-learn documentation). Interior-point methods produce such a pair at every step: the point on their central path with barrier weight tt comes with dual multipliers whose gap is exactly m/tm/t, for mm inequalities (Boyd and Vandenberghe, 2004, §11.2.2).

Derivation: the gap m/t on the central path

Take inequality constraints only. The central point x(t)x(t) minimizes tf(x)−∑ilog⁡(−gi(x))t f(x) - \sum_i \log(-g_i(x)) over the strictly feasible points, so its gradient vanishes:

t ∇f(x(t))+∑i∇gi(x(t))−gi(x(t))=0.t\,\nabla f(x(t)) + \sum_i \frac{\nabla g_i(x(t))}{-g_i(x(t))} = 0 .

Divide by tt and set λi=1/(−t gi(x(t)))>0\lambda_i = 1/(-t\,g_i(x(t))) \gt 0. Then ∇f+∑iλi∇gi=0\nabla f + \sum_i \lambda_i\nabla g_i = 0 at x(t)x(t), so for a convex problem x(t)x(t) minimizes the convex L(⋅,λ)\mathcal L(\cdot, \lambda), and

g(λ)=f(x(t))+∑iλigi(x(t))=f(x(t))−∑i1/t=f(x(t))−m/t.\begin{aligned} g(\lambda) &= f(x(t)) + \textstyle\sum_i \lambda_i g_i(x(t)) \\ &= f(x(t)) - \textstyle\sum_i 1/t = f(x(t)) - m/t . \end{aligned}

Take the SVM of the opening. Absorb the bias into the weights by appending a constant feature 1 to every point, as many linear SVM solvers do (Hsieh et al., 2008). The primal is then unconstrained,

P(w)=12∥w∥2+C∑imax⁡(0,1−yiw⊤zi),P(w) = \tfrac12\lVert w\rVert^2 + C\sum_i \max(0, 1 - y_i w^\top z_i) ,

with zi=(xi,1)z_i = (x_i, 1) and C=1C = 1. The derivation of the SVM note without bb gives the dual

D(α)=∑iαi−12∥∑iαiyizi∥2D(\alpha) = \sum_i \alpha_i - \tfrac12\Big\lVert\sum_i \alpha_i y_i z_i\Big\rVert^2

over the box 0≤αi≤C0 \le \alpha_i \le C, with no equality constraint. Projected gradient ascent alternates a gradient step on DD with clipping to the box. Every iterate α\alpha is dual feasible, w(α)=∑iαiyiziw(\alpha) = \sum_i \alpha_i y_i z_i is a primal point, and their gap bounds the error:

IterationP(w(α))P(w(\alpha))D(α)D(\alpha)gap P−DP - Dtrue error P−p∗P - p^*
119.6820.291196.5
1016.2601.744153.0
10013.47812.3931.10.26
1,00013.23113.2228.3⋅10−38.3 \cdot 10^{-3}8.2⋅10−38.2 \cdot 10^{-3}
5,00013.222613.22262⋅10−112 \cdot 10^{-11}2⋅10−112 \cdot 10^{-11}

Stopping at a gap below 10−310^{-3} ends the run at iteration 1,422, where the true error is 9.9⋅10−49.9 \cdot 10^{-4}, inside the guarantee. The gap is pessimistic early, when the dual iterate is poor, and tight near the end. At the solution 15 of the 40 points have αi>0\alpha_i \gt 0, the support vectors, and 12 of them sit at the bound CC. By complementary slackness those have positive slack: they lie inside the margin or are misclassified.

Where it breaks: a non-convex problem#

Weak duality holds for any problem, so the dual still gives a bound when the primal is hard; strong duality is what fails. Split nn items into two groups to minimize x⊤Wxx^\top W x over sign vectors x∈{−1,1}nx \in \{-1, 1\}^n, so that x⊤Wxx^\top W x adds WijW_{ij} when items ii and jj share a group and subtracts it when they do not. Written as xi2=1x_i^2 = 1, the constraints are equalities. The Lagrangian is

L(x,ν)=x⊤Wx+∑iνi(xi2−1)=x⊤(W+diag⁡(ν))x−1⊤ν,\begin{aligned} \mathcal L(x, \nu) &= x^\top W x + \sum_i \nu_i (x_i^2 - 1) \\ &= x^\top \big(W + \operatorname{diag}(\nu)\big)x - \mathbf 1^\top\nu , \end{aligned}

a quadratic form whose infimum over xx is 0 if the matrix is positive semidefinite and −∞-\infty otherwise: a direction vv with v⊤(W+diag⁡(ν))v<0v^\top(W + \operatorname{diag}(\nu))v \lt 0 gives values that fall without limit along x=svx = sv. So g(ν)=−1⊤νg(\nu) = -\mathbf 1^\top\nu when W+diag⁡(ν)⪰0W + \operatorname{diag}(\nu) \succeq 0. The choice νi=−λmin⁡(W)\nu_i = -\lambda_{\min}(W) for every ii is feasible and gives the bound p∗≥n λmin⁡(W)p^* \ge n\,\lambda_{\min}(W) (Boyd and Vandenberghe, 2004, §5.1.5; Merkulov, 2024, lecture 7).

For a random symmetric 8×88 \times 8 matrix WW, checking all 256 sign vectors gives p∗=−22.235p^* = -22.235. The eigenvalue bound is −28.855-28.855. The best multipliers, found by maximizing the concave dual function numerically, raise it to −22.834-22.834, so d∗≥−22.834d^* \ge -22.834. An upper bound on d∗d^* shows that the gap is real. For any positive semidefinite XX with unit diagonal and any dual feasible ν\nu, the trace of a product of two positive semidefinite matrices is non-negative, so

0≤tr⁡((W+diag⁡(ν))X)=tr⁡(WX)+1⊤ν,\begin{aligned} 0 &\le \operatorname{tr}\big((W + \operatorname{diag}(\nu))X\big) \\ &= \operatorname{tr}(WX) + \mathbf 1^\top\nu , \end{aligned}

that is g(ν)≤tr⁡(WX)g(\nu) \le \operatorname{tr}(WX). A matrix XX found numerically gives tr⁡(WX)=−22.833\operatorname{tr}(WX) = -22.833. So d∗d^* lies between −22.834-22.834 and −22.833-22.833, and the gap is 0.600.60.

Two smaller failures appear in practice. Recovering a primal solution from a dual one needs the minimizer of the Lagrangian to be unique. It fails for linear programs: at the dual optimum the coefficient of xx vanishes, the Lagrangian b⊤y∗b^\top y^* is constant in xx, and every xx minimizes it. And the dual can be larger than the primal: the SVM dual has one variable per training point and a dense n×nn \times n matrix, so for millions of points the primal is solved instead, by stochastic gradients.

What you should remember#

  • The dual function g=inf⁡xLg = \inf_x \mathcal L is concave, and g(λ,ν)≤p∗g(\lambda, \nu) \le p^* whenever λ≥0\lambda \ge 0.
  • p∗=inf⁡xsup⁡λLp^* = \inf_x \sup_\lambda \mathcal L and d∗=sup⁡λinf⁡xLd^* = \sup_\lambda \inf_x \mathcal L; weak duality is the max–min inequality.
  • Convex problem plus a strictly feasible point (Slater) gives d∗=p∗d^* = p^*, and with it the KKT conditions.
  • LP: the dual of min⁡{c⊤x:Ax≥b, x≥0}\min\{c^\top x : Ax \ge b,\ x \ge 0\} is max⁡{b⊤y:A⊤y≤c, y≥0}\max\{b^\top y : A^\top y \le c,\ y \ge 0\}.
  • The hard-margin SVM dual finds the nearest points of the two hulls; the margin is half their distance.
  • f(x)−g(λ,ν)f(x) - g(\lambda, \nu) bounds the suboptimality of both points: a safe stopping rule.

Code#

The bound of the figure, a convex problem with a duality gap, an LP and its dual, norm-constrained least squares, the SVM dual as nearest points, an SVM solver stopped by its duality gap, and the two-way partitioning bound, with NumPy and SciPy. It runs in a few seconds. Exact digits can differ slightly across platforms and library versions.

import numpy as np
import itertools
from scipy.optimize import brentq, linprog, minimize

# min x^2 s.t. 1 - x <= 0: the dual function g(lam) = lam - lam^2/4 lies below p* = 1 and touches it at lam = 2
lam = np.linspace(0, 5, 501); g = lam - lam**2 / 4
print(g.max(), lam[g.argmax()], (g <= 1 + 1e-12).all())
# 1.0 2.0 True

# a convex problem without a Slater point: min e^{-x} s.t. x^2 / y <= 0 (y > 0); p* = 1 but g(lam) = 0
for x in (1., 5., 20., 100.):                              # along y = x^3 the Lagrangian tends to 0 for every lam
    print(x, f'{np.exp(-x) + 1.0 * x**2 / x**3:.4f}', end=' | ')
print()
# 1.0 1.3679 | 5.0 0.2067 | 20.0 0.0500 | 100.0 0.0100

# an LP and its dual: min 3x1 + 2x2 s.t. x1 + x2 >= 2, x1 + 3x2 >= 3, x >= 0;  max 2y1 + 3y2 s.t. y1 + y2 <= 3, y1 + 3y2 <= 2, y >= 0
A = np.array([[1., 1], [1, 3]]); b = np.array([2., 3]); c = np.array([3., 2])
pr = linprog(c, A_ub=-A, b_ub=-b, bounds=[(0, None)] * 2)
du = linprog(-b, A_ub=A.T, b_ub=c, bounds=[(0, None)] * 2)
print(pr.x, pr.fun, du.x, -du.fun, A @ pr.x - b, c - A.T @ du.x)   # slacks: primal constraints, dual constraints
# [0. 2.] 4.0 [2. 0.] 4.0 [0. 3.] [1. 0.]

# least squares under a norm constraint: min |Ax - b|^2 s.t. |x|^2 <= t; the dual is a function of one multiplier
A = np.array([[1., 0], [0, 1], [1, 1]]); b = np.array([1., 2, 2]); t = 1.0
x_lam = lambda l: np.linalg.solve(A.T @ A + l * np.eye(2), A.T @ b)          # the minimizer of the Lagrangian: ridge
gd = lambda l: np.sum((A @ x_lam(l) - b) ** 2) + l * (x_lam(l) @ x_lam(l) - t)
ls = x_lam(0); print(ls.round(4), round(ls @ ls, 4), round(gd(0), 4))      # unconstrained solution is infeasible
lam_s = brentq(lambda l: x_lam(l) @ x_lam(l) - t, 0, 100); xs = x_lam(lam_s)
r = minimize(lambda x: np.sum((A @ x - b) ** 2), np.zeros(2), method='SLSQP',
             constraints=[{'type': 'ineq', 'fun': lambda x: t - x @ x}], options={'ftol': 1e-14})
print(round(lam_s, 4), xs.round(4), round(gd(lam_s), 6), round(r.fun, 6), [round(float(gd(l)), 4) for l in (0.5, 1, 2, 4)])
# [0.6667 1.6667] 3.2222 0.3333
# 2.0851 [0.5262 0.8504] 1.934833 1.934833 [1.1667, 1.625, 1.9333, 1.4]

# the hard-margin SVM of the ML note: the dual optimum is the nearest pair of points of the two convex hulls
P = np.array([[1., 2], [3, 3]]); Nn = np.array([[0., 0], [2, 0]])         # positive points C, D; negative points A, B
best = min(((np.linalg.norm(u @ P - v @ Nn), u, v) for s in np.linspace(0, 1, 401) for q in np.linspace(0, 1, 401)
            for u, v in [(np.array([s, 1 - s]), np.array([q, 1 - q]))]), key=lambda z: z[0])
D, u, v = best
print(round(D, 4), (u @ P).round(3), (v @ Nn).round(3), round(D / 2, 4), (2 / D**2 * (u @ P - v @ Nn)).round(4), round(2 / D**2, 4))
# 2.0 [1. 2.] [1. 0.] 1.0 [0. 1.] 0.5: distance, nearest points, margin, w = s (c+ - c-), dual optimum 2/D^2

# a soft-margin SVM (bias absorbed into w) solved by projected gradient ascent on its box-constrained dual;
# the duality gap P(w(alpha)) - D(alpha) bounds the suboptimality of every iterate
rng = np.random.default_rng(0)
n, C = 40, 1.0
y = np.repeat([1., -1.], n // 2)
X = rng.normal(size=(n, 2)) + y[:, None]                     # two overlapping clouds around (1, 1) and (-1, -1)
Z = np.column_stack([X, np.ones(n)]) * y[:, None]            # rows y_i (x_i, 1)
Q = Z @ Z.T
Pobj = lambda w: 0.5 * w @ w + C * np.maximum(0, 1 - Z @ w).sum()
Dobj = lambda a: a.sum() - 0.5 * a @ Q @ a
eta = 1 / np.linalg.eigvalsh(Q)[-1]
a, hist = np.zeros(n), []
for k in range(1, 20_001):
    a = np.clip(a + eta * (1 - Q @ a), 0, C)                 # gradient step, then projection onto the box [0, C]
    hist.append((Pobj(Z.T @ a), Dobj(a)))
p_star = hist[-1][0]
print(round(p_star, 4), f'{hist[-1][0] - hist[-1][1]:.0e}', (a > 1e-8).sum(), (a > C - 1e-8).sum())
for k in (1, 10, 100, 1000, 5000):
    pk, dk = hist[k - 1]
    print(k, round(pk, 4), round(dk, 4), f'{pk - dk:.1e}', f'{pk - p_star:.1e}', end=' | ')
print()
stop = next(k for k, (pk, dk) in enumerate(hist, 1) if pk - dk < 1e-3)
print(stop, f'{hist[stop - 1][0] - p_star:.1e}', all(pk - p_star <= pk - dk + 1e-12 for pk, dk in hist))
# 13.2226 4e-15 15 12
# 1 19.6823 0.291 1.9e+01 6.5e+00 | 10 16.2603 1.7443 1.5e+01 3.0e+00 | 100 13.4784 12.3925 1.1e+00 2.6e-01 | 1000 13.2308 13.2224 8.3e-03 8.2e-03 | 5000 13.2226 13.2226 2.1e-11 2.1e-11
# 1422 9.9e-04 True

# a non-convex problem: min x'Wx over x in {-1, 1}^8; the dual is g(nu) = -1'nu where W + diag(nu) is PSD
rng = np.random.default_rng(0)
n = 8
W = rng.normal(size=(n, n)); W = (W + W.T) / 2
p_star = min(np.array(x) @ W @ np.array(x) for x in itertools.product([-1, 1], repeat=n))
def dual(nu):                                                # shift nu to feasibility; value and a supergradient
    lam, V = np.linalg.eigh(W + np.diag(nu))
    return -nu.sum() + n * lam[0], -1 + n * V[:, 0] ** 2
nu, best = np.zeros(n), -np.inf
for k in range(1, 100_001):                                  # supergradient ascent on the concave dual
    val, g = dual(nu)
    best = max(best, val)
    nu = nu + 0.5 / np.sqrt(k) * g
print(round(p_star, 3), round(n * np.linalg.eigvalsh(W)[0], 3), round(best, 3), round(p_star - best, 1))
V = np.random.default_rng(1).normal(size=(n, n))             # an upper bound on d*: tr(WX) for X = VV' with unit rows
for _ in range(5000):
    V = V - 0.01 * W @ V; V /= np.linalg.norm(V, axis=1, keepdims=True)
print(round(np.trace(W @ V @ V.T), 3))
# -22.235 -28.855 -22.834 0.6
# -22.833

Next#

Every number in this section was computed in floating point and treated as exact. Mostly that is harmless, but not always. The Newton step solves a linear system whose matrix can be nearly singular, the ridge solution xλx_\lambda can be computed through A⊤AA^\top A or through a factorization of AA with very different accuracy, and the softmax of the maximum-entropy solution overflows for large scores. The next note measures how errors in the data and in the arithmetic grow through a computation, and which rewrites keep machine learning code accurate.

References#

  • S. Boyd, L. Vandenberghe. Convex Optimization. Cambridge University Press, 2004. §2.5.2 (the supporting hyperplane theorem). Chapter 5: §5.1 (the Lagrange dual function; §5.1.5, two-way partitioning), §5.2 (the dual problem, weak and strong duality, §5.2.3 Slater’s condition and its refinement for affine inequalities), §5.3.2 (proof of strong duality), §5.4.1 (max–min characterization), §5.5.1 (certificates and stopping criteria), Exercise 5.21 (a convex problem with a duality gap). §11.2.2 (dual points from the central path).
  • D. Merkulov. Optimization for ML, HSE University, 2024. Course site; lecture 7 (duality: the dual function, weak and strong duality, Slater’s condition, two-way partitioning, sensitivity).
  • M. P. Deisenroth, A. A. Faisal, C. S. Ong. Mathematics for Machine Learning. Cambridge University Press, 2020. Section 7.2 (Lagrange duality and the minimax inequality).
  • K. P. Bennett, E. J. Bredensteiner. Duality and geometry in SVM classifiers. ICML, 2000, 57–64. The hard-margin dual as nearest points of convex hulls, reduced hulls for the soft margin.
  • C.-J. Hsieh, K.-W. Chang, C.-J. Lin, S. S. Keerthi, S. Sundararajan. A dual coordinate descent method for large-scale linear SVM. ICML, 2008. The box-constrained dual with the bias absorbed into the weights.
  • scikit-learn documentation. sklearn.linear_model.Lasso: the tol parameter, a tolerance on the duality gap.