Constrained Optimization and Lagrange Multipliers
Sometimes the parameters must obey constraints: probabilities must sum to 1, a weight vector must have unit length, a budget must not be exceeded. Lagrange multipliers turn a constrained problem into an unconstrained one with extra variables, and the resulting dual problem is how SVMs are actually solved.
Equality constraints
To minimize subject to , form the Lagrangian
and set all its partial derivatives to zero. At a constrained optimum, must be parallel to : otherwise you could slide along the constraint surface and still decrease . That is the condition .
Example: the entropy-maximizing distribution. Maximize subject to . With :
All are equal, so the uniform distribution has maximum entropy.
Example: PCA. Maximize the variance subject to . The Lagrangian condition gives : the optimum is an eigenvector of the covariance matrix (PCA: Maximum Variance).
import numpy as np
S = np.array([[3.0, 1.0], [1.0, 2.0]])
# brute force over the unit circle
t = np.linspace(0, np.pi, 100001)
B = np.vstack([np.cos(t), np.sin(t)])
values = np.einsum("in,ij,jn->n", B, S, B)
best = B[:, np.argmax(values)]
lam, Q = np.linalg.eigh(S)
print("brute-force maximizer:", np.round(best, 4), " value", round(values.max(), 4))
print("top eigenvector: ", np.round(Q[:, -1] * np.sign(Q[0, -1]), 4), " eigenvalue", round(lam[-1], 4))brute-force maximizer: [0.8506 0.5257] value 3.618
top eigenvector: [0.8507 0.5257] eigenvalue 3.618
Inequality constraints and KKT
For subject to , the multipliers must be non-negative, . At an optimum (under mild conditions) the Karush-Kuhn-Tucker (KKT) conditions hold:
Complementary slackness says each constraint is either active () or has a zero multiplier. In an SVM, the training points with nonzero multipliers are the support vectors; all others could be deleted without changing the solution.
The dual problem
The Lagrangian dual function is . For every it is a lower bound on the optimal value (weak duality). Maximizing it gives the dual problem; for convex problems with mild conditions the bound is tight (strong duality).
import numpy as np
# min x^2 subject to x >= 1, i.e. g(x) = 1 - x <= 0. Optimum x* = 1, f* = 1.
# L(x, lam) = x^2 + lam (1 - x); minimizing over x gives x = lam/2, D(lam) = lam - lam^2/4.
lam = np.linspace(0, 4, 401)
D = lam - lam**2 / 4
print("max of dual:", round(D.max(), 6), "at lambda =", lam[np.argmax(D)])
print("every D(lam) <= 1:", bool(np.all(D <= 1 + 1e-12)))
print("x* = lambda*/2 =", lam[np.argmax(D)] / 2, "; complementary slackness: lam * g =", lam[np.argmax(D)] * (1 - 1.0))max of dual: 1.0 at lambda = 2.0
every D(lam) <= 1: True
x* = lambda*/2 = 1.0 ; complementary slackness: lam * g = 0.0
The dual optimum equals the primal optimum (1), and the constraint is active with .
In software
scipy.optimize.minimize(method="SLSQP", constraints=...)handles equality and inequality constraints.- SVM solvers such as libsvm solve the dual problem (The Dual SVM and Kernels).
- Softmax and maximum-entropy models come from Lagrangian derivations like the one above.
Exercises
Exercise 1: A classic
Maximize subject to .
Answer
, maximum 25.
Solution
. Then , , so , and the constraint gives .
Exercise 2: Closest point on a line
Minimize subject to .
Answer
, minimum value 5.
Solution
, , so . Then gives , . It is the projection of the origin onto the line, and the distance is .
Exercise 3: KKT by inspection (code)
Minimize subject to . Which constraint is active, and what is ?
Answer
(active), .
Solution
The unconstrained minimum is infeasible, so the constraint is active. Stationarity: at gives .
import numpy as np
x = np.linspace(-2, 1, 3001)
print(x[np.argmin((x - 3) ** 2)], -2 * (1 - 3))1.0 4
Test yourself
At a constrained optimum with one equality constraint …
- is parallel to
- is orthogonal to
Complementary slackness means…
- all constraints are active
- an inactive constraint has a zero multiplier
- all multipliers are zero
- the problem is infeasible
Weak duality says the dual function is…
- an upper bound on the primal optimum (for minimization)
- a lower bound on the primal optimum (for minimization)
- always equal to it
- unrelated