Skip to main content
© 2026 ePowerAI. All rights reserved.
CoursesContact
eAI
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· Foundations22 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.

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:

| Function | Convex on | Why | |---|---|---| | f(x)=x2f(x) = x^2f(x)=x2 | R\mathbb{R}R | Parabola opens upward | | f(x)=exf(x) = e^xf(x)=ex | R\mathbb{R}R | Second derivative ex>0e^x > 0ex>0 everywhere | | f(x)=xlog⁡xf(x) = x \log xf(x)=xlogx | x>0x > 0x>0 | Second derivative 1/x>01/x > 01/x>0 | | f(x)=∥x∥22f(x) = \|x\|_2^2f(x)=∥x∥22​ | Rd\mathbb{R}^dRd | Quadratic form with Q=I≻0Q = I \succ 0Q=I≻0 | | f(x)=−log⁡xf(x) = -\log xf(x)=−logx | x>0x > 0x>0 | Second derivative 1/x2>01/x^2 > 01/x2>0 | | Logistic loss | Rd\mathbb{R}^dRd | Hessian is PSD (Week 5) | | f(x)=x3f(x) = x^3f(x)=x3 | NOT convex | Second derivative changes sign at 0 | | f(x)=−x2f(x) = -x^2f(x)=−x2 | NOT convex | Concave — 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 (PSD) for all xxx.

Quadratic Forms and PSD#

A quadratic form f(x)=xTQxf(x) = x^T Q xf(x)=xTQx is convex if and only if the symmetric matrix QQQ is 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.

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.

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#

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

xs = np.linspace(-2, 4, 300)

def f1(x): return x**2
def f2(x): return np.exp(x)
def f3(x): return -np.log(np.maximum(x, 1e-6))
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 in [
    (axes[0], f1, "$x^2$", lambda x: 2*x),
    (axes[1], f2, "$e^x$", lambda x: np.exp(x)),
    (axes[2], f3, "$-\\log x$", lambda x: -1/x),
    (axes[3], f4, "$x^3-3x$", lambda x: 3*x**2 - 3),
]:
    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("First three: tangent below curve ✓ (convex)")
print("Last one:    tangent crosses curve ✗ (nonconvex)")
← Previous
Week 1: Optimization for Learning
Next →
Week 3: Gradient Descent Theory
On this page
  • Convex Sets
  • Convex Functions: Definitions
  • Convex Functions: Examples
  • Quadratic Forms and PSD
  • Local = Global
  • Browser lab