Before this lesson:lesson 02 (convex
functions, the first-order condition); gradients and level sets (lesson 01,
calculus 02); projections
(linear algebra 03); the ℓ1 and
ℓ∞ norms (linear algebra 11) for the lasso dual.
Time: about 95 minutes, plus the notebook.
Constraints are everywhere in ML. A probability vector must lie on the simplex. A physical parameter
has bounds. A fairness or safety requirement limits a model. A simulation must satisfy its equations.
The KKT conditions tell you what a constrained solution looks like, so you can often write it in
closed form: projections, sparsemax, the support vector machine (SVM). Each multiplier is the price
of its constraint, which tells you which constraints matter. Duality turns every price into a lower
bound on the optimal value. This gives stopping rules with a guarantee (scikit-learn's Lasso stops
on a duality gap), and the SVM dual, where kernels and support vectors appear. In science, the
multipliers of a PDE constraint are the adjoint variables used to compute gradients of
simulations.
2.1 One equality constraint: walk along the curve #
Take a function f(x) of x=(x1,x2) and a constraint h(x)=0. The constraint is a curve in
the plane. We want the point of the curve where f is smallest.
Walk along the curve and watch the contour lines (level sets) of f. Recall two facts from
lesson 01: ∇f is perpendicular to the
level set of f through the point, and ∇h is perpendicular to the curve h=0.
Where the curve crosses a level set, one direction along the curve goes downhill. So this
point is not the minimum: you can still lower f.
At the minimum, no direction along the curve goes downhill. The curve touches the level set
there: they are tangent. Two curves that are tangent have the same normal direction.
So at a constrained minimum x⋆, the gradients are parallel:
∇f(x⋆)+ν∇h(x⋆)=0for some number ν.
The number ν is the Lagrange multiplier. It can be positive or negative. We assume
∇h(x⋆)=0, so that the curve has a normal direction at x⋆.
The same argument with a formula. Let τ be a unit tangent vector of the curve at x. The
rate of change of f along the curve is the directional derivative ∇f(x)⊤τ. At a
minimum along the curve this rate must be 0; otherwise a small step along −τ or +τ lowers
f. So ∇f(x⋆)⊥τ. In the plane, the only directions perpendicular to τ
are the multiples of ∇h(x⋆). Hence ∇f(x⋆) is a multiple of ∇h(x⋆).
Interactive figure: loading…
Drag the black point along the yellow constraint curve and find where f
is smallest. The blue arrow is the direction of ∇f, the
yellow arrow the direction of ∇h, and the
magenta arrow the downhill direction along the curve. The dashed blue curve
is the level set of f through the point. At the minimum the magenta arrow vanishes, the angle
between the gradients is 0° or 180°, and the dashed level set touches the curve. The chart shows
f along the curve. Try the circle: it has a minimum and a maximum, and both satisfy the condition.
Example. Minimize f(x)=x12+x22 subject to x1+x2=1. Here h(x)=x1+x2−1,∇f=(2x1,2x2) and ∇h=(1,1).
Parallel gradients: 2x1+ν=0 and 2x2+ν=0. So x1=x2=−ν/2.
The constraint: −ν/2−ν/2=1, so ν=−1.
Hence x⋆=(0.5,0.5) and f(x⋆)=0.5.
In the figure, the level set x12+x22=0.5 is the circle of radius 1/2≈0.707,
and it touches the line at (0.5,0.5). This answers question 1 of section 1: at the lowest point,
the road touches the contour line.
Seen in 3D. The constrained minimum is not the bottom of the bowl. Lift the constraint curve onto
the surface z=f(x). The constrained minimum is the lowest point of this lifted curve.
Interactive figure: loading…
Move the slider to walk along the lifted curve, and drag the figure to
rotate it. The blue surface is f(x)=x12+x22; the
yellow curve on the floor is the constraint, and the vertical lines connect
it to its lift. The bottom of the bowl, (0,0), is not on the curve. Press Go to the lowest point:
for the line, it is (0.5,0.5) with f=0.5, the same answer as the calculation above.
Put the objective and the constraint into one function, the Lagrangian:
L(x,ν)=f(x)+νh(x).
Its stationary points are exactly the candidates of section 2.1:
∇xL=∇f(x)+ν∇h(x)=0 is the tangency condition;
∂L/∂ν=h(x)=0 is the constraint.
With x∈Rd this is d+1 equations for d+1 unknowns. Two warnings:
A stationary point of L is a saddle point of L, not a minimum. In the
example, L is linear in ν, so it has no minimum in ν.
The condition finds candidates: constrained minima, maxima and other critical points. You must
compare them (or use convexity, section 3).
Quick check
Minimize f(x)=x12+x22 on the circle (x1−2)2+x22=1. The condition ∇f+ν∇h=0 holds at two points, (1,0) and (3,0). What are they?
Why.
At (1,0):∇f=(2,0),∇h=(2(x1−2),2x2)=(−2,0), so ν=1. At (3,0):∇f=(6,0),∇h=(2,0), so ν=−3. Opposite directions are still parallel; the condition only asks for ∇f+ν∇h=0. It finds every point where the circle touches a level set, the nearest point and the farthest point. Compare the values of f to choose.
Move the constraint: require x1+x2=c instead of =1. Repeat the example: x⋆=(c/2,c/2)
and the optimal value is
p⋆(c)=4c2+4c2=2c2,dcdp⋆=c.
With h(x)=x1+x2−c, the same steps give ν=−c. So dcdp⋆=−ν. At
c=1 the slope is 1 and ν=−1.
Why this holds in general (intuition). Write the constraint as a(x)=c, so h(x)=a(x)−c
and ∇h=∇a. Change c by a small dc. The optimal point moves by some dx that keeps
the constraint, so ∇a(x⋆)⊤dx=dc. Then
dp⋆≈∇f(x⋆)⊤dx=−ν∇a(x⋆)⊤dx=−νdc.
So −ν is the price of the constraint: one more unit of c changes the best value by about
−ν. Section 3 states this precisely.
Interactive figure: loading…
Move c to slide the constraint line. The left plot shows the contours of
f and the constrained minimizer x⋆; the right plot shows the optimal value p⋆(c)
(black) and its tangent line, whose slope is −λ. For the inequality
x1+x2≤c, the multiplier is λ=2 at c=1 and becomes 0 when c≥3: then the
unconstrained minimizer (2,1) is feasible and the constraint costs nothing. Switch to the equality
to see a multiplier ν that changes sign.
Now take a constraint x≤1 in one variable, and minimize a smooth convex f0(x). There are
two cases.
Inactive. The unconstrained minimizer satisfies x≤1. The constraint does nothing:
f0′(x⋆)=0.
Active. The unconstrained minimizer is to the right of 1. The solution sits on the wall,
x⋆=1. There f0′(x⋆)<0:f0 wants to move right, but the wall stops it. Write
this balance of forces as f0′(x⋆)+λ=0, with a wall force λ>0.
Both cases are one statement. Write the constraint as f1(x)=x−1≤0. Then
f0′(x⋆)+λf1′(x⋆)=0,λ≥0,λf1(x⋆)=0.
The last condition says: the wall pushes only if you touch it. If f1(x⋆)<0 (not
touching), then λ=0. This is complementary slackness. The sign λ≥0 says
that a wall can only push you back into the feasible set, never pull you out.
Example (question 2 of section 1). Minimize (x−3)2 subject to x≤1. The constraint is
active: x⋆=1, and 2(1−3)+λ=0 gives λ=4. Relaxing to x≤1+δ
gives p⋆(δ)=(1+δ−3)2=(2−δ)2=4−4δ+δ2. So p⋆
goes down by about 4δ=λδ: the multiplier is again the price.
In two dimensions the picture is the same. The feasible set is a region; the wall is its boundary.
If the unconstrained minimizer is inside, λ=0. If it is outside, the solution is on the
boundary, where the level set of f touches the boundary, and λ>0.
Interactive figure: loading…
Drag the blue point a, the unconstrained minimizer of
f0(x)=∥x−a∥2, inside and outside the yellow feasible setf1(x)≤0. Inside, λ=0 and x⋆=a. Outside, x⋆ (black star) is on the
boundary, and the push of the wall −λ∇f1 (yellow arrow) cancels
the pull of f0,−∇f0 (blue arrow). The four KKT conditions are
listed with ticks. In the mode choose λ yourself, the orange point minimizes
f0+λf1 for your λ: too small and it is infeasible, too large and complementary
slackness fails.
For a=(2,1) and the half-plane x1+x2≤1: stationarity gives
x=(2−λ/2,1−λ/2), the active constraint gives 3−λ=1, so λ=2,x⋆=(1,0) and p⋆=2. Section 4 checks every condition.
Quick check
Minimize (x1−2)2+(x2−1)2 subject to x1+x2≤5. What is the multiplier λ of the constraint?
Why.
The unconstrained minimizer (2,1) has x1+x2=3≤5, so it is feasible: the constraint is inactive and complementary slackness forces λ=0. A multiplier of an inequality is never negative; λ=2 belongs to the active constraint x1+x2≤1.
with optimal value p⋆. A point that satisfies all constraints is feasible. The problem is
convex when f0,f1,…,fm are convex and every hj is affine (so the equalities read
Ax=b). Write every constraint in this form first: x1+x2≥2 becomes 2−x1−x2≤0.
The Lagrangian adds each constraint with its own multiplier:
L(x,λ,ν)=f0(x)+i=1∑mλifi(x)+j=1∑pνjhj(x),
with λi≥0 for an inequality and νj∈R (any sign) for an equality. In this
lesson, λ is a Lagrange multiplier, not an eigenvalue. We follow the notation of Boyd and
Vandenberghe.
A constraint with fi(x)=0 is active; one with fi(x)<0 is inactive. Complementary
slackness says that an inactive constraint has λi=0. In practice it is a case split:
for each inequality, guess "active" (fi=0) or "inactive" (λi=0), solve, and check the
signs. If the guess "active" gives λi<0, the guess was wrong.
Theorem (KKT points of convex problems are optimal) #
Let the problem be convex, with differentiable f0,…,fm. If (x~,λ~,ν~)
satisfies the four KKT conditions, then x~ is a global minimizer.
Why it is true (intuition). With the prices λ~,ν~ fixed, the Lagrangian is
a convex function of x whose gradient is zero at x~, so x~ minimizes it. On the
feasible set, the Lagrangian is never above f0, and at x~ it equals f0.
Proof.
λ~i≥0, each fi is convex and each hj is affine. So
x↦L(x,λ~,ν~) is a sum of convex functions: it is convex.
Stationarity says that its gradient is zero at x~. For a differentiable convex function,
a zero gradient means a global minimum (lesson 02,
first-order condition). So L(x,λ~,ν~)≥L(x~,λ~,ν~) for every x.
Complementary slackness and hj(x~)=0 remove the added terms at x~:L(x~,λ~,ν~)=f0(x~).
Let x be any feasible point. Then λ~ifi(x)≤0 and hj(x)=0, so
f0(x)≥L(x,λ~,ν~).
Steps 4, 2 and 3 give f0(x)≥L(x,λ~,ν~)≥L(x~,λ~,ν~)=f0(x~).■
For a nonconvex problem this fails: a KKT point can be a maximum or a saddle (the circle quiz above).
The theorem says KKT ⇒ optimal (for convex problems). The converse needs an extra assumption, a
constraint qualification. The standard one for convex problems is Slater's condition: there
is a strictly feasible point, that is, some x with fi(x)<0 for every non-affine fi
(affine inequalities only need fi(x)≤0) and Ax=b. Under Slater's condition, x⋆ is
optimal if and only if there are multipliers with which the KKT conditions hold (the proof
follows from strong duality, below).
For nonconvex problems, KKT is necessary at a local minimum under a constraint qualification such as
LICQ (the gradients of the active constraints are linearly independent), and it is not sufficient.
An example where KKT fails at the optimum
Minimize x subject to x2≤0. Only x=0 is feasible, so x⋆=0 and p⋆=0.
Stationarity asks for 1+2λx⋆=1+0=0, which no λ satisfies. So no KKT
multiplier exists at the optimum. The problem is convex, but Slater's condition fails: x2<0 is
impossible. Geometrically, ∇f1(x⋆)=2x⋆=0: the "wall" has no normal direction to
push with.
Problem. Given y∈Rn, find the closest point of the probability simplexΔ={x:x≥0,1⊤x=1}:
xminsubject to21∥x−y∥21⊤x=1,−xi≤0(i=1,…,n).
The Lagrangian is 21∥x−y∥2+ν(1⊤x−1)−λ⊤x.
Stationarity: xi−yi+ν−λi=0 for each i.
If xi>0, complementary slackness gives λi=0, so xi=yi−ν (and yi>ν).
If xi=0, then λi=ν−yi, and dual feasibility λi≥0 gives yi≤ν.
Together: xi=max(yi−ν,0).Shift every entry down by one constant ν, then clip at 0.
ν solves ∑imax(yi−ν,0)=1. The left side is continuous and strictly decreasing
where it is positive, so ν is unique. To find it, sort y in decreasing order,
u1≥⋯≥un. Let ρ be the largest j with uj−j1(∑i≤jui−1)>0.
Then ν=ρ1(∑i≤ρui−1). The cost is O(nlogn).
The problem is convex with affine constraints, so the KKT conditions are sufficient: this x is the
projection. The constant ν is a threshold (some papers call it τ). Entries below it become exactly 0.
Quick check
Project y=(0.3,0.3,0.3) onto the probability simplex. What are x and ν?
Why.
y is not on the simplex (its sum is 0.9). All three tests are positive, so ρ=3 and ν=(0.9−1)/3=−1/30. Then xi=0.3+1/30=1/3. A negative ν shifts the entries up. This is fine: ν belongs to an equality, so it has no sign condition.
Fix the prices λ≥0 and ν, and minimize the Lagrangian over allx, with no
constraints. The result is the dual function:
g(λ,ν)=xinfL(x,λ,ν).
It can be −∞. It is always concave, even when the original problem is not convex: for each
fixed x,L(x,λ,ν) is an affine function of (λ,ν) (a line, in one
dimension), and g is the lower envelope of these affine functions. A pointwise infimum of affine
functions is concave.
Take any feasible x~. Then λifi(x~)≤0 (because λi≥0 and
fi(x~)≤0), and hj(x~)=0.
So L(x~,λ,ν)≤f0(x~).
The infimum is at most the value at one point: g(λ,ν)≤L(x~,λ,ν)≤f0(x~).
This holds for every feasible x~, so g(λ,ν)≤p⋆.■
The dual problem looks for the best lower bound: maximize g(λ,ν) subject to
λ≥0. Its value is d⋆, and weak duality gives d⋆≤p⋆. The difference
p⋆−d⋆≥0 is the optimal duality gap.
Interactive figure: loading…
Move λ. Each thin line is f0(x)+λf1(x) for one fixed x:yellow for feasible x (slope f1(x)≤0), grey for infeasible x. Their
lower envelope is the dual function g(λ) (thick blue), and the
orange line belongs to the x that attains the minimum. The blue dot never
rises above the dashed line p⋆. In the convex problem the best bound reaches p⋆=4 at
λ=4. In the nonconvex problem the best bound is −3 while p⋆=−1: a gap of 2.
Strong duality means d⋆=p⋆. It does not always hold, not even for every convex
problem. Theorem (Boyd and Vandenberghe §5.2.3): if the problem is convex and Slater's condition
holds, then d⋆=p⋆, and the dual optimum is attained when d⋆>−∞.
The duality gap as a certificate. Let x be feasible and (λ,ν) dual feasible
(λ≥0,g>−∞). Then f0(x)−p⋆≤f0(x)−g(λ,ν). The right side
can be computed without knowing p⋆. When it is below a tolerance, you can stop: x is that
close to optimal. This is how scikit-learn's Lasso decides to stop.
Quick check
For some problem, a dual feasible price gives g(λ)=3.2, and a feasible point x gives f0(x)=3.5. What do you know?
Why.
Weak duality (g(λ)≤p⋆) holds for every problem, convex or not, and a feasible point gives p⋆≤f0(x). So p⋆∈[3.2,3.5] and f0(x)−p⋆≤0.3. The value 3.0 would contradict weak duality: no price can give a bound above p⋆.
Why KKT is necessary under strong duality
Suppose strong duality holds, x⋆ is primal optimal and (λ⋆,ν⋆) is dual
optimal. Then
So both inequalities are equalities. The second one gives ∑iλi⋆fi(x⋆)=0.
Every term is ≤0, so every term is 0: complementary slackness. The first one says that x⋆
minimizes L(⋅,λ⋆,ν⋆), so its gradient is zero there: stationarity.
Feasibility of x⋆ and λ⋆≥0 are given. So all four KKT conditions hold. With
Slater's condition (which gives strong duality and an attained dual optimum), optimal ⟺ KKT.
A convex problem with a positive duality gap
Boyd and Vandenberghe, exercise 5.21: minimize e−x subject to x2/y≤0, on the domain
y>0. Only x=0 is feasible, so p⋆=1. But for every λ≥0,g(λ)=infx,y>0(e−x+λx2/y)=0: take y=x3 and let
x→∞, so that both terms go to 0. So d⋆=0 and the gap is 1. The problem is convex,
but Slater's condition fails, because x2/y<0 is impossible.
Perturb the constraints to fi(x)≤ui and hj(x)=vj, with optimal value p⋆(u,v).
If strong duality holds and (λ⋆,ν⋆) is dual optimal for u=0,v=0, then for
every (u,v) (Boyd and Vandenberghe §5.6):
p⋆(u,v)≥p⋆(0,0)−λ⋆⊤u−ν⋆⊤v.
Proof. For any x feasible for the perturbed problem,
p⋆(0,0)=g(λ⋆,ν⋆)≤L(x,λ⋆,ν⋆)≤f0(x)+λ⋆⊤u+ν⋆⊤v,
because λi⋆fi(x)≤λi⋆ui and νj⋆hj(x)=νj⋆vj. Take the
infimum over such x.■
If p⋆ is differentiable at 0, then ∂p⋆/∂ui=−λi⋆ and
∂p⋆/∂vj=−νj⋆. A large λi⋆ means that tightening
constraint i is expensive; λi⋆=0 means a small change costs nothing.
Least norm. Minimize x⊤x subject to Ax=b, with A of full row rank. Here
L=x⊤x+ν⊤(Ax−b), and ∇xL=2x+A⊤ν=0 gives
x=−A⊤ν/2. Substitute:
g(ν)=−41ν⊤AA⊤ν−b⊤ν,
a concave quadratic. Setting its gradient −21AA⊤ν−b to zero gives
ν⋆=−2(AA⊤)−1b and d⋆=b⊤(AA⊤)−1b. Then
x⋆=−A⊤ν⋆/2=A⊤(AA⊤)−1b=A+b, the pseudoinverse solution, and
p⋆=∥x⋆∥2=d⋆.
Lasso. Minimize P(x)=21∥Ax−b∥2+λ∥x∥1, with optimal value P⋆. Here
λ>0 is the penalty weight, not a multiplier. There are no constraints, so first introduce a
copy z=Ax−b, with a multiplier θ for the constraint z−Ax+b=0:L=21∥z∥2+λ∥x∥1+θ⊤(z−Ax+b).
Over z:21∥z∥2+θ⊤z is smallest at z=−θ, with value −21∥θ∥2.
Over x:λ∥x∥1−(A⊤θ)⊤x is 0 if ∥A⊤θ∥∞≤λ, and
−∞ otherwise (grow x along a coordinate where ∣(A⊤θ)i∣>λ).
The dual: maximize D(θ)=b⊤θ−21∥θ∥2=21∥b∥2−21∥b−θ∥2
subject to ∥A⊤θ∥∞≤λ.
All constraints are affine, so strong duality holds. From step 1, θ⋆=−z⋆=b−Ax⋆:the optimal dual point is the residual.
A certificate from any x: let r=b−Ax and θ=sr with
s=min(1,λ/∥A⊤r∥∞). This θ is dual feasible, so
P(x)−D(θ)≥P(x)−P⋆≥0.
The SVM dual (statement; the ML fundamentals track covers SVMs in detail). The dual
of the support vector machine uses the data only through inner products xi⊤xj, so a kernel
k(xi,xj) can replace them. Stationarity gives w=∑iαiyixi, and complementary
slackness gives αi>0 only for points on the margin: the support vectors.
Answers to section 1. (1) The road touches the contour line (section 2.1). (2) By about
4δ, because λ=4 (section 2.4). (3) No. The projection shifts all entries by the same
ν and then clips; it does not rescale. The answer is (0.7,0.3,0) (section 4). The rescaled
point is feasible but farther from y.
(a) KKT, multiplier, dual and certificate. Minimize f0(x)=(x1−2)2+(x2−1)2
subject to f1(x)=x1+x2−1≤0. This is the projection of (2,1) onto a half-plane.
Which case? The unconstrained minimizer (2,1) has x1+x2=3>1. It is infeasible, so
the constraint must be active (λ>0).
Stationarity:∇f0+λ∇f1=0 reads 2(x1−2)+λ=0 and
2(x2−1)+λ=0. So x=(2−λ/2,1−λ/2).
Active constraint:(2−λ/2)+(1−λ/2)=1 gives 3−λ=1, so
λ⋆=2. Then x⋆=(1,0) and p⋆=1+1=2.
Check all four conditions:f1(x⋆)=0≤0 ✓; λ⋆=2≥0 ✓;
λ⋆f1(x⋆)=0 ✓; ∇f0(x⋆)+λ⋆∇f1=(−2,−2)+2(1,1)=0 ✓.
The problem is convex, so x⋆ is optimal (theorem above).
Sensitivity: with x1+x2≤1+u, the same steps give λ=2−u and
p⋆(u)=2(λ/2)2=(2−u)2/2. So dp⋆/du=−(2−u)=−2=−λ⋆ at
u=0. For u=0.1:p⋆=1.92/2=1.805, and the linear estimate gives 2−2(0.1)=1.8.
Dual: for fixed λ,L is minimized at x(λ)=(2−λ/2,1−λ/2).
Substitute: g(λ)=2(λ/2)2+λ(3−λ−1)=2λ−λ2/2. Its maximum
is at λ=2 with d⋆=4−2=2=p⋆. (Slater holds: x=(0,0) is strictly
feasible.)
Certificate: the feasible guess x=(0.5,0.5) has f0=2.25+0.25=2.5. The price
λ=1 gives g(1)=2−0.5=1.5. The gap is 2.5−1.5=1, so f0(x)−p⋆≤1
without solving anything. (The true value is 2.5−2=0.5.)
(b) Simplex projection of y=(1.0,0.6,−0.4). Sorted: u=(1.0,0.6,−0.4), partial sums
1.0,1.6,1.2.
j=1:1.0−(1.0−1)/1=1.0>0.
j=2:0.6−(1.6−1)/2=0.3>0.
j=3:−0.4−(1.2−1)/3≈−0.467<0.
So ρ=2 and ν=(1.6−1)/2=0.3. Then x=max(y−0.3,0)=(0.7,0.3,0), which sums
to 1 ✓. The multipliers are λ=(0,0,ν−y3)=(0,0,0.7), and stationarity
x−y+ν1−λ=(0.7−1+0.3,0.3−0.6+0.3,0+0.4+0.3−0.7)=0 ✓.
The squared distance to y is 0.09+0.09+0.16=0.34, against 0.35125 for the rescaled point
(0.625,0.375,0).
Quick check
A friend projects y=(2,0,0) onto the probability simplex by "clip the negatives, then divide by the sum" and gets (1,0,0). Is this the correct projection?
Why.
Sorting gives u=(2,0,0); the tests are 2−1=1>0,0−0.5<0, so ρ=1,ν=1 and x=max(y−1,0)=(1,0,0). Here both methods agree by luck. For y=(1.0,0.6,−0.4) they do not: the projection shifts by one constant, (0.7,0.3,0), while dividing gives (0.625,0.375,0), which is farther from y.
(c) A lasso certificate.A=110101,b=(1,2,−0.5),λ=1. (Lesson 09 solves it:
x⋆=(1,0),P⋆=1.625.) Take the guess x=(0.5,0).
r=b−Ax=(1,2,−0.5)−(0.5,0.5,0)=(0.5,1.5,−0.5) and A⊤r=(0.5+1.5,0.5−0.5)=(2,0).
So s=min(1,1/2)=0.5 and θ=(0.25,0.75,−0.25).
SciPy's general constrained solver (SLSQP) checks the hand results. The notebook builds the simplex
projection, the multipliers and the lasso gap yourself.
importnumpyasnpfromscipy.optimizeimportminimize# (a) min (x1-2)^2 + (x2-1)^2 s.t. x1 + x2 <= c, at c = 1f=lambdax:(x[0]-2)**2+(x[1]-1)**2defsolve(c):con={"type":"ineq","fun":lambdax:c-x[0]-x[1]}# SciPy wants fun(x) >= 0returnminimize(f,np.zeros(2),method="SLSQP",constraints=[con],tol=1e-12)res=solve(1.0)print(res.x.round(6)+0.0,round(res.fun,6))# [1. 0.] 2.0print(res.multipliers.round(6))# [2.] (SciPy >= 1.16)h=1e-3# multiplier = sensitivityprint(round(-(solve(1+h).fun-solve(1-h).fun)/(2*h),6))# 2.0# (b) projection of y onto the probability simplexy=np.array([1.0,0.6,-0.4])res=minimize(lambdax:0.5*np.sum((x-y)**2),np.ones(3)/3,method="SLSQP",bounds=[(0,None)]*3,tol=1e-12,constraints=[{"type":"eq","fun":lambdax:x.sum()-1}])naive=np.maximum(y,0)/np.maximum(y,0).sum()# clip and renormalizeprint(res.x.round(6)+0.0,naive)# [0.7 0.3 0. ] [0.625 0.375 0. ]print(np.sum((res.x-y)**2).round(5),np.sum((naive-y)**2).round(5))# 0.34 0.35125# (c) least norm: min ||x||^2 s.t. 3 x1 + 4 x2 = 10A,b=np.array([[3.0,4.0]]),np.array([10.0])x_star=np.linalg.pinv(A)@bd_star=b@np.linalg.solve(A@A.T,b)print(x_star,round(x_star@x_star,12),d_star)# [1.2 1.6] 4.0 4.0
SLSQP does not use the structure of the problem and is slow for large n. The KKT derivation of
the simplex projection is exact and costs O(nlogn).
Support vector machines. The SVM is usually solved in the dual: the data enter only through
inner products, so kernels can replace them, and complementary slackness makes the solution depend
only on the support vectors.
Stopping rules with a guarantee. scikit-learn's Lasso and ElasticNet build the dual point
of section 3 from the residual and stop when the duality gap is small. "Gap safe" screening rules
use the same gap to remove features that must be zero.
Simplex constraints. Sparsemax (Martins and Astudillo, 2016) is exactly the Euclidean
projection onto the simplex. Unlike softmax, it gives exact zeros, which makes attention weights
sparse. Mixture weights and portfolio weights use the same projection.
Constrained training. Fairness constraints, safety constraints in reinforcement learning
(constrained MDPs) and budget constraints are often handled with a Lagrangian: the multiplier is
learned by gradient ascent while the model is trained by gradient descent.
ML for science: PDE constraints and adjoints. To minimize a loss J(u,θ) subject to a
discretized PDE F(u,θ)=0 (state u, parameters θ), form J+μ⊤F with a
multiplier vector μ. Stationarity in u is the adjoint equation(∂F/∂u)⊤μ=−(∂J/∂u)⊤, and then
dJ/dθ=∂J/∂θ+μ⊤∂F/∂θ. The adjoint vector is
the multiplier of the state equation (Johnson's notes below). Strong-constraint 4D-Var in weather
forecasting uses the forecast model as the constraint; its multipliers solve the adjoint model
backward in time.
Wrong sign of λ. The rule λ≥0 holds for constraints written as
fi(x)≤0 and added as +λifi. For x1+x2≥2, write 2−x1−x2≤0
first. Equality multipliers ν have no sign condition.
Checking only one case. Complementary slackness is a case split: each constraint is either
active or has a zero multiplier. If the guess "active" gives λ<0, the constraint is inactive.
Treating every Lagrange point as a minimum.∇f+ν∇h=0 also holds at
constrained maxima. Without convexity, compare the candidates.
"Strong duality, because it is convex." Convexity is not enough. You need a constraint
qualification such as Slater's condition (exercise 5.21 above).
Ignoring the implicit constraints of the dual. Where g=−∞ the dual point gives no
bound. For the lasso this is ∥A⊤θ∥∞≤λ; a "gap" computed from an infeasible
θ certifies nothing.
Clip-and-renormalize for the simplex. The result is feasible, but it is not the projection.
Why is the dual function concave even when the primal problem is not convex?AnswerFor each fixed x,L(x,λ,ν) is affine in (λ,ν). The dual function is the pointwise infimum of this family of affine functions (their lower envelope), and such an infimum is concave.
Minimize x1+2x2 on the ellipse 41x12+x22=1 with a Lagrange multiplier. Give the minimizer and ν.Answer∇f=(1,2) and ∇h=(x1/2,2x2). Stationarity: 1+νx1/2=0 and 2+2νx2=0, so x1=−2/ν and x2=−1/ν. The constraint gives 1/ν2+1/ν2=1, so ν=±2. For ν=2:x=(−2,−1/2) with f=−22≈−2.83, the minimum. For ν=−2:x=(2,1/2) with f=22, the maximum.
Solve min∥x∥2 s.t. 3x1+4x2=10 through the dual. What does the price ν=−1 tell you?AnswerAA⊤=25, so g(ν)=−425ν2−10ν. Its maximum is at ν⋆=−0.8 with d⋆=−4+8=4, and x⋆=(3,4)⋅10/25=(1.2,1.6) with p⋆=4. Any ν gives a lower bound: g(−1)=−6.25+10=3.75≤4.
Take minx s.t. x2≤0. Find p⋆ and d⋆. Is the dual optimum attained?AnswerOnly x=0 is feasible, so p⋆=0. For λ>0,g(λ)=infx(x+λx2)=−1/(4λ) (at x=−1/(2λ)), and g(0)=−∞. So d⋆=supλg=0=p⋆, but no λ attains it. No KKT multiplier exists (stationarity 1=0 fails), because Slater's condition fails.
In a hard-margin SVM, a training point has yi(w⊤xi+b)>1. What is αi, and what happens if you delete the point?Answerαi=0 by complementary slackness, because its constraint is not tight. It does not appear in w=∑iαiyixi, so deleting it does not change the solution.
For the lasso, show that x=0 is optimal exactly when λ≥∥A⊤b∥∞.AnswerIf ∥A⊤b∥∞≤λ, then θ=b is dual feasible, and the gap at x=0 is P(0)−D(b)=21∥b∥2−21∥b∥2=0, so x=0 is optimal. Conversely, if x⋆=0, then θ⋆=b−A⋅0=b must be dual feasible. So λmax=∥A⊤b∥∞ is where the lasso path starts.
Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
At a constrained minimum of f on the curve h(x)=0, how are ∇f and ∇h related, and why?
They are parallel: ∇f+ν∇h=0. A level set of f touches the curve there, so both have the same normal direction.
Write the Lagrangian of minf0(x) s.t. fi(x)≤0,hj(x)=0.
L(x,λ,ν)=f0(x)+∑iλifi(x)+∑jνjhj(x), with λi≥0 and νj∈R.
What does a Lagrange multiplier λi⋆ mean?
A price (sensitivity): relaxing constraint i to fi(x)≤u changes the optimal value at rate −λi⋆. It is 0 for an inactive constraint.
"What does a Lagrange multiplier mean?" — A price: the rate at which the optimal value improves
when the constraint is relaxed. Zero for an inactive constraint.
"Explain Lagrange multipliers geometrically." — At the constrained optimum a level set of the
objective is tangent to the constraint surface, so the two gradients are parallel.
"Are the KKT conditions necessary? Sufficient?" — Sufficient for convex problems; necessary under
Slater's condition (convex) or a constraint qualification such as LICQ (general).
"Weak versus strong duality?" — d⋆≤p⋆ always; equality for convex problems under Slater.
"Why solve the SVM in the dual?" — Kernels (the data enter only through inner products), simple
box constraints, and a sparse solution (support vectors).
"How do you know an iterative solver is done?" — A duality gap bounds the suboptimality without
knowing p⋆.
"Project a vector onto the simplex in O(nlogn)." — Sort, find ρ and ν, shift and clip.