AI Knowledge Tree

Lagrangian duality

Form the dual problem, prove weak duality, and state when strong duality holds.

Every constrained minimization has a shadow problem, its dual, built from the Lagrangian. The dual is always a concave maximization (even when the original is not convex), it always lower-bounds the original, and for convex problems the two have the same optimal value. This is the theory behind SVM solvers and the certificates that prove a solution optimal.

The primal and the Lagrangian

The primal problem, now with inequality constraints, is

p⋆=min⁡xf(x)s.t.gi(x)≤0,  hj(x)=0.p^\star = \min_x f(x) \quad \text{s.t.} \quad g_i(x) \le 0,\ \ h_j(x) = 0.

The Lagrangian attaches a multiplier to each constraint, with λi≥0\lambda_i \ge 0 for inequalities and free νj\nu_j for equalities:

L(x,λ,ν)=f(x)+∑iλigi(x)+∑jνjhj(x).\mathcal{L}(x, \lambda, \nu) = f(x) + \sum_i \lambda_i g_i(x) + \sum_j \nu_j h_j(x).

The dual function

The Lagrange dual function minimizes the Lagrangian over xx for fixed multipliers:

D(λ,ν)=min⁡xL(x,λ,ν).\mathcal{D}(\lambda, \nu) = \min_x \mathcal{L}(x, \lambda, \nu).

It is a pointwise minimum of functions that are affine in (λ,ν)(\lambda, \nu), and a pointwise minimum of affine functions is concave. So D\mathcal{D} is concave regardless of whether ff or the constraints are convex, which is why the dual is always a well-behaved maximization.

Weak duality

Theorem. For any λ≥0\lambda \ge 0 and any ν\nu, D(λ,ν)≤p⋆\mathcal{D}(\lambda, \nu) \le p^\star[1].

Proof. Let xx be any feasible point for the primal, so gi(x)≤0g_i(x) \le 0 and hj(x)=0h_j(x) = 0. With λi≥0\lambda_i \ge 0, the term ∑iλigi(x)≤0\sum_i \lambda_i g_i(x) \le 0 and ∑jνjhj(x)=0\sum_j \nu_j h_j(x) = 0, hence

L(x,λ,ν)=f(x)+∑iλigi(x)⏟≤0+∑jνjhj(x)⏟=0≤f(x).\mathcal{L}(x, \lambda, \nu) = f(x) + \underbrace{\sum_i \lambda_i g_i(x)}_{\le 0} + \underbrace{\sum_j \nu_j h_j(x)}_{= 0} \le f(x).

Minimizing the left side over all xx only decreases it, so D(λ,ν)=min⁡xL≤f(x)\mathcal{D}(\lambda, \nu) = \min_x \mathcal{L} \le f(x) for every feasible xx, and in particular ≤p⋆\le p^\star. ■\qquad\blacksquare

The dual problem and the duality gap

The best (largest) lower bound is the dual problem:

d⋆=max⁡λ≥0, νD(λ,ν).d^\star = \max_{\lambda \ge 0,\, \nu} \mathcal{D}(\lambda, \nu).

Weak duality says d⋆≤p⋆d^\star \le p^\star always. The difference p⋆−d⋆≥0p^\star - d^\star \ge 0 is the duality gap. Any dual-feasible (λ,ν)(\lambda, \nu) produces a certified lower bound on the primal optimum, a way to bound how suboptimal a candidate solution is.

Strong duality

Strong duality holds when d⋆=p⋆d^\star = p^\star (zero gap). It is not automatic, but for convex primal problems it holds under a mild condition.

Slater's condition: if the primal is convex and there exists a strictly feasible point (some xx with gi(x)<0g_i(x) < 0 for all inequality constraints), then strong duality holds. Most convex problems met in practice (linear programs, convex QPs, SVMs) satisfy Slater, so their dual solves the primal exactly[2]. When strong duality holds, the primal and dual solutions are linked by the KKT conditions of the next lesson.

Check it on arrays

python · numpy
import numpy as np

# Convex QP: min 0.5 x^T x s.t. a^T x >= 1  (i.e. g(x) = 1 - a^T x <= 0)
a = np.array([1.0, 2.0])

# Dual function: D(lam) = min_x 0.5 x^T x + lam (1 - a^T x), lam >= 0.
# Inner min at x = lam a, giving D(lam) = lam - 0.5 lam^2 ||a||^2.
# Maximize over lam >= 0: lam* = 1/||a||^2, d* = 1/(2||a||^2).
norm2 = a @ a
lam_star = 1.0 / norm2
d_star = lam_star - 0.5 * lam_star**2 * norm2
print("dual optimum d* =", round(d_star, 6))

# Primal optimum: closest point to origin on the half-space a^T x >= 1
# is x* = a / ||a||^2, with f = 0.5 / ||a||^2.
x_star = a / norm2
p_star = 0.5 * x_star @ x_star
print("primal optimum p* =", round(p_star, 6))
print("duality gap:", round(p_star - d_star, 8), " (zero -> strong duality)")

# Weak duality: any lam >= 0 gives a valid lower bound
for lam in [0.05, 0.1, 0.2, 0.5]:
    D = lam - 0.5 * lam**2 * norm2
    print(f"lam={lam}: D(lam)={D:.5f} <= p*={p_star:.5f}: {D <= p_star + 1e-12}")

The dual and primal optima coincide (zero gap, strong duality via Slater), and every nonnegative multiplier yields a lower bound below the primal optimum.

Exercises

