Skip to main content
© 2026 ePowerAI — free, open courseware. No login required.
CoursesContact
ePOWERAI
CoursesContact
Week 2: Convex Sets and Functions
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 02· Optimization· Foundations7 min read

Week 2: Convex Sets and Functions

Learning Outcomes
  • Test whether a function is convex with the chord (Jensen) inequality, and a set with the line-segment definition
  • Show that xTQxx^T Q xxTQx is convex exactly when QQQ is PSD, and test convexity with the Hessian or the tangent plane
  • Build convex functions from known pieces: sums, maxima, affine maps and norms
  • Explain why local minima of convex functions are global, and why a bowl at every minimum is not enough
Prerequisites

Background: Week 1 (ERMEmpirical Risk Minimization, gradient descent, gradient mechanics). Comfort with vectors and dot products from Linear Algebra.

Later weeks: Convexity is the main theoretical engine of Weeks 3–5 (GD theory, LS, classification losses) and underpins the constraint/duality story in Weeks 11–12.

Fit a logistic regression twice from two random starts and you reach the same loss. Fit a small network twice and you get two models with two different losses. Both runs use gradient descent. The difference is the shape of the loss, and one inequality separates the two shapes. Each surface below has an amber chord from (y,f(y))(y, f(y))(y,f(y)) to (x,f(x))(x, f(x))(x,f(x)), above the same two points x=(1,0.5)x = (1, 0.5)x=(1,0.5) and y=(−1,−0.5)y = (-1, -0.5)y=(−1,−0.5).

The convex surface f(u, v) = 3u² + 2uv + 2v², with a chord from y = (−1.00, −0.50) to x = (1.00, 0.50). At θ = 0.50 the point θx + (1 − θ)y is (0.00, 0.00). The surface there is at 0.00 and the chord at 4.50, so the chord sits 4.50 above.

At θ = 0.50 the point is (0.00, 0.00). The surface is at 0.00 and the chord at 4.50, so the chord sits 4.50 above.

Slide θ\thetaθ to move the point θx+(1−θ)y\theta x + (1-\theta)yθx+(1−θ)y from yyy to xxx. The short segment joins it to the surface, and the caption prints both heights. On the convex surface the chord stays above the graph at every θ\thetaθ. Switch to the double well, and at θ=0.5\theta = 0.5θ=0.5 the chord runs 2.5 under the hump.

The chord test#

A function f:Rd→Rf: \mathbb{R}^d \to \mathbb{R}f:Rd→R is convex if its domain is convex and, for all x,yx, yx,y and θ∈[0,1]\theta \in [0, 1]θ∈[0,1],

f(θx+(1−θ)y)≤θf(x)+(1−θ)f(y)f(\theta x + (1-\theta) y) \leq \theta f(x) + (1-\theta) f(y)f(θx+(1−θ)y)≤θf(x)+(1−θ)f(y)

This is Jensen's inequality, the test you ran in the scene. The left side is the surface under the marked point. The right side is the chord. On the convex surface f(x)=f(y)=4.5f(x) = f(y) = 4.5f(x)=f(y)=4.5, so the chord is flat at 4.5, and at θ=0.5\theta = 0.5θ=0.5 the surface is at 0. The double well is f(u,v)=(u2−2)2+2v2+u/2f(u, v) = (u^2 - 2)^2 + 2v^2 + u/2f(u,v)=(u2−2)2+2v2+u/2, with f(x)=2f(x) = 2f(x)=2 and f(y)=1f(y) = 1f(y)=1. At θ=0.5\theta = 0.5θ=0.5 the chord is at 1.5 and the surface at 4, so the inequality fails by 2.5.

Why the quadratic passes: PSD#

The convex surface is f(x)=xTQxf(x) = x^T Q xf(x)=xTQx with Q=(3112)Q = \begin{pmatrix} 3 & 1 \\ 1 & 2 \end{pmatrix}Q=(31​12​). For any quadratic, expand both sides of Jensen with d=x−yd = x - yd=x−y:

θf(x)+(1−θ)f(y)−f(θx+(1−θ)y)=θ(1−θ) dTQd\theta f(x) + (1-\theta) f(y) - f(\theta x + (1-\theta)y) = \theta(1-\theta)\, d^T Q dθf(x)+(1−θ)f(y)−f(θx+(1−θ)y)=θ(1−θ)dTQd

