Skip to main content
© 2026 ePowerAI — instrumented learning, no login required.
CoursesContact
ePOWERAI
CoursesContact
Week 2: Convex Sets and Functions
Optimization
01Week 1: Optimization for Learning
02Week 2: Convex Sets and Functions
03Week 3: Gradient Descent Theory
04Week 4: Least Squares and Conditioning
05Week 5: Classification Losses
06Week 6: Stochastic Gradient Descent
07Week 7: Momentum and Acceleration
08Week 8: Adaptive First-Order Methods
09Week 9: Practical Training Dynamics
10Week 10: Nonsmooth and Composite Objectives
11Week 11: Constraints: Projections and Penalties
12Week 12: Duality for Practitioners
13Week 13: Nonconvex Deep Learning
14Week 14: Capstone — Theory Meets Training
Week 02· Optimization· Foundations9 min read

Week 2: Convex Sets and Functions

Learning Outcomes
  • Test whether a set is convex using the line-segment definition
  • Define convex functions via Jensen's inequality and the first-order condition
  • Recognize convex functions: quadratics with PSD Hessian, norms, logistic loss
  • Explain why local minima of convex functions are global
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.

Convex Sets#

A set C⊆RdC \subseteq \mathbb{R}^dC⊆Rd is convex if for any two points x,y∈Cx, y \in Cx,y∈C, the entire line segment connecting them lies inside CCC:

θx+(1−θ)y∈Cfor all θ∈[0,1]\theta x + (1-\theta)y \in C \quad \text{for all } \theta \in [0, 1]θx+(1−θ)y∈Cfor all θ∈[0,1]

In plain English: you can draw a straight line between any two points in the set and the line never leaves the set.

Examples of convex sets:

  • A line or a plane through the origin (a subspace)
  • A halfspace H={x∣aTx≤b}H = \{x \mid a^T x \leq b\}H={x∣aTx≤b}
  • The Euclidean ball Br(c)={x∣∥x−c∥2≤r}B_r(c) = \{x \mid \|x - c\|_2 \leq r\}Br​(c)={x∣∥x−c∥2​≤r}
  • A polyhedron P={x∣Ax≤b}P = \{x \mid Ax \leq b\}P={x∣Ax≤b} (intersection of halfspaces)
  • The probability simplex {x∣∑ixi=1,  xi≥0}\{x \mid \sum_i x_i = 1,\; x_i \geq 0\}{x∣∑i​xi​=1,xi​≥0}

Non-examples:

  • A crescent moon shape (the line segment between two tips exits the shape)
  • The set {0,1}\{0, 1\}{0,1} (only two points — not a continuous segment)
  • A donut (the hole breaks convexity)

An important fact: the intersection of convex sets is convex. Since hyperplanes and halfspaces are convex, any set defined by linear equalities and inequalities is convex — which is why LP, QP, and SOCP constraints all produce convex feasible regions.

Convex Functions: Definitions#

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 in the domain 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 for two points. Geometrically: the graph of fff between xxx and yyy lies below the chord connecting (x,f(x))(x, f(x))(x,f(x)) and (y,f(y))(y, f(y))(y,f(y)).

For differentiable fff, there's an equivalent and very useful first-order condition:

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

The right-hand side is the first-order Taylor approximation at xxx. Convexity says: the function always lies above its tangent line/hyperplane. This is the property we exploit to bound GD progress in Week 3.

Convex versus nonconvex curves, showing the chord and tangent test

Left: for a convex function the chord between any two points lies above the graph and every tangent lies below it. Right: a nonconvex function violates this — its chord cuts under the curve, which is exactly what allows bad local minima.

Strict convexity means the inequality is strict for x≠yx \neq yx=y, θ∈(0,1)\theta \in (0,1)θ∈(0,1). A strictly convex function has at most one global minimum.

Strong convexity means there exists μ>0\mu > 0μ>0 such that:

f(y)≥f(x)+∇f(x)T(y−x)+μ2∥y−x∥22f(y) \geq f(x) + \nabla f(x)^T (y - x) + \frac{\mu}{2}\|y - x\|_2^2f(y)≥f(x)+∇f(x)T(y−x)+2μ​∥y−x∥22​

This adds a quadratic term, making the function "curved enough." Strong convexity gives GD linear convergence (Week 3).

Convex Functions: Examples#

Which functions are convex? Here's a practical field guide:

