Skip to main content
© 2026 ePowerAI — free, open courseware. No login required.
CoursesContact
ePOWERAI
CoursesContact
Week 12: Duality for Practitioners
Optimization

Foundations

  1. 01Week 1: Optimization for Learning
  2. 02Week 2: Convex Sets and Functions
  3. 03Week 3: Gradient Descent Theory

Model problems

  1. 04Week 4: Least Squares and Conditioning
  2. 05Week 5: Classification Losses

First-order stack

  1. 06Week 6: Stochastic Gradient Descent
  2. 07Week 7: Momentum and Acceleration
  3. 08Week 8: Adaptive First-Order Methods
  4. 09Week 9: Practical Training Dynamics

Structure

  1. 10Week 10: Nonsmooth and Composite Objectives
  2. 11Week 11: Constraints: Projections and Penalties
  3. 12Week 12: Duality for Practitioners

Nonconvex practice

  1. 13Week 13: Nonconvex Deep Learning
  2. 14Week 14: Capstone — Theory Meets Training
Week 12· Optimization· Structure8 min read

Week 12: Duality for Practitioners

Learning Outcomes
  • Form the Lagrangian for an inequality-constrained convex problem
  • Define the dual function and state weak duality (dual ≤\leq≤ primal)
  • State strong duality under Slater's condition
  • Derive the soft-margin SVM dual as an ML-flavoured dual objective
Prerequisites

Background: Weeks 2 (convexity), 5 (classification losses), and 11 (Lagrange multipliers, constraint intuition). The Lagrange multiplier idea from Week 11 is the core building block.

Later weeks: Advanced Optimization takes duality to Boyd depth (KKT system, sensitivity, conic duality). The capstone (Week 14) uses dual concepts in method selection.

Lagrangian#

Consider a convex optimisation problem with inequality constraints:

min⁡x  f(x)subject tohi(x)≤0,  i=1,…,m\min_{x} \; f(x) \quad \text{subject to} \quad h_i(x) \leq 0, \; i = 1, \ldots, mminx​f(x)subject tohi​(x)≤0,i=1,…,m

The Lagrangian combines the objective and the constraints with Lagrange multipliers λi≥0\lambda_i \geq 0λi​≥0:

L(x,λ)=f(x)+∑i=1mλihi(x)L(x, \lambda) = f(x) + \sum_{i=1}^m \lambda_i h_i(x)L(x,λ)=f(x)+∑i=1m​λi​hi​(x)

where λ∈Rm\lambda \in \mathbb{R}^mλ∈Rm, λ≥0\lambda \geq 0λ≥0 (elementwise). The multipliers penalise constraint violations: if hi(x)>0h_i(x) > 0hi​(x)>0, the term λihi(x)\lambda_i h_i(x)λi​hi​(x) adds a positive penalty.

The primal problem can be rewritten as:

min⁡xmax⁡λ≥0L(x,λ)\min_{x} \max_{\lambda \geq 0} L(x, \lambda)minx​maxλ≥0​L(x,λ)

If xxx violates any constraint (hi(x)>0h_i(x) > 0hi​(x)>0), the inner max drives L→∞L \to \inftyL→∞ by letting λi→∞\lambda_i \to \inftyλi​→∞. If xxx is feasible (all hi(x)≤0h_i(x) \leq 0hi​(x)≤0), the max over λ\lambdaλ sets each λi=0\lambda_i = 0λi​=0 or keeps it at the boundary, recovering f(x)f(x)f(x). So max⁡λ≥0L(x,λ)\max_{\lambda \geq 0} L(x, \lambda)maxλ≥0​L(x,λ) equals f(x)f(x)f(x) when xxx is feasible and +∞+\infty+∞ otherwise.

Dual function#

The Lagrange dual function is obtained by swapping min and max:

g(λ)=min⁡xL(x,λ)=min⁡x[f(x)+∑iλihi(x)]g(\lambda) = \min_{x} L(x, \lambda) = \min_{x} \left[ f(x) + \sum_i \lambda_i h_i(x) \right]g(λ)=minx​L(x,λ)=minx​[f(x)+∑i​λi​hi​(x)]

For each fixed λ≥0\lambda \geq 0λ≥0, g(λ)g(\lambda)g(λ) is the minimum of an unconstrained (but λ\lambdaλ-parametrised) problem. The domain of ggg is {λ≥0∣g(λ)>−∞}\{\lambda \geq 0 \mid g(\lambda) > -\infty\}{λ≥0∣g(λ)>−∞}.