In the scene d=(2,1)d = (2, 1)d=(2,1) and dTQd=18d^T Q d = 18dTQd=18, so the gap is 18 θ(1−θ)18\,\theta(1-\theta)18θ(1−θ), which is 4.5 at θ=0.5\theta = 0.5θ=0.5. The gap is nonnegative for every pair exactly when dTQd≥0d^T Q d \geq 0dTQd≥0 for every ddd. That is positive semidefinite (PSDPositive Semidefinite): xTQxx^T Q xxTQx is convex if and only if the symmetric QQQ has no negative eigenvalue. Here det⁡(Q−λI)=λ2−5λ+5\det(Q - \lambda I) = \lambda^2 - 5\lambda + 5det(Q−λI)=λ2−5λ+5 gives eigenvalues 5±52≈3.618\frac{5 \pm \sqrt{5}}{2} \approx 3.61825±5​​≈3.618 and 1.3821.3821.382, both positive.

A twice-differentiable fff is convex exactly when its Hessian ∇2f(x)\nabla^2 f(x)∇2f(x) is PSD at every xxx. In one dimension that is f′′≥0f'' \geq 0f′′≥0, so exe^xex and −log⁡x-\log x−logx pass. The double well has Hessian diag(−8,4)\mathrm{diag}(-8, 4)diag(−8,4) at the origin. Along the chord's direction, dT∇2f(0) d=4(−8)+4=−28d^T \nabla^2 f(0)\, d = 4(-8) + 4 = -28dT∇2f(0)d=4(−8)+4=−28. The surface curves down under the chord.

The common mistake. Each well of the double well is a bowl, and its Hessian is positive definite at both minima. That does not make fff convex. Convexity is a claim about every chord, not about the minima, and the chord across the hump breaks it.

First-order condition: the tangent plane#

For differentiable fff, convexity is equivalent to the first-order condition: the function lies above every tangent plane.

f(y)≥f(x)+∇f(x)T(y−x)for all x,yf(y) \geq f(x) + \nabla f(x)^T (y - x) \quad \text{for all } x, yf(y)≥f(x)+∇f(x)T(y−x)for all x,y

On the convex surface ∇f(x)=2Qx=(7,4)\nabla f(x) = 2Qx = (7, 4)∇f(x)=2Qx=(7,4), so the tangent plane at xxx predicts 4.5−18=−13.54.5 - 18 = -13.54.5−18=−13.5 at yyy. The surface is at 4.5, above it by dTQd=18d^T Q d = 18dTQd=18. On the double well ∇f(x)=(−3.5,2)\nabla f(x) = (-3.5, 2)∇f(x)=(−3.5,2), so the tangent predicts 2+7−2=72 + 7 - 2 = 72+7−2=7 at yyy, but f(y)=1f(y) = 1f(y)=1. The tangent passes over the surface by 6. Week 3 adds a quadratic floor, strong convexity.

Convex sets#

A set CCC is convex if θx+(1−θ)y∈C\theta x + (1-\theta)y \in Cθx+(1−θ)y∈C for all x,y∈Cx, y \in Cx,y∈C and θ∈[0,1]\theta \in [0, 1]θ∈[0,1]. Halfspaces, balls and the probability simplex are convex. A donut is not. Intersections of convex sets are convex, so linear constraints always cut out a convex feasible region.

Building convex functions#

These operations preserve convexity:

  • Nonnegative weighted sums ∑iwifi\sum_i w_i f_i∑i​wi​fi​ with wi≥0w_i \geq 0wi​≥0.
  • Pointwise maxima max⁡ifi(x)\max_i f_i(x)maxi​fi​(x), which is why hinge loss is convex.
  • Affine composition f(Ax+b)f(Ax + b)f(Ax+b).
  • Norms: the triangle inequality and ∥θx∥=∣θ∣ ∥x∥\|\theta x\| = |\theta|\,\|x\|∥θx∥=∣θ∣∥x∥ give ∥θx+(1−θ)y∥≤θ∥x∥+(1−θ)∥y∥\|\theta x + (1-\theta)y\| \leq \theta\|x\| + (1-\theta)\|y\|∥θx+(1−θ)y∥≤θ∥x∥+(1−θ)∥y∥, so ∥x∥1\|x\|_1∥x∥1​ and ∥x∥2\|x\|_2∥x∥2​ are convex penalties (Week 10).

Nonlinear composition need not: ∣t−1∣|t - 1|∣t−1∣ and x2x^2x2 are convex, but ∣x2−1∣|x^2 - 1|∣x2−1∣ is not.

Local equals global#

Theorem: For a convex fff, every local minimum is a global minimum.

Proof sketch: Suppose x∗x^*x∗ is a local minimum and some yyy has f(y)<f(x∗)f(y) < f(x^*)f(y)<f(x∗). By convexity, for every θ∈(0,1]\theta \in (0, 1]θ∈(0,1],

