Course contents 13 / 16

13 / 16 · Optimization

Lagrange multipliers and KKT conditions

Minimizing under constraints: why the gradient at a constrained minimum is balanced by the constraint normals, what the multipliers measure, the KKT conditions for inequalities, and projections, maximum entropy and PCA worked out with them.

On this page 8

Minimize f(x)=x12+2x22f(x) = x_1^2 + 2x_2^2 over the points of the line x1+x2=1x_1 + x_2 = 1. Here the constraint can be solved for one variable. Substituting x1=1−x2x_1 = 1 - x_2 leaves a function of x2x_2 alone:

φ(x2)=(1−x2)2+2x22,φ′(x2)=−2(1−x2)+4x2=0.\begin{aligned} \varphi(x_2) &= (1 - x_2)^2 + 2x_2^2 , \\ \varphi'(x_2) &= -2(1 - x_2) + 4x_2 = 0 . \end{aligned}

So x2=13x_2 = \tfrac13, x1=23x_1 = \tfrac23 and f=23f = \tfrac23.

Elimination rarely goes this smoothly. On the unit circle x12+x22=1x_1^2 + x_2^2 = 1 it gives two branches x1=±1−x22x_1 = \pm\sqrt{1 - x_2^2} with infinite slopes at their ends. With the inequalities xi≥0x_i \ge 0 of a probability vector it turns into a case split over which coordinates are zero, 2n2^n cases in nn dimensions. The SVM has one inequality per training point. These problems need a method that keeps all the variables and treats every constraint the same way.

It starts from an observation about the minimum itself. Over all of Rn\mathbb R^n, as in the previous note, a minimum has a zero gradient. Under a constraint it need not: the function may keep decreasing, but only in directions the constraint forbids.

Equality constraints: the gradient is parallel to the normal#

Figure

A constrained minimum: the level set touches the constraint

Level sets of the objective and the constraint lineNested ellipses centred at the origin and a slanted line. One ellipse touches the line; at the point of contact two parallel gradient arrows are perpendicular to the line. Higher up the line a larger ellipse crosses it; there a vertical gradient arrow and a green arrow along the line point towards the point of contact.∇f∇hx*∇ff = 2/3black line: x₁ + x₂ = 1ellipses: level sets of f = x₁² + 2x₂²at x* = (2/3, 1/3): ∇f = −ν ∇h, ν = −4/3at (0, 1): ∇f has the component −2.83along the line (green): not a minimumLevel sets of the objective and the constraint lineNested ellipses centred at the origin and a slanted line. One ellipse touches the line; at the point of contact two parallel gradient arrows are perpendicular to the line. Higher up the line a larger ellipse crosses it; there a vertical gradient arrow and a green arrow along the line point towards the point of contact.∇f∇hx*∇ff = 2/3black line: x₁ + x₂ = 1ellipses: level sets of f = x₁² + 2x₂²at x* = (2/3, 1/3): ∇f = −ν ∇h, ν = −4/3at (0, 1): ∇f has the component −2.83along the line (green): not a minimum
Level sets of f = x₁² + 2x₂² and the line x₁ + x₂ = 1. The smallest ellipse that reaches the line touches it at x* = (2/3, 1/3), where ∇f (blue) and ∇h (amber) are both perpendicular to the line, with ν = −4/3. At (0, 1) the ellipse f = 2 crosses the line instead.

At a constrained minimum, no direction that stays on the constraint surface can decrease ff to first order. So the gradient of ff is perpendicular to the surface, and so is the gradient of the constraint. Two vectors perpendicular to the same surface of dimension n−1n - 1 are parallel. For one constraint h(x)=0h(x) = 0 with ∇h(x∗)≠0\nabla h(x^*) \ne 0, a local minimum x∗x^* therefore satisfies

  ∇f(x∗)+ν ∇h(x∗)=0  \boxed{\;\nabla f(x^*) + \nu\,\nabla h(x^*) = 0\;}

for some number ν\nu, together with h(x∗)=0h(x^*) = 0.

The proof takes three steps. Write a=∇h(x∗)a = \nabla h(x^*).

Step 1: the gradient is orthogonal to every tangent direction. Take any smooth curve x(t)x(t) on the surface with x(0)=x∗x(0) = x^* and velocity x′(0)=vx'(0) = v. The chain rule at t=0t = 0 gives two facts:

h(x(t))=0 ⟹ a⊤v=0,f(x(t))≥f(x∗) ⟹ ∇f(x∗)⊤v=0.\begin{aligned} h(x(t)) = 0 \ &\Longrightarrow\ a^\top v = 0 , \\ f(x(t)) \ge f(x^*) \ &\Longrightarrow\ \nabla f(x^*)^\top v = 0 . \end{aligned}

The first holds because h(x(t))h(x(t)) is constant, so its derivative a⊤va^\top v is zero. The second holds because, for small ∣t∣\lvert t\rvert, t=0t = 0 is a minimum of the one-variable function f(x(t))f(x(t)), so its derivative vanishes there.

Step 2: every vv with a⊤v=0a^\top v = 0 is a tangent direction. Correct the straight line x∗+tvx^* + tv along the normal: look for a curve x(t)=x∗+tv+s(t) ax(t) = x^* + tv + s(t)\,a. The function F(t,s)=h(x∗+tv+s a)F(t, s) = h(x^* + tv + s\,a) has F(0,0)=0F(0, 0) = 0 and ∂F/∂s (0,0)=a⊤a≠0\partial F/\partial s\,(0, 0) = a^\top a \ne 0. The implicit function theorem (Lebl, 2026, Theorem 8.5.6) gives a differentiable s(t)s(t) with s(0)=0s(0) = 0 and F(t,s(t))=0F(t, s(t)) = 0 near t=0t = 0, with derivative

s′(0)=−∂F/∂t∂F/∂s=−a⊤va⊤a=0.s'(0) = -\frac{\partial F/\partial t}{\partial F/\partial s} = -\frac{a^\top v}{a^\top a} = 0 .

So the curve stays on the surface, and its velocity is v+s′(0) a=vv + s'(0)\,a = v.

