Type at least 2 letters. Enter opens the first result, Esc closes.

    Optimization · Lesson 8 · Constraints and nonsmooth problems

    Lagrange multipliers, KKT conditions and duality

    Intermediate 95 min 5 interactive figures 4 videos Colab notebook

    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\ell_1 and ℓ∞\ell_\infty norms (linear algebra 11) for the lasso dual. Time: about 95 minutes, plus the notebook.


    Why this matters #

    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.

    1. Predict first #

    2. The picture #

    2.1 One equality constraint: walk along the curve #

    Take a function f(x)f(x) of x=(x1,x2)x = (x_1, x_2) and a constraint h(x)=0h(x) = 0. The constraint is a curve in the plane. We want the point of the curve where ff is smallest.

    Walk along the curve and watch the contour lines (level sets) of ff. Recall two facts from lesson 01: ∇f\nabla f is perpendicular to the level set of ff through the point, and ∇h\nabla h is perpendicular to the curve h=0h = 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 ff.
    • 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⋆x^\star, the gradients are parallel:

    ∇f(x⋆)+ν ∇h(x⋆)=0for some number ν.\nabla f(x^\star) + \nu\,\nabla h(x^\star) = 0 \quad \text{for some number } \nu .

    The number ν\nu is the Lagrange multiplier. It can be positive or negative. We assume ∇h(x⋆)≠0\nabla h(x^\star) \ne 0, so that the curve has a normal direction at x⋆x^\star.

    The same argument with a formula. Let τ\tau be a unit tangent vector of the curve at xx. The rate of change of ff along the curve is the directional derivative ∇f(x)⊤τ\nabla f(x)^\top \tau. At a minimum along the curve this rate must be 0; otherwise a small step along −τ-\tau or +τ+\tau lowers ff. So ∇f(x⋆)⊥τ\nabla f(x^\star) \perp \tau. In the plane, the only directions perpendicular to τ\tau are the multiples of ∇h(x⋆)\nabla h(x^\star). Hence ∇f(x⋆)\nabla f(x^\star) is a multiple of ∇h(x⋆)\nabla h(x^\star).

    Interactive figure: loading…

    Drag the black point along the yellow constraint curve and find where ff is smallest. The blue arrow is the direction of ∇f\nabla f, the yellow arrow the direction of ∇h\nabla h, and the magenta arrow the downhill direction along the curve. The dashed blue curve is the level set of ff through the point. At the minimum the magenta arrow vanishes, the angle between the gradients is 0°0° or 180°180°, and the dashed level set touches the curve. The chart shows ff along the curve. Try the circle: it has a minimum and a maximum, and both satisfy the condition.

    Example. Minimize f(x)=x12+x22f(x) = x_1^2 + x_2^2 subject to x1+x2=1x_1 + x_2 = 1. Here h(x)=x1+x2−1h(x) = x_1 + x_2 - 1, ∇f=(2x1,2x2)\nabla f = (2x_1, 2x_2) and ∇h=(1,1)\nabla h = (1, 1).

    1. Parallel gradients: 2x1+ν=02x_1 + \nu = 0 and 2x2+ν=02x_2 + \nu = 0. So x1=x2=−ν/2x_1 = x_2 = -\nu/2.
    2. The constraint: −ν/2−ν/2=1-\nu/2 - \nu/2 = 1, so ν=−1\nu = -1.
    3. Hence x⋆=(0.5,0.5)x^\star = (0.5, 0.5) and f(x⋆)=0.5f(x^\star) = 0.5.

    In the figure, the level set x12+x22=0.5x_1^2 + x_2^2 = 0.5 is the circle of radius 1/2≈0.7071/\sqrt2 \approx 0.707, and it touches the line at (0.5,0.5)(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)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+x22f(x) = x_1^2 + x_2^2; 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)(0, 0), is not on the curve. Press Go to the lowest point: for the line, it is (0.5,0.5)(0.5, 0.5) with f=0.5f = 0.5, the same answer as the calculation above.

    2.2 The Lagrangian #

    Put the objective and the constraint into one function, the Lagrangian:

    L(x,ν)=f(x)+ν h(x).\mathcal L(x, \nu) = f(x) + \nu\, h(x).

    Its stationary points are exactly the candidates of section 2.1:

    • ∇xL=∇f(x)+ν∇h(x)=0\nabla_x \mathcal L = \nabla f(x) + \nu \nabla h(x) = 0 is the tangency condition;
    • ∂L/∂ν=h(x)=0\partial \mathcal L / \partial \nu = h(x) = 0 is the constraint.

    With x∈Rdx \in \R^d this is d+1d + 1 equations for d+1d + 1 unknowns. Two warnings:

    • A stationary point of L\mathcal L is a saddle point of L\mathcal L, not a minimum. In the example, L\mathcal L is linear in ν\nu, so it has no minimum in ν\nu.
    • 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+x22f(x) = x_1^2 + x_2^2 on the circle (x1−2)2+x22=1(x_1 - 2)^2 + x_2^2 = 1. The condition ∇f+ν∇h=0\nabla f + \nu \nabla h = 0 holds at two points, (1,0)(1, 0) and (3,0)(3, 0). What are they?

    2.3 The multiplier as a price #

    Move the constraint: require x1+x2=cx_1 + x_2 = c instead of =1= 1. Repeat the example: x⋆=(c/2,c/2)x^\star = (c/2, c/2) and the optimal value is

    p⋆(c)=c24+c24=c22,dp⋆dc=c.p^\star(c) = \frac{c^2}{4} + \frac{c^2}{4} = \frac{c^2}{2}, \qquad \frac{dp^\star}{dc} = c .

    With h(x)=x1+x2−ch(x) = x_1 + x_2 - c, the same steps give ν=−c\nu = -c. So dp⋆dc=−ν\frac{dp^\star}{dc} = -\nu. At c=1c = 1 the slope is 1 and ν=−1\nu = -1.

    Why this holds in general (intuition). Write the constraint as a(x)=ca(x) = c, so h(x)=a(x)−ch(x) = a(x) - c and ∇h=∇a\nabla h = \nabla a. Change cc by a small dcdc. The optimal point moves by some dxdx that keeps the constraint, so ∇a(x⋆)⊤dx=dc\nabla a(x^\star)^\top dx = dc. Then

    dp⋆≈∇f(x⋆)⊤dx=−ν ∇a(x⋆)⊤dx=−ν dc.dp^\star \approx \nabla f(x^\star)^\top dx = -\nu\, \nabla a(x^\star)^\top dx = -\nu\, dc .

    So −ν-\nu is the price of the constraint: one more unit of cc changes the best value by about −ν-\nu. Section 3 states this precisely.

    Interactive figure: loading…

    Move cc to slide the constraint line. The left plot shows the contours of ff and the constrained minimizer x⋆x^\star; the right plot shows the optimal value p⋆(c)p^\star(c) (black) and its tangent line, whose slope is −λ-\lambda. For the inequality x1+x2≤cx_1 + x_2 \le c, the multiplier is λ=2\lambda = 2 at c=1c = 1 and becomes 0 when c≥3c \ge 3: then the unconstrained minimizer (2,1)(2, 1) is feasible and the constraint costs nothing. Switch to the equality to see a multiplier ν\nu that changes sign.

    2.4 Inequality constraints: the wall #

    Now take a constraint x≤1x \le 1 in one variable, and minimize a smooth convex f0(x)f_0(x). There are two cases.

    • Inactive. The unconstrained minimizer satisfies x≤1x \le 1. The constraint does nothing: f0′(x⋆)=0f_0'(x^\star) = 0.
    • Active. The unconstrained minimizer is to the right of 1. The solution sits on the wall, x⋆=1x^\star = 1. There f0′(x⋆)<0f_0'(x^\star) < 0: f0f_0 wants to move right, but the wall stops it. Write this balance of forces as f0′(x⋆)+λ=0f_0'(x^\star) + \lambda = 0, with a wall force λ>0\lambda > 0.

    Both cases are one statement. Write the constraint as f1(x)=x−1≤0f_1(x) = x - 1 \le 0. Then

    f0′(x⋆)+λf1′(x⋆)=0,λ≥0,λ f1(x⋆)=0.\begin{aligned} &f_0'(x^\star) + \lambda f_1'(x^\star) = 0, \\ &\lambda \ge 0, \qquad \lambda\, f_1(x^\star) = 0 . \end{aligned}

    The last condition says: the wall pushes only if you touch it. If f1(x⋆)<0f_1(x^\star) < 0 (not touching), then λ=0\lambda = 0. This is complementary slackness. The sign λ≥0\lambda \ge 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(x - 3)^2 subject to x≤1x \le 1. The constraint is active: x⋆=1x^\star = 1, and 2(1−3)+λ=02(1 - 3) + \lambda = 0 gives λ=4\lambda = 4. Relaxing to x≤1+δx \le 1 + \delta gives p⋆(δ)=(1+δ−3)2=(2−δ)2=4−4δ+δ2p^\star(\delta) = (1 + \delta - 3)^2 = (2 - \delta)^2 = 4 - 4\delta + \delta^2. So p⋆p^\star goes down by about 4δ=λδ4\delta = \lambda\delta: 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\lambda = 0. If it is outside, the solution is on the boundary, where the level set of ff touches the boundary, and λ>0\lambda > 0.

    Interactive figure: loading…

    Drag the blue point aa, the unconstrained minimizer of f0(x)=∥x−a∥2f_0(x) = \|x - a\|^2, inside and outside the yellow feasible set f1(x)≤0f_1(x) \le 0. Inside, λ=0\lambda = 0 and x⋆=ax^\star = a. Outside, x⋆x^\star (black star) is on the boundary, and the push of the wall −λ∇f1-\lambda\nabla f_1 (yellow arrow) cancels the pull of f0f_0, −∇f0-\nabla f_0 (blue arrow). The four KKT conditions are listed with ticks. In the mode choose λ yourself, the orange point minimizes f0+λf1f_0 + \lambda f_1 for your λ\lambda: too small and it is infeasible, too large and complementary slackness fails.

    For a=(2,1)a = (2, 1) and the half-plane x1+x2≤1x_1 + x_2 \le 1: stationarity gives x=(2−λ/2, 1−λ/2)x = (2 - \lambda/2,\ 1 - \lambda/2), the active constraint gives 3−λ=13 - \lambda = 1, so λ=2\lambda = 2, x⋆=(1,0)x^\star = (1, 0) and p⋆=2p^\star = 2. Section 4 checks every condition.

    Quick check

    Minimize (x1−2)2+(x2−1)2(x_1 - 2)^2 + (x_2 - 1)^2 subject to x1+x2≤5x_1 + x_2 \le 5. What is the multiplier λ\lambda of the constraint?

    3. Definitions and main results #

    Standard form and the Lagrangian #

    The variable is x∈Rdx \in \R^d. The problem in standard form is

    min⁡x f0(x)subject to fi(x)≤0(i=1,…,m),hj(x)=0(j=1,…,p),\begin{aligned} \min_x\ & f_0(x) \\ \text{subject to}\ & f_i(x) \le 0 \quad (i = 1, \dots, m), \\ & h_j(x) = 0 \quad (j = 1, \dots, p), \end{aligned}

    with optimal value p⋆p^\star. A point that satisfies all constraints is feasible. The problem is convex when f0,f1,…,fmf_0, f_1, \dots, f_m are convex and every hjh_j is affine (so the equalities read Ax=bAx = b). Write every constraint in this form first: x1+x2≥2x_1 + x_2 \ge 2 becomes 2−x1−x2≤02 - x_1 - x_2 \le 0.

    The Lagrangian adds each constraint with its own multiplier:

    L(x,λ,ν)=f0(x)+∑i=1mλifi(x)+∑j=1pνjhj(x),\mathcal L(x, \lambda, \nu) = f_0(x) + \sum_{i=1}^m \lambda_i f_i(x) + \sum_{j=1}^p \nu_j h_j(x),

    with λi≥0\lambda_i \ge 0 for an inequality and νj∈R\nu_j \in \R (any sign) for an equality. In this lesson, λ\lambda is a Lagrange multiplier, not an eigenvalue. We follow the notation of Boyd and Vandenberghe.

    The KKT conditions #

    A point (x,λ,ν)(x, \lambda, \nu) satisfies the Karush–Kuhn–Tucker (KKT) conditions if (for differentiable fif_i, hjh_j):

    1. primal feasibility: fi(x)≤0f_i(x) \le 0 for all ii and hj(x)=0h_j(x) = 0 for all jj;
    2. dual feasibility: λi≥0\lambda_i \ge 0 for all ii;
    3. complementary slackness: λifi(x)=0\lambda_i f_i(x) = 0 for all ii;
    4. stationarity: ∇f0(x)+∑iλi∇fi(x)+∑jνj∇hj(x)=0\nabla f_0(x) + \sum_i \lambda_i \nabla f_i(x) + \sum_j \nu_j \nabla h_j(x) = 0.

    A constraint with fi(x)=0f_i(x) = 0 is active; one with fi(x)<0f_i(x) < 0 is inactive. Complementary slackness says that an inactive constraint has λi=0\lambda_i = 0. In practice it is a case split: for each inequality, guess "active" (fi=0f_i = 0) or "inactive" (λi=0\lambda_i = 0), solve, and check the signs. If the guess "active" gives λi<0\lambda_i < 0, the guess was wrong.

    Theorem (KKT points of convex problems are optimal) #

    Let the problem be convex, with differentiable f0,…,fmf_0, \dots, f_m. If (x~,λ~,ν~)(\tilde x, \tilde\lambda, \tilde\nu) satisfies the four KKT conditions, then x~\tilde x is a global minimizer.

    Why it is true (intuition). With the prices λ~,ν~\tilde\lambda, \tilde\nu fixed, the Lagrangian is a convex function of xx whose gradient is zero at x~\tilde x, so x~\tilde x minimizes it. On the feasible set, the Lagrangian is never above f0f_0, and at x~\tilde x it equals f0f_0.

    Proof.

    1. λ~i≥0\tilde\lambda_i \ge 0, each fif_i is convex and each hjh_j is affine. So x↦L(x,λ~,ν~)x \mapsto \mathcal L(x, \tilde\lambda, \tilde\nu) is a sum of convex functions: it is convex.
    2. Stationarity says that its gradient is zero at x~\tilde x. For a differentiable convex function, a zero gradient means a global minimum (lesson 02, first-order condition). So L(x,λ~,ν~)≥L(x~,λ~,ν~)\mathcal L(x, \tilde\lambda, \tilde\nu) \ge \mathcal L(\tilde x, \tilde\lambda, \tilde\nu) for every xx.
    3. Complementary slackness and hj(x~)=0h_j(\tilde x) = 0 remove the added terms at x~\tilde x: L(x~,λ~,ν~)=f0(x~)\mathcal L(\tilde x, \tilde\lambda, \tilde\nu) = f_0(\tilde x).
    4. Let xx be any feasible point. Then λ~ifi(x)≤0\tilde\lambda_i f_i(x) \le 0 and hj(x)=0h_j(x) = 0, so f0(x)≥L(x,λ~,ν~)f_0(x) \ge \mathcal L(x, \tilde\lambda, \tilde\nu).
    5. Steps 4, 2 and 3 give f0(x)≥L(x,λ~,ν~)≥L(x~,λ~,ν~)=f0(x~)f_0(x) \ge \mathcal L(x, \tilde\lambda, \tilde\nu) \ge \mathcal L(\tilde x, \tilde\lambda, \tilde\nu) = f_0(\tilde x). ■\blacksquare

    For a nonconvex problem this fails: a KKT point can be a maximum or a saddle (the circle quiz above).

    When are the KKT conditions necessary? #

    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 xx with fi(x)<0f_i(x) < 0 for every non-affine fif_i (affine inequalities only need fi(x)≤0f_i(x) \le 0) and Ax=bAx = b. Under Slater's condition, x⋆x^\star 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 xx subject to x2≤0x^2 \le 0. Only x=0x = 0 is feasible, so x⋆=0x^\star = 0 and p⋆=0p^\star = 0. Stationarity asks for 1+2λx⋆=1+0=01 + 2\lambda x^\star = 1 + 0 = 0, which no λ\lambda satisfies. So no KKT multiplier exists at the optimum. The problem is convex, but Slater's condition fails: x2<0x^2 < 0 is impossible. Geometrically, ∇f1(x⋆)=2x⋆=0\nabla f_1(x^\star) = 2x^\star = 0: the "wall" has no normal direction to push with.

    Projection onto the probability simplex #

    Problem. Given y∈Rny \in \R^n, find the closest point of the probability simplex Δ={x:x≥0, 1⊤x=1}\Delta = \{x : x \ge 0,\ \mathbf 1^\top x = 1\}:

    min⁡x 12∥x−y∥2subject to 1⊤x=1,−xi≤0 (i=1,…,n).\begin{aligned} \min_x\ & \tfrac12 \|x - y\|^2 \\ \text{subject to}\ & \mathbf 1^\top x = 1, \quad -x_i \le 0 \ (i = 1, \dots, n). \end{aligned}

    The Lagrangian is 12∥x−y∥2+ν(1⊤x−1)−λ⊤x\tfrac12\|x - y\|^2 + \nu(\mathbf 1^\top x - 1) - \lambda^\top x.

    1. Stationarity: xi−yi+ν−λi=0x_i - y_i + \nu - \lambda_i = 0 for each ii.
    2. If xi>0x_i > 0, complementary slackness gives λi=0\lambda_i = 0, so xi=yi−νx_i = y_i - \nu (and yi>νy_i > \nu).
    3. If xi=0x_i = 0, then λi=ν−yi\lambda_i = \nu - y_i, and dual feasibility λi≥0\lambda_i \ge 0 gives yi≤νy_i \le \nu.
    4. Together: xi=max⁡(yi−ν,0)x_i = \max(y_i - \nu, 0). Shift every entry down by one constant ν\nu, then clip at 0.
    5. ν\nu solves ∑imax⁡(yi−ν,0)=1\sum_i \max(y_i - \nu, 0) = 1. The left side is continuous and strictly decreasing where it is positive, so ν\nu is unique. To find it, sort yy in decreasing order, u1≥⋯≥unu_1 \ge \dots \ge u_n. Let ρ\rho be the largest jj with uj−1j(∑i≤jui−1)>0u_j - \frac{1}{j}\big(\sum_{i \le j} u_i - 1\big) > 0. Then ν=1ρ(∑i≤ρui−1)\nu = \frac{1}{\rho}\big(\sum_{i \le \rho} u_i - 1\big). The cost is O(nlog⁡n)O(n \log n).

    The problem is convex with affine constraints, so the KKT conditions are sufficient: this xx is the projection. The constant ν\nu is a threshold (some papers call it τ\tau). Entries below it become exactly 0.

    Quick check

    Project y=(0.3,0.3,0.3)y = (0.3, 0.3, 0.3) onto the probability simplex. What are xx and ν\nu?

    Duality: every price gives a lower bound #

    Fix the prices λ≥0\lambda \ge 0 and ν\nu, and minimize the Lagrangian over all xx, with no constraints. The result is the dual function:

    g(λ,ν)=inf⁡xL(x,λ,ν).g(\lambda, \nu) = \inf_x \mathcal L(x, \lambda, \nu).

    It can be −∞-\infty. It is always concave, even when the original problem is not convex: for each fixed xx, L(x,λ,ν)\mathcal L(x, \lambda, \nu) is an affine function of (λ,ν)(\lambda, \nu) (a line, in one dimension), and gg is the lower envelope of these affine functions. A pointwise infimum of affine functions is concave.

    Theorem (weak duality) #

    For every λ≥0\lambda \ge 0 and every ν\nu: g(λ,ν)≤p⋆g(\lambda, \nu) \le p^\star.

    Proof.

    1. Take any feasible x~\tilde x. Then λifi(x~)≤0\lambda_i f_i(\tilde x) \le 0 (because λi≥0\lambda_i \ge 0 and fi(x~)≤0f_i(\tilde x) \le 0), and hj(x~)=0h_j(\tilde x) = 0.
    2. So L(x~,λ,ν)≤f0(x~)\mathcal L(\tilde x, \lambda, \nu) \le f_0(\tilde x).
    3. The infimum is at most the value at one point: g(λ,ν)≤L(x~,λ,ν)≤f0(x~)g(\lambda, \nu) \le \mathcal L(\tilde x, \lambda, \nu) \le f_0(\tilde x).
    4. This holds for every feasible x~\tilde x, so g(λ,ν)≤p⋆g(\lambda, \nu) \le p^\star. ■\blacksquare

    The dual problem looks for the best lower bound: maximize g(λ,ν)g(\lambda, \nu) subject to λ≥0\lambda \ge 0. Its value is d⋆d^\star, and weak duality gives d⋆≤p⋆d^\star \le p^\star. The difference p⋆−d⋆≥0p^\star - d^\star \ge 0 is the optimal duality gap.

    Interactive figure: loading…

    Move λ\lambda. Each thin line is f0(x)+λf1(x)f_0(x) + \lambda f_1(x) for one fixed xx: yellow for feasible xx (slope f1(x)≤0f_1(x) \le 0), grey for infeasible xx. Their lower envelope is the dual function g(λ)g(\lambda) (thick blue), and the orange line belongs to the xx that attains the minimum. The blue dot never rises above the dashed line p⋆p^\star. In the convex problem the best bound reaches p⋆=4p^\star = 4 at λ=4\lambda = 4. In the nonconvex problem the best bound is −3-3 while p⋆=−1p^\star = -1: a gap of 2.

    Strong duality means d⋆=p⋆d^\star = p^\star. 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⋆d^\star = p^\star, and the dual optimum is attained when d⋆>−∞d^\star > -\infty.

    The duality gap as a certificate. Let xx be feasible and (λ,ν)(\lambda, \nu) dual feasible (λ≥0\lambda \ge 0, g>−∞g > -\infty). Then f0(x)−p⋆≤f0(x)−g(λ,ν)f_0(x) - p^\star \le f_0(x) - g(\lambda, \nu). The right side can be computed without knowing p⋆p^\star. When it is below a tolerance, you can stop: xx 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.2g(\lambda) = 3.2, and a feasible point xx gives f0(x)=3.5f_0(x) = 3.5. What do you know?

    Why KKT is necessary under strong duality

    Suppose strong duality holds, x⋆x^\star is primal optimal and (λ⋆,ν⋆)(\lambda^\star, \nu^\star) is dual optimal. Then

    f0(x⋆)=g(λ⋆,ν⋆)≤L(x⋆,λ⋆,ν⋆)=f0(x⋆)+∑iλi⋆fi(x⋆)≤f0(x⋆).\begin{aligned} f_0(x^\star) = g(\lambda^\star, \nu^\star) &\le \mathcal L(x^\star, \lambda^\star, \nu^\star) \\ &= f_0(x^\star) + \textstyle\sum_i \lambda_i^\star f_i(x^\star) \le f_0(x^\star). \end{aligned}

    So both inequalities are equalities. The second one gives ∑iλi⋆fi(x⋆)=0\sum_i \lambda_i^\star f_i(x^\star) = 0. Every term is ≤0\le 0, so every term is 0: complementary slackness. The first one says that x⋆x^\star minimizes L(⋅,λ⋆,ν⋆)\mathcal L(\cdot, \lambda^\star, \nu^\star), so its gradient is zero there: stationarity. Feasibility of x⋆x^\star and λ⋆≥0\lambda^\star \ge 0 are given. So all four KKT conditions hold. With Slater's condition (which gives strong duality and an attained dual optimum), optimal   ⟺  \iff KKT.

    A convex problem with a positive duality gap

    Boyd and Vandenberghe, exercise 5.21: minimize e−xe^{-x} subject to x2/y≤0x^2/y \le 0, on the domain y>0y > 0. Only x=0x = 0 is feasible, so p⋆=1p^\star = 1. But for every λ≥0\lambda \ge 0, g(λ)=inf⁡x, y>0(e−x+λx2/y)=0g(\lambda) = \inf_{x,\ y > 0} \big(e^{-x} + \lambda x^2/y\big) = 0: take y=x3y = x^3 and let x→∞x \to \infty, so that both terms go to 0. So d⋆=0d^\star = 0 and the gap is 1. The problem is convex, but Slater's condition fails, because x2/y<0x^2/y < 0 is impossible.

    Multipliers as sensitivities (precise form) #

    Perturb the constraints to fi(x)≤uif_i(x) \le u_i and hj(x)=vjh_j(x) = v_j, with optimal value p⋆(u,v)p^\star(u, v). If strong duality holds and (λ⋆,ν⋆)(\lambda^\star, \nu^\star) is dual optimal for u=0u = 0, v=0v = 0, then for every (u,v)(u, v) (Boyd and Vandenberghe §5.6):

    p⋆(u,v)≥p⋆(0,0)−λ⋆⊤u−ν⋆⊤v.p^\star(u, v) \ge p^\star(0, 0) - \lambda^{\star\top} u - \nu^{\star\top} v .

    Proof. For any xx feasible for the perturbed problem, p⋆(0,0)=g(λ⋆,ν⋆)≤L(x,λ⋆,ν⋆)≤f0(x)+λ⋆⊤u+ν⋆⊤vp^\star(0, 0) = g(\lambda^\star, \nu^\star) \le \mathcal L(x, \lambda^\star, \nu^\star) \le f_0(x) + \lambda^{\star\top} u + \nu^{\star\top} v, because λi⋆fi(x)≤λi⋆ui\lambda_i^\star f_i(x) \le \lambda_i^\star u_i and νj⋆hj(x)=νj⋆vj\nu_j^\star h_j(x) = \nu_j^\star v_j. Take the infimum over such xx. ■\blacksquare

    If p⋆p^\star is differentiable at 0, then ∂p⋆/∂ui=−λi⋆\partial p^\star / \partial u_i = -\lambda_i^\star and ∂p⋆/∂vj=−νj⋆\partial p^\star / \partial v_j = -\nu_j^\star. A large λi⋆\lambda_i^\star means that tightening constraint ii is expensive; λi⋆=0\lambda_i^\star = 0 means a small change costs nothing.

    Two duals you should know #

    Least norm. Minimize x⊤xx^\top x subject to Ax=bAx = b, with AA of full row rank. Here L=x⊤x+ν⊤(Ax−b)\mathcal L = x^\top x + \nu^\top(Ax - b), and ∇xL=2x+A⊤ν=0\nabla_x \mathcal L = 2x + A^\top \nu = 0 gives x=−A⊤ν/2x = -A^\top \nu / 2. Substitute:

    g(ν)=−14 ν⊤AA⊤ν−b⊤ν,g(\nu) = -\tfrac14\, \nu^\top A A^\top \nu - b^\top \nu ,

    a concave quadratic. Setting its gradient −12AA⊤ν−b-\tfrac12 AA^\top\nu - b to zero gives ν⋆=−2(AA⊤)−1b\nu^\star = -2(AA^\top)^{-1} b and d⋆=b⊤(AA⊤)−1bd^\star = b^\top (AA^\top)^{-1} b. Then x⋆=−A⊤ν⋆/2=A⊤(AA⊤)−1b=A+bx^\star = -A^\top\nu^\star/2 = A^\top(AA^\top)^{-1} b = A^+ b, the pseudoinverse solution, and p⋆=∥x⋆∥2=d⋆p^\star = \|x^\star\|^2 = d^\star.

    Lasso. Minimize P(x)=12∥Ax−b∥2+λ∥x∥1P(x) = \tfrac12\|Ax - b\|^2 + \lambda\|x\|_1, with optimal value P⋆P^\star. Here λ>0\lambda > 0 is the penalty weight, not a multiplier. There are no constraints, so first introduce a copy z=Ax−bz = Ax - b, with a multiplier θ\theta for the constraint z−Ax+b=0z - Ax + b = 0: L=12∥z∥2+λ∥x∥1+θ⊤(z−Ax+b)\mathcal L = \tfrac12\|z\|^2 + \lambda\|x\|_1 + \theta^\top(z - Ax + b).

    1. Over zz: 12∥z∥2+θ⊤z\tfrac12\|z\|^2 + \theta^\top z is smallest at z=−θz = -\theta, with value −12∥θ∥2-\tfrac12\|\theta\|^2.
    2. Over xx: λ∥x∥1−(A⊤θ)⊤x\lambda\|x\|_1 - (A^\top\theta)^\top x is 0 if ∥A⊤θ∥∞≤λ\|A^\top\theta\|_\infty \le \lambda, and −∞-\infty otherwise (grow xx along a coordinate where ∣(A⊤θ)i∣>λ|(A^\top\theta)_i| > \lambda).
    3. The dual: maximize D(θ)=b⊤θ−12∥θ∥2=12∥b∥2−12∥b−θ∥2D(\theta) = b^\top\theta - \tfrac12\|\theta\|^2 = \tfrac12\|b\|^2 - \tfrac12\|b - \theta\|^2 subject to ∥A⊤θ∥∞≤λ\|A^\top\theta\|_\infty \le \lambda.
    4. All constraints are affine, so strong duality holds. From step 1, θ⋆=−z⋆=b−Ax⋆\theta^\star = -z^\star = b - Ax^\star: the optimal dual point is the residual.
    5. A certificate from any xx: let r=b−Axr = b - Ax and θ=s r\theta = s\,r with s=min⁡(1,λ/∥A⊤r∥∞)s = \min\big(1, \lambda / \|A^\top r\|_\infty\big). This θ\theta is dual feasible, so P(x)−D(θ)≥P(x)−P⋆≥0P(x) - D(\theta) \ge P(x) - P^\star \ge 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⊤xjx_i^\top x_j, so a kernel k(xi,xj)k(x_i, x_j) can replace them. Stationarity gives w=∑iαiyixiw = \sum_i \alpha_i y_i x_i, and complementary slackness gives αi>0\alpha_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δ4\delta, because λ=4\lambda = 4 (section 2.4). (3) No. The projection shifts all entries by the same ν\nu and then clips; it does not rescale. The answer is (0.7,0.3,0)(0.7, 0.3, 0) (section 4). The rescaled point is feasible but farther from yy.

    4. Worked example #

    (a) KKT, multiplier, dual and certificate. Minimize f0(x)=(x1−2)2+(x2−1)2f_0(x) = (x_1 - 2)^2 + (x_2 - 1)^2 subject to f1(x)=x1+x2−1≤0f_1(x) = x_1 + x_2 - 1 \le 0. This is the projection of (2,1)(2, 1) onto a half-plane.

    • Which case? The unconstrained minimizer (2,1)(2, 1) has x1+x2=3>1x_1 + x_2 = 3 > 1. It is infeasible, so the constraint must be active (λ>0\lambda > 0).
    • Stationarity: ∇f0+λ∇f1=0\nabla f_0 + \lambda \nabla f_1 = 0 reads 2(x1−2)+λ=02(x_1 - 2) + \lambda = 0 and 2(x2−1)+λ=02(x_2 - 1) + \lambda = 0. So x=(2−λ/2, 1−λ/2)x = (2 - \lambda/2,\ 1 - \lambda/2).
    • Active constraint: (2−λ/2)+(1−λ/2)=1(2 - \lambda/2) + (1 - \lambda/2) = 1 gives 3−λ=13 - \lambda = 1, so λ⋆=2\lambda^\star = 2. Then x⋆=(1,0)x^\star = (1, 0) and p⋆=1+1=2p^\star = 1 + 1 = 2.
    • Check all four conditions: f1(x⋆)=0≤0f_1(x^\star) = 0 \le 0 ✓; λ⋆=2≥0\lambda^\star = 2 \ge 0 ✓; λ⋆f1(x⋆)=0\lambda^\star f_1(x^\star) = 0 ✓; ∇f0(x⋆)+λ⋆∇f1=(−2,−2)+2(1,1)=0\nabla f_0(x^\star) + \lambda^\star \nabla f_1 = (-2, -2) + 2(1, 1) = 0 ✓. The problem is convex, so x⋆x^\star is optimal (theorem above).
    • Sensitivity: with x1+x2≤1+ux_1 + x_2 \le 1 + u, the same steps give λ=2−u\lambda = 2 - u and p⋆(u)=2(λ/2)2=(2−u)2/2p^\star(u) = 2(\lambda/2)^2 = (2 - u)^2/2. So dp⋆/du=−(2−u)=−2=−λ⋆dp^\star/du = -(2 - u) = -2 = -\lambda^\star at u=0u = 0. For u=0.1u = 0.1: p⋆=1.92/2=1.805p^\star = 1.9^2/2 = 1.805, and the linear estimate gives 2−2(0.1)=1.82 - 2(0.1) = 1.8.
    • Dual: for fixed λ\lambda, L\mathcal L is minimized at x(λ)=(2−λ/2, 1−λ/2)x(\lambda) = (2 - \lambda/2,\ 1 - \lambda/2). Substitute: g(λ)=2(λ/2)2+λ(3−λ−1)=2λ−λ2/2g(\lambda) = 2(\lambda/2)^2 + \lambda(3 - \lambda - 1) = 2\lambda - \lambda^2/2. Its maximum is at λ=2\lambda = 2 with d⋆=4−2=2=p⋆d^\star = 4 - 2 = 2 = p^\star. (Slater holds: x=(0,0)x = (0, 0) is strictly feasible.)
    • Certificate: the feasible guess x=(0.5,0.5)x = (0.5, 0.5) has f0=2.25+0.25=2.5f_0 = 2.25 + 0.25 = 2.5. The price λ=1\lambda = 1 gives g(1)=2−0.5=1.5g(1) = 2 - 0.5 = 1.5. The gap is 2.5−1.5=12.5 - 1.5 = 1, so f0(x)−p⋆≤1f_0(x) - p^\star \le 1 without solving anything. (The true value is 2.5−2=0.52.5 - 2 = 0.5.)

    (b) Simplex projection of y=(1.0,0.6,−0.4)y = (1.0, 0.6, -0.4). Sorted: u=(1.0,0.6,−0.4)u = (1.0, 0.6, -0.4), partial sums 1.0, 1.6, 1.21.0,\ 1.6,\ 1.2.

    • j=1j = 1: 1.0−(1.0−1)/1=1.0>01.0 - (1.0 - 1)/1 = 1.0 > 0.
    • j=2j = 2: 0.6−(1.6−1)/2=0.3>00.6 - (1.6 - 1)/2 = 0.3 > 0.
    • j=3j = 3: −0.4−(1.2−1)/3≈−0.467<0-0.4 - (1.2 - 1)/3 \approx -0.467 < 0.

    So ρ=2\rho = 2 and ν=(1.6−1)/2=0.3\nu = (1.6 - 1)/2 = 0.3. Then x=max⁡(y−0.3,0)=(0.7,0.3,0)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)\lambda = (0, 0, \nu - y_3) = (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)=0x - y + \nu\mathbf 1 - \lambda = (0.7 - 1 + 0.3,\ 0.3 - 0.6 + 0.3,\ 0 + 0.4 + 0.3 - 0.7) = 0 ✓. The squared distance to yy is 0.09+0.09+0.16=0.340.09 + 0.09 + 0.16 = 0.34, against 0.351250.35125 for the rescaled point (0.625,0.375,0)(0.625, 0.375, 0).

    Quick check

    A friend projects y=(2,0,0)y = (2, 0, 0) onto the probability simplex by "clip the negatives, then divide by the sum" and gets (1,0,0)(1, 0, 0). Is this the correct projection?

    (c) A lasso certificate. A=(111001)A = \begin{pmatrix} 1 & 1 \\ 1 & 0 \\ 0 & 1 \end{pmatrix}, b=(1,2,−0.5)b = (1, 2, -0.5), λ=1\lambda = 1. (Lesson 09 solves it: x⋆=(1,0)x^\star = (1, 0), P⋆=1.625P^\star = 1.625.) Take the guess x=(0.5,0)x = (0.5, 0).

    • r=b−Ax=(1,2,−0.5)−(0.5,0.5,0)=(0.5,1.5,−0.5)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)A^\top r = (0.5 + 1.5,\ 0.5 - 0.5) = (2, 0). So s=min⁡(1,1/2)=0.5s = \min(1, 1/2) = 0.5 and θ=(0.25,0.75,−0.25)\theta = (0.25, 0.75, -0.25).
    • P(x)=12(0.25+2.25+0.25)+0.5=1.875P(x) = \tfrac12(0.25 + 2.25 + 0.25) + 0.5 = 1.875.
    • D(θ)=12∥b∥2−12∥b−θ∥2=12(5.25)−12(0.5625+1.5625+0.0625)=1.53125D(\theta) = \tfrac12\|b\|^2 - \tfrac12\|b - \theta\|^2 = \tfrac12(5.25) - \tfrac12(0.5625 + 1.5625 + 0.0625) = 1.53125.
    • The gap 1.875−1.53125=0.343751.875 - 1.53125 = 0.34375 bounds the true suboptimality P(x)−P⋆=0.25P(x) - P^\star = 0.25 ✓. At x⋆=(1,0)x^\star = (1, 0): r=(0,1,−0.5)r = (0, 1, -0.5) and A⊤r=(1,−0.5)A^\top r = (1, -0.5), so s=1s = 1, θ=r\theta = r, and the gap is 0.

    5. In code #

    SciPy's general constrained solver (SLSQP) checks the hand results. The notebook builds the simplex projection, the multipliers and the lasso gap yourself.

    import numpy as np
    from scipy.optimize import minimize
    
    # (a) min (x1-2)^2 + (x2-1)^2  s.t.  x1 + x2 <= c, at c = 1
    f = lambda x: (x[0] - 2) ** 2 + (x[1] - 1) ** 2
    
    def solve(c):
        con = {"type": "ineq", "fun": lambda x: c - x[0] - x[1]}   # SciPy wants fun(x) >= 0
        return minimize(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.0
    print(res.multipliers.round(6))                                 # [2.]  (SciPy >= 1.16)
    h = 1e-3                                                        # multiplier = sensitivity
    print(round(-(solve(1 + h).fun - solve(1 - h).fun) / (2 * h), 6))   # 2.0
    
    # (b) projection of y onto the probability simplex
    y = np.array([1.0, 0.6, -0.4])
    res = minimize(lambda x: 0.5 * np.sum((x - y) ** 2), np.ones(3) / 3, method="SLSQP",
                   bounds=[(0, None)] * 3, tol=1e-12,
                   constraints=[{"type": "eq", "fun": lambda x: x.sum() - 1}])
    naive = np.maximum(y, 0) / np.maximum(y, 0).sum()               # clip and renormalize
    print(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 = 10
    A, b = np.array([[3.0, 4.0]]), np.array([10.0])
    x_star = np.linalg.pinv(A) @ b
    d_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 nn. The KKT derivation of the simplex projection is exact and costs O(nlog⁡n)O(n \log n).

    6. Where it shows up #

    • 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,θ)J(u, \theta) subject to a discretized PDE F(u,θ)=0F(u, \theta) = 0 (state uu, parameters θ\theta), form J+μ⊤FJ + \mu^\top F with a multiplier vector μ\mu. Stationarity in uu is the adjoint equation (∂F/∂u)⊤μ=−(∂J/∂u)⊤(\partial F/\partial u)^\top \mu = -(\partial J/\partial u)^\top, and then dJ/dθ=∂J/∂θ+μ⊤∂F/∂θdJ/d\theta = \partial J/\partial\theta + \mu^\top \partial F/\partial\theta. 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.

    7. Common mistakes #

    • Wrong sign of λ\lambda. The rule λ≥0\lambda \ge 0 holds for constraints written as fi(x)≤0f_i(x) \le 0 and added as +λifi+\lambda_i f_i. For x1+x2≥2x_1 + x_2 \ge 2, write 2−x1−x2≤02 - x_1 - x_2 \le 0 first. Equality multipliers ν\nu 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\lambda < 0, the constraint is inactive.
    • Treating every Lagrange point as a minimum. ∇f+ν∇h=0\nabla f + \nu\nabla 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=−∞g = -\infty the dual point gives no bound. For the lasso this is ∥A⊤θ∥∞≤λ\|A^\top\theta\|_\infty \le \lambda; a "gap" computed from an infeasible θ\theta certifies nothing.
    • Clip-and-renormalize for the simplex. The result is feasible, but it is not the projection.

    8. Check yourself #

    1. Why is the dual function concave even when the primal problem is not convex?
      AnswerFor each fixed xx, L(x,λ,ν)\mathcal L(x, \lambda, \nu) is affine in (λ,ν)(\lambda, \nu). The dual function is the pointwise infimum of this family of affine functions (their lower envelope), and such an infimum is concave.
    2. Minimize x1+2x2x_1 + 2x_2 on the ellipse 14x12+x22=1\tfrac14 x_1^2 + x_2^2 = 1 with a Lagrange multiplier. Give the minimizer and ν\nu.
      Answer∇f=(1,2)\nabla f = (1, 2) and ∇h=(x1/2,2x2)\nabla h = (x_1/2, 2x_2). Stationarity: 1+νx1/2=01 + \nu x_1/2 = 0 and 2+2νx2=02 + 2\nu x_2 = 0, so x1=−2/νx_1 = -2/\nu and x2=−1/νx_2 = -1/\nu. The constraint gives 1/ν2+1/ν2=11/\nu^2 + 1/\nu^2 = 1, so ν=±2\nu = \pm\sqrt2. For ν=2\nu = \sqrt2: x=(−2,−1/2)x = (-\sqrt2, -1/\sqrt2) with f=−22≈−2.83f = -2\sqrt2 \approx -2.83, the minimum. For ν=−2\nu = -\sqrt2: x=(2,1/2)x = (\sqrt2, 1/\sqrt2) with f=22f = 2\sqrt2, the maximum.
    3. Solve min⁡∥x∥2\min \|x\|^2 s.t. 3x1+4x2=103x_1 + 4x_2 = 10 through the dual. What does the price ν=−1\nu = -1 tell you?
      AnswerAA⊤=25AA^\top = 25, so g(ν)=−254ν2−10νg(\nu) = -\tfrac{25}{4}\nu^2 - 10\nu. Its maximum is at ν⋆=−0.8\nu^\star = -0.8 with d⋆=−4+8=4d^\star = -4 + 8 = 4, and x⋆=(3,4)⋅10/25=(1.2,1.6)x^\star = (3, 4) \cdot 10/25 = (1.2, 1.6) with p⋆=4p^\star = 4. Any ν\nu gives a lower bound: g(−1)=−6.25+10=3.75≤4g(-1) = -6.25 + 10 = 3.75 \le 4.
    4. Take min⁡x\min x s.t. x2≤0x^2 \le 0. Find p⋆p^\star and d⋆d^\star. Is the dual optimum attained?
      AnswerOnly x=0x = 0 is feasible, so p⋆=0p^\star = 0. For λ>0\lambda > 0, g(λ)=inf⁡x(x+λx2)=−1/(4λ)g(\lambda) = \inf_x (x + \lambda x^2) = -1/(4\lambda) (at x=−1/(2λ)x = -1/(2\lambda)), and g(0)=−∞g(0) = -\infty. So d⋆=sup⁡λg=0=p⋆d^\star = \sup_\lambda g = 0 = p^\star, but no λ\lambda attains it. No KKT multiplier exists (stationarity 1=01 = 0 fails), because Slater's condition fails.
    5. In a hard-margin SVM, a training point has yi(w⊤xi+b)>1y_i(w^\top x_i + b) > 1. What is αi\alpha_i, and what happens if you delete the point?
      Answerαi=0\alpha_i = 0 by complementary slackness, because its constraint is not tight. It does not appear in w=∑iαiyixiw = \sum_i \alpha_i y_i x_i, so deleting it does not change the solution.
    6. For the lasso, show that x=0x = 0 is optimal exactly when λ≥∥A⊤b∥∞\lambda \ge \|A^\top b\|_\infty.
      AnswerIf ∥A⊤b∥∞≤λ\|A^\top b\|_\infty \le \lambda, then θ=b\theta = b is dual feasible, and the gap at x=0x = 0 is P(0)−D(b)=12∥b∥2−12∥b∥2=0P(0) - D(b) = \tfrac12\|b\|^2 - \tfrac12\|b\|^2 = 0, so x=0x = 0 is optimal. Conversely, if x⋆=0x^\star = 0, then θ⋆=b−A⋅0=b\theta^\star = b - A \cdot 0 = b must be dual feasible. So λmax⁡=∥A⊤b∥∞\lambda_{\max} = \|A^\top b\|_\infty 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.
    1. At a constrained minimum of ff on the curve h(x)=0h(x) = 0, how are ∇f\nabla f and ∇h\nabla h related, and why?

    2. Write the Lagrangian of min⁡f0(x)\min f_0(x) s.t. fi(x)≤0f_i(x) \le 0, hj(x)=0h_j(x) = 0.

    3. What does a Lagrange multiplier λi⋆\lambda_i^\star mean?

    4. Name the four KKT conditions.

    5. What does complementary slackness say in words?

    6. What is Slater's condition, and what does it give?

    7. State weak duality.

    8. How does a duality gap act as a stopping certificate?

    9. Give the Euclidean projection of yy onto the probability simplex.

    10. Compute the multiplier of min⁡(x−3)2\min (x - 3)^2 s.t. x≤1x \le 1.

    Open the review →

    9. Interview angle #

    • "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⋆d^\star \le p^\star 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⋆p^\star.
    • "Project a vector onto the simplex in O(nlog⁡n)O(n \log n)." — Sort, find ρ\rho and ν\nu, shift and clip.

    10. Practice #

    The notebook for this lesson runs in Colab. Each exercise has a check cell and a folded solution.

    1. Solve the equality example as a linear KKT system, and check that the multiplier equals the numerical sensitivity −dp⋆/dc-dp^\star/dc.
    2. Project onto a box and onto the probability simplex, return the multipliers, and verify all four KKT conditions.
    3. Solve the inequality example with scipy.optimize.minimize (SLSQP) for many values of cc, and compare the slope of p⋆(c)p^\star(c) with your λ\lambda.
    4. Least norm through the dual: build dual feasible points and compute the duality gap of a guess.
    5. Lasso on the diabetes data: use the duality gap of the iterates of a simple solver as a stopping rule.

    11. Watch and read #

    The tangency picture of section 2.1, with contour lines and a constraint curve.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    Why the multiplier is the rate of change of the optimal value (section 2.3).
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    Duality and the KKT conditions with clear animations; the last part shows how interior-point solvers use them.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    The Lagrange dual function, weak and strong duality, and Slater's condition (book chapter 5).
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    • Boyd and Vandenberghe, Convex Optimization, free PDF: §5.1 (dual function; §5.1.5 least norm), §5.2 (weak duality, Slater), §5.5 (certificates, complementary slackness, KKT), §5.6 (sensitivity), exercise 5.21 (a positive gap).
    • Ryan Tibshirani, CMU 10-725 Convex Optimization, lecture notes on KKT conditions and duality.
    • Wang and Carreira-Perpiñán, Projection onto the probability simplex (the sort-based algorithm and its KKT proof).
    • Martins and Astudillo, From softmax to sparsemax (the simplex projection as an activation).
    • scikit-learn user guide, Lasso (coordinate descent with the duality-gap stopping rule).
    • S. G. Johnson, Notes on adjoint methods (the adjoint vector as a Lagrange multiplier).