f((1−θ)x∗+θy)≤(1−θ)f(x∗)+θf(y)<f(x∗)f((1-\theta)x^* + \theta y) \leq (1-\theta)f(x^*) + \theta f(y) < f(x^*)f((1−θ)x∗+θy)≤(1−θ)f(x∗)+θf(y)<f(x∗)

Small θ\thetaθ gives points arbitrarily close to x∗x^*x∗ with lower loss, which contradicts local minimality. □\square□

The double well shows what nonconvexity costs. Its global minimum is at (−1.44,0)(-1.44, 0)(−1.44,0) with f=−0.71f = -0.71f=−0.71, and its other minimum at (1.38,0)(1.38, 0)(1.38,0) has f=0.70f = 0.70f=0.70. The gradient at x=(1,0.5)x = (1, 0.5)x=(1,0.5) is (−3.5,2)(-3.5, 2)(−3.5,2), so small-step gradient descent from xxx settles in the worse well. Least squares and logistic regression have no such trap. Week 13 returns to the nonconvex case.

Knowledge Check#

Question 1 of 3

Convex surface, θ = 0.2. How far above the surface does the caption put the chord?

2.88, which is 18θ(1 − θ)
4.50, the chord height
1.62, the surface height
0

Browser lab: tangents and the first-order condition#

Plot three convex functions and one nonconvex function on the same axes, and verify the first-order condition: the tangent line at any point always lies below a convex function's graph.

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

def f1(x): return x**2
def f2(x): return np.exp(x)
def f3(x): return -np.log(x)   # domain: x > 0
def f4(x): return x**3 - 3*x

fig, axes = plt.subplots(2, 2, figsize=(10, 8))
axes = axes.flat

for ax, fn, name, grad_fn, xr, x0 in [
    (axes[0], f1, "$x^2$", lambda x: 2*x, (-2.0, 4.0), 2.0),
    (axes[1], f2, "$e^x$", lambda x: np.exp(x), (-2.0, 4.0), 2.0),
    (axes[2], f3, "$-\\log x$", lambda x: -1/x, (0.05, 4.0), 2.0),
    (axes[3], f4, "$x^3-3x$", lambda x: 3*x**2 - 3, (-2.0, 4.0), -1.0),
]:
    xs = np.linspace(*xr, 300)
    ys = fn(xs)
    ax.plot(xs, ys, label=name)
    tangent = fn(x0) + grad_fn(x0) * (xs - x0)
    ax.plot(xs, tangent, "--", color="tab:orange", label=f"tangent at x={x0}")
    ax.axvline(x0, color="gray", linewidth=0.5, linestyle=":")
    ax.legend(fontsize=9)
    ax.set_title(f"{'Convex' if name != '$x^3-3x$' else 'Nonconvex'}: {name}")

plt.tight_layout()
plt.show()

print("Notice: for the three convex panels the tangent line stays below (or touches) the curve —")
print("        that is the first-order condition. For x^3-3x, where f''(-1) < 0, the tangent at -1")
print("        lies above the curve on (-2, 2) and crosses it at x = 2.")

Key takeaways#

  • fff is convex when every chord lies on or above its graph.
  • For xTQxx^T Q xxTQx the chord gap is θ(1−θ) dTQd\theta(1-\theta)\,d^T Q dθ(1−θ)dTQd, so convexity is Q⪰0Q \succeq 0Q⪰0. In general the Hessian must be PSD everywhere.
  • A bowl at every minimum is not convexity. The double well fails the chord test across its hump.
  • For a convex function every local minimum is global.

Further reading#

  • Stephen Boyd & Lieven Vandenberghe, Convex Optimization (Cambridge University Press, 2004). Chapters 2 and 3 cover convex sets and functions. Free at stanford.edu/~boyd/cvxbook.
  • Yurii Nesterov, Introductory Lectures on Convex Optimization (Springer, 2004). Chapter 2 covers smooth and strongly convex functions.
  • Sébastien Bubeck, Convex Optimization: Algorithms and Complexity (2015). A concise free treatment, at arXiv:1405.4980.

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

← Previous
Week 1: Optimization for Learning
Next →
Week 3: Gradient Descent Theory
On this page
  • The chord test
  • Why the quadratic passes: PSD
  • First-order condition: the tangent plane
  • Convex sets
  • Building convex functions
  • Local equals global
  • Knowledge Check
  • Browser lab: tangents and the first-order condition
  • Key takeaways
  • Further reading