Skip to content
AI360XpertBeta

Constrained Optimisation and Duality

Optimising with a rule you cannot break, and the discovery that the multiplier enforcing the rule is also the price of relaxing it.

The best feasible point is where an objective contour just touches the constraint, so the two gradients line up and lambda 5 prices the budget.
The best feasible point is where an objective contour just touches the constraint, so the two gradients line up and lambda 5 prices the budget.

Why Does This Exist?

Support vector machines are usually taught as maximise the margin, and then the derivation abruptly switches to a different problem over per-example coefficients where most coefficients are zero and the data appears only inside dot products. That switch is Lagrangian duality, and without this page it has to be accepted on faith.

Three things it explains are why SVMs have support vectors at all, why the kernel trick is possible, and what a multiplier means as a shadow price. The same mathematics covers regularisation, fairness constraints, and KL-constrained policy updates, where the multiplier is the knob trading objective against budget.

This page is the full constrained-to-dual story with the SVM payoff. The equality-only tangency mechanism lives in Lagrange multipliers. The inequality test with its switching logic lives in KKT conditions and is checked step by step in optimality conditions. The pricing view with primal and dual gaps lives in Lagrangian duality, and convexity background lives in convex optimization.

The one prerequisite is the gradient as a list of slopes. If ff takes one input and f′(3)=4f'(3) = 4, a tiny increase in xx near 3 raises ff by about four times that nudge, and with several inputs each partial derivative freezes the rest. The full treatment is in gradients.

Think of It Like This

The biggest rectangle on 100 metres of fence

You want the largest rectangular field and you own exactly 100 metres of fence. More area is always better and the fence is fixed.

Start with any rectangle and try a trade: widen it and you must shorten it to stay within the fence. Sometimes the trade gains area and sometimes it loses. You are at the best rectangle exactly when no trade helps, when the gain from widening cancels the loss from shortening.

Geometrically that is tangency. Curves of constant area run across a plot of width against height, and the fence is a straight line. The best reachable curve just touches the line, because any better curve misses it entirely. At the touching point the two curves share a tangent, so their gradients are parallel, and the multiplier is the ratio between them.

Where it stops: a fence is one straight budget while ML constraints bend, multiply, and go slack. The tangency picture stays correct, but inequalities need an on-off switch for idle constraints, which is why this page hands over to the KKT conditions.

How It Actually Works

To minimise f(x)f(x) subject to g(x)=0g(x) = 0, build the Lagrangian, where ff is the objective you want small, gg is the constraint function that must equal zero, xx collects the variables, and λ\lambda is one multiplier per constraint:

L(x,λ)=f(x)−λ g(x)\mathcal{L}(x, \lambda) = f(x) - \lambda\, g(x)

Set every partial derivative to zero, with respect to xx and with respect to λ\lambda. Differentiating by λ\lambda recovers the constraint itself. Differentiating by xx gives stationarity:

∇f=λ∇g\nabla f = \lambda \nabla g

In plain words, at the optimum the objective gradient is parallel to the constraint gradient. If it were not, some component of ∇f\nabla f would lie along the constraint surface, and sliding along the constraint would improve the objective, so you would not be at the optimum. A constrained problem in nn variables becomes an unconstrained problem in n+1n + 1.

Inequalities need the KKT switch

Real problems use inequalities gi(x)≤0g_i(x) \le 0, where each gig_i is one constraint function that must stay non-positive. For minimising ff they add four requirements with multipliers λi≥0\lambda_i \ge 0:

  1. Stationarity: ∇f+∑iλi∇gi=0\nabla f + \sum_i \lambda_i \nabla g_i = 0.
  2. Primal feasibility: gi(x)≤0g_i(x) \le 0.
  3. Dual feasibility: λi≥0\lambda_i \ge 0.
  4. Complementary slackness: λi gi(x)=0\lambda_i\, g_i(x) = 0 for every ii.

The fourth does the work: for each constraint either the multiplier is zero or the constraint is tight. A constraint you are not pressing against has price zero. That single condition produces support vectors, because comfortably classified examples have inactive margin constraints, zero multipliers, and drop out, leaving only boundary examples. The full four-condition treatment is in KKT conditions.

The dual problem

Fix λ≥0\lambda \ge 0 and minimise the Lagrangian over xx to get the dual function, then maximise that over λ\lambda. That maximisation is the dual problem. It always lower-bounds the primal minimum, which is weak duality, and for convex problems under mild regularity the bound is tight, which is strong duality.

Three practical reasons to bother are that the dual can be easier, it can reveal structure, and the gap is a certificate. The SVM dual is a quadratic program with simple box constraints, far more tractable than the primal in high-dimensional feature space. It contains the data only as dot products xiTxjx_i^T x_j, so replacing them with a kernel evaluates an implicit high-dimensional similarity at no extra cost, and that is why the kernel trick works. The primal-minus-dual gap bounds suboptimality, which is how interior-point solvers know when to stop.