FunctionConvex onWhy
f(x)=x2f(x) = x^2f(x)=x2R\mathbb{R}RParabola opens upward
f(x)=exf(x) = e^xf(x)=exR\mathbb{R}RSecond derivative ex>0e^x > 0ex>0 everywhere
f(x)=xlog⁡xf(x) = x \log xf(x)=xlogxx>0x > 0x>0Second derivative 1/x>01/x > 01/x>0
f(x)=∥x∥22f(x) = \lVert x \rVert_2^2f(x)=∥x∥22​Rd\mathbb{R}^dRdQuadratic form with Q=I≻0Q = I \succ 0Q=I≻0
f(x)=−log⁡xf(x) = -\log xf(x)=−logxx>0x > 0x>0Second derivative 1/x2>01/x^2 > 01/x2>0
Logistic lossRd\mathbb{R}^dRdHessian is PSD (Week 5)
f(x)=x3f(x) = x^3f(x)=x3NOT convexSecond derivative changes sign at 0
f(x)=−x2f(x) = -x^2f(x)=−x2NOT convexConcave — parabola opens downward

A handy test for 1D functions: if f′′(x)≥0f''(x) \geq 0f′′(x)≥0 for all xxx in the domain, then fff is convex.

For multivariate functions, the condition is that the Hessian ∇2f(x)\nabla^2 f(x)∇2f(x) is positive semidefinite (PSDPositive Semidefinite) for all xxx.

Building Convex Functions#

You rarely prove convexity straight from the definition. Instead, build new convex functions from ones you already know using operations that preserve convexity:

  • Nonnegative weighted sums: if f1,…,fmf_1, \ldots, f_mf1​,…,fm​ are convex and wi≥0w_i \geq 0wi​≥0, then ∑iwifi\sum_i w_i f_i∑i​wi​fi​ is convex. (A sum of convex functions is convex.)
  • Pointwise maximum: if f1,…,fmf_1, \ldots, f_mf1​,…,fm​ are convex, so is f(x)=max⁡ifi(x)f(x) = \max_i f_i(x)f(x)=maxi​fi​(x). This is why hinge loss and piecewise-linear penalties are convex.
  • Affine composition: if fff is convex, then g(x)=f(Ax+b)g(x) = f(Ax + b)g(x)=f(Ax+b) is convex. Precomposing with an affine map preserves convexity; composing with a general nonlinear map does not.
  • Partial minimisation: if f(x,y)f(x, y)f(x,y) is jointly convex, then g(x)=min⁡yf(x,y)g(x) = \min_y f(x, y)g(x)=miny​f(x,y) is convex (whenever the minimum is attained).

Norms are convex. Every norm ∥⋅∥\|\cdot\|∥⋅∥ satisfies the triangle inequality (∥x+y∥≤∥x∥+∥y∥\|x + y\| \leq \|x\| + \|y\|∥x+y∥≤∥x∥+∥y∥) and positive homogeneity (∥θx∥=∣θ∣ ∥x∥\|\theta x\| = |\theta|\,\|x\|∥θx∥=∣θ∣∥x∥). Combining the two, for θ∈[0,1]\theta \in [0, 1]θ∈[0,1]:

∥θx+(1−θ)y∥≤∥θx∥+∥(1−θ)y∥=θ∥x∥+(1−θ)∥y∥\|\theta x + (1-\theta)y\| \leq \|\theta x\| + \|(1-\theta)y\| = \theta\|x\| + (1-\theta)\|y\|∥θx+(1−θ)y∥≤∥θx∥+∥(1−θ)y∥=θ∥x∥+(1−θ)∥y∥

so every norm — ∥x∥1\|x\|_1∥x∥1​, ∥x∥2\|x\|_2∥x∥2​, and beyond — is convex, not only the squared norm ∥x∥22\|x\|_2^2∥x∥22​ from the table above. Norms are the workhorses of regularisation (Week 10).

A caution: composition is direction-sensitive. Convex ∘\circ∘ convex is not automatically convex: g(x)=x2g(x) = x^2g(x)=x2 and f(y)=∣y−1∣f(y) = |y - 1|f(y)=∣y−1∣ are both convex, yet f(g(x))=∣x2−1∣f(g(x)) = |x^2 - 1|f(g(x))=∣x2−1∣ is not. You need fff convex and nondecreasing composed with ggg convex, or ggg affine.

Quadratic Forms and PSD#

A quadratic form f(x)=xTQxf(x) = x^T Q xf(x)=xTQx is convex if and only if QQQ is symmetric and positive semidefinite (PSD): all its eigenvalues are ≥0\geq 0≥0, or equivalently vTQv≥0v^T Q v \geq 0vTQv≥0 for all vectors vvv. (For a non-symmetric QQQ, only its symmetric part 12(Q+QT)\tfrac{1}{2}(Q + Q^T)21​(Q+QT) matters, since xTQx=xT12(Q+QT)xx^T Q x = x^T \tfrac{1}{2}(Q + Q^T) xxTQx=xT21​(Q+QT)x.)

