Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 38 · Convex Sets and Functions

Testing Convexity with Derivatives

Use f'' ≥ 0 and the Hessian's eigenvalues to prove convexity, check quadratic functions, and build a numerical convexity tester with sympy and numpy.

The chord definition is beautiful but hard to apply to a messy formula. Calculus gives a much faster test. For smooth functions, convexity is equivalent to non-negative curvature everywhere. In this lesson you learn the second-derivative test in one variable and its matrix version, then automate it.

One variable: f'' ≥ 0

If f is twice differentiable on an interval, then

Text
f is convex  ⟺  f''(x) ≥ 0 for every x in the interval

If f''(x) > 0 everywhere, f is strictly convex (the converse can fail: x⁴ is strictly convex but f''(0) = 0).

Examples: f(x) = x² has f'' = 2 > 0. f(x) = eˣ has f'' = eˣ > 0. f(x) = x³ has f'' = 6x, which is negative for x < 0, so x³ is not convex on the real line (but is convex on [0, ∞)). f(x) = -ln x has f'' = 1/x² > 0 on x > 0.

Many variables: the Hessian is positive semidefinite

For f: ℝⁿ → ℝ twice differentiable on a convex open domain:

Text
f is convex  ⟺  ∇²f(x) ⪰ 0 for every x

The symbol ⪰ 0 means positive semidefinite: vᵀ∇²f(x)v ≥ 0 for every vector v. For a symmetric matrix this is equivalent to all eigenvalues being ≥ 0. Positive definite (all eigenvalues > 0) gives strict convexity.

For a quadratic f(x) = ½xᵀQx + cᵀx + d with symmetric Q, the Hessian is the constant matrix Q. So a quadratic is convex exactly when Q is positive semidefinite.

Checking quadratics in code

Python Is this quadratic convex?

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The "trough" has eigenvalues 0 and 2: the function is flat along one direction (like a gutter), so it is convex but not strictly convex, and its minimizers form a line instead of a single point.

Symbolic Hessians with sympy

Computing second derivatives by hand is error-prone. The sympy library can differentiate for you and test the signs.

Python Symbolic Hessian of a function

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Here H = [[12x², 1], [1, 2]] and its determinant is 24x² - 1. That is negative for small |x|, so this function is not convex near x = 0, even though each term looks convex. The cross term xy spoils it. Always test the whole function, not its pieces by eye.

Sums and sanity checks

If curvature tests feel heavy, a numerical sanity check with finite differences is easy: estimate the Hessian at random points and check its smallest eigenvalue.

Python Numerical convexity probe

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A clearly negative result for f2 is evidence against convexity; non-negative values for the others (up to rounding) agree with the theory.

A numerical test only checks the points you sample. Use it to find counterexamples, then prove convexity with algebra or the rules in the next lesson.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. A twice-differentiable function of one variable is convex exactly when

    Non-negative second derivative means the curve never bends downward.
  2. 2. A quadratic ½xᵀQx + cᵀx is convex when Q is

    The Hessian of the quadratic is Q, and convexity requires it to be positive semidefinite.
  3. 3. A matrix with eigenvalues -1 and 3 is

    Mixed signs of eigenvalues mean the quadratic form is a saddle.
  4. 4. f(x) = x⁴ has f''(0) = 0. Is f convex?

    The test needs only f'' ≥ 0, and f''(x) = 12x² is never negative.

Summary

  • Smooth f is convex iff f'' ≥ 0 (one variable) or the Hessian is positive semidefinite (many variables).
  • A positive definite Hessian everywhere means strict convexity.
  • Quadratics are convex exactly when their matrix has no negative eigenvalues.
  • Sampling Hessians numerically can expose non-convexity, but proofs need algebra.

Next: Operations That Preserve Convexity