Step 3: orthogonal to all tangents means parallel to aa. Split ∇f(x∗)=αa+w\nabla f(x^*) = \alpha a + w with α=a⊤∇f(x∗)/a⊤a\alpha = a^\top\nabla f(x^*)/a^\top a, so that a⊤w=0a^\top w = 0. By step 2, ww is a tangent direction, and by step 1

0=∇f(x∗)⊤w=α a⊤w+w⊤w=∥w∥2.\begin{aligned} 0 &= \nabla f(x^*)^\top w \\ &= \alpha\, a^\top w + w^\top w \\ &= \lVert w\rVert^2 . \end{aligned}

So w=0w = 0 and ∇f(x∗)=αa\nabla f(x^*) = \alpha a, which is the boxed condition with ν=−α\nu = -\alpha.

With several constraints h1,…,hph_1, \dots, h_p, stack the gradients ∇hj(x∗)⊤\nabla h_j(x^*)^\top as the rows of a p×np \times n matrix JJ. The tangent directions are those with Jv=0Jv = 0. Step 2 corrects along J⊤s(t)J^\top s(t) with s(t)∈Rps(t) \in \mathbb R^p, and needs the p×pp \times p matrix JJ⊤JJ^\top to be invertible. It is, when the rows of JJ are linearly independent: JJ⊤s=0JJ^\top s = 0 gives ∥J⊤s∥2=0\lVert J^\top s\rVert^2 = 0, so J⊤s=0J^\top s = 0 and s=0s = 0. Step 3 splits ∇f(x∗)\nabla f(x^*) into J⊤αJ^\top\alpha plus a ww with Jw=0Jw = 0, and gives

∇f(x∗)+∑j=1pνj∇hj(x∗)=0.\nabla f(x^*) + \sum_{j=1}^p \nu_j \nabla h_j(x^*) = 0 .

Linear independence of the ∇hj(x∗)\nabla h_j(x^*) is a constraint qualification. Without it the multipliers may not exist; the section on failures below has an example.

For the opening problem, ∇f=(2x1,4x2)\nabla f = (2x_1, 4x_2) and ∇h=(1,1)\nabla h = (1, 1). At x∗=(23,13)x^* = (\tfrac23, \tfrac13), ∇f=(43,43)\nabla f = (\tfrac43, \tfrac43), parallel to (1,1)(1, 1), with ν=−43\nu = -\tfrac43. At the feasible point (0,1)(0, 1), instead, ∇f=(0,4)\nabla f = (0, 4) is not parallel to (1,1)(1, 1). Its component along the line, in the unit direction (1,−1)/2(1, -1)/\sqrt2, is −4/2=−2.83-4/\sqrt2 = -2.83, so moving along the line towards larger x1x_1 lowers ff. In the figure, the level set of ff through x∗x^* touches the line; through (0,1)(0, 1) it crosses it.

The Lagrangian#

The conditions are the stationarity conditions of one function. The Lagrangian adds each constraint to the objective with its multiplier (Deisenroth, Faisal and Ong, 2020, Section 7.2):

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

Its gradient in xx is ∇f+∑jνj∇hj\nabla f + \sum_j \nu_j\nabla h_j, and its gradient in ν\nu is the vector of constraint values hj(x)h_j(x). Setting both to zero gives the conditions above: a constrained problem in nn variables becomes n+pn + p equations in n+pn + p unknowns, with no constraints left. For the opening problem they are 2x1+ν=02x_1 + \nu = 0, 4x2+ν=04x_2 + \nu = 0 and x1+x2=1x_1 + x_2 = 1, a linear system with the solution (23,13,−43)(\tfrac23, \tfrac13, -\tfrac43).

The point (x∗,ν∗)(x^*, \nu^*) is only a stationary point of L\mathcal L. At any xx off the constraint, the Lagrangian is linear in ν\nu with the nonzero slope h(x)h(x), so it is unbounded below and has no minimum. Like a zero gradient, the conditions give candidates. The section on failures below shows how to sort them, and the duality note when they give the answer.

The nearest point on a plane#

The shortest path from a point to a plane runs along the plane’s normal. For the plane a⊤x=ba^\top x = b and a point pp, minimize ∥x−p∥2\lVert x - p\rVert^2 with the Lagrangian ∥x−p∥2+ν(a⊤x−b)\lVert x - p\rVert^2 + \nu(a^\top x - b). Stationarity in xx, then the constraint, give

2(x−p)+ν a=0 ⟹ x=p−ν2 a,a⊤p−ν2∥a∥2=b ⟹ ν=2(a⊤p−b)∥a∥2.\begin{aligned} 2(x - p) + \nu\,a = 0 \ &\Longrightarrow\ x = p - \tfrac\nu2\,a , \\ a^\top p - \tfrac\nu2\lVert a\rVert^2 = b \ &\Longrightarrow\ \nu = \frac{2(a^\top p - b)}{\lVert a\rVert^2} . \end{aligned}

Putting ν\nu back gives the nearest point and its distance:

x∗=p−a⊤p−b∥a∥2 a,∥x∗−p∥=∣a⊤p−b∣∥a∥.\begin{aligned} x^* &= p - \frac{a^\top p - b}{\lVert a\rVert^2}\,a , \\ \lVert x^* - p\rVert &= \frac{\lvert a^\top p - b\rvert}{\lVert a\rVert} . \end{aligned}

The objective is convex and the constraint affine, so by the sufficiency result below this stationary point is the global minimum. The distance formula is the one the SVM note uses for the margin of a hyperplane.

For a=(1,2,2)a = (1, 2, 2), b=3b = 3 and p=(1,1,1)p = (1, 1, 1): a⊤p=5a^\top p = 5, ∥a∥2=9\lVert a\rVert^2 = 9 and ν=49\nu = \tfrac49. The nearest point is x∗=p−29a=(79,59,59)x^* = p - \tfrac29 a = (\tfrac79, \tfrac59, \tfrac59), which satisfies 79+109+109=3\tfrac79 + \tfrac{10}9 + \tfrac{10}9 = 3, at distance 23\tfrac23.

