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.
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 , . 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 (PSDPositive Semidefinite) for all .
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 are convex and , then is convex. (A sum of convex functions is convex.)
- Pointwise maximum: if are convex, so is . This is why hinge loss and piecewise-linear penalties are convex.
- Affine composition: if is convex, then is convex. Precomposing with an affine map preserves convexity; composing with a general nonlinear map does not.
- Partial minimisation: if is jointly convex, then is convex (whenever the minimum is attained).
Norms are convex. Every norm satisfies the triangle inequality () and positive homogeneity (). Combining the two, for :
so every norm — , , and beyond — is convex, not only the squared norm from the table above. Norms are the workhorses of regularisation (Week 10).
A caution: composition is direction-sensitive. Convex convex is not automatically convex: and are both convex, yet is not. You need convex and nondecreasing composed with convex, or affine.
Quadratic Forms and PSD#
A quadratic form is convex if and only if is symmetric and positive semidefinite (PSD): all its eigenvalues are , or equivalently for all vectors . (For a non-symmetric , only its symmetric part matters, since .)
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.
Knowledge Check#
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: 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.
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.