TExcelTAD
Home › Optimization › Lagrange Multipliers

Lagrange Multipliers

For equality constraints, the geometric condition "no feasible direction goes downhill" becomes a clean algebraic one: at the optimum, the gradient of the objective is a combination of the gradients of the constraints. The coefficients are the Lagrange multipliers, and they turn out to measure how much each constraint costs. A first pass is in Math for ML: Lagrange multipliers; here we develop the optimization side.

The condition

For min⁡f(x)\min f(\mathbf{x}) subject to hj(x)=0h_j(\mathbf{x}) = 0, j=1,…,pj = 1, \dots, p, define the Lagrangian

L(x,ν)=f(x)+∑j=1pνjhj(x)\mathcal{L}(\mathbf{x}, \boldsymbol{\nu}) = f(\mathbf{x}) + \sum_{j=1}^p \nu_j h_j(\mathbf{x})

If x∗\mathbf{x}^* is a local minimum and the constraint gradients ∇hj(x∗)\nabla h_j(\mathbf{x}^*) are linearly independent (LICQ), there are multipliers ν∗\boldsymbol{\nu}^* with

∇xL(x∗,ν∗)=∇f(x∗)+∑jνj∗∇hj(x∗)=0,hj(x∗)=0\nabla_{\mathbf{x}}\mathcal{L}(\mathbf{x}^*, \boldsymbol{\nu}^*) = \nabla f(\mathbf{x}^*) + \sum_j \nu_j^*\nabla h_j(\mathbf{x}^*) = \mathbf{0}, \qquad h_j(\mathbf{x}^*) = 0

That is n+pn + p equations in n+pn + p unknowns. Geometrically, ∇f\nabla f must be perpendicular to the constraint surface, because any component along the surface would be a feasible descent direction.

Equality-constrained quadratic programs: one linear solve

For min⁡12xTQx+cTx\min \frac{1}{2}\mathbf{x}^TQ\mathbf{x} + \mathbf{c}^T\mathbf{x} subject to Ax=bA\mathbf{x} = \mathbf{b}, the conditions are linear, the KKT system:

[QATA0][xν]=[−cb]\begin{bmatrix} Q & A^T \\ A & 0 \end{bmatrix}\begin{bmatrix} \mathbf{x} \\ \boldsymbol{\nu} \end{bmatrix} = \begin{bmatrix} -\mathbf{c} \\ \mathbf{b} \end{bmatrix}
python · try it yourself
import numpy as np

# minimize x^2 + 2y^2 + 3z^2 subject to x + y + z = 1 and x - y = 0.2
Q = np.diag([2.0, 4.0, 6.0]); c = np.zeros(3)
A = np.array([[1.0, 1.0, 1.0], [1.0, -1.0, 0.0]]); b = np.array([1.0, 0.2])
K = np.block([[Q, A.T], [A, np.zeros((2, 2))]])
sol = np.linalg.solve(K, np.concatenate([-c, b]))
x, nu = sol[:3], sol[3:]
print("x  =", np.round(x, 6))
print("nu =", np.round(nu, 6))
print("constraints:", np.round(A @ x, 6), " gradient of L:", np.round(Q @ x + c + A.T @ nu, 10) + 0)
x  = [0.506667 0.306667 0.186667]
nu = [-1.12      0.106667]
constraints: [1.  0.2]  gradient of L: [0. 0. 0.]

This one solve is the inner step of SQP methods and of Newton's method with equality constraints.

Multipliers are prices

Change the constraint to h(x)=εh(\mathbf{x}) = \varepsilon. The optimal value changes at the rate

dp∗(ε)dε=−ν∗\frac{d p^*(\varepsilon)}{d\varepsilon} = -\nu^*

So ν∗\nu^* is the marginal cost of tightening the constraint, a shadow price. Check it numerically:

python · try it yourself
import numpy as np

Q = np.diag([2.0, 4.0, 6.0]); A = np.array([[1.0, 1.0, 1.0], [1.0, -1.0, 0.0]])
K = np.block([[Q, A.T], [A, np.zeros((2, 2))]])
def solve(b):
    s = np.linalg.solve(K, np.concatenate([np.zeros(3), b]))
    return 0.5 * s[:3] @ Q @ s[:3], s[3:]
p0, nu = solve(np.array([1.0, 0.2]))
eps = 1e-6
p1, _ = solve(np.array([1.0 + eps, 0.2]))
print("dp*/d(b1) numerically:", round((p1 - p0) / eps, 4), "  -nu_1 =", round(-nu[0], 4))
dp*/d(b1) numerically: 1.12   -nu_1 = 1.12

