Skip to content
LeetQuant
E05 Advanced Optimisation

E05 · Advanced Optimisation · Lesson 2 of 4

Duality and KKT

The Lagrangian dual, weak and strong duality, Slater's condition, complementary slackness, a small QP solved both through KKT and through its dual, and dual variables as prices, including state prices in an arbitrage LP.

20 min

From multipliers to a second problem

The lesson on Lagrange multipliers treated the KKT conditions as a recipe and multipliers as shadow prices. Duality explains why the recipe works and turns the multipliers into the variables of a second optimisation problem, the dual, whose value is a lower bound on the original. That bound is what solvers report as the "duality gap", what certifies that a portfolio is optimal, and, in linear programmes, what becomes the no-arbitrage pricing theorem.

Objective: derive a dual, use weak duality to bound a problem, know when strong duality holds, derive the KKT conditions from strong duality, and solve and interpret a small QP both ways.

The Lagrangian dual

Take a primal problem in standard form,

p∗=min⁡x f0(x)s.t.fi(x)≤0 (i=1..m),hj(x)=0 (j=1..p),p^* = \min_x\ f_0(x) \quad \text{s.t.}\quad f_i(x) \le 0\ (i = 1..m), \qquad h_j(x) = 0\ (j = 1..p),

with Lagrangian L(x,λ,ν)=f0(x)+∑iλifi(x)+∑jνjhj(x)\mathcal L(x, \lambda, \nu) = f_0(x) + \sum_i \lambda_i f_i(x) + \sum_j \nu_j h_j(x). The dual function is

g(λ,ν)=inf⁡x L(x,λ,ν).g(\lambda, \nu) = \inf_x\ \mathcal L(x, \lambda, \nu) .
  • gg is always concave, whatever f0f_0 and fif_i are: it is a pointwise infimum of functions that are affine in (λ,ν)(\lambda, \nu).
  • Weak duality: for any λ≥0\lambda \ge 0 and any feasible xx, g(λ,ν)≤L(x,λ,ν)≤f0(x)g(\lambda, \nu) \le \mathcal L(x, \lambda, \nu) \le f_0(x), because λifi(x)≤0\lambda_i f_i(x) \le 0 and hj(x)=0h_j(x) = 0. Taking the best of each side, d∗=max⁡λ≥0,νg≤p∗d^* = \max_{\lambda \ge 0, \nu} g \le p^*.

The dual problem max⁡λ≥0, νg(λ,ν)\max_{\lambda \ge 0,\,\nu} g(\lambda, \nu) is a convex problem even when the primal is not, which is why duals give bounds for hard nonconvex problems (for example semidefinite relaxations of combinatorial ones). The duality gap f0(x)−g(λ,ν)f_0(x) - g(\lambda, \nu) for any primal-feasible xx and dual-feasible (λ,ν)(\lambda, \nu) bounds how suboptimal xx is: a gap of 10−810^{-8} is a proof that xx is optimal to 10−810^{-8}, and it is the stopping test interior-point solvers use.

Strong duality and Slater's condition

Strong duality, d∗=p∗d^* = p^*, does not always hold, but for convex problems it holds under a mild condition. Slater's condition: the problem is convex (convex f0,fif_0, f_i, affine hjh_j) and there is a strictly feasible point, fi(x)<0f_i(x) < 0 for every nonlinear inequality and all affine constraints satisfied. Then d∗=p∗d^* = p^*, and the dual optimum is attained when p∗p^* is finite. For linear programmes and for QPs with linear constraints, feasibility alone is enough.

What can go wrong without it:

  • Gap. min⁡e−x\min e^{-x} s.t. x2/y≤0x^2/y \le 0 on the domain y>0y > 0. Feasibility forces x=0x = 0, so p∗=1p^* = 1. But g(λ)=inf⁡x, y>0e−x+λx2/y=0g(\lambda) = \inf_{x,\,y > 0} e^{-x} + \lambda x^2/y = 0 (send x→∞x \to \infty with y=x3y = x^3), so d∗=0d^* = 0. The problem is convex, but no point satisfies x2/y<0x^2/y < 0.
  • No attained multiplier. min⁡x\min x s.t. x2≤0x^2 \le 0: p∗=0p^* = 0, g(λ)=−1/(4λ)g(\lambda) = -1/(4\lambda), so d∗=0d^* = 0 but only as λ→∞\lambda \to \infty. The KKT conditions have no solution: 1+2λx=01 + 2\lambda x = 0 is impossible at x=0x = 0. Constraints that are tangent at the optimum (degenerate risk limits, for instance) make multipliers blow up and solvers struggle.

KKT from strong duality

