TExcelTAD
Home › Math for Machine Learning › Constrained Optimization and Lagrange Multipliers

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 f(x)f(\mathbf{x}) subject to h(x)=0h(\mathbf{x}) = 0, form the Lagrangian

L(x,λ)=f(x)+λ h(x)\mathfrak{L}(\mathbf{x}, \lambda) = f(\mathbf{x}) + \lambda\,h(\mathbf{x})

and set all its partial derivatives to zero. At a constrained optimum, ∇f\nabla f must be parallel to ∇h\nabla h: otherwise you could slide along the constraint surface and still decrease ff. That is the condition ∇f+λ∇h=0\nabla f + \lambda\nabla h = 0.

Example: the entropy-maximizing distribution. Maximize H(p)=−∑kpklog⁡pkH(\mathbf{p}) = -\sum_k p_k\log p_k subject to ∑kpk=1\sum_k p_k = 1. With L=−∑pklog⁡pk+λ(∑pk−1)\mathfrak{L} = -\sum p_k\log p_k + \lambda(\sum p_k - 1):

∂L∂pk=−log⁡pk−1+λ=0  ⟹  pk=eλ−1\frac{\partial \mathfrak{L}}{\partial p_k} = -\log p_k - 1 + \lambda = 0 \implies p_k = e^{\lambda - 1}

All pkp_k are equal, so the uniform distribution pk=1/Kp_k = 1/K has maximum entropy.

Example: PCA. Maximize the variance bTSb\mathbf{b}^TS\mathbf{b} subject to bTb=1\mathbf{b}^T\mathbf{b} = 1. The Lagrangian condition gives Sb=λbS\mathbf{b} = \lambda\mathbf{b}: the optimum is an eigenvector of the covariance matrix (PCA: Maximum Variance).

python · try it yourself
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 min⁡f(x)\min f(\mathbf{x}) subject to gi(x)≤0g_i(\mathbf{x}) \le 0, the multipliers must be non-negative, λi≥0\lambda_i \ge 0. At an optimum (under mild conditions) the Karush-Kuhn-Tucker (KKT) conditions hold:

∇f(x)+∑iλi∇gi(x)=0stationaritygi(x)≤0,λi≥0feasibilityλi gi(x)=0complementary slackness\begin{aligned} &\nabla f(\mathbf{x}) + \sum_i \lambda_i\nabla g_i(\mathbf{x}) = \mathbf{0} && \text{stationarity} \\ &g_i(\mathbf{x}) \le 0, \quad \lambda_i \ge 0 && \text{feasibility} \\ &\lambda_i\,g_i(\mathbf{x}) = 0 && \text{complementary slackness} \end{aligned}

Complementary slackness says each constraint is either active (gi=0g_i = 0) 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 D(λ)=min⁡xL(x,λ)\mathfrak{D}(\boldsymbol{\lambda}) = \min_{\mathbf{x}}\mathfrak{L}(\mathbf{x}, \boldsymbol{\lambda}). For every λ≥0\boldsymbol{\lambda} \ge 0 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).

python · try it yourself
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 λ∗=2>0\lambda^* = 2 > 0.

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 xyxy subject to x+y=10x + y = 10.

Answer

x=y=5x = y = 5, maximum 25.

Solution

L=xy+λ(10−x−y)\mathfrak{L} = xy + \lambda(10 - x - y). Then y−λ=0y - \lambda = 0, x−λ=0x - \lambda = 0, so x=yx = y, and the constraint gives x=y=5x = y = 5.

Exercise 2: Closest point on a line

Minimize x2+y2x^2 + y^2 subject to x+2y=5x + 2y = 5.

Answer

(1,2)(1, 2), minimum value 5.

Solution

2x=λ2x = \lambda, 2y=2λ2y = 2\lambda, so y=2xy = 2x. Then x+4x=5x + 4x = 5 gives x=1x = 1, y=2y = 2. It is the projection of the origin onto the line, and the distance is 5\sqrt{5}.

Exercise 3: KKT by inspection (code)

Minimize (x−3)2(x - 3)^2 subject to x≤1x \le 1. Which constraint is active, and what is λ\lambda?

Answer

x∗=1x^* = 1 (active), λ=4\lambda = 4.

Solution

The unconstrained minimum x=3x = 3 is infeasible, so the constraint g(x)=x−1≤0g(x) = x - 1 \le 0 is active. Stationarity: 2(x−3)+λ=02(x - 3) + \lambda = 0 at x=1x = 1 gives λ=4≥0\lambda = 4 \ge 0.

python · try it yourself
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 h(x)=0h(\mathbf{x}) = 0…

  1. ∇f=0\nabla f = 0
  2. ∇f\nabla f is parallel to ∇h\nabla h
  3. ∇f\nabla f is orthogonal to ∇h\nabla h
  4. h(x)=1h(\mathbf{x}) = 1

Complementary slackness λigi(x)=0\lambda_i g_i(\mathbf{x}) = 0 means…

  1. all constraints are active
  2. an inactive constraint has a zero multiplier
  3. all multipliers are zero
  4. the problem is infeasible

Weak duality says the dual function is…

  1. an upper bound on the primal optimum (for minimization)
  2. a lower bound on the primal optimum (for minimization)
  3. always equal to it
  4. unrelated