Maximum entropy#

Among all distributions with a given mean, which one spreads its probability most evenly? Take values x1,…,xkx_1, \dots, x_k and maximize the entropy H(p)=−∑ipilog⁡piH(p) = -\sum_i p_i\log p_i subject to ∑ipi=1\sum_i p_i = 1 and ∑ipixi=m\sum_i p_i x_i = m. Equivalently, minimize ∑ipilog⁡pi\sum_i p_i \log p_i with a multiplier α\alpha for the normalization and β\beta for the mean. The derivative of the Lagrangian in pip_i is log⁡pi+1+α+βxi\log p_i + 1 + \alpha + \beta x_i, and setting it to zero gives

pi=e−1−α e−βxi,pi=e−βxi∑je−βxj,\begin{aligned} p_i &= e^{-1 - \alpha}\, e^{-\beta x_i} , \\ p_i &= \frac{e^{-\beta x_i}}{\sum_j e^{-\beta x_j}} , \end{aligned}

where the normalization fixes e−1−αe^{-1-\alpha}, and β\beta is chosen so that the mean is mm. Such a β\beta exists when mm lies strictly between the smallest and the largest xix_i. The mean of this pp is continuous in β\beta; it tends to the largest xix_i as β→−∞\beta \to -\infty and to the smallest as β→+∞\beta \to +\infty, so by the intermediate value theorem it passes through mm. A constraint on any expected value gives, by the same steps, an exponential of the constrained quantity (Jaynes, 1957).

Every pip_i is positive, so the omitted constraints pi≥0p_i \ge 0 are inactive. The objective ∑ipilog⁡pi\sum_i p_i\log p_i is convex: its Hessian is diagonal with entries 1/pi>01/p_i \gt 0. The constraints are affine, so by the sufficiency result below the stationary point is the global minimum of ∑ipilog⁡pi\sum_i p_i \log p_i, the maximum of the entropy. Without the mean constraint, β=0\beta = 0 and the answer is the uniform distribution.

A die with faces 1,…,61, \dots, 6 constrained to the mean 4.54.5 gives:

QuantityValue
β\beta−0.371-0.371
p1,…,p6p_1, \dots, p_60.054,0.079,0.114,0.165,0.240,0.3470.054, 0.079, 0.114, 0.165, 0.240, 0.347
entropy1.6141.614 nats
entropy of the fair die, log⁡6\log 61.7921.792 nats

The probabilities grow geometrically, by the factor e0.371=1.45e^{0.371} = 1.45 per face, the least committal way to raise the mean.

The formula is the softmax of the scores −βxi-\beta x_i. The softmax is the maximum-entropy distribution with prescribed expected scores, and the exponential families of statistics arise the same way. Replace the entropy by minus a KL divergence to a reference distribution, and move the reward from a constraint into the objective, and the same steps give the optimum of KL-regularized reinforcement learning: the reference reweighted by an exponential of the reward. Entropy itself is the subject of the entropy note.

What the multiplier measures#

The multiplier is the price of the constraint. Move the constraint to h(x)=uh(x) = u and let the solution x∗(u)x^*(u) depend smoothly on uu. The optimal value f∗(u)f^*(u) then changes at the rate

df∗du=∇f(x∗)⊤dx∗du=−ν∗ ∇h(x∗)⊤dx∗du=−ν∗ dduh(x∗(u))=−ν∗.\begin{aligned} \frac{d f^*}{du} &= \nabla f(x^*)^\top\frac{d x^*}{du} \\ &= -\nu^*\,\nabla h(x^*)^\top\frac{d x^*}{du} \\ &= -\nu^*\,\frac{d}{du}h\big(x^*(u)\big) = -\nu^* . \end{aligned}

The first line is the chain rule, the second the stationarity ∇f=−ν∇h\nabla f = -\nu\nabla h, and the third the chain rule backwards: h(x∗(u))=uh(x^*(u)) = u has derivative 1.

For the plane, f∗(b)=(a⊤p−b)2/∥a∥2f^*(b) = (a^\top p - b)^2/\lVert a\rVert^2, whose derivative at b=3b = 3 is −2⋅2/9=−49=−ν-2 \cdot 2/9 = -\tfrac49 = -\nu; here h(x)=a⊤x−bh(x) = a^\top x - b, so moving bb is moving uu. For the opening problem, f∗(s)=23s2f^*(s) = \tfrac23 s^2 on the line x1+x2=sx_1 + x_2 = s, with derivative 43=−ν\tfrac43 = -\nu at s=1s = 1. In economics the multiplier is called a shadow price (Boyd and Vandenberghe, 2004, §5.4.4; §5.6.3 derives the same rate under strong duality, assuming only that the optimal value is differentiable in uu). In the SVM it measures how much the optimal objective would fall if the margin required of one training point were relaxed a little.

Inequality constraints: active or inactive#

Figure

An inequality constraint: active or inactive

Projecting a point onto a triangle in two casesTwo panels with a shaded triangle and concentric circles. Left, the centre of the circles lies inside the triangle and is the solution. Right, the centre lies outside; the smallest circle that reaches the triangle touches its slanted side, and from the point of contact two arrows in the same direction point outward: the constraint gradient and the descent direction.g₁ = 0g₂ = 0g₃ = 0cx*c inside: no constraint activex* = c, ∇f(x*) = 0, all λᵢ = 0g₁ = 0g₂ = 0g₃ = 0∇g₁−∇fcx*c outside: g₁ active, g₂, g₃ inactive−∇f = λ₁∇g₁ with λ₁ = 1; λ₂ = λ₃ = 0Projecting a point onto a triangle in two casesTwo panels with a shaded triangle and concentric circles. Left, the centre of the circles lies inside the triangle and is the solution. Right, the centre lies outside; the smallest circle that reaches the triangle touches its slanted side, and from the point of contact two arrows in the same direction point outward: the constraint gradient and the descent direction.g₁ = 0g₂ = 0g₃ = 0cx*c inside: no constraint activex* = c, ∇f(x*) = 0, all λᵢ = 0g₁ = 0g₂ = 0g₃ = 0∇g₁−∇fcx*c outside: g₁ active, g₂, g₃ inactive−∇f = λ₁∇g₁ with λ₁ = 1; λ₂ = λ₃ = 0
Minimizing ‖x − c‖² over the triangle x₁ + x₂ ≤ 2, x₁ ≥ 0, x₂ ≥ 0; level sets are circles around c. Left, c = (0.8, 0.6) inside: x* = c and every multiplier is zero. Right, c = (2, 1) outside: x* = (1.5, 0.5) on the side x₁ + x₂ = 2, where −∇f points along the outward normal and λ₁ = 1.