The dual problem is to maximise the dual function:

max⁡λ≥0  g(λ)\max_{\lambda \geq 0} \; g(\lambda)maxλ≥0​g(λ)

Worked example: 1D quadratic with constraint.

min⁡x  12x2\min_x \; \frac{1}{2}x^2minx​21​x2 subject to x≥1x \geq 1x≥1 (rewritten as 1−x≤01 - x \leq 01−x≤0).

Lagrangian: L(x,λ)=12x2+λ(1−x)L(x, \lambda) = \frac{1}{2}x^2 + \lambda(1 - x)L(x,λ)=21​x2+λ(1−x), λ≥0\lambda \geq 0λ≥0.

Dual function: min⁡xL(x,λ)\min_x L(x, \lambda)minx​L(x,λ). Set ∂L∂x=x−λ=0  ⟹  x=λ\frac{\partial L}{\partial x} = x - \lambda = 0 \implies x = \lambda∂x∂L​=x−λ=0⟹x=λ. Substitute: g(λ)=12λ2+λ(1−λ)=λ−12λ2g(\lambda) = \frac{1}{2}\lambda^2 + \lambda(1 - \lambda) = \lambda - \frac{1}{2}\lambda^2g(λ)=21​λ2+λ(1−λ)=λ−21​λ2.

Dual problem: max⁡λ≥0(λ−12λ2)\max_{\lambda \geq 0} (\lambda - \frac{1}{2}\lambda^2)maxλ≥0​(λ−21​λ2). Set derivative to zero: 1−λ=0  ⟹  λ∗=11 - \lambda = 0 \implies \lambda^* = 11−λ=0⟹λ∗=1. Dual value: g(1)=1−0.5=0.5g(1) = 1 - 0.5 = 0.5g(1)=1−0.5=0.5. Primal optimum: x∗=1x^* = 1x∗=1, f(1)=0.5f(1) = 0.5f(1)=0.5. Duality gap: 000.

primal f(x)=½x², x≥1 x* = 1 feasible x ≥ 1 dual g(λ)=λ−½λ², λ≥0 λ* = 1 at the optimum f* = g(λ*): the duality gap is zero

For min⁡12x2\min \tfrac12 x^2min21​x2 s.t. x≥1x \ge 1x≥1, the dual g(λ)=λ−12λ2g(\lambda) = \lambda - \tfrac12\lambda^2g(λ)=λ−21​λ2 peaks at λ∗=1\lambda^* = 1λ∗=1 with g(1)=0.5g(1) = 0.5g(1)=0.5, exactly the primal optimum. Weak duality says the dual can never exceed the primal, while strong duality closes the gap to zero.

Weak and strong duality#

Weak duality: For any feasible xxx and any λ≥0\lambda \geq 0λ≥0,

g(λ)≤f(x)g(\lambda) \leq f(x)g(λ)≤f(x)

The dual function always lower-bounds the primal objective. Maximising ggg gives the best possible lower bound. The duality gap f(x∗)−g(λ∗)f(x^*) - g(\lambda^*)f(x∗)−g(λ∗) is always nonnegative.

Strong duality: Under certain conditions, f(x∗)=g(λ∗)f(x^*) = g(\lambda^*)f(x∗)=g(λ∗), so the duality gap is zero. For convex problems, Slater's condition is sufficient: if there exists a strictly feasible point (all hi(x)<0h_i(x) < 0hi​(x)<0), then strong duality holds.

In practice, most convex ML problems satisfy Slater's condition trivially. Strong duality means we can solve either the primal or the dual and get the same answer, whichever is easier.

Complementary slackness#

At the primal-dual optimum (x∗,λ∗)(x^*, \lambda^*)(x∗,λ∗) with strong duality:

λi∗ hi(x∗)=0for all i\lambda_i^* \, h_i(x^*) = 0 \quad \text{for all } iλi∗​hi​(x∗)=0for all i

This is complementary slackness. For each constraint:

  • If hi(x∗)<0h_i(x^*) < 0hi​(x∗)<0 (constraint is inactive/slack), then λi∗=0\lambda_i^* = 0λi∗​=0 (the multiplier for that constraint is zero)
  • If λi∗>0\lambda_i^* > 0λi∗​>0 (the multiplier is active), then hi(x∗)=0h_i(x^*) = 0hi​(x∗)=0 (the constraint is tight/binding)