More generally, f(x)=12xTQx+cTx+df(x) = \frac{1}{2} x^T Q x + c^T x + df(x)=21​xTQx+cTx+d has Hessian ∇2f(x)=Q\nabla^2 f(x) = Q∇2f(x)=Q. So:

  • Q⪰0Q \succeq 0Q⪰0 (all eigenvalues ≥0\geq 0≥0) → fff is convex
  • Q≻0Q \succ 0Q≻0 (all eigenvalues >0> 0>0) → fff is strictly convex
  • QQQ has a negative eigenvalue → fff is nonconvex

Worked example: Is f(x1,x2)=3x12+2x22+2x1x2f(x_1, x_2) = 3x_1^2 + 2x_2^2 + 2x_1 x_2f(x1​,x2​)=3x12​+2x22​+2x1​x2​ convex?

Write as f(x)=xTQxf(x) = x^T Q xf(x)=xTQx where Q=(3112)Q = \begin{pmatrix} 3 & 1 \\ 1 & 2 \end{pmatrix}Q=(31​12​). The eigenvalues solve det⁡(Q−λI)=(3−λ)(2−λ)−1=λ2−5λ+5=0\det(Q - \lambda I) = (3-\lambda)(2-\lambda) - 1 = \lambda^2 - 5\lambda + 5 = 0det(Q−λI)=(3−λ)(2−λ)−1=λ2−5λ+5=0, giving λ=5±52≈3.618\lambda = \frac{5 \pm \sqrt{5}}{2} \approx 3.618λ=25±5​​≈3.618 and 1.3821.3821.382. Both are positive, so fff is convex (in fact, strictly convex).

This connects directly to Linear Algebra's eigendecomposition material — Q=VΛVTQ = V \Lambda V^TQ=VΛVT tells you everything about the curvature of fff along each principal direction.

Local = Global#

Theorem: For a convex function fff, any local minimum is also a global minimum.

Proof sketch: Suppose x∗x^*x∗ is a local minimum but not global — there exists yyy with f(y)<f(x∗)f(y) < f(x^*)f(y)<f(x∗). By convexity, for small θ\thetaθ:

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∗)

But points (1−θ)x∗+θy(1-\theta)x^* + \theta y(1−θ)x∗+θy are arbitrarily close to x∗x^*x∗ for small θ\thetaθ, contradicting that x∗x^*x∗ is a local minimum. □\square□

Why this matters: If you can prove your loss function is convex (e.g., linear regression with squared error, logistic regression with cross-entropy), then gradient descent — if it converges at all — is converging to a global optimum. You never have to worry about getting stuck in a bad local minimum.

For nonconvex objectives (deep nets, Week 13), this guarantee vanishes. That's why nonconvex optimization is a different, messier game.

Knowledge Check#

Exercise · Multiple choice

Which of the following functions is convex on $\\mathbb{R}$?

$f(x) = x^2 + e^x$
$f(x) = x^3$
$f(x) = -x^2$
$f(x) = \sin x$
Question 1 of 4

A set C is convex if for any x,y in C, the entire line ___ connecting them lies in C.

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

x0 = 2.0

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

for ax, fn, name, grad_fn, xr in [
    (axes[0], f1, "$x^2$", lambda x: 2*x, (-2.0, 4.0)),
    (axes[1], f2, "$e^x$", lambda x: np.exp(x), (-2.0, 4.0)),
    (axes[2], f3, "$-\\log x$", lambda x: -1/x, (0.05, 4.0)),
    (axes[3], f4, "$x^3-3x$", lambda x: 3*x**2 - 3, (-2.0, 4.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 the tangent crosses the curve.")

Further Reading#

  • Stephen Boyd & Lieven Vandenberghe, Convex Optimization (Cambridge University Press, 2004). Chapter 2 covers convex sets and Chapter 3 covers convex functions, including the operations that preserve convexity. Freely available at stanford.edu/~boyd/cvxbook.
  • Yurii Nesterov, Introductory Lectures on Convex Optimization (Springer, 2004). Chapter 2 is the standard reference for smooth and strongly convex functions and the definitions used in Week 3.
  • Sébastien Bubeck, Convex Optimization: Algorithms and Complexity (2015). Sections 2–3 give a concise, free treatment of convexity and the first-order condition. Available at arXiv:1405.4980.
  • Jorge Nocedal & Stephen Wright, Numerical Optimization (2nd ed., Springer, 2006). Chapter 2 collects the convexity fundamentals practitioners need before touching algorithms.
← Previous
Week 1: Optimization for Learning
Next →
Week 3: Gradient Descent Theory
On this page
  • Convex Sets
  • Convex Functions: Definitions
  • Convex Functions: Examples
  • Building Convex Functions
  • Quadratic Forms and PSD
  • Local = Global
  • Knowledge Check
  • Browser lab: tangents and the first-order condition
  • Further Reading