An inequality constraint g(x)≤0g(x) \le 0 is active at xx if g(x)=0g(x) = 0 and inactive if g(x)<0g(x) \lt 0. At a local minimum x∗x^* there are two cases.

If the constraint is inactive, every small move keeps the point feasible, so x∗x^* is an unconstrained local minimum and ∇f(x∗)=0\nabla f(x^*) = 0. The constraint plays no role; give it the multiplier λ=0\lambda = 0.

If it is active, x∗x^* is also a local minimum of ff on the smaller set {g=0}\{g = 0\}, which contains it. The equality case then gives ∇f(x∗)+λ∇g(x∗)=0\nabla f(x^*) + \lambda\nabla g(x^*) = 0 for some λ\lambda, provided ∇g(x∗)≠0\nabla g(x^*) \ne 0. Now the sign matters: λ≥0\lambda \ge 0. Suppose λ<0\lambda \lt 0, so that ∇f=−λ∇g≠0\nabla f = -\lambda\nabla g \ne 0, and step from x∗x^* along the descent direction d=−∇f(x∗)=λ∇g(x∗)d = -\nabla f(x^*) = \lambda\nabla g(x^*). First-order Taylor expansions in t>0t \gt 0 give

g(x∗+td)=tλ ∥∇g∥2+o(t)<0,f(x∗+td)=f(x∗)−t ∥∇f∥2+o(t)<f(x∗)\begin{aligned} g(x^* + td) &= t\lambda\,\lVert\nabla g\rVert^2 + o(t) \lt 0 , \\ f(x^* + td) &= f(x^*) - t\,\lVert\nabla f\rVert^2 + o(t) \lt f(x^*) \end{aligned}

for small tt. The step stays feasible and lowers ff, which contradicts the minimum. With λ≥0\lambda \ge 0 the descent direction points outward, against the wall.

In one variable: minimize (x−2)2(x - 2)^2 subject to x≤1x \le 1, that is g(x)=x−1≤0g(x) = x - 1 \le 0. The unconstrained minimum x=2x = 2 is infeasible, so the constraint is active, x∗=1x^* = 1, and f′(1)+λg′(1)=−2+λ=0f'(1) + \lambda g'(1) = -2 + \lambda = 0 gives λ=2≥0\lambda = 2 \ge 0. With the looser constraint x≤3x \le 3, the unconstrained minimum is feasible, the constraint is inactive and λ=0\lambda = 0. The multiplier is again a price: moving the wall from 1 to 1+u1 + u lowers the optimal value (u−1)2(u - 1)^2 at the rate 2=λ2 = \lambda.

Both panels of the figure minimize ∥x−c∥2\lVert x - c\rVert^2 over the triangle x1+x2≤2x_1 + x_2 \le 2, x1≥0x_1 \ge 0, x2≥0x_2 \ge 0, written as g1=x1+x2−2g_1 = x_1 + x_2 - 2, g2=−x1g_2 = -x_1 and g3=−x2g_3 = -x_2, all ≤0\le 0. For c=(0.8,0.6)c = (0.8, 0.6) inside the triangle the answer is cc itself, no constraint is active and every multiplier is zero. For c=(2,1)c = (2, 1) outside it the answer is x∗=(1.5,0.5)x^* = (1.5, 0.5) on the slanted side, with ∇f(x∗)=2(x∗−c)=(−1,−1)\nabla f(x^*) = 2(x^* - c) = (-1, -1) and ∇g1=(1,1)\nabla g_1 = (1, 1), so λ1=1\lambda_1 = 1; g2g_2 and g3g_3 are inactive and λ2=λ3=0\lambda_2 = \lambda_3 = 0.

The KKT conditions#

The general problem has mm inequalities and pp equalities, all with differentiable functions:

minimizef(x)subject togi(x)≤0,i=1,…,m,hj(x)=0,j=1,…,p.\begin{aligned} \text{minimize}\quad & f(x) \\ \text{subject to}\quad & g_i(x) \le 0, \quad i = 1, \dots, m , \\ & h_j(x) = 0, \quad j = 1, \dots, p . \end{aligned}

A point that satisfies all constraints is feasible. Each inequality gets a multiplier λi\lambda_i and each equality a multiplier νj\nu_j. Combining the Lagrange condition for the equalities with the two cases for each inequality, a point x∗x^* with multipliers λ,ν\lambda, \nu satisfies the Karush–Kuhn–Tucker conditions if

∇f(x∗)+∑iλi∇gi(x∗)+∑jνj∇hj(x∗)=0,gi(x∗)≤0,hj(x∗)=0,λi≥0,λi gi(x∗)=0.\boxed{ \begin{aligned} &\nabla f(x^*) + \textstyle\sum_i \lambda_i\nabla g_i(x^*) + \sum_j \nu_j\nabla h_j(x^*) = 0 , \\ &g_i(x^*) \le 0, \quad h_j(x^*) = 0 , \\ &\lambda_i \ge 0 , \\ &\lambda_i\, g_i(x^*) = 0 . \end{aligned} }

The four lines are called stationarity, primal feasibility, dual feasibility and complementary slackness.

Complementary slackness encodes the case split. The product λigi\lambda_i g_i of a non-negative and a non-positive number is zero only if one factor is zero. Either the constraint is active (gi=0g_i = 0) and its multiplier may be positive, or it is inactive and its multiplier is zero. A positive multiplier certifies that the constraint is tight; a slack constraint has no price. In the SVM, the training points with positive multipliers are exactly the support vectors, which lie on or inside the margin. Both factors can vanish at once, for a constraint that touches the solution without pushing on it.