Exercise 1. For the problem min⁡x12x⊤x\min_x \tfrac12 x^\top x subject to a⊤x≥1a^\top x \ge 1 (so g(x)=1−a⊤x≤0g(x) = 1 - a^\top x \le 0) with a=(1,2)a = (1, 2), derive the dual function D(λ)\mathcal{D}(\lambda) in closed form and find d⋆d^\star.

Solution

Step 1. Write the Lagrangian for λ≥0\lambda \ge 0:

L(x,λ)=12x⊤x+λ(1−a⊤x).\mathcal{L}(x, \lambda) = \tfrac12 x^\top x + \lambda(1 - a^\top x).

Step 2. Minimize over xx. Setting ∇xL=x−λa=0\nabla_x \mathcal{L} = x - \lambda a = 0 gives the inner minimizer x=λax = \lambda a.

Step 3. Substitute back, using a⊤a=∥a∥2a^\top a = \lVert a\rVert^2:

D(λ)=12λ2∥a∥2+λ(1−λ∥a∥2)=λ−12λ2∥a∥2.\mathcal{D}(\lambda) = \tfrac12 \lambda^2 \lVert a\rVert^2 + \lambda\big(1 - \lambda\lVert a\rVert^2\big) = \lambda - \tfrac12 \lambda^2 \lVert a\rVert^2.

Step 4. Maximize the concave D\mathcal{D} over λ≥0\lambda \ge 0. The derivative 1−λ∥a∥2=01 - \lambda\lVert a\rVert^2 = 0 gives λ⋆=1/∥a∥2\lambda^\star = 1/\lVert a\rVert^2, which is nonnegative. With ∥a∥2=1+4=5\lVert a\rVert^2 = 1 + 4 = 5, λ⋆=1/5\lambda^\star = 1/5 and

d⋆=1∥a∥2−12⋅1∥a∥4∥a∥2=12∥a∥2=110=0.1.d^\star = \tfrac{1}{\lVert a\rVert^2} - \tfrac12 \cdot \tfrac{1}{\lVert a\rVert^4}\lVert a\rVert^2 = \tfrac{1}{2\lVert a\rVert^2} = \tfrac{1}{10} = 0.1.

Exercise 2. Prove weak duality in the form: for the primal optimum p⋆p^\star and any dual-feasible (λ,ν)(\lambda, \nu) with λ≥0\lambda \ge 0, the value D(λ,ν)\mathcal{D}(\lambda, \nu) is a lower bound on ff at every primal-feasible point.

Solution

Step 1. Let xx be primal-feasible, so gi(x)≤0g_i(x) \le 0 and hj(x)=0h_j(x) = 0. Because λi≥0\lambda_i \ge 0 and gi(x)≤0g_i(x) \le 0, each product λigi(x)≤0\lambda_i g_i(x) \le 0, and the equality terms vanish: νjhj(x)=0\nu_j h_j(x) = 0.

Step 2. Therefore

L(x,λ,ν)=f(x)+∑iλigi(x)+∑jνjhj(x)≤f(x).\mathcal{L}(x, \lambda, \nu) = f(x) + \sum_i \lambda_i g_i(x) + \sum_j \nu_j h_j(x) \le f(x).

Step 3. The dual function minimizes the Lagrangian over all xx, so it is no larger than this particular value:

D(λ,ν)=min⁡x′L(x′,λ,ν)≤L(x,λ,ν)≤f(x).\mathcal{D}(\lambda, \nu) = \min_{x'} \mathcal{L}(x', \lambda, \nu) \le \mathcal{L}(x, \lambda, \nu) \le f(x).

This holds for every primal-feasible xx, so D(λ,ν)≤p⋆\mathcal{D}(\lambda, \nu) \le p^\star.

Exercise 3 (code). For a linear program min⁡xc⊤x\min_x c^\top x subject to Ax≥bAx \ge b, the dual is max⁡y≥0b⊤y\max_{y \ge 0} b^\top y subject to A⊤y=cA^\top y = c. Take c=(1,1)c = (1, 1), A=I2A = I_2, b=(2,3)b = (2, 3). Solve the primal directly (the constraint x≥bx \ge b forces x=bx = b since c>0c > 0), construct the dual point y=cy = c, and verify weak duality holds with zero gap.

Solution
python · numpy
import numpy as np

c = np.array([1.0, 1.0])
A = np.eye(2)
b = np.array([2.0, 3.0])

# Primal: minimize c^T x s.t. x >= b. With c > 0, push x down to the bound.
x_star = b.copy()
p_star = c @ x_star

# Dual: max b^T y s.t. A^T y = c, y >= 0. Here A^T = I so y = c, and c >= 0.
y = np.linalg.solve(A.T, c)
assert np.all(y >= 0)            # dual feasibility
d_star = b @ y

assert np.isclose(p_star, d_star)   # zero duality gap
print("p* =", p_star, " d* =", d_star, " gap =", p_star - d_star)

The primal optimum p⋆=5p^\star = 5 and the dual optimum d⋆=b⊤c=5d^\star = b^\top c = 5 coincide, so the duality gap is zero, as strong duality predicts for a feasible LP.

The next lesson collects the optimality conditions linking primal and dual: the KKT conditions.


References

  1. [1]Boyd, S., & Vandenberghe, L. (2004). Convex Optimization, §5.1-5.2. Cambridge University Press. The Lagrange dual function and weak duality.↩
  2. [2]Bertsekas, D. P. (1999). Nonlinear Programming (2nd ed.), §5.3. Athena Scientific. Strong duality and the Slater constraint qualification.↩