Lagrange Multipliers
The best point on an equality boundary is where your goal's contours just touch it. The multiplier measures how much loosening the boundary would be worth.
Why Does This Exist?
Unconstrained optimization is the easy case, and it is rarely the real one. Budgets are fixed, margins must hold, probabilities must sum to one, and weight norms get capped. The moment a rule you cannot break enters, setting the gradient to zero stops working, because the unconstrained peak usually sits outside the allowed region.
Lagrange multipliers handle equality constraints with one geometric insight: at the best allowed point, you cannot improve without leaving the region, which means the goal's contours run tangent to the boundary there. That tangency becomes a system of equations you can solve.
This page covers equalities only: the tangency, the Lagrangian, the worked fixture, the shadow price. Inequalities need the slackness switch in KKT conditions, certified step by step in optimality conditions. The pricing view with primal dual gaps is Lagrangian duality, and the full story with the SVM dual and the kernel trick is constrained optimisation and duality. The gradients-alignment picture underneath is pure gradients geometry.
The one idea you need first
The gradient points in the steepest-ascent direction, perpendicular to the level curves. If the best feasible point were not a tangency, the goal's contour would cut across the constraint, and walking along the constraint in the cutting direction would improve the objective while staying feasible. So at an optimum the contour cannot cut; it must touch, and touching curves share parallel gradients.
Think of It Like This
Splitting a fixed bonus pool between two teams
Two teams share one fixed bonus pool, and joint output grows with each team's share but with diminishing returns. Any split that leaves money undistributed is wasteful, so the winner uses the whole pool. Now consider shifting one dollar from team A to team B: the trade gains output exactly when B's marginal return on that dollar beats A's.
You stop when no shift helps, when the marginal returns are equal. Geometrically the output contours just touch the fixed-pool line. And the multiplier answers the follow-up question: what is one more dollar of pool worth? That price interpretation survives into economics, where multipliers are called shadow prices, and into ML, where a multiplier on a fairness constraint prices fairness in accuracy.
The analogy stops at smoothness. It assumes returns diminish smoothly so equal margins mean a global best. Real objectives kink and flatten, and then tangency proposes candidates without crowning one, which is the first pitfall below.
How It Actually Works
To maximize or minimize subject to , build the Lagrangian:
In plain words: the objective minus one penalty term per constraint, where each multiplier is an unknown price for violating its constraint. Then solve the stationary system: alongside . The first block says , the gradients are parallel, which is the tangency. The second block says the point is feasible. With variables and constraints that is equations for unknowns.
Read the multiplier after solving: is the rate at which the optimal value improves per unit of loosened constraint. If , one more unit of budget buys about 3 units of objective. That reading is what makes the answer interpretable rather than mechanical.
Worked example
Minimize subject to .
The Lagrangian is . Stationarity gives and , so . Feasibility gives , so and the candidate is with value .
Confirm it is the minimum, not a maximum or saddle: the objective is convex and the constraint is linear, so any stationary point of the Lagrangian is a global minimum. Concretely, neighbours on the line such as give , and far points grow without bound. So wins. And the multiplier prices the constraint: raising the sum to gives with value , an increase of for one unit, close to the predicted and exact in the limit, since gives at .
Code
The point is the check: stationarity, feasibility, and the shadow-price confirmation, numerically.
import numpy as np
def lagrangian(v: np.ndarray, lam: float) -> float: x, y = v # objective x^2 + y^2 minus price times violation return float(x**2 + y**2 - lam * (x + y - 3))
opt = np.array([1.5, 1.5])print(round(lagrangian(opt, 3.0), 3)) # -> 4.5grad = np.array([2 * opt[0] - 3.0, 2 * opt[1] - 3.0]) # stationarity residualprint(np.round(grad, 6).tolist()) # -> [0.0, 0.0]print(round(float(opt.sum()) - 3, 6)) # -> 0.0 feasibleprint(round((2.0**2 + 2.0**2) - 4.5, 3)) # -> 3.5 about lam * 1Residual zero, constraint met, value 4.5, and relaxing the sum to 4 gains 3.5 against the priced 3. Tangency with the receipt.
Watch Out For
Solving the system and skipping the verification
Stationarity finds candidates, and constraints can hide minima, maxima, and saddles among them. The method never labels which is which. On this page convexity did the labelling: a convex objective with a linear constraint makes the candidate the global minimum, checked against the neighbour at value 5. Without convexity there is no such shortcut.
Always classify the candidate: check second-order conditions on the bordered Hessian, verify convexity of the problem, or evaluate every candidate plus the feasible region's endpoints or boundary. An unverified candidate is a guess with algebra.
Applying equality machinery to inequalities
The tangency picture assumes the optimum sits on the constraint. With the unconstrained peak of a concave objective may sit strictly inside, where the constraint is irrelevant and forcing tangency invents a wrong answer. Equality constraints use this page; inequalities need KKT conditions, which add the complementary-slackness switch that turns idle constraints off.
The Quick Version
- Equality constraints are solved by tangency: at the optimum the objective contours touch the feasible region.
- The Lagrangian subtracts one priced penalty per constraint, and stationarity plus feasibility give the candidates.
- The multiplier is the shadow price: optimal value gained per unit of loosened constraint.
- Minimizing on a sum of 3 picks with value 4.5 and multiplier 3.
- Convexity plus a linear constraint certifies the candidate; otherwise classify every candidate before trusting it.