At a local minimum the KKT conditions hold if a constraint qualification holds there. The one derived below is LICQ: the gradients of the active inequalities and of the equalities are linearly independent at x∗x^*. Two others are stated with references. For a convex problem, Slater’s condition, a point that satisfies every inequality strictly, makes the KKT conditions necessary and sufficient (Boyd and Vandenberghe, 2004, §5.5.3); the duality note proves the key step. Affine constraints need no qualification at all (Merkulov, 2024, lecture 6).

Derivation: the KKT conditions under LICQ

Let AA be the set of active inequalities at x∗x^*. Stack the gradients ∇gi(x∗)\nabla g_i(x^*) for i∈Ai \in A and ∇hj(x∗)\nabla h_j(x^*) as the rows of a matrix JJ; LICQ says these rows are independent. An inactive constraint has gi(x∗)<0g_i(x^*) \lt 0 and, by continuity, stays negative near x∗x^*, so it plays no role: give it λi=0\lambda_i = 0. This makes complementary slackness hold.

Stationarity. The point x∗x^* is a local minimum of ff on the smaller set where every active constraint holds with equality. That set is defined by equalities with independent gradients, so the equality case gives multipliers with ∇f(x∗)+∑i∈Aλi∇gi(x∗)+∑jνj∇hj(x∗)=0\nabla f(x^*) + \sum_{i \in A}\lambda_i\nabla g_i(x^*) + \sum_j \nu_j\nabla h_j(x^*) = 0.

Signs. Fix k∈Ak \in A and let ee be the vector with −1-1 in the row of gkg_k and zeros elsewhere. Independent rows make JJ⊤JJ^\top invertible, so v=J⊤(JJ⊤)−1ev = J^\top(JJ^\top)^{-1}e solves Jv=eJv = e. Stack the active constraint functions into c(x)c(x) and apply step 2 of the equality case to F(t,s)=c(x∗+tv+J⊤s)−teF(t, s) = c(x^* + tv + J^\top s) - te. It gives a curve with x′(0)=vx'(0) = v along which gk=−tg_k = -t, the other active constraints stay at zero, and the inactive ones stay negative. For small t>0t \gt 0 the curve is feasible, so f(x(t))≥f(x∗)f(x(t)) \ge f(x^*) and the derivative at t=0t = 0 is non-negative:

0≤∇f(x∗)⊤v=−∑i∈Aλi∇gi⊤v−∑jνj∇hj⊤v=λk,\begin{aligned} 0 &\le \nabla f(x^*)^\top v \\ &= -\textstyle\sum_{i \in A}\lambda_i\nabla g_i^\top v - \sum_j \nu_j\nabla h_j^\top v \\ &= \lambda_k , \end{aligned}

because ∇gk⊤v=−1\nabla g_k^\top v = -1 and every other row of JvJv is zero.

For a convex problem, with ff and every gig_i convex and every hjh_j affine, the conditions are also sufficient: any point that satisfies them is a global minimum. For every feasible xx,

f(x)≥f(x)+∑iλigi(x)+∑jνjhj(x)=L(x,λ,ν)≥L(x∗,λ,ν)=f(x∗).\begin{aligned} f(x) &\ge f(x) + \textstyle\sum_i \lambda_i g_i(x) + \sum_j \nu_j h_j(x) \\ &= \mathcal L(x, \lambda, \nu) \\ &\ge \mathcal L(x^*, \lambda, \nu) \\ &= f(x^*) . \end{aligned}

The first line holds because each λigi(x)≤0\lambda_i g_i(x) \le 0 and each hj(x)=0h_j(x) = 0. The third holds because L(⋅,λ,ν)\mathcal L(\cdot, \lambda, \nu), a non-negative combination of convex functions plus affine terms, is convex and stationary at x∗x^*, so it lies above its tangent there (convexity note). The last uses complementary slackness and hj(x∗)=0h_j(x^*) = 0 (Boyd and Vandenberghe, 2004, §5.5.3). In the figure, the multipliers λ=(1,0,0)\lambda = (1, 0, 0) and the point (1.5,0.5)(1.5, 0.5) satisfy all four conditions, so the point is the global minimum.

Projection onto a box and onto the simplex#

Many algorithms need the nearest point of a convex set CC to a given vv, the projection

ΠC(v)=arg⁡min⁡x∈C 12∥x−v∥2.\Pi_C(v) = \arg\min_{x \in C}\ \tfrac12\lVert x - v\rVert^2 .

Projected gradient descent alternates a gradient step with a projection, x←ΠC(x−η∇f(x))x \leftarrow \Pi_C(x - \eta\nabla f(x)), and so solves constrained problems with the machinery of the previous note. For two sets used constantly in machine learning, the KKT conditions give the projection in closed form.

The box {x:li≤xi≤ui}\{x : l_i \le x_i \le u_i\}. The problem separates into coordinates. For one coordinate, with multipliers λiu\lambda_i^u for xi−ui≤0x_i - u_i \le 0 and λil\lambda_i^l for li−xi≤0l_i - x_i \le 0, stationarity reads xi−vi+λiu−λil=0x_i - v_i + \lambda_i^u - \lambda_i^l = 0. If vi>uiv_i \gt u_i, the upper bound must be active, since otherwise xi=vi+λil≥vix_i = v_i + \lambda_i^l \ge v_i would be infeasible; then xi=uix_i = u_i and λiu=vi−ui>0\lambda_i^u = v_i - u_i \gt 0. The lower bound is symmetric. So the projection is clipping,

Πbox(v)i=min⁡(max⁡(vi,li),ui),\Pi_{\text{box}}(v)_i = \min\big(\max(v_i, l_i), u_i\big) ,

with multipliers equal to the amounts clipped away. For v=(1.7,−0.4,0.3)v = (1.7, -0.4, 0.3) and the unit box, x∗=(1,0,0.3)x^* = (1, 0, 0.3), with λ1u=0.7\lambda_1^u = 0.7, λ2l=0.4\lambda_2^l = 0.4 and all other multipliers zero. The soft-margin SVM dual is a problem over a box, 0≤αi≤C0 \le \alpha_i \le C, and the duality note solves it by projected gradient ascent with exactly this clipping.