Complementary slackness tells us which constraints matter at the optimum. In the 1D example above: h(x∗)=1−1=0h(x^*) = 1 - 1 = 0h(x∗)=1−1=0 (active), so λ∗\lambda^*λ∗ can be nonzero; indeed λ∗=1\lambda^* = 1λ∗=1.

SVM dual as worked example#

The soft-margin SVMSupport Vector Machine is a canonical ML application of duality. The primal problem:

min⁡w,b,ξ  12∥w∥22+C∑i=1nξi\min_{w, b, \xi} \; \frac{1}{2}\|w\|_2^2 + C \sum_{i=1}^n \xi_iminw,b,ξ​21​∥w∥22​+C∑i=1n​ξi​ s.t.yi(wTxi+b)≥1−ξi,ξi≥0,  i=1,…,n\text{s.t.} \quad y_i (w^T x_i + b) \geq 1 - \xi_i, \quad \xi_i \geq 0, \; i = 1,\ldots,ns.t.yi​(wTxi​+b)≥1−ξi​,ξi​≥0,i=1,…,n

The Lagrangian introduces multipliers αi≥0\alpha_i \geq 0αi​≥0 (for the margin constraints) and μi≥0\mu_i \geq 0μi​≥0 (for ξi≥0\xi_i \geq 0ξi​≥0):

L=12∥w∥22+C∑iξi−∑iαi(yi(wTxi+b)−1+ξi)−∑iμiξiL = \frac{1}{2}\|w\|_2^2 + C\sum_i \xi_i - \sum_i \alpha_i(y_i(w^T x_i + b) - 1 + \xi_i) - \sum_i \mu_i \xi_iL=21​∥w∥22​+C∑i​ξi​−∑i​αi​(yi​(wTxi​+b)−1+ξi​)−∑i​μi​ξi​

Setting ∂L/∂w=0\partial L/\partial w = 0∂L/∂w=0, ∂L/∂b=0\partial L/\partial b = 0∂L/∂b=0, ∂L/∂ξi=0\partial L/\partial \xi_i = 0∂L/∂ξi​=0 gives:

  • w=∑iαiyixiw = \sum_i \alpha_i y_i x_iw=∑i​αi​yi​xi​
  • ∑iαiyi=0\sum_i \alpha_i y_i = 0∑i​αi​yi​=0
  • αi+μi=C  ⟹  0≤αi≤C\alpha_i + \mu_i = C \implies 0 \leq \alpha_i \leq Cαi​+μi​=C⟹0≤αi​≤C

Substituting back, the SVM dual is:

max⁡α  ∑i=1nαi−12∑i=1n∑j=1nαiαjyiyj(xiTxj)\max_{\alpha} \; \sum_{i=1}^n \alpha_i - \frac{1}{2} \sum_{i=1}^n \sum_{j=1}^n \alpha_i \alpha_j y_i y_j (x_i^T x_j)maxα​∑i=1n​αi​−21​∑i=1n​∑j=1n​αi​αj​yi​yj​(xiT​xj​) s.t.0≤αi≤C,∑iαiyi=0\text{s.t.} \quad 0 \leq \alpha_i \leq C, \quad \sum_i \alpha_i y_i = 0s.t.0≤αi​≤C,∑i​αi​yi​=0

This is a quadratic program in nnn variables with box constraints. The number of variables is now nnn (number of examples) instead of ddd (feature dimension), a win when d≫nd \gg nd≫n.

Kernel trick preview: The dual depends on the data only through inner products xiTxjx_i^T x_jxiT​xj​. Replacing xiTxjx_i^T x_jxiT​xj​ with a kernel K(xi,xj)K(x_i, x_j)K(xi​,xj​) allows SVMs to learn nonlinear decision boundaries without ever computing features in the kernel space explicitly.

Knowledge check#

Exercise · Multiple choice

If hᵢ(x*) < 0 (the constraint is inactive) at the optimum, complementary slackness forces:

λᵢ* = 0
λᵢ* > 0
hᵢ(x*) = 0
The dual to be infeasible
Question 1 of 4

Weak duality states that the dual objective is always ___ the primal objective for feasible points.

Browser lab: the SVM dual by projected gradient#

Solve a tiny 2D linearly separable SVM via projected gradient on the dual variables α\alphaα. Plot the decision boundary and highlight support vectors.

