Convex Functions
A convex function is one whose graph never bends downward. That single property means a local minimum is a global minimum and a zero gradient is a certificate of optimality, so the whole toolbox of the next parts works with guarantees.
Definition and the three tests
is convex if its domain is convex and its graph lies below every chord:
It is strictly convex if the inequality is strict for and , and concave if is convex. For differentiable functions there are two equivalent tests:
| Test | Condition |
|---|---|
| first order | for all |
| second order | (positive semidefinite) for all |
The first-order test says the tangent plane is a global under-estimator. Put into it and you get for every : a stationary point is a global minimum.
A catalogue
| Function | Domain | Convex because |
|---|---|---|
| affine (both convex and concave) | ||
| Hessian | ||
| (any norm) | triangle inequality | |
| second derivative | ||
| , | second derivatives and | |
| (log-sum-exp) | Hessian is a covariance matrix | |
| maximum of linear functions |
Testing convexity numerically
The second-order test is easy to run on random points. For log-sum-exp the Hessian is where is the softmax of :
import numpy as np
rng = np.random.default_rng(0)
def lse_hessian(x):
p = np.exp(x - x.max()); p /= p.sum()
return np.diag(p) - np.outer(p, p)
smallest = min(np.linalg.eigvalsh(lse_hessian(rng.normal(size=4) * 3)).min() for _ in range(1000))
print("smallest Hessian eigenvalue over 1000 points >= -1e-12:", bool(smallest >= -1e-12))
# a non-convex function fails the chord test somewhere
f = lambda x: np.cos(x)
x, y = 0.0, 2 * np.pi
print("cos at midpoint:", f((x + y) / 2), " chord value:", (f(x) + f(y)) / 2)smallest Hessian eigenvalue over 1000 points >= -1e-12: True
cos at midpoint: -1.0 chord value: 1.0
The smallest eigenvalue is zero up to rounding error (log-sum-exp is flat along the direction , since adding a constant to every adds that constant to the function), never really negative.
The epigraph and sublevel sets
The epigraph is the region above the graph. A function is convex exactly when its epigraph is a convex set, which links the set and function views. The sublevel sets of a convex function are convex, so a constraint with convex describes a convex feasible region. (The converse fails: functions with convex sublevel sets are called quasiconvex; is one but is not convex.)
Jensen's inequality
Convexity extends from two points to any average: for a random variable ,
import numpy as np
rng = np.random.default_rng(2)
X = rng.exponential(scale=2.0, size=200000)
print("f(E[X]) with f = exp(x/4):", round(float(np.exp(X.mean() / 4)), 3))
print("E[f(X)]: ", round(float(np.exp(X / 4).mean()), 3))f(E[X]) with f = exp(x/4): 1.649
E[f(X)]: 1.999
Jensen's inequality is the reason KL divergence is non-negative (Information Theory) and the reason averaging predictions of convex losses helps.
In software
- CVXPY's "disciplined convex programming" checks convexity by the rules of the next lesson, not by sampling.
- Library loss functions (squared error, logistic loss, hinge, cross-entropy in the logits) are convex in the parameters of a linear model, so training them is reliable.
Exercises
Exercise 1: Second derivative
For which is convex on ?
Answer
For .
Solution
. At this is , so we need ; then everywhere.
Exercise 2: First-order bound
Use the first-order condition for at to show for every .
Answer
The tangent at 0 is , and a convex function lies above its tangents.
Solution
and , so .
Exercise 3: Quadratic form (code)
Is convex? Check the Hessian eigenvalues.
Answer
Yes: the eigenvalues of are about 0.29 and 13.71, both positive.
Solution
import numpy as np
print(np.round(np.linalg.eigvalsh(np.array([[4.0, -6.0], [-6.0, 10.0]])), 4))[ 0.2918 13.7082]
Both eigenvalues are positive, so the Hessian is positive definite and is strictly convex.
Test yourself
For a differentiable convex , implies…
- is a saddle point
- is a global minimizer
- is a local maximizer
- nothing
Which function is convex on ?
Jensen's inequality for convex states…