Convex Sets#
A set is convex if for any two points , the entire line segment connecting them lies inside :
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
- The Euclidean ball
- A polyhedron (intersection of halfspaces)
- The probability simplex
Non-examples:
- A crescent moon shape (the line segment between two tips exits the shape)
- The set (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 is convex if its domain is convex and for all in the domain and :
This is Jensen's inequality for two points. Geometrically: the graph of between and lies below the chord connecting and .
For differentiable , there's an equivalent and very useful first-order condition:
The right-hand side is the first-order Taylor approximation at . 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 , . A strictly convex function has at most one global minimum.
Strong convexity means there exists such that:
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 | |---|---|---| | | | Parabola opens upward | | | | Second derivative everywhere | | | | Second derivative | | | | Quadratic form with | | | | Second derivative | | Logistic loss | | Hessian is PSD (Week 5) | | | NOT convex | Second derivative changes sign at 0 | | | NOT convex | Concave — parabola opens downward |
A handy test for 1D functions: if for all in the domain, then is convex.
For multivariate functions, the condition is that the Hessian is positive semidefinite (PSD) for all .
Quadratic Forms and PSD#
A quadratic form is convex if and only if the symmetric matrix is positive semidefinite (PSD): all its eigenvalues are , or equivalently for all vectors .
More generally, has Hessian . So:
- (all eigenvalues ) → is convex
- (all eigenvalues ) → is strictly convex
- has a negative eigenvalue → is nonconvex
Worked example: Is convex?
Write as where . The eigenvalues solve , giving and . Both are positive, so is convex (in fact, strictly convex).
This connects directly to Linear Algebra's eigendecomposition material — tells you everything about the curvature of along each principal direction.
Local = Global#
Theorem: For a convex function , any local minimum is also a global minimum.
Proof sketch: Suppose is a local minimum but not global — there exists with . By convexity, for small :
But points are arbitrarily close to for small , contradicting that is a local minimum.
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.
Which of the following functions is convex on $\\mathbb{R}$?
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.
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)")