TExcelTAD
Home › Optimization › Convex Functions

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

f:Rn→Rf : \mathbb{R}^n \to \mathbb{R} is convex if its domain is convex and its graph lies below every chord:

f(θx+(1−θ)y)≤θf(x)+(1−θ)f(y),θ∈[0,1]f(\theta\mathbf{x} + (1 - \theta)\mathbf{y}) \le \theta f(\mathbf{x}) + (1 - \theta)f(\mathbf{y}), \qquad \theta \in [0, 1]

It is strictly convex if the inequality is strict for x≠y\mathbf{x} \ne \mathbf{y} and 0<θ<10 < \theta < 1, and concave if −f-f is convex. For differentiable functions there are two equivalent tests:

Test Condition
first order f(y)≥f(x)+∇f(x)T(y−x)f(\mathbf{y}) \ge f(\mathbf{x}) + \nabla f(\mathbf{x})^T(\mathbf{y} - \mathbf{x}) for all x,y\mathbf{x}, \mathbf{y}
second order ∇2f(x)⪰0\nabla^2 f(\mathbf{x}) \succeq 0 (positive semidefinite) for all x\mathbf{x}

The first-order test says the tangent plane is a global under-estimator. Put ∇f(x∗)=0\nabla f(\mathbf{x}^*) = \mathbf{0} into it and you get f(y)≥f(x∗)f(\mathbf{y}) \ge f(\mathbf{x}^*) for every y\mathbf{y}: a stationary point is a global minimum.

A catalogue

Function Domain Convex because
aTx+b\mathbf{a}^T\mathbf{x} + b Rn\mathbb{R}^n affine (both convex and concave)
12xTQx\frac{1}{2}\mathbf{x}^TQ\mathbf{x} Rn\mathbb{R}^n Hessian Q⪰0Q \succeq 0
∥x∥\lVert \mathbf{x} \rVert (any norm) Rn\mathbb{R}^n triangle inequality
eaxe^{ax} R\mathbb{R} second derivative a2eax≥0a^2e^{ax} \ge 0
−log⁡x-\log x, xlog⁡xx\log x x>0x > 0 second derivatives 1/x21/x^2 and 1/x1/x
log⁡∑iexi\log\sum_i e^{x_i} (log-sum-exp) Rn\mathbb{R}^n Hessian is a covariance matrix
max⁡ixi\max_i x_i Rn\mathbb{R}^n 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 diag⁡(p)−ppT\operatorname{diag}(\mathbf{p}) - \mathbf{p}\mathbf{p}^T where p\mathbf{p} is the softmax of x\mathbf{x}:

python · try it yourself
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 1\mathbf{1}, since adding a constant to every xix_i adds that constant to the function), never really negative.

The epigraph and sublevel sets

The epigraph epi⁡f={(x,t):f(x)≤t}\operatorname{epi} f = \{(\mathbf{x}, t) : f(\mathbf{x}) \le t\} 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 {x:f(x)≤α}\{\mathbf{x} : f(\mathbf{x}) \le \alpha\} of a convex function are convex, so a constraint fi(x)≤0f_i(\mathbf{x}) \le 0 with convex fif_i describes a convex feasible region. (The converse fails: functions with convex sublevel sets are called quasiconvex; ∣x∣\sqrt{\lvert x \rvert} is one but is not convex.)

Jensen's inequality

Convexity extends from two points to any average: for a random variable XX,

f(E[X])≤E[f(X)]f(\mathbb{E}[X]) \le \mathbb{E}[f(X)]
python · try it yourself
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 aa is f(x)=x4+ax2f(x) = x^4 + ax^2 convex on R\mathbb{R}?

Answer

For a≥0a \ge 0.

Solution

f′′(x)=12x2+2af''(x) = 12x^2 + 2a. At x=0x = 0 this is 2a2a, so we need a≥0a \ge 0; then f′′≥0f'' \ge 0 everywhere.

Exercise 2: First-order bound

Use the first-order condition for f(x)=exf(x) = e^x at x=0x = 0 to show ey≥1+ye^y \ge 1 + y for every yy.

Answer

The tangent at 0 is 1+y1 + y, and a convex function lies above its tangents.

Solution

f(0)=1f(0) = 1 and f′(0)=1f'(0) = 1, so f(y)≥f(0)+f′(0)(y−0)=1+yf(y) \ge f(0) + f'(0)(y - 0) = 1 + y.

Exercise 3: Quadratic form (code)

Is f(x,y)=2x2−6xy+5y2f(x, y) = 2x^2 - 6xy + 5y^2 convex? Check the Hessian eigenvalues.

Answer

Yes: the eigenvalues of [4−6−610]\begin{bmatrix} 4 & -6 \\ -6 & 10 \end{bmatrix} are about 0.29 and 13.71, both positive.

Solution
python · try it yourself
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 ff is strictly convex.

Test yourself

For a differentiable convex ff, ∇f(x∗)=0\nabla f(\mathbf{x}^*) = \mathbf{0} implies…

  1. x∗\mathbf{x}^* is a saddle point
  2. x∗\mathbf{x}^* is a global minimizer
  3. x∗\mathbf{x}^* is a local maximizer
  4. nothing

Which function is convex on R\mathbb{R}?

  1. sin⁡x\sin x
  2. −x2-x^2
  3. ∣x∣+x2\lvert x \rvert + x^2
  4. ∣x∣\sqrt{\lvert x \rvert}

Jensen's inequality for convex ff states…

  1. f(E[X])≤E[f(X)]f(\mathbb{E}[X]) \le \mathbb{E}[f(X)]
  2. f(E[X])≥E[f(X)]f(\mathbb{E}[X]) \ge \mathbb{E}[f(X)]
  3. f(E[X])=E[f(X)]f(\mathbb{E}[X]) = \mathbb{E}[f(X)]
  4. E[X]≤f(X)\mathbb{E}[X] \le f(X)