python · runs in browser
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(42)
n = 30
X_pos = rng.normal(loc=[2, 2], scale=1.0, size=(n//2, 2))
X_neg = rng.normal(loc=[-2, -2], scale=1.0, size=(n//2, 2))
X = np.vstack([X_pos, X_neg])
y = np.hstack([np.ones(n//2), -np.ones(n//2)])
n = len(y)

C = 1.0
K = X @ X.T

def dual_obj(alpha):
    return np.sum(alpha) - 0.5 * np.sum(np.outer(alpha * y, alpha * y) * K)

def dual_grad(alpha):
    return 1.0 - (K @ (alpha * y)) * y

alpha = np.zeros(n)
eta = 0.01
for _ in range(2000):
    g = dual_grad(alpha)
    alpha = alpha + eta * g
    alpha = np.clip(alpha, 0, C) * (np.abs(alpha) > 1e-5)

sv_idx = alpha > 1e-3
w_dual = X.T @ (alpha * y)

print(f"Support vectors: {sv_idx.sum()} / {n}")
print(f"Dual objective: {dual_obj(alpha):.4f}")
print(f"w = {np.round(w_dual, 3)}")
print("Notice: box-projected gradient ascent on the dual recovers a separating boundary, and the points it sits on are the support vectors — the equality constraint Σ αᵢyᵢ = 0 is satisfied only approximately on this balanced toy.")

plt.figure(figsize=(6, 5))
plt.scatter(X_pos[:, 0], X_pos[:, 1], c="tab:blue", alpha=0.6, label="y=+1")
plt.scatter(X_neg[:, 0], X_neg[:, 1], c="tab:red", alpha=0.6, label="y=−1")
plt.scatter(X[sv_idx, 0], X[sv_idx, 1], facecolors="none", edgecolors="black", s=80, linewidths=1.5, label="support vectors")
xx = np.linspace(-5, 5, 100)
yy = -w_dual[0] / w_dual[1] * xx
plt.plot(xx, yy, "k--", label="decision boundary")
plt.xlim(-5, 5); plt.ylim(-5, 5)
plt.xlabel("x1"); plt.ylabel("x2")
plt.title("SVM Dual: Decision Boundary and Support Vectors")
plt.legend(); plt.gca().set_aspect("equal"); plt.show()

Key takeaways#

  • The Lagrangian folds constraints into the objective with multipliers λ≥0\lambda \ge 0λ≥0, turning a constrained problem into a min-max.
  • The dual function g(λ)=min⁡xL(x,λ)g(\lambda) = \min_x L(x, \lambda)g(λ)=minx​L(x,λ) always lower-bounds the primal (weak duality); strong duality is what makes the two meet.
  • Slater's condition, the existence of a strictly feasible point, guarantees strong duality for convex problems.
  • Complementary slackness identifies which constraints bind at the optimum, so you know which ones actually matter.
  • The soft-margin SVM dual trades ddd-dimensional www for nnn multipliers αi\alpha_iαi​, a win when d≫nd \gg nd≫n, and depends on the data only through inner products xiTxjx_i^T x_jxiT​xj​, the opening for the kernel trick.
  • The sensitivity reading, that a small tightening ε\varepsilonε changes the optimum by about λ∗ε\lambda^* \varepsilonλ∗ε, is why dual variables are useful even when you solve the primal.

Further reading#

  • Stephen Boyd & Lieven Vandenberghe, Convex Optimization (Cambridge University Press, 2004). Chapter 5 is the standard treatment of the Lagrangian, weak/strong duality, and complementary slackness.
  • Corinna Cortes & Vladimir Vapnik, "Support-Vector Networks" (Machine Learning, 1995). The soft-margin SVM and the dual problem derived here.
  • Bernhard Schölkopf & Alexander Smola, Learning with Kernels (MIT Press, 2001). Chapters 6–7 derive the SVM dual and the kernel trick.

Reading is free. PDF and ePub unlock with the newsletter. Join once and every lesson opens in this browser.

← Previous
Week 11: Constraints: Projections and Penalties
Next →
Week 13: Nonconvex Deep Learning
On this page
  • Lagrangian
  • Dual function
  • Weak and strong duality
  • Complementary slackness
  • SVM dual as worked example
  • Knowledge check
  • Browser lab: the SVM dual by projected gradient
  • Key takeaways
  • Further reading