Suppose strong duality holds with x∗x^* primal optimal and (λ∗,ν∗)(\lambda^*, \nu^*) dual optimal. Then

f0(x∗)=g(λ∗,ν∗)=inf⁡xL(x,λ∗,ν∗)≤L(x∗,λ∗,ν∗)=f0(x∗)+∑iλi∗fi(x∗)≤f0(x∗).f_0(x^*) = g(\lambda^*, \nu^*) = \inf_x \mathcal L(x, \lambda^*, \nu^*) \le \mathcal L(x^*, \lambda^*, \nu^*) = f_0(x^*) + \sum_i \lambda_i^* f_i(x^*) \le f_0(x^*) .

Both inequalities are equalities, which yields two facts:

  • x∗x^* minimises L(⋅,λ∗,ν∗)\mathcal L(\cdot, \lambda^*, \nu^*), so (if differentiable) ∇f0+∑iλi∗∇fi+∑jνj∗∇hj=0\nabla f_0 + \sum_i\lambda_i^*\nabla f_i + \sum_j\nu_j^*\nabla h_j = 0: stationarity.
  • ∑iλi∗fi(x∗)=0\sum_i \lambda_i^* f_i(x^*) = 0, and since every term is ≤0\le 0, each is zero: λi∗fi(x∗)=0\lambda_i^* f_i(x^*) = 0, complementary slackness.

With primal and dual feasibility these are the KKT conditions. So under strong duality KKT is necessary; for a convex problem any KKT point is optimal (the Lagrangian is convex in xx, so stationarity means x∗x^* minimises it, and then g=f0g = f_0 closes the gap), so KKT is also sufficient.

The dual of a QP

For min⁡x12x⊤Px+q⊤x\min_x \tfrac12 x^\top Px + q^\top x s.t. Gx≤hGx \le h with P≻0P \succ 0: minimising the Lagrangian over xx gives x(λ)=−P−1(q+G⊤λ)x(\lambda) = -P^{-1}(q + G^\top\lambda), and

g(λ)=−12(q+G⊤λ)⊤P−1(q+G⊤λ)−h⊤λ,dual: max⁡λ≥0g(λ).g(\lambda) = -\tfrac12 (q + G^\top\lambda)^\top P^{-1}(q + G^\top\lambda) - h^\top\lambda, \qquad \text{dual: } \max_{\lambda \ge 0} g(\lambda) .

The dual is a QP with only sign constraints on λ\lambda. Projected-gradient and coordinate-ascent methods exploit exactly this (it is how support-vector machines are trained), and from any λ≥0\lambda \ge 0 you recover a candidate x(λ)x(\lambda).

Worked example

a small QP via KKT and via its dual

Minimise (x1−2)2+(x2−1)2(x_1 - 2)^2 + (x_2 - 1)^2 subject to x1+x2≤2x_1 + x_2 \le 2, x1≤1.2x_1 \le 1.2 and x2≥0x_2 \ge 0. Find x∗x^* and the multipliers from the KKT conditions, then derive the dual function, maximise it, and confirm there is no gap.

Show solution

Write the constraints as f1=x1+x2−2≤0f_1 = x_1 + x_2 - 2 \le 0, f2=x1−1.2≤0f_2 = x_1 - 1.2 \le 0, f3=−x2≤0f_3 = -x_2 \le 0, with multipliers λ1,λ2,λ3≥0\lambda_1, \lambda_2, \lambda_3 \ge 0. Stationarity:

2(x1−2)+λ1+λ2=0,2(x2−1)+λ1−λ3=0.2(x_1 - 2) + \lambda_1 + \lambda_2 = 0, \qquad 2(x_2 - 1) + \lambda_1 - \lambda_3 = 0 .

Active-set search. The unconstrained minimiser (2,1)(2, 1) violates f1f_1 and f2f_2.

  • Only f1f_1 active: project onto x1+x2=2x_1 + x_2 = 2, giving (1.5,0.5)(1.5, 0.5). Violates x1≤1.2x_1 \le 1.2.
  • Only f2f_2 active: (1.2,1)(1.2, 1). Violates x1+x2≤2x_1 + x_2 \le 2.
  • f1f_1 and f2f_2 active: x=(1.2,0.8)x = (1.2, 0.8), and x2>0x_2 > 0 so λ3=0\lambda_3 = 0 by complementary slackness. The second stationarity equation gives λ1=2(1−0.8)=0.4\lambda_1 = 2(1 - 0.8) = 0.4; the first gives λ2=2(2−1.2)−0.4=1.2\lambda_2 = 2(2 - 1.2) - 0.4 = 1.2.

Both multipliers are non-negative, all constraints hold, the problem is convex: x∗=(1.2,0.8)x^* = (1.2, 0.8) with p∗=0.64+0.04=0.68p^* = 0.64 + 0.04 = 0.68.

The dual. Minimising L\mathcal L over xx: x1=2−(λ1+λ2)/2x_1 = 2 - (\lambda_1 + \lambda_2)/2, x2=1−(λ1−λ3)/2x_2 = 1 - (\lambda_1 - \lambda_3)/2. Substituting and collecting terms,

g(λ)=λ1+0.8λ2−λ3−14[(λ1+λ2)2+(λ1−λ3)2].g(\lambda) = \lambda_1 + 0.8\lambda_2 - \lambda_3 - \tfrac14\big[(\lambda_1 + \lambda_2)^2 + (\lambda_1 - \lambda_3)^2\big] .

(The linear coefficients are the constraint values at the unconstrained minimiser (2,1)(2, 1): 11, 0.80.8 and −1-1.) Maximise over λ≥0\lambda \ge 0. Increasing λ3\lambda_3 from 0 lowers gg while λ1<2\lambda_1 < 2, so λ3=0\lambda_3 = 0. Setting the other partial derivatives to zero:

∂g∂λ2=0.8−12(λ1+λ2)=0,∂g∂λ1=1−12(λ1+λ2)−12λ1=0,\frac{\partial g}{\partial\lambda_2} = 0.8 - \tfrac12(\lambda_1 + \lambda_2) = 0, \qquad \frac{\partial g}{\partial\lambda_1} = 1 - \tfrac12(\lambda_1 + \lambda_2) - \tfrac12\lambda_1 = 0,

so λ1+λ2=1.6\lambda_1 + \lambda_2 = 1.6 and λ1=0.4\lambda_1 = 0.4, λ2=1.2\lambda_2 = 1.2, exactly the KKT multipliers. Then d∗=0.4+0.96−14(2.56+0.16)=0.68=p∗d^* = 0.4 + 0.96 - \tfrac14(2.56 + 0.16) = 0.68 = p^*: no gap, as Slater predicts (the point (0,0.5)(0, 0.5) is strictly feasible).

Weak duality in action. Any λ≥0\lambda \ge 0 gives a bound: g(1,0,0)=0.5g(1, 0, 0) = 0.5 and g(0.5,0.5,0)=0.5875g(0.5, 0.5, 0) = 0.5875 are both below 0.680.68.

Prices. λ2=1.2\lambda_2 = 1.2 says raising the cap from 1.21.2 to 1.211.21 lowers the optimum by about 0.0120.012 (exactly: x=(1.21,0.79)x = (1.21, 0.79), value 0.66820.6682, a fall of 0.01180.0118). λ1=0.4\lambda_1 = 0.4 says loosening the budget is worth a third as much per unit. The cap is the expensive constraint.

Dual variables as prices

Perturb the constraints to fi(x)≤uif_i(x) \le u_i and hj(x)=vjh_j(x) = v_j and let p∗(u,v)p^*(u, v) be the optimal value. For a convex problem with strong duality:

  • Local: ∂p∗/∂ui=−λi∗\partial p^*/\partial u_i = -\lambda_i^* and ∂p∗/∂vj=−νj∗\partial p^*/\partial v_j = -\nu_j^* where differentiable. Loosening constraint ii by one unit improves the optimum by λi∗\lambda_i^*.
  • Global: p∗(u,v)≥p∗(0,0)−λ∗⊤u−ν∗⊤vp^*(u, v) \ge p^*(0, 0) - \lambda^{*\top}u - \nu^{*\top}v for every perturbation. In the example, loosening the budget by 0.10.1 cannot improve the value by more than 0.4×0.1=0.040.4 \times 0.1 = 0.04; the true improvement is 0.030.03, because p∗p^* is convex in the perturbation and the linear estimate is a bound.
  • Signs. A tightened constraint can only hurt, so inequality multipliers are ≥0\ge 0. A large multiplier flags the constraint that is driving the solution; in a portfolio that is the one worth negotiating with the risk manager.

Sign conventions differ by author and solver (f−λ⊤gf - \lambda^\top g versus f+λ⊤gf + \lambda^\top g, ≤\le versus ≥\ge), so when a solver reports a multiplier, check its sign against a finite-difference re-solve before interpreting it.

In a linear programme the dual variables are literally prices. In the classic resource-allocation LP (maximise revenue subject to resource limits), the dual chooses a price for each resource so that every activity's resource cost is at least its revenue, and minimises the total value of the resources. At the optimum, by complementary slackness, an activity is run only if it exactly breaks even at those prices, and a resource with spare capacity has price zero.

Worked example

arbitrage, state prices and LP duality

Three horses race; a stake of 1 on horse A, B or C returns 2, 4 or 6 if that horse wins and nothing otherwise. Choose stakes x≥0x \ge 0 with ∑sxs=1\sum_s x_s = 1 to maximise the guaranteed payout tt. Write the dual and interpret its variables.

Show solution

Primal (an LP): maximise tt subject to t≤osxst \le o_s x_s for each horse ss, with o=(2,4,6)o = (2, 4, 6), ∑sxs=1\sum_s x_s = 1, x≥0x \ge 0.

Dual. Attach qs≥0q_s \ge 0 to each payout constraint and ν\nu to the budget:

L=t+∑sqs(osxs−t)+ν(1−∑sxs).\mathcal L = t + \sum_s q_s(o_s x_s - t) + \nu\Big(1 - \sum_s x_s\Big) .

The supremum over the free variable tt is finite only if ∑sqs=1\sum_s q_s = 1; the supremum over xs≥0x_s \ge 0 is finite only if qsos≤νq_s o_s \le \nu. What remains is ν\nu, so the dual is

min⁡q, ν νs.t.q≥0,  ∑sqs=1,  qsos≤ν for all s.\min_{q,\,\nu}\ \nu \quad \text{s.t.}\quad q \ge 0,\ \ \sum_s q_s = 1,\ \ q_s o_s \le \nu \ \text{for all } s .

The qsq_s form a probability vector, and qsosq_s o_s is the expected payout of a unit bet on ss under qq. The dual looks for the pricing measure that makes the best bet look as unattractive as possible.

Solve. To minimise max⁡sqsos\max_s q_s o_s over the simplex, equalise: qs∝1/osq_s \propto 1/o_s, so q=(12,14,16)/1112=(6,3,2)/11q = (\tfrac12, \tfrac14, \tfrac16)/\tfrac{11}{12} = (6, 3, 2)/11 and ν=1/∑sos−1=12/11\nu = 1/\sum_s o_s^{-1} = 12/11.

Primal from complementary slackness. Every qs>0q_s > 0, so every payout constraint binds: osxs=to_s x_s = t for all ss, so x∝1/osx \propto 1/o_s, i.e. x=(6,3,2)/11x = (6, 3, 2)/11 and t=12/11≈1.091t = 12/11 \approx 1.091, matching ν\nu. (A solver, scipy.optimize.linprog with HiGHS, returns these multipliers, up to its sign convention.)

Reading it. qsq_s is the value of raising the guaranteed floor in state ss: a state price. If some probability vector qq made every bet fair or worse, qsos≤1q_s o_s \le 1, then ν≤1\nu \le 1 and no arbitrage would exist. That is the fundamental theorem of asset pricing in miniature: no arbitrage if and only if a positive pricing measure exists, and the proof is LP duality (Farkas' lemma). Here ∑s1/os=11/12<1\sum_s 1/o_s = 11/12 < 1, so no such qq exists and the arbitrage returns 12/1112/11 per unit staked.

Knowledge check

For a nonconvex primal problem (say, minimum variance with a cardinality constraint written using nonconvex functions), the Lagrangian dual function g(λ, ν) is:

Knowledge check

A solver for a convex minimisation reports a feasible point with objective 0.70 and a dual-feasible multiplier vector with dual value 0.62. What do you know about the optimal value p*?

Knowledge check

In a convex minimisation, the constraint x1≤1.2x_1 \le 1.2 is active with multiplier λ=1.2\lambda = 1.2, and another constraint x2≥0x_2 \ge 0 holds with x2=0.8x_2 = 0.8. Which statement is correct?

Knowledge check

Which problem can have a nonzero duality gap?

Key takeaways

  • The dual function g=inf⁡xLg = \inf_x \mathcal L is always concave and max⁡g≤p∗\max g \le p^* (weak duality); a primal–dual pair with a small gap certifies near-optimality.
  • Strong duality holds for convex problems under Slater's condition (a strictly feasible point) and for any feasible LP or linearly constrained QP. Without it there can be a gap or unattainable multipliers.
  • Strong duality implies the KKT conditions, including complementary slackness λifi(x∗)=0\lambda_i f_i(x^*) = 0; for convex problems KKT is also sufficient.
  • Multipliers are prices: ∂p∗/∂ui=−λi\partial p^*/\partial u_i = -\lambda_i, and p∗(u)≥p∗−λ⊤up^*(u) \ge p^* - \lambda^\top u globally. In an arbitrage LP the dual variables are state prices, and LP duality is the no-arbitrage theorem.

Practice problems

Finished reading?

Answer the 4 knowledge checks to complete this lesson (0 done).