The probability simplex Δ={x:xi≥0,∑ixi=1}\Delta = \{x : x_i \ge 0, \sum_i x_i = 1\}. Take the multiplier τ\tau for ∑ixi=1\sum_i x_i = 1 and λi\lambda_i for −xi≤0-x_i \le 0. Stationarity of 12∥x−v∥2+τ(∑ixi−1)−∑iλixi\tfrac12\lVert x - v\rVert^2 + \tau(\sum_i x_i - 1) - \sum_i\lambda_i x_i is xi−vi+τ−λi=0x_i - v_i + \tau - \lambda_i = 0, and the two cases of complementary slackness give

xi>0 ⟹ λi=0, xi=vi−τ,xi=0 ⟹ λi=τ−vi≥0.\begin{aligned} x_i \gt 0 \ &\Longrightarrow\ \lambda_i = 0,\ x_i = v_i - \tau , \\ x_i = 0 \ &\Longrightarrow\ \lambda_i = \tau - v_i \ge 0 . \end{aligned}

The second case happens exactly when vi≤τv_i \le \tau, so both cases are one formula:

ΠΔ(v)i=max⁡(vi−τ,0),with ∑imax⁡(vi−τ,0)=1.\Pi_\Delta(v)_i = \max(v_i - \tau, 0) , \quad \text{with } \textstyle\sum_i \max(v_i - \tau, 0) = 1 .

Subtract the same threshold from every coordinate and cut at zero. The left side of the condition on τ\tau is continuous in τ\tau, tends to infinity as τ→−∞\tau \to -\infty, is 0 once τ≥max⁡ivi\tau \ge \max_i v_i, and strictly decreases wherever it is positive. So exactly one τ\tau works. Sort vv in decreasing order, u1≥u2≥…u_1 \ge u_2 \ge \dots; for r=1,2,…r = 1, 2, \dots compute the candidate τr=1r(∑s≤rus−1)\tau_r = \frac1r\big(\sum_{s \le r} u_s - 1\big), and take the largest rr with ur>τru_r \gt \tau_r. The sort dominates the cost, O(nlog⁡n)O(n\log n) operations (Martins and Astudillo, 2016, Proposition 1 and Algorithm 1).

Derivation: why the largest such r gives the threshold

Let ρ\rho be the number of positive coordinates of the projection. They are the ρ\rho largest usu_s, and the condition ∑s≤ρ(us−τ)=1\sum_{s \le \rho}(u_s - \tau) = 1 gives τ=τρ\tau = \tau_\rho. At r=ρr = \rho the test passes, since uρ>τ=τρu_\rho \gt \tau = \tau_\rho. For r>ρr \gt \rho we have ur≤τu_r \le \tau, and every usu_s with s≤rs \le r is at least uru_r, so

r τr=∑s≤ρus−1+∑ρ<s≤rus=ρ τ+∑ρ<s≤rus≥ρ ur+(r−ρ) ur=r ur,\begin{aligned} r\,\tau_r &= \textstyle\sum_{s \le \rho} u_s - 1 + \sum_{\rho \lt s \le r} u_s \\ &= \rho\,\tau + \textstyle\sum_{\rho \lt s \le r} u_s \\ &\ge \rho\, u_r + (r - \rho)\, u_r = r\, u_r , \end{aligned}

and the test fails. So ρ\rho is the largest rr that passes.

For v=(0.5,1.2,−0.3)v = (0.5, 1.2, -0.3) the sorted values are 1.2,0.5,−0.31.2, 0.5, -0.3:

rrτr\tau_rur−τru_r - \tau_r
10.20.21.0>01.0 \gt 0
20.350.350.15>00.15 \gt 0
30.1330.133−0.433<0-0.433 \lt 0

So τ=0.35\tau = 0.35, ΠΔ(v)=(0.15,0.85,0)\Pi_\Delta(v) = (0.15, 0.85, 0) and λ3=0.35+0.3=0.65\lambda_3 = 0.35 + 0.3 = 0.65. Applied to the logits of a classifier, this projection is sparsemax (Martins and Astudillo, 2016), an alternative to the softmax that gives exactly zero probability to low-scoring classes. The softmax was the maximum-entropy answer above; sparsemax is the nearest-point answer.

The direction of maximum variance is an eigenvector#

The symmetric matrices note bounds the Rayleigh quotient using the spectral theorem. The Lagrange conditions reach the same answer without it and show why only eigenvectors can be candidates. For a covariance matrix Σ\Sigma, the variance of the data projected on a unit vector uu is u⊤Σuu^\top\Sigma u, and PCA maximizes it subject to u⊤u=1u^\top u = 1. With the Lagrangian u⊤Σu−ν(u⊤u−1)u^\top\Sigma u - \nu(u^\top u - 1) and ∇(u⊤Σu)=2Σu\nabla(u^\top\Sigma u) = 2\Sigma u for symmetric Σ\Sigma:

2Σu−2νu=0,Σu=νu,u⊤Σu=ν u⊤u=ν.\begin{aligned} 2\Sigma u - 2\nu u &= 0 , \\ \Sigma u &= \nu u , \\ u^\top\Sigma u &= \nu\,u^\top u = \nu . \end{aligned}

Every stationary point is a unit eigenvector, and the objective there is its eigenvalue. The sphere is closed and bounded, so the continuous variance attains a maximum and a minimum on it. The constraint gradient 2u2u never vanishes there, so both are stationary points, and comparing the candidates picks the largest eigenvalue as the maximum and the smallest as the minimum. The multiplier is the variance captured.

For Σ=(21.21.21)\Sigma = \begin{pmatrix} 2 & 1.2 \\ 1.2 & 1 \end{pmatrix}, the covariance of the multivariate Gaussian note, the eigenvalues are 2.8 and 0.2. The direction u=(0.832,0.555)u = (0.832, 0.555) captures variance 2.8 of the total 3, and a search over 100,001 unit vectors finds the same maximum. The dimensionality reduction note builds PCA on it.

