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.
Why Does This Exist?
Support vector machines are usually taught as "maximise the margin", and then the derivation abruptly switches to a completely different problem — one over per-example coefficients, where most of them turn out to be zero and the data appears only inside dot products. That switch is not a trick or a convenience. It is Lagrangian duality, and without this page it has to be accepted on faith.
Three things it explains:
- Why SVMs have "support vectors" at all. The KKT conditions force most multipliers to zero, so only the examples on the boundary matter. That is a consequence of the mathematics, not a design choice.
- Why the kernel trick is possible. In the dual problem the data appears only as dot products, so you can replace them with a kernel and never touch the feature space.
- What a multiplier means. It is a shadow price — the rate at which loosening a constraint improves your objective. That reading makes constrained problems interpretable rather than mechanical.
More broadly: regularisation, fairness constraints, KL-constrained policy updates in RLHF, and trust-region methods are all constrained optimisation, and the multiplier is the knob.
Think of It Like This
Buying the biggest rectangle you can afford in fence
You want to enclose the largest possible area, and you have exactly 100 metres of fence. More area is always better; the fence is fixed.
Start with any rectangle and try to improve it. Make it wider and you must make it shorter to stay within the fence. Sometimes that trade gains area, sometimes it loses. You are at the best rectangle when no trade helps — when the gain from widening exactly cancels the loss from shortening.
Geometrically, that is tangency. Picture curves of constant area on a plot of width against height. You want the highest one you can reach, and the constraint is a straight line. The best you can do is the curve that just touches the line — any better curve misses it entirely. At that touching point the two curves share a tangent, so their gradients are parallel, and the multiplier is the ratio between them.
Now the part worth keeping. Ask what one extra metre of fence is worth. You can compute it, and the answer is exactly the multiplier. It was never just bookkeeping — it was the price all along.
How It Actually Works
To minimise subject to , build the Lagrangian:
Then set all its partial derivatives — with respect to and — to zero. Differentiating by recovers the constraint; differentiating by gives the stationarity condition:
In plain words: at the optimum, the objective's gradient is parallel to the constraint's gradient. If it were not, some component of would lie along the constraint surface, and you could slide along the constraint and improve — so you would not be at the optimum. That argument is the method.
The trick is that a constrained problem in variables has become an unconstrained problem in .
Inequalities need the KKT conditions
Real problems have inequalities, , and those add two requirements. For minimising subject to :
- Stationarity — .
- Primal feasibility — .
- Dual feasibility — .
- Complementary slackness — for every .
The fourth is the one that does the work. It says that for each constraint, either the multiplier is zero or the constraint is tight. A constraint you are not pressing against has no influence, so its price is zero; a constraint that binds has a positive price.
That single condition produces support vectors. In an SVM, examples comfortably inside their margin have inactive constraints, so their multipliers are zero and they drop out of the solution entirely. Only the examples on the margin have nonzero multipliers — and those are the support vectors. Sparsity was forced by the mathematics.
The dual problem
Minimising the Lagrangian over for a fixed gives the dual function, and maximising that over is the dual problem. It always provides a lower bound on the primal minimum — weak duality — and for convex problems satisfying mild conditions the bound is tight, which is strong duality.
Why bother? Three reasons, all practical:
- The dual can be easier. The SVM dual is a quadratic program in variables with simple box constraints, which is far more tractable than the primal in high-dimensional feature space.
- The dual can reveal structure. The SVM dual contains the data only as . Replace that dot product with a kernel and you operate in an implicit high-dimensional space at no cost. The kernel trick is a consequence of duality, not an independent idea.
- The gap is a certificate. The difference between primal and dual values bounds how far from optimal you are, which is how interior-point solvers know when to stop.
The multiplier is a price
The most useful interpretation. If the constraint is , then:
In plain words: the multiplier is the rate at which the optimal value improves as you relax the constraint by one unit. Economists call it the shadow price, and it makes the number interpretable — "this constraint is costing me 5 units of objective per unit of tightness".
This is also why regularisation strength and a constraint budget are two views of one thing. Minimising is the Lagrangian of minimising subject to , and every corresponds to some . Weight decay is a constrained problem in disguise, which is the same duality that makes KL-constrained policy optimisation in RLHF equivalent to a KL penalty.
Worked example
Maximise subject to .
Form the Lagrangian:
Set the partial derivatives to zero:
So and . Substituting into the constraint gives , hence and , with .
Sanity-check by trying other splits: and , both worse than 25 ✓. Equal shares win, which is what the tangency in the diagram shows.
Now the shadow price, which is the point. Loosen the constraint to . By the same algebra and — an increase of for one extra unit of budget, and predicted 5.
The prediction is first-order, and you can confirm it exactly. With a general budget , the optimum is so , and:
Exactly . The multiplier was never bookkeeping.
Show Me the Code
import numpy as npfrom scipy.optimize import minimize
# Maximise xy subject to x + y = 10, i.e. minimise -xy.res = minimize( fun=lambda v: -v[0] * v[1], x0=np.array([1.0, 9.0]), constraints=[{"type": "eq", "fun": lambda v: v[0] + v[1] - 10}],)print(np.round(res.x, 6).tolist(), round(-res.fun, 6)) # -> [5.0, 5.0] 25.0
# The optimum for a general budget c is c^2/4, so df*/dc = c/2 = lambda.f_star = lambda c: c**2 / 4print(f_star(10), f_star(11), round(f_star(11) - f_star(10), 4)) # -> 25.0 30.25 5.25print(round((f_star(10.001) - f_star(9.999)) / 0.002, 6)) # -> 5.0 == lambda
# Complementary slackness: an inactive constraint has a zero multiplier.# Minimise x^2 subject to x >= -1. The bound is slack, so lambda = 0 and x = 0.res2 = minimize(lambda v: v[0] ** 2, x0=np.array([0.5]), constraints=[{"type": "ineq", "fun": lambda v: v[0] + 1}])print(round(float(res2.x[0]), 6)) # -> 0.0 bound never bindsThe solver lands on with , and the numerical derivative of the optimum with respect to the budget returns exactly 5 — the multiplier, confirmed as a shadow price.
Watch Out For
Treating a stationary point of the Lagrangian as a solution
Setting finds candidates, not answers. The Lagrangian's stationary points include constrained minima, constrained maxima, and saddles, and the equations do not distinguish them.
In the worked example the algebra gave and nothing in it said "maximum". It happened to be one because on that line is concave, which we knew separately. Change the objective and the same procedure can hand you the worst point on the constraint.
There is a second, subtler failure. Lagrange's method requires the constraint gradients to be linearly independent at the solution — a regularity condition. Where it fails, at a cusp or where constraints are redundant, no valid multiplier exists and the method finds nothing, even though a solution does exist.
Three defences. Check second-order conditions on the bordered Hessian, or verify convexity of the problem, which makes any stationary point a global solution. Evaluate all candidates and compare, including the boundary, since with several constraints there are several combinations of active sets. And for anything real, use a solver — scipy.optimize.minimize with constraints, or cvxpy for convex problems, both of which check the conditions properly. Deriving multipliers by hand is for understanding; solvers are for answers.
Assuming the dual solves the primal
Weak duality always holds — the dual optimum is a lower bound on the primal minimum. Strong duality, where they are equal, requires convexity plus a regularity condition such as Slater's, and neither is automatic.
For non-convex problems there is generally a duality gap, and solving the dual gives you a bound rather than a solution. Recover a primal point from a dual solution of a non-convex problem and it may be infeasible or simply not optimal. This matters because neural network training is non-convex, so intuitions imported from SVM duality do not transfer.
The gap is not always bad news. A dual bound tells you how far from optimal you might be, which is genuinely useful — it is how branch-and-bound prunes. But it is a bound, and reporting it as the answer is wrong.
Where this shows up in ML: KL-constrained policy optimisation is usually solved as a penalty because the constrained form is harder, and the two are only equivalent under convexity, so the penalty coefficient must be tuned rather than derived. Similarly, adding a fairness constraint as a penalty term is not the same problem as enforcing it, and a model can satisfy the penalty on average while violating the constraint on subgroups.
The habit: state whether your problem is convex before relying on duality. If it is, the dual is a legitimate route to the answer. If it is not, the dual gives you a bound and a diagnostic.
The Quick Version
- Build and set every partial derivative to zero. A constrained problem in variables becomes unconstrained in .
- Stationarity says : the gradients are parallel at the optimum, which is tangency.
- Inequalities need the four KKT conditions: stationarity, primal feasibility, dual feasibility, complementary slackness.
- Complementary slackness — either the multiplier is zero or the constraint is tight — is what produces support vectors.
- The dual is always a lower bound (weak duality); equality needs convexity plus regularity (strong duality).
- The SVM dual contains the data only as dot products, and that is why the kernel trick works.
- The multiplier is a shadow price: , the value of one more unit of budget.
- Weight decay and a norm constraint are the same problem — every corresponds to some budget .
- Verified: maximising under gives , , , and relaxing to 11 gains 5.25.
- finds candidates, not solutions. Check second-order conditions or convexity.
- Non-convex problems have a duality gap, so a dual solution is a bound rather than an answer.
What to Read Next
- Convexity and Loss Landscapes is the condition that decides whether duality is tight.
- Gradients are the objects the stationarity condition sets parallel.
- Jacobian and Hessian supplies the second-order check for classifying a candidate.
- Dot Product is the operation the kernel trick replaces in the dual.
- Norms is the constraint that weight decay's multiplier prices.
- Definitions worth a look: Convex Function, Stationary Point, Gradient, and Hessian Matrix.
- Related interview question: When would you choose an L1 penalty over L2?