Fit a logistic regression twice from two random starts and you reach the same loss. Fit a small network twice and you get two models with two different losses. Both runs use gradient descent. The difference is the shape of the loss, and one inequality separates the two shapes. Each surface below has an amber chord from to , above the same two points and .
The convex surface f(u, v) = 3u² + 2uv + 2v², with a chord from y = (−1.00, −0.50) to x = (1.00, 0.50). At θ = 0.50 the point θx + (1 − θ)y is (0.00, 0.00). The surface there is at 0.00 and the chord at 4.50, so the chord sits 4.50 above.
Slide to move the point from to . The short segment joins it to the surface, and the caption prints both heights. On the convex surface the chord stays above the graph at every . Switch to the double well, and at the chord runs 2.5 under the hump.
The chord test#
A function is convex if its domain is convex and, for all and ,
This is Jensen's inequality, the test you ran in the scene. The left side is the surface under the marked point. The right side is the chord. On the convex surface , so the chord is flat at 4.5, and at the surface is at 0. The double well is , with and . At the chord is at 1.5 and the surface at 4, so the inequality fails by 2.5.
Why the quadratic passes: PSD#
The convex surface is with . For any quadratic, expand both sides of Jensen with :
In the scene and , so the gap is , which is 4.5 at . The gap is nonnegative for every pair exactly when for every . That is positive semidefinite (PSD): is convex if and only if the symmetric has no negative eigenvalue. Here gives eigenvalues and , both positive.
A twice-differentiable is convex exactly when its Hessian is PSD at every . In one dimension that is , so and pass. The double well has Hessian at the origin. Along the chord's direction, . The surface curves down under the chord.
The common mistake. Each well of the double well is a bowl, and its Hessian is positive definite at both minima. That does not make convex. Convexity is a claim about every chord, not about the minima, and the chord across the hump breaks it.
First-order condition: the tangent plane#
For differentiable , convexity is equivalent to the first-order condition: the function lies above every tangent plane.
On the convex surface , so the tangent plane at predicts at . The surface is at 4.5, above it by . On the double well , so the tangent predicts at , but . The tangent passes over the surface by 6. Week 3 adds a quadratic floor, strong convexity.
Convex sets#
A set is convex if for all and . Halfspaces, balls and the probability simplex are convex. A donut is not. Intersections of convex sets are convex, so linear constraints always cut out a convex feasible region.
Building convex functions#
These operations preserve convexity:
- Nonnegative weighted sums with .
- Pointwise maxima , which is why hinge loss is convex.
- Affine composition .
- Norms: the triangle inequality and give , so and are convex penalties (Week 10).
Nonlinear composition need not: and are convex, but is not.
Local equals global#
Theorem: For a convex , every local minimum is a global minimum.
Proof sketch: Suppose is a local minimum and some has . By convexity, for every ,
Small gives points arbitrarily close to with lower loss, which contradicts local minimality.
The double well shows what nonconvexity costs. Its global minimum is at with , and its other minimum at has . The gradient at is , so small-step gradient descent from settles in the worse well. Least squares and logistic regression have no such trap. Week 13 returns to the nonconvex case.
Knowledge Check#
Convex surface, θ = 0.2. How far above the surface does the caption put the chord?
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
fig, axes = plt.subplots(2, 2, figsize=(10, 8))
axes = axes.flat
for ax, fn, name, grad_fn, xr, x0 in [
(axes[0], f1, "$x^2$", lambda x: 2*x, (-2.0, 4.0), 2.0),
(axes[1], f2, "$e^x$", lambda x: np.exp(x), (-2.0, 4.0), 2.0),
(axes[2], f3, "$-\\log x$", lambda x: -1/x, (0.05, 4.0), 2.0),
(axes[3], f4, "$x^3-3x$", lambda x: 3*x**2 - 3, (-2.0, 4.0), -1.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, where f''(-1) < 0, the tangent at -1")
print(" lies above the curve on (-2, 2) and crosses it at x = 2.")
Key takeaways#
- is convex when every chord lies on or above its graph.
- For the chord gap is , so convexity is . In general the Hessian must be PSD everywhere.
- A bowl at every minimum is not convexity. The double well fails the chord test across its hump.
- For a convex function every local minimum is global.
Further reading#
- Stephen Boyd & Lieven Vandenberghe, Convex Optimization (Cambridge University Press, 2004). Chapters 2 and 3 cover convex sets and functions. Free at stanford.edu/~boyd/cvxbook.
- Yurii Nesterov, Introductory Lectures on Convex Optimization (Springer, 2004). Chapter 2 covers smooth and strongly convex functions.
- Sébastien Bubeck, Convex Optimization: Algorithms and Complexity (2015). A concise free treatment, at arXiv:1405.4980.
Reading is free. PDF and ePub unlock with the newsletter. Join once and every lesson opens in this browser.