Where it breaks: candidates that are not minima#

Without convexity the conditions list stationary points, and some of them are neither minima nor maxima. In three dimensions the maximum-variance problem shows it. Take Σ=diag⁡(3,2,1)\Sigma = \operatorname{diag}(3, 2, 1) with eigenvectors q1,q2,q3q_1, q_2, q_3, the coordinate axes. All three are KKT points, with ν=3,2,1\nu = 3, 2, 1. At u=q2u = q_2 the variance is 2, yet turning uu by 0.10.1 radians towards q1q_1 raises it to 2.012.01, and turning it towards q3q_3 lowers it to 1.991.99. The middle eigenvector is a saddle of the variance on the sphere.

A second-order test sorts the candidates. Why the Hessian of the Lagrangian, and not of ff? Take a curve x(t)x(t) on the surface through x∗x^* with velocity vv. On the surface h=0h = 0, so f(x(t))=L(x(t),ν∗)f(x(t)) = \mathcal L(x(t), \nu^*), and two differentiations by the chain rule give

ddtf(x(t))=∇xL(x(t))⊤x′(t),d2dt2f(x(t))∣0=v⊤∇xx2L v+∇xL(x∗)⊤x′′(0)=v⊤∇xx2L v,\begin{aligned} \tfrac{d}{dt} f(x(t)) &= \nabla_x\mathcal L(x(t))^\top x'(t) , \\ \tfrac{d^2}{dt^2} f(x(t))\big|_{0} &= v^\top \nabla^2_{xx}\mathcal L\, v + \nabla_x\mathcal L(x^*)^\top x''(0) \\ &= v^\top \nabla^2_{xx}\mathcal L\, v , \end{aligned}

since ∇xL(x∗)=0\nabla_x\mathcal L(x^*) = 0. The acceleration x′′(0)x''(0), the bending of the surface, drops out only for L\mathcal L; with ff alone it would stay. The test: if v⊤∇xx2L v>0v^\top \nabla^2_{xx}\mathcal L\, v \gt 0 for every nonzero tangent vv, those with ∇hj(x∗)⊤v=0\nabla h_j(x^*)^\top v = 0, the KKT point is a strict local minimum (Merkulov, 2024, lecture 6). Applied to −f-f, negative values for every such vv mean a strict local maximum.

Here ∇uu2L=2(Σ−νI)\nabla^2_{uu}\mathcal L = 2(\Sigma - \nu I), and at u=q2u = q_2, ν=2\nu = 2, it gives q1⊤(2Σ−4I)q1=2q_1^\top(2\Sigma - 4I)q_1 = 2 and q3⊤(2Σ−4I)q3=−2q_3^\top(2\Sigma - 4I)q_3 = -2: mixed signs, a saddle. At q1q_1 both tangent directions give negative values, a strict maximum. For one active inequality with λ>0\lambda \gt 0 the same test runs over the directions with ∇g(x∗)⊤v=0\nabla g(x^*)^\top v = 0 (Merkulov, lecture 6).

The first-order conditions can also miss the answer. Minimize xx subject to x2≤0x^2 \le 0. The only feasible point, x=0x = 0, is the solution, but there ∇g=0\nabla g = 0, and 1+λ⋅0=01 + \lambda \cdot 0 = 0 has no solution. The constraint qualification fails: the constraint’s gradient vanishes, so no multiple of it can balance ∇f\nabla f.

Even when everything is regular, the KKT system is hard to solve in general, because nobody knows in advance which constraints are active. Guessing the active set is the combinatorial core of constrained optimization; for the box and the simplex above it is trivial, but a projection onto a general polyhedron is already a quadratic program.

What you should remember#

  • With equality constraints, ∇f+∑jνj∇hj=0\nabla f + \sum_j\nu_j\nabla h_j = 0 at a minimum: the Lagrangian is stationary.
  • The multiplier is the price of the constraint: df∗/du=−νdf^*/du = -\nu for h=uh = u.
  • KKT: stationarity, feasibility, λi≥0\lambda_i \ge 0, λigi=0\lambda_i g_i = 0; necessary under a constraint qualification, sufficient for convex problems.
  • Projection on a box clips; on the simplex it is max⁡(vi−τ,0)\max(v_i - \tau, 0).
  • Maximum entropy with a fixed mean gives pi∝e−βxip_i \propto e^{-\beta x_i}; maximum variance on the sphere gives the top eigenvector.
  • Without convexity, check the Hessian of the Lagrangian on tangent directions.

Code#

The opening problem as a linear system, the plane, maximum entropy, the two KKT cases of the figure, the box and simplex projections checked against SciPy’s SLSQP solver, the maximum-variance direction, and the saddle among its KKT points. Exact digits can differ slightly across platforms and library versions.

import numpy as np
from scipy.optimize import brentq, minimize

# min x1^2 + 2 x2^2 s.t. x1 + x2 = 1: the stationarity equations of the Lagrangian are linear
K = np.array([[2., 0, 1], [0, 4, 1], [1, 1, 0]])          # 2 x1 + nu = 0, 4 x2 + nu = 0, x1 + x2 = 1
x1, x2, nu = np.linalg.solve(K, [0, 0, 1])
print(round(x1, 4), round(x2, 4), round(nu, 4), round(x1**2 + 2 * x2**2, 4))
# 0.6667 0.3333 -1.3333 0.6667
fstar = lambda s: np.linalg.solve(K, [0, 0, s])[:2] @ np.diag([1., 2]) @ np.linalg.solve(K, [0, 0, s])[:2]
print(round((fstar(1 + 1e-6) - fstar(1 - 1e-6)) / 2e-6, 4))   # d f*/ds = -nu
# 1.3333

# the nearest point on the plane a.x = b to p
a, b, p = np.array([1., 2, 2]), 3.0, np.array([1., 1, 1])
nu = 2 * (a @ p - b) / (a @ a); x = p - nu / 2 * a
print(x.round(4), round(a @ x, 10), round(np.linalg.norm(x - p), 4), round(nu, 4))
pst = lambda b: (a @ p - b) ** 2 / (a @ a)                 # the optimal value as a function of b
print(round((pst(b + 1e-6) - pst(b - 1e-6)) / 2e-6, 4))
# [0.7778 0.5556 0.5556] 3.0 0.6667 0.4444
# -0.4444