Sensitivity and Shadow Prices develops this for inequalities and linear programs.

A nonlinear example: maximize volume

Maximize the volume xyzxyz of a box with surface area 2(xy+yz+zx)=62(xy + yz + zx) = 6. With L=−xyz+ν(2(xy+yz+zx)−6)\mathcal{L} = -xyz + \nu(2(xy + yz + zx) - 6), the conditions yz=2ν(y+z)yz = 2\nu(y + z), xz=2ν(x+z)xz = 2\nu(x + z), xy=2ν(x+y)xy = 2\nu(x + y) force x=y=zx = y = z, so the cube with side 1 wins. SymPy confirms:

python · try it yourself
import sympy as sp

x, y, z, nu = sp.symbols("x y z nu", positive=True)
L = -x * y * z + nu * (2 * (x * y + y * z + z * x) - 6)
eqs = [sp.diff(L, v) for v in (x, y, z)] + [2 * (x * y + y * z + z * x) - 6]
sol = sp.solve(eqs, [x, y, z, nu], dict=True)[0]
print({str(v): sol[v] for v in (x, y, z, nu)})
{'x': 1, 'y': 1, 'z': 1, 'nu': 1/4}

In software

  • SciPy's SLSQP and trust-constr solve the Lagrange (KKT) equations iteratively; trust-constr returns the multipliers in res.v.
  • In ML, Lagrange multipliers give the maximum-entropy distributions (The Maximum-Entropy Principle), the PCA eigenvalue problem and the SVM dual.

Exercises

Exercise 1: Closest point on a line

Minimize x2+y2x^2 + y^2 subject to x+2y=5x + 2y = 5 with a Lagrange multiplier.

Answer

(1,2)(1, 2), ν=−2\nu = -2, minimum value 5.

Solution

∇L=(2x+ν,2y+2ν)=0\nabla\mathcal{L} = (2x + \nu, 2y + 2\nu) = \mathbf{0} gives x=−ν/2x = -\nu/2, y=−νy = -\nu. Substituting into the constraint: −ν2−2ν=−52ν=5-\frac{\nu}{2} - 2\nu = -\frac{5}{2}\nu = 5, so ν=−2\nu = -2, x=1x = 1, y=2y = 2, and the value is 1+4=51 + 4 = 5.

Exercise 2: Interpret the multiplier

In Exercise 1, estimate the optimal value if the constraint becomes x+2y=5.1x + 2y = 5.1.

Answer

About 5+2⋅0.1=5.25 + 2 \cdot 0.1 = 5.2 (exactly 5.202).

Solution

dp∗/db=−ν=2dp^*/db = -\nu = 2. Exactly, p∗(b)=b2/5p^*(b) = b^2/5, so p∗(5.1)=26.01/5=5.202p^*(5.1) = 26.01/5 = 5.202.

Exercise 3: KKT system (code)

Solve min⁡12∥x∥2\min \frac{1}{2}\lVert \mathbf{x} \rVert^2 subject to x1+x2+x3+x4=2x_1 + x_2 + x_3 + x_4 = 2 with the KKT system.

Answer

x=(0.5,0.5,0.5,0.5)\mathbf{x} = (0.5, 0.5, 0.5, 0.5), ν=−0.5\nu = -0.5.

Solution
python · try it yourself
import numpy as np
Q = np.eye(4); A = np.ones((1, 4))
K = np.block([[Q, A.T], [A, np.zeros((1, 1))]])
print(np.linalg.solve(K, np.array([0, 0, 0, 0, 2.0])))
[ 0.5  0.5  0.5  0.5 -0.5]

By symmetry all coordinates are equal, and the minimum-norm point on the hyperplane is b∥a∥2a\frac{b}{\lVert \mathbf{a} \rVert^2}\mathbf{a}.

Test yourself

At a solution of min⁡f\min f s.t. h(x)=0h(\mathbf{x}) = 0 (with LICQ), ∇f(x∗)\nabla f(\mathbf{x}^*) is…

  1. zero
  2. parallel to ∇h(x∗)\nabla h(\mathbf{x}^*)
  3. perpendicular to ∇h(x∗)\nabla h(\mathbf{x}^*)
  4. random

An equality-constrained QP can be solved by…

  1. one linear solve of the KKT system
  2. the simplex method only
  3. random search
  4. it has no solution

The multiplier ν∗\nu^* measures…

  1. the number of constraints
  2. the rate of change of the optimal value as the constraint is perturbed
  3. the step size
  4. the condition number