Optimization and Lagrange Multipliers
Finding the best value of a function of several variables works like the one-variable case: look where the derivative (now the gradient) is zero, then check what kind of point it is. When the variables must satisfy a constraint, such as a fixed budget, Lagrange multipliers turn the problem into a system of equations. The same idea sits underneath support vector machines and many other constrained optimisers.
Critical points
At a local maximum or minimum of a smooth , the tangent plane is horizontal, so
Points where this holds are critical points. As in one variable, not every critical point is an extremum. In two variables there is a third kind, the saddle point: a minimum along one direction and a maximum along another, like the centre of in Surfaces in 3D.
The second-derivative test
At a critical point, compute the discriminant
| Condition | Conclusion |
|---|---|
| and | local minimum (curves up in every direction) |
| and | local maximum |
| saddle point | |
| the test says nothing |
is the determinant of the Hessian matrix; Jacobian and Hessian explains the general version with eigenvalues.
Example. . Then and , so the critical points are and . With , , , : at , and , a minimum; at , , a saddle.
import sympy as sp
x, y = sp.symbols('x y', real=True)
f = x**3 - 3*x + y**2 - 4*y
crit = sp.solve([sp.diff(f, x), sp.diff(f, y)], [x, y], dict=True)
fxx, fyy, fxy = sp.diff(f, x, 2), sp.diff(f, y, 2), sp.diff(f, x, y)
for p in crit:
D = (fxx * fyy - fxy**2).subs(p)
kind = "saddle" if D < 0 else ("minimum" if fxx.subs(p) > 0 else "maximum")
print(p, " D =", D, " f =", f.subs(p), "->", kind){x: -1, y: 2} D = -12 f = -2 -> saddle
{x: 1, y: 2} D = 12 f = -6 -> minimum
Constraints and Lagrange multipliers
Often the variables are tied together: maximise subject to . Picture the level curves of and the constraint curve . Walking along the constraint, stops increasing exactly where the constraint curve touches a level curve of without crossing it. There the two curves share a tangent, so their normals, the gradients, are parallel:
The number is the Lagrange multiplier. That gives three equations (two components plus the constraint) for three unknowns , , . Solve them, evaluate at each solution, and compare.
Example. A rectangular plot has width and length . Each metre of width costs €2, each metre of length €3, and the budget is €24. Maximise the area subject to .
import sympy as sp
from scipy.optimize import minimize
x, y, lam = sp.symbols('x y lambda', real=True)
f = x * y
g = 2*x + 3*y - 24
eqs = [sp.Eq(sp.diff(f, x), lam * sp.diff(g, x)),
sp.Eq(sp.diff(f, y), lam * sp.diff(g, y)),
sp.Eq(g, 0)]
print("Lagrange:", sp.solve(eqs, [x, y, lam], dict=True))
res = minimize(lambda p: -p[0] * p[1], x0=[1.0, 1.0],
constraints=[{"type": "eq", "fun": lambda p: 2*p[0] + 3*p[1] - 24}])
print("SciPy :", [round(float(v), 4) for v in res.x], " area", round(float(-res.fun), 4))Lagrange: [{lambda: 2, x: 6, y: 4}]
SciPy : [6.0, 4.0] area 24.0
What means
The multiplier is a price: , the rate at which the optimal value improves per unit of extra budget. Here , so one more euro of budget () should add about of area. Check: with the optimum is , , , two more than 24. Economists call the shadow price; in optimisation software it is the dual variable of the constraint.
Tip: Lagrange's condition finds candidates. If the constraint curve is closed and bounded (a circle, an ellipse), the largest and smallest of over the candidates are the global max and min. If it is unbounded, check that an optimum exists at all.
In software
- Support vector machines. An SVM looks for the separating line (or hyperplane) with the widest margin, a minimisation of subject to one inequality constraint per training point. Writing it with Lagrange multipliers gives the "dual" problem. At the solution only the points lying on the margin get : those are the support vectors, and they alone determine the boundary. See SVM.
- Softmax and maximum entropy. Maximising entropy subject to and a fixed average score gives , the softmax distribution behind LLM sampling.
- Solvers. SciPy's SLSQP and trust-constr methods (Constrained Optimization) and portfolio optimisers (maximise return subject to a risk budget) all work with Lagrange-style conditions (the KKT conditions for inequality constraints).
Exercises
Exercise 1: Classify critical points
Find and classify the critical points of .
Answer
One critical point, , a local (and global) minimum with .
Solution
Exercise 2: A saddle
Show that has a saddle point at the origin.
Answer
, so is a saddle.
Solution
and vanish only at . and , so . Directly: along , (a minimum), along , (a maximum).
Exercise 3: Closest point on a line
Use a Lagrange multiplier to find the point of the line closest to the origin (minimise ).
Answer
, at distance .
Solution
The answer is a multiple of the line's normal , as geometry predicts.
Exercise 4: Extremes on a circle (code)
Find the maximum and minimum of on the circle , first with Lagrange multipliers in SymPy, then check by sampling the circle.
Answer
Maximum 5 at (0.6, 0.8); minimum −5 at (−0.6, −0.8).
Solution
gives , ; the constraint gives , .
import sympy as sp
import numpy as np
x, y, lam = sp.symbols('x y lambda', real=True)
sols = sp.solve([3 - 2*lam*x, 4 - 2*lam*y, x**2 + y**2 - 1], [x, y, lam], dict=True)
for s in sols:
print(s, " f =", 3*s[x] + 4*s[y])
t = np.linspace(0, 2 * np.pi, 100001)
vals = 3 * np.cos(t) + 4 * np.sin(t)
print("sampled max/min:", round(float(vals.max()), 6), round(float(vals.min()), 6)){lambda: -5/2, x: -3/5, y: -4/5} f = -5
{lambda: 5/2, x: 3/5, y: 4/5} f = 5
sampled max/min: 5.0 -5.0
Test yourself
At a critical point, . What kind of point is it?
- Local minimum
- Local maximum
- Saddle point
- The test is inconclusive
What does the Lagrange condition say geometrically?
- and are equal
- The level curve of is tangent to the constraint curve
- The gradient of is zero
- is the optimal value
In an SVM, which training points have non-zero Lagrange multipliers?
- All of them
- Only misclassified points
- Only the support vectors on the margin
- Only the points farthest from the boundary