The multiplier is a price

If the constraint is g(x)=cg(x) = c, where cc is the budget, then at the optimum f⋆f^{\star}:

∂f⋆∂c=λ\frac{\partial f^{\star}}{\partial c} = \lambda

The multiplier is the rate at which the optimal value improves per unit of relaxed budget, called the shadow price. This is also why minimising f(x)+λ∥x∥2f(x) + \lambda \lVert x \rVert^2 is the Lagrangian of minimising f(x)f(x) subject to ∥x∥2≤c\lVert x \rVert^2 \le c: every penalty strength corresponds to some budget, so weight decay is a constrained problem in disguise.

Worked example

Maximise f(x,y)=xyf(x, y) = xy subject to x+y=10x + y = 10.

Form the Lagrangian:

L=xy−λ(x+y−10)\mathcal{L} = xy - \lambda(x + y - 10)

Set the partial derivatives to zero:

∂L∂x=y−λ=0,∂L∂y=x−λ=0\frac{\partial \mathcal{L}}{\partial x} = y - \lambda = 0, \qquad \frac{\partial \mathcal{L}}{\partial y} = x - \lambda = 0

So x=λx = \lambda and y=λy = \lambda. The constraint gives 2λ=102\lambda = 10, hence λ=5\lambda = 5 and x=y=5x = y = 5, with f=25f = 25. Other splits verify it: 4×6=244 \times 6 = 24 and 3×7=213 \times 7 = 21, both worse than 25. Equal shares win, which is the tangency in the diagram.

Now the shadow price. Loosen the budget to x+y=11x + y = 11. The same algebra gives x=y=5.5x = y = 5.5 and f=30.25f = 30.25, a gain of 5.255.25 for one extra unit, while λ=5\lambda = 5 predicted 5. With general budget cc the optimum is x=y=c/2x = y = c/2 so f⋆=c2/4f^{\star} = c^2/4, and df⋆/dc=c/2=5df^{\star}/dc = c/2 = 5 at c=10c = 10. Exactly λ\lambda.

Code

The snippet checks the shadow price with only the standard library. It evaluates the closed form optimum for two budgets and differentiates it numerically.

def f_star(c: float) -> float:    return c ** 2 / 4

print(f_star(10))  # -> 25.0print(f_star(11))  # -> 30.25print(round(f_star(11) - f_star(10), 4))  # -> 5.25print(round((f_star(10.001) - f_star(9.999)) / 0.002, 6))  # -> 5.0print(4 * 6, 3 * 7)  # -> 24 21

Budgets 10 and 11 give 25.0 and 30.25, the numerical derivative returns 5.0 which matches λ=5\lambda = 5, and the off-center splits 24 and 21 confirm 25 is the peak.

Watch Out For

Reading a Lagrangian stationary point as the answer

Setting the Lagrangian derivatives to zero finds candidates, not answers. Those points include constrained minima, maxima, and saddles, and the equations do not label which is which.

The worked example returned x=y=5x = y = 5 with no tag saying maximum. It is one because xyxy along that line is concave, known separately. A second failure is regularity: the method needs independent constraint gradients at the solution, and at a cusp or with redundant constraints no valid multiplier exists even though a solution does.

Check convexity or second-order conditions, compare every candidate including boundary cases, and use a solver for real problems. Hand multipliers are for understanding.

Spending a dual solution on a non-convex primal

Weak duality always holds, so the dual optimum bounds the primal minimum. Equality needs convexity plus regularity such as Slater's condition, and neural network training is non-convex, so SVM intuitions do not transfer automatically.

The symptom is a recovered primal point that is infeasible or suboptimal while the dual value looked excellent. Treating a KL penalty coefficient as an exact constraint price in RLHF has the same root: penalty and constraint match only under convexity, otherwise the coefficient must be tuned.

State convexity before relying on duality. Convex means the dual route reaches the answer, and otherwise it gives a bound plus a diagnostic gap.

The Quick Version

  • Build L=f−λg\mathcal{L} = f - \lambda g and zero every partial derivative to turn nn constrained variables into n+1n + 1 unconstrained ones.
  • Stationarity says ∇f=λ∇g\nabla f = \lambda \nabla g: gradients run parallel at the optimum, which is tangency.
  • Inequalities need KKT stationarity, primal feasibility, dual feasibility, and complementary slackness.
  • Complementary slackness forces idle multipliers to zero, which is what makes most SVM coefficients vanish into support vectors.
  • The SVM dual holds data only as dot products, which is exactly what the kernel trick replaces.
  • The multiplier is a shadow price: relaxing budget cc by one unit improves the optimum by about λ\lambda.
  • The xyxy example under x+y=10x + y = 10 peaks at x=y=5x = y = 5 with value 25 and λ=5\lambda = 5, confirmed by 24 and 21 nearby.
  • Stationary points are candidates until convexity or second-order checks promote them.
  • Without convexity the dual is a lower bound with a gap, not a solution.