# maximum entropy on {1, ..., 6} with mean 4.5: p_i proportional to exp(-beta i)
k = np.arange(1, 7)
mean = lambda beta: k @ np.exp(-beta * k) / np.exp(-beta * k).sum()
beta = brentq(lambda t: mean(t) - 4.5, -5, 5)
pm = np.exp(-beta * k); pm /= pm.sum()
res = minimize(lambda q: q @ np.log(q), np.ones(6) / 6, method='SLSQP', bounds=[(1e-9, 1)] * 6,
               constraints=[{'type': 'eq', 'fun': lambda q: q.sum() - 1}, {'type': 'eq', 'fun': lambda q: q @ k - 4.5}],
               options={'ftol': 1e-14, 'maxiter': 500})
print(round(beta, 4), pm.round(4), round(-pm @ np.log(pm), 4), round(np.log(6), 4), np.abs(res.x - pm).max() < 1e-5)
print(round(np.exp(-beta), 3))                              # the ratio of neighbouring probabilities
# -0.371 [0.0544 0.0788 0.1142 0.1654 0.2398 0.3475] 1.6136 1.7918 True
# 1.449

# KKT with active and inactive constraints: min |x - c|^2 s.t. x1 + x2 <= 2, x1 >= 0, x2 >= 0
for c in (np.array([2., 1]), np.array([0.8, 0.6])):
    r = minimize(lambda x: (x - c) @ (x - c), np.zeros(2), method='SLSQP', bounds=[(0, None)] * 2,
                 constraints=[{'type': 'ineq', 'fun': lambda x: 2 - x.sum()}], options={'ftol': 1e-14})
    xs = r.x; lam1 = -2 * (xs - c)[0] if abs(xs.sum() - 2) < 1e-9 else 0.0   # stationarity: 2(x - c) + lam1 (1, 1) = 0
    print(c, xs.round(4), round(lam1, 4), abs(round(lam1 * (xs.sum() - 2), 12)))
# [2. 1.] [1.5 0.5] 1.0 0.0
# [0.8 0.6] [0.8 0.6] 0.0 0.0

# projection onto a box is clipping; the multiplier of a bound is the distance clipped away
v = np.array([1.7, -0.4, 0.3]); xb = np.clip(v, 0, 1)
print(xb, np.maximum(v - 1, 0), np.maximum(-v, 0))         # x, multipliers of x <= 1, multipliers of x >= 0
# [1.  0.  0.3] [0.7 0.  0. ] [0.  0.4 0. ]

# projection onto the probability simplex: x = max(v - tau, 0), tau from the sorted values
def proj_simplex(v):
    u = np.sort(v)[::-1]; css = np.cumsum(u) - 1
    rho = np.nonzero(u - css / np.arange(1, len(v) + 1) > 0)[0][-1]
    tau = css[rho] / (rho + 1)
    return np.maximum(v - tau, 0), tau
v = np.array([0.5, 1.2, -0.3]); xs, tau = proj_simplex(v)
lam = xs - v + tau                                          # stationarity of 1/2|x - v|^2 + tau (sum x - 1) - lam.x
r = minimize(lambda x: 0.5 * (x - v) @ (x - v), np.ones(3) / 3, method='SLSQP', bounds=[(0, None)] * 3,
             constraints=[{'type': 'eq', 'fun': lambda x: x.sum() - 1}], options={'ftol': 1e-14})
print(xs, tau, lam.round(4), lam @ xs, np.abs(r.x - xs).max() < 1e-6)
rng = np.random.default_rng(0)                              # 1,000 random vectors against SLSQP
print(max(np.abs(proj_simplex(w)[0] - minimize(lambda x: 0.5 * (x - w) @ (x - w), np.ones(5) / 5, method='SLSQP', bounds=[(0, None)] * 5,
      constraints=[{'type': 'eq', 'fun': lambda x: x.sum() - 1}], options={'ftol': 1e-14}).x).max() for w in rng.normal(size=(1000, 5))) < 1e-6)
# [0.15 0.85 0.  ] 0.35 [0.   0.   0.65] 0.0 True
# True

# the direction of maximum variance: stationary points of u' S u on the unit circle are eigenvectors
S = np.array([[2., 1.2], [1.2, 1]])
ang = np.linspace(0, np.pi, 100_001); U = np.stack([np.cos(ang), np.sin(ang)])
var = np.einsum('in,ij,jn->n', U, S, U)
w, V = np.linalg.eigh(S)
print(w, np.abs(V[:, 1]).round(3), round(var.max(), 6), round(var.min(), 6), U[:, var.argmax()].round(3))
# [0.2 2.8] [0.832 0.555] 2.8 0.2 [0.832 0.555]

# in three dimensions the middle eigenvector is a KKT point but a saddle: S = diag(3, 2, 1)
S = np.diag([3., 2, 1]); q1, q2, q3 = np.eye(3)
turn = lambda q, s: (np.cos(s) * q2 + np.sin(s) * q) @ S @ (np.cos(s) * q2 + np.sin(s) * q)   # variance after turning q2 by s
H = 2 * (S - 2 * np.eye(3))                                 # Hessian of the Lagrangian at u = q2, nu = 2
print(round(turn(q1, 0.1), 4), round(turn(q3, 0.1), 4), q1 @ H @ q1, q3 @ H @ q3)
# 2.01 1.99 2.0 -2.0

Next#

The sufficiency proof compared f(x)f(x) with the Lagrangian, which on the feasible set is never larger than the objective. The next note makes that bound the main object. Minimizing the Lagrangian over xx for fixed multipliers gives a function of the multipliers alone, the dual function, and its maximum is the best such lower bound. For convex problems it equals the optimum, which turns the SVM into a problem over its multipliers and gives every solver a certificate of how far it is from optimal.

References#