Duality
Every minimisation problem casts a shadow — a maximisation that bounds it from below, and for convex problems meets it exactly.
- Build the Lagrangian and the dual function of a constrained problem
- Explain weak duality, strong duality and the duality gap
- Read a Lagrange multiplier as the price of a constraint
Take a constrained problem: minimise \(f(x)\) subject to \(h(x) \le 0\). Instead of enforcing the constraint, charge for breaking it. Pick a price \(\lambda \ge 0\) and form the Lagrangian
\[L(x, \lambda) = f(x) + \lambda\, h(x)\]Now minimise over \(x\) with no constraint at all. The result depends on the price, and is called the dual function:
\[g(\lambda) = \min_x \; L(x, \lambda)\]Weak duality: a lower bound for free
For any feasible \(x\) (where \(h(x) \le 0\)) and any \(\lambda \ge 0\), the penalty \(\lambda h(x)\) is at most zero, so \(L(x, \lambda) \le f(x)\). Minimising both sides gives
\[g(\lambda) \;\le\; p^\star \qquad \text{for every } \lambda \ge 0\]Every price gives a lower bound on the true optimum \(p^\star\). The dual problem looks for the best one: maximise \(g(\lambda)\). Its optimum \(d^\star\) satisfies \(d^\star \le p^\star\), and the difference \(p^\star - d^\star\) is the duality gap.
Strong duality, and what the multiplier means
In the convex example, \(\min x^2\) subject to \(x \ge 1\):
\[L = x^2 + \lambda(1 - x), \qquad g(\lambda) = \lambda - \tfrac{\lambda^2}{4}\]which peaks at \(\lambda^\star = 2\) with \(g = 1 = p^\star\). The gap is zero: strong duality. For convex problems this is the rule (under a mild condition called Slater’s), and it is why solvers can certify optimality: a primal point and a dual point with matching values prove each other optimal.
\(\lambda^\star = 2\) also has a meaning. It is the derivative of the optimal cost with respect to the constraint: tighten \(x \ge 1\) to \(x \ge 1 + \epsilon\) and the cost rises by about \(2\epsilon\). Multipliers are shadow prices — how much each constraint is costing you.
Non-convex problems can leave a gap
The script studies \(f(x) = x^2(1-x)^2\), a double well, and plots a duality gap of 0.0625. That number is a genuine duality gap — for the problem of minimising \(f\) with \(x\) pinned to 0.5:
\[\min \; x^2(1-x)^2 \quad \text{s.t.} \quad x = 0.5 \qquad\Rightarrow\qquad p^\star = 0.0625,\;\; d^\star = 0\]The dual can only “see” the convex envelope of \(f\), which is flat at zero between the wells, so it never climbs above 0. That is the animation’s non-convex case.
Two details in the script are worth correcting as you read it:
- It marks \(x = 0.5\) as the global minimum, but \(f \ge 0\) everywhere and \(f(0) = f(1) = 0\). Those are the global minima; \(x = 0.5\) is a local maximum (\(f’‘(0.5) = -1\)).
- In
duality_demonstrationthe constraints are \(0 \le x \le 1\). Both minima are feasible, so \(p^\star = 0\), and the best dual bound is also 0 at \(\lambda = 0\) — no gap. The plotted gap comes from taking \(f(0.5)\) as \(p^\star\). The sweep also includes negative \(\lambda_2\), which is not a valid multiplier for an inequality.
The same pattern appears in Part 3, which minimises \(x^2 + y^2\) outside the circle \((x-2)^2 + y^2 \ge 1\). The origin satisfies that constraint, so the optimum is \((0, 0)\) with value 0 rather than \((1, 0)\) with value 1 — \((1, 0)\) is the answer to the inside-the-circle version, which is convex.
Try it yourself. Change the script’s primal_optimal to the true value 0 and restrict the sweep to \(\lambda_2 \ge 0\): confirm the gap closes. Then reproduce the animation’s convex example in CVXPY — prob.solve() then constraints[0].dual_value — and check that it returns \(\lambda^\star = 2\).