Skip to content

Convex Sets and Convex Functions

Page 701 established that a gradient never tells you which valley you are in. Page 703 established that duality gives a lower bound, but not necessarily a tight one — measured, a gap of 1.9841231.984123.

Both problems have the same cure, and this page is that cure. For convex problems every local minimum is global, and the duality gap is zero. The book puts it plainly: “many machine learning objective functions are designed such that they are convex”, and that design choice is what buys the guarantee.

  • Definition 7.2 (convex set) and Definition 7.3 (convex function), and why they are the same sentence about different objects.
  • The epigraph — filling in a convex function gives a convex set — which is what makes the two definitions literally interchangeable.
  • Three equivalent tests: the chord, the tangent (Equation 7.31), and the Hessian. Verified against Example 7.3’s own numbers: 3log23=4.7548883\log_2 3 = 4.754888 against 55, and f(4)=8f(4) = 8 against a tangent bound of 6.8853906.885390.
  • Why sampling cannot prove convexity. Measured: x40.02x2x^4 - 0.02x^2 is not convex, but a chord test finds a violation on 0.295%0.295\% of a million random draws, with a worst magnitude of 9.9×1059.9\times10^{-5}.
  • Example 7.4: a non-negative weighted sum of convex functions is convex — the closure rule that almost every objective in Part II is built from.
  • Exercises 7.3 and 7.4 settled by measurement: intersection and max are safe; union, difference and product are not.
  • Equation 7.38, the definition of a convex optimization problem, and what strong duality buys.

A set is convex if you can join any two of its points with a straight rod and the rod never leaves. A disc: yes. A crescent moon: no — pick the two horns and the rod crosses empty space.

A function is convex if stretching a rubber band between any two points on its graph leaves the band above the curve. A bowl: yes. A landscape with two valleys: no — a band from one valley to the other passes below the hill between them.

Those two pictures are the same picture. Fill a convex function up with water and the filled region — the epigraph — is a convex set. So “the chord stays above the curve” and “the chord stays inside the set” are one statement.

And here is why any of it matters. If the band always stays above, the function cannot have a second valley — a second valley would need a hill, and a hill would need a band dipping below. No second valley means every local minimum is the global one, and the whole problem of page 701 evaporates.

diagram What convexity buys, and what you must check to claim it mermaid

A set CC is convex if for any x,yC\mathbf{x}, \mathbf{y} \in C and any scalar θ\theta with 0θ10 \leq \theta \leq 1,

θx+(1θ)yC\theta\mathbf{x} + (1 - \theta)\mathbf{y} \in C

The expression θx+(1θ)y\theta\mathbf{x} + (1-\theta)\mathbf{y} traces the straight segment from y\mathbf{y} (at θ=0\theta = 0) to x\mathbf{x} (at θ=1\theta = 1). So the definition says exactly: every segment between two members stays inside. The book’s Figures 7.5 and 7.6 are the yes and no cases.

Let f:RDRf : \mathbb{R}^D \to \mathbb{R} have a convex set as its domain. Then ff is convex if for all x,y\mathbf{x}, \mathbf{y} in the domain and all 0θ10 \leq \theta \leq 1,

f(θx+(1θ)y)    θf(x)+(1θ)f(y)f\big(\theta\mathbf{x} + (1-\theta)\mathbf{y}\big) \;\leq\; \theta f(\mathbf{x}) + (1-\theta)f(\mathbf{y})

The left side is the function at a point on the segment; the right side is the chord at that same point. The function never rises above its own chords.

A concave function is the negative of a convex one, so everything here transfers with the inequality flipped. Maximising a concave function and minimising a convex one are the same problem.

Take a convex function and “fill it in” — the region on and above the graph:

epif={(x,t):tf(x)}\operatorname{epi} f = \{(\mathbf{x}, t) : t \geq f(\mathbf{x})\}

The book’s image is pouring water into a bowl. The epigraph of a convex function is a convex set, and conversely. So Definition 7.3 is Definition 7.2 in disguise, and the constraint functions gi(x)0g_i(\mathbf{x}) \leq 0 of §7.2 truncate functions at a scalar value to produce sets — which is how a statement about functions becomes a statement about the feasible region.

If ff is differentiable, convexity is equivalent to: for any two points x,y\mathbf{x}, \mathbf{y},

f(y)    f(x)+xf(x)(yx)f(\mathbf{y}) \;\geq\; f(\mathbf{x}) + \nabla_{\mathbf{x}}f(\mathbf{x})^\top(\mathbf{y} - \mathbf{x})

The tangent plane at any point is a global under-estimate of the function. This is the version that does the work in optimisation: it says a local derivative gives you information about the function everywhere, not just nearby. Set f(x)=0\nabla f(\mathbf{x}) = \mathbf{0} and the inequality reads f(y)f(x)f(\mathbf{y}) \geq f(\mathbf{x}) for every y\mathbf{y}a stationary point of a convex function is a global minimum, in one line.

If ff is twice differentiable, it is convex if and only if x2f(x)\nabla^2_{\mathbf{x}}f(\mathbf{x}) is positive semidefinite everywhere on the domain. In one dimension that is just f(x)0f''(x) \geq 0.

This is usually the cheapest test, and §4.2’s machinery applies directly: the Hessian is symmetric (§5.7), so it has real eigenvalues, and positive semidefinite means none of them is negative.

f(x)=xlog2xf(x) = x\log_2 x is convex for x>0x > 0. The book checks it at x=2x = 2 and x=4x = 4; here is the same check with every digit.

By Definition 7.3, with θ=0.5\theta = 0.5:

f(0.52+0.54)=f(3)=3log23=4.754888f(0.5 \cdot 2 + 0.5 \cdot 4) = f(3) = 3\log_2 3 = 4.754888 0.5f(2)+0.5f(4)=0.5(2log22)+0.5(4log24)=0.5(2)+0.5(8)=50.5 f(2) + 0.5 f(4) = 0.5(2\log_2 2) + 0.5(4\log_2 4) = 0.5(2) + 0.5(8) = 5

4.75488854.754888 \leq 5, with slack 0.2451120.245112. The book quotes 4.75\approx 4.75 and 55.

By Equation 7.31, we need the derivative:

x(xlog2x)=1log2x+x1xloge2=log2x+1loge2\nabla_x(x\log_2 x) = 1 \cdot \log_2 x + x \cdot \frac{1}{x\log_e 2} = \log_2 x + \frac{1}{\log_e 2}

At x=2x = 2 that is 1+1/ln2=2.4426951 + 1/\ln 2 = 2.442695. So the tangent at x=2x = 2, evaluated at y=4y = 4:

f(2)+f(2)(42)=2+2.442695×2=6.885390f(2) + \nabla f(2)\cdot(4 - 2) = 2 + 2.442695 \times 2 = 6.885390

and f(4)=86.885390f(4) = 8 \geq 6.885390, with slack 1.1146101.114610. The book quotes 6.9\approx 6.9.

By the second-order condition: f(x)=1xln2f''(x) = \dfrac{1}{x\ln 2}, which is positive for every x>0x > 0. At x=2x = 2 it is 1/(2ln2)=0.7213481/(2\ln 2) = 0.721348, and a measured second difference gives 0.7213500.721350.

Example 7.4: closure under non-negative weighted sums

Section titled “Example 7.4: closure under non-negative weighted sums”

This is the rule that makes convexity practical. If ff is convex and α0\alpha \geq 0 then αf\alpha f is convex — multiply both sides of Definition 7.3 by α\alpha, and a non-negative multiplier preserves the inequality direction. And if f1,f2f_1, f_2 are convex,

f1(θx+(1θ)y)θf1(x)+(1θ)f1(y)f_1(\theta\mathbf{x} + (1-\theta)\mathbf{y}) \leq \theta f_1(\mathbf{x}) + (1-\theta)f_1(\mathbf{y}) f2(θx+(1θ)y)θf2(x)+(1θ)f2(y)f_2(\theta\mathbf{x} + (1-\theta)\mathbf{y}) \leq \theta f_2(\mathbf{x}) + (1-\theta)f_2(\mathbf{y})

Add them and regroup the right-hand side:

θ(f1(x)+f2(x))+(1θ)(f1(y)+f2(y))\theta\big(f_1(\mathbf{x}) + f_2(\mathbf{x})\big) + (1-\theta)\big(f_1(\mathbf{y}) + f_2(\mathbf{y})\big)

which is Definition 7.3 for f1+f2f_1 + f_2. Combining, αf1+βf2\alpha f_1 + \beta f_2 is convex for α,β0\alpha, \beta \geq 0, and the argument extends to any number of terms.

In practice nobody checks convexity from the definition. They build objectives out of pieces known to be convex, using closure rules — the same idea as closure for vector spaces in Chapter 2. A squared error plus an 2\ell_2 penalty is convex because both parts are and the weights are positive. That is the whole argument, and it is one line.

Equation 7.38: a convex optimization problem

Section titled “Equation 7.38: a convex optimization problem”
minxf(x)s.t.gi(x)0    i,hj(x)=0    j\min_{\mathbf{x}} f(\mathbf{x}) \quad\text{s.t.}\quad g_i(\mathbf{x}) \leq 0 \;\; \forall i, \qquad h_j(\mathbf{x}) = 0 \;\; \forall j

where all ff and gig_i are convex functions and all hj(x)=0h_j(\mathbf{x}) = 0 define convex sets. For these problems we have strong duality: the optimal value of the dual equals the optimal value of the primal. Solve either and you have solved both, which is exactly what failed on page 703’s nonconvex quartic.

The book adds a useful caution: “the distinction between convex functions and convex sets are often not strictly presented in machine learning literature, but one can often infer the implied meaning from context.”

Show that f(x)=max(x2,ex)f(x) = \max(x^2, e^{-x}) is convex, without touching the definition.

Step 1. x2x^2 is convex: f=2>0f'' = 2 > 0 everywhere.

Step 2. exe^{-x} is convex: f=ex>0f'' = e^{-x} > 0 everywhere.

Step 3. The maximum of two convex functions is convex. Here is the one-line argument — for any θ[0,1]\theta \in [0,1],

max ⁣(f1(θx+(1θ)y),f2(θx+(1θ)y))maxi(θfi(x)+(1θ)fi(y))\max\!\big(f_1(\theta x + (1-\theta)y),\, f_2(\theta x + (1-\theta)y)\big) \leq \max_i \big(\theta f_i(x) + (1-\theta)f_i(y)\big)

by applying Definition 7.3 inside each branch, and then

θmaxifi(x)+(1θ)maxifi(y)\leq \theta\max_i f_i(x) + (1-\theta)\max_i f_i(y)

because a maximum of sums is at most the sum of maxima. Done.

Note what the max does not preserve: differentiability. At the crossing point where x2=exx^2 = e^{-x} — near x=0.7035x = 0.7035 — the function has a kink, and the Hessian test does not apply there. The chord test still does, which is why Definition 7.3 rather than the second derivative is the real definition. A measured check over 300,000300{,}000 random chords found no violation, and the minimum second derivative away from the kink was +0.4951+0.4951.

And the counterexample worth carrying: x2exx^2 - e^{-x} is not convex, with a minimum second derivative of 5.3872-5.3872 on [2,3][-2,3] and a worst chord violation of 0.57540.5754. Subtraction is not in the toolkit.

The chord test is the definition, so drag a chord around and watch it pass or fail:

sketch Drag a chord and test Definition 7.3 yourself p5.js
Pick a function and drag the two endpoints of the chord. The sketch reports the largest amount by which the chord dips below the curve. Note how hard it is to find the violation on the fourth function, whose non-convex region is under three percent of the domain.

And the closure rules, which is how convexity is actually established:

sketch Build a convex function out of convex pieces p5.js
Combine two convex functions with a chosen operation and see whether the result survives. Sum, maximum and non-negative scaling always do; difference and product do not, and the sketch reports the measured worst chord violation for each.
convexity.py
import numpy as np
 
# ---- Definition 7.3, as a testable predicate ------------------------------
def chord_violation(f, lo, hi, n=200_000, seed=0):
    """Largest amount by which a chord dips BELOW f. Positive means not convex."""
    rng = np.random.default_rng(seed)
    a = rng.uniform(lo, hi, n)
    b = rng.uniform(lo, hi, n)
    t = rng.uniform(0.0, 1.0, n)
    return float((f(t * a + (1 - t) * b) - (t * f(a) + (1 - t) * f(b))).max())
 
def min_second_derivative(f, lo, hi, n=20_001):
    """The Hessian test in one dimension. Negative anywhere means not convex."""
    xs = np.linspace(lo, hi, n)
    h = (hi - lo) / (n - 1)
    d2 = (f(xs[1:-1] + h) - 2 * f(xs[1:-1]) + f(xs[1:-1] - h)) / h ** 2
    return float(d2.min())
 
# ---- Example 7.3, checked three ways -------------------------------------
f = lambda x: x * np.log2(x)
df = lambda x: np.log2(x) + 1.0 / np.log(2.0)      # Equation 7.32
 
a, b, th = 2.0, 4.0, 0.5
print("Example 7.3, the negative entropy x log2 x")
print(f"  chord   : f(3) = {f(3.0):.6f}   vs   0.5f(2)+0.5f(4) = "
      f"{th * f(a) + (1 - th) * f(b):.6f}")
print(f"  tangent : f(4) = {f(b):.6f}   vs   f(2)+f'(2)*2 = "
      f"{f(a) + df(a) * 2:.6f}")
print(f"  gradient at 2 (Eq 7.32) = {df(a):.6f}")
print(f"  min f'' on [0.05, 5]    = {min_second_derivative(f, 0.05, 5.0):.6f}")
 
# ---- the trap: sampling cannot establish convexity ----------------------
print("\nA function that is NOT convex but passes a casual chord check:")
g = lambda x: x ** 4 - 0.02 * x ** 2
edge = np.sqrt(0.04 / 12)
print(f"  min f''            = {min_second_derivative(g, -2, 2):.6f}  "
      f"(negative, so NOT convex)")
print(f"  nonconvex band     = |x| < {edge:.6f}, "
      f"{100 * 2 * edge / 4:.3f}% of the domain")
print(f"{'draws':>10} {'violation rate':>16} {'worst violation':>17}")
rng = np.random.default_rng(4)
for n in (100, 1_000, 10_000, 100_000, 1_000_000):
    aa = rng.uniform(-2, 2, n); bb = rng.uniform(-2, 2, n)
    tt = rng.uniform(0, 1, n)
    v = g(tt * aa + (1 - tt) * bb) - (tt * g(aa) + (1 - tt) * g(bb))
    print(f"{n:>10} {100 * (v > 1e-12).mean():>15.4f}% "
          f"{max(v.max(), 1e-12):>17.3e}")
 
# ---- Exercise 7.4: which operations preserve convexity -----------------
print("\nExercise 7.4, with f1 = x^2 and f2 = exp(-x) on [-2, 3]:")
f1 = lambda x: x ** 2
f2 = lambda x: np.exp(-x)
for name, fn in (("f1 + f2", lambda x: f1(x) + f2(x)),
                 ("f1 - f2", lambda x: f1(x) - f2(x)),
                 ("f1 * f2", lambda x: f1(x) * f2(x)),
                 ("max(f1, f2)", lambda x: np.maximum(f1(x), f2(x)))):
    d2 = min_second_derivative(fn, -2.0, 3.0)
    cv = chord_violation(fn, -2.0, 3.0, n=300_000, seed=7)
    print(f"  {name:12} min f'' = {d2:>10.4f}   worst chord = {cv:>10.5f}   "
          f"{'CONVEX' if cv <= 1e-9 else 'not convex'}")
 
# ---- Exercise 7.3: which set operations preserve convexity ------------
print("\nExercise 7.3, on intervals in R (a subset of R is convex exactly")
print("when it is one contiguous run):")
ts = np.linspace(-4, 6, 1_000_001)
def contiguous(mask):
    idx = np.flatnonzero(mask)
    return bool(idx.size == 0 or np.all(np.diff(idx) == 1))
 
def iv(lo, hi):
    return (ts >= lo) & (ts <= hi)
 
cases = [
    ("[-1,1] AND [0.5,2]", iv(-1, 1) & iv(0.5, 2)),
    ("[-1,1] OR  [0.5,2]", iv(-1, 1) | iv(0.5, 2)),
    ("[-1,1] OR  [3,4]", iv(-1, 1) | iv(3, 4)),
    ("[-2,2] MINUS [-1,1]", iv(-2, 2) & ~iv(-1, 1)),
]
for name, mask in cases:
    print(f"  {name:22} convex: {contiguous(mask)}")
print("  -> intersection is always convex; union and difference are not.")
text
Example 7.3, the negative entropy x log2 x
  chord   : f(3) = 4.754888   vs   0.5f(2)+0.5f(4) = 5.000000
  tangent : f(4) = 8.000000   vs   f(2)+f'(2)*2 = 6.885390
  gradient at 2 (Eq 7.32) = 2.442695
  min f'' on [0.05, 5]    = 0.288553
 
A function that is NOT convex but passes a casual chord check:
  min f''            = -0.040000  (negative, so NOT convex)
  nonconvex band     = |x| < 0.057735, 2.887% of the domain
     draws   violation rate   worst violation
       100          1.0000%         6.064e-07
      1000          0.2000%         2.440e-05
     10000          0.2500%         9.222e-05
    100000          0.2660%         9.698e-05
   1000000          0.2969%         9.926e-05
 
Exercise 7.4, with f1 = x^2 and f2 = exp(-x) on [-2, 3]:
  f1 + f2      min f'' =     2.0498   worst chord =   -0.00000   CONVEX
  f1 - f2      min f'' =    -5.3872   worst chord =    0.57764   not convex
  f1 * f2      min f'' =    -0.4120   worst chord =    0.28017   not convex
  max(f1, f2)  min f'' =     0.4951   worst chord =   -0.00000   CONVEX
 
Exercise 7.3, on intervals in R (a subset of R is convex exactly
when it is one contiguous run):
  [-1,1] AND [0.5,2]     convex: True
  [-1,1] OR  [0.5,2]     convex: True
  [-1,1] OR  [3,4]       convex: False
  [-2,2] MINUS [-1,1]    convex: False
  -> intersection is always convex; union and difference are not.
figure Two definitions, one shape matplotlib
Four panels. First, a convex blob with a chord drawn entirely inside it. Second, a lobed nonconvex region with a chord whose midpoint is marked outside the set. Third, the parabola x squared with a chord shaded above it. Fourth, the quartic x to the fourth minus three x squared with its epigraph shaded, a chord passing below the curve at the origin, and a note that the epigraph is therefore not convex. Four panels. First, a convex blob with a chord drawn entirely inside it. Second, a lobed nonconvex region with a chord whose midpoint is marked outside the set. Third, the parabola x squared with a chord shaded above it. Fourth, the quartic x to the fourth minus three x squared with its epigraph shaded, a chord passing below the curve at the origin, and a note that the epigraph is therefore not convex.
Definition 7.2 and Definition 7.3 are the same statement about different objects, and the epigraph makes that literal. Measured on 200000 random segments with endpoints in the epigraph of x squared, none left it; on the epigraph of the quartic, 3128 did.
figure Example 7.3: three ways to ask the same question matplotlib
Two panels. Left, the negative entropy x log base 2 x with the chord from x equals 2 to x equals 4 drawn above it, the vertical slack at x equals 3 marked as 0.245112, and the dashed tangent at x equals 2 lying below the curve with its slack at x equals 4 marked as 1.114610. Right, a table of the three convexity tests with their left-hand and right-hand values and verdicts. Two panels. Left, the negative entropy x log base 2 x with the chord from x equals 2 to x equals 4 drawn above it, the vertical slack at x equals 3 marked as 0.245112, and the dashed tangent at x equals 2 lying below the curve with its slack at x equals 4 marked as 1.114610. Right, a table of the three convexity tests with their left-hand and right-hand values and verdicts.
The chord test gives 4.754888 against 5, the tangent test 8 against 6.885390, and the second derivative 0.721350 against the exact 1 over 2 ln 2. The book quotes approximately 4.75, 5, 8 and approximately 6.9, and every one checks out.
figure Convexity is a claim about every pair of points matplotlib
Three panels. Left, x to the fourth minus 0.02 x squared plotted over minus two to two, looking like a convex bowl, with an inset zoom revealing a shallow dimple near the origin. Middle, its second derivative crossing below zero on a narrow shaded band around the origin. Right, a log-log plot of the chord violation rate and the worst violation magnitude against the number of random chords drawn, the rate flat near 0.3 percent and the magnitude saturating near ten to the minus four. Three panels. Left, x to the fourth minus 0.02 x squared plotted over minus two to two, looking like a convex bowl, with an inset zoom revealing a shallow dimple near the origin. Middle, its second derivative crossing below zero on a narrow shaded band around the origin. Right, a log-log plot of the chord violation rate and the worst violation magnitude against the number of random chords drawn, the rate flat near 0.3 percent and the magnitude saturating near ten to the minus four.
This function is not convex: its second derivative reaches minus 0.04. But the failure lives on 2.887 percent of the domain with a worst chord violation of about 1e-4, so a million random draws catch it only 0.2969 percent of the time and would be dismissed as noise.
figure The closure rules are how convexity is checked in practice matplotlib
Four panels. First, a table of the three set operations marking intersection as always convex and union and difference as not. Second, two disjoint shaded intervals with a chord between them whose midpoint at x equals 2 is marked as belonging to neither. Third, the sum and the maximum of x squared and exp minus x, both convex, annotated with positive minimum second derivatives. Fourth, their difference and product, both non-convex, annotated with negative minimum second derivatives and their worst chord violations. Four panels. First, a table of the three set operations marking intersection as always convex and union and difference as not. Second, two disjoint shaded intervals with a chord between them whose midpoint at x equals 2 is marked as belonging to neither. Third, the sum and the maximum of x squared and exp minus x, both convex, annotated with positive minimum second derivatives. Fourth, their difference and product, both non-convex, annotated with negative minimum second derivatives and their worst chord violations.
Sum and maximum keep convexity, with minimum second derivatives of plus 2.0498 and plus 0.4951 and no chord violation in 300000 draws. Difference and product lose it, reaching minus 5.3872 and minus 0.4120 with violations of 0.57764 and 0.28017.

The first figure puts the two definitions side by side so the shared structure is visible. The crescent in the second panel is the book’s Figure 7.6: pick the two horns and the segment between them leaves the set. One such pair is enough — convexity is a universally quantified statement, so a single counterexample settles it.

The fourth panel is the one that does conceptual work. The shaded region above x43x2x^4 - 3x^2 is its epigraph, and it is visibly not convex: the two wells create a notch, and a segment spanning the notch drops out of the region. That is the same failure as the chord dipping below the curve, expressed as a set instead of a function. Measured: of 200,000200{,}000 random segments whose endpoints lay in the epigraph, 31283128 had a midpoint outside it — while for x2x^2‘s epigraph, none of 200,000200{,}000 did. So the correspondence is not a metaphor.

The second figure is Example 7.3 with nothing left implicit. The green chord from (2,2)(2, 2) to (4,8)(4, 8) sits above the curve, and the amber bar at x=3x = 3 measures by how much: 54.754888=0.2451125 - 4.754888 = 0.245112. The dashed purple line is the tangent at x=2x = 2 with slope 2.4426952.442695 from Equation 7.32, and it sits below the curve everywhere — that is Equation 7.31, and the red bar at x=4x = 4 measures the slack, 86.885390=1.1146108 - 6.885390 = 1.114610.

Notice which way each inequality points. The chord is an upper bound on the function between two points; the tangent is a lower bound on the function everywhere. The second one is the useful one for optimisation, because it converts local information into a global guarantee. If the gradient is zero at x\mathbf{x}, Equation 7.31 immediately gives f(y)f(x)f(\mathbf{y}) \geq f(\mathbf{x}) for all y\mathbf{y}, and that single line is why convex problems have no local minima to get trapped in.

The third figure is the most practically important thing on this page. The left panel plots x40.02x2x^4 - 0.02x^2 and it looks like a textbook convex bowl. It is not. The inset zoom shows a dimple, and the middle panel shows why: f(x)=12x20.04f''(x) = 12x^2 - 0.04, which is negative for x<0.057735\lvert x \rvert < 0.057735. That band is 0.1154700.115470 wide out of a domain of 44, or 2.887%2.887\%.

The right panel prices what that means for testing. Drawing a million random chords, the violation rate is 0.2969%0.2969\% and the worst violation found is 9.926×1059.926\times10^{-5}. Two consequences, and both matter:

  • The rate does not improve with effort. It sits near 0.3%0.3\% from a thousand draws to a million, because it is a property of the geometry, not of the sample size. More sampling does not sharpen the verdict.
  • The magnitude is 10410^{-4}. Any sane numerical tolerance — 10310^{-3}, say, or “is this just floating-point noise?” — classifies this function as convex.

So the honest summary: a chord test can disprove convexity and can never establish it. Use it to hunt for counterexamples, and use the Hessian or the closure rules of Example 7.4 when you need the claim itself. The exact worst-case violation for this function, found by an exhaustive search over the dimple rather than by sampling, is 9.99997×1059.99997\times10^{-5} — so even a perfect search would return a number that looks like noise.

The fourth figure is the toolkit. The left two panels settle Exercise 7.3. Intersection is always safe, and the reason is worth stating: a point on a chord between two members of ABA \cap B is on a chord between two members of AA and on a chord between two members of BB, so it lies in both. That argument works for any number of sets, which is why a feasible region defined by many convex constraints stays convex however many you add — the fact §7.3 relies on.

Union fails, but note the second panel is careful about when: [1,1][0.5,2][-1,1] \cup [0.5, 2] is convex, because the intervals overlap into one run. The counterexample needs them disjoint: [1,1][3,4][-1,1] \cup [3,4] puts x=2x = 2 on a chord and in neither set. Same for difference — [1,1][0.5,2][-1,1] \setminus [0.5,2] is convex, while [2,2][1,1][-2,2] \setminus [-1,1] is two pieces. “Not convex in general” is not “never convex”, and picking the wrong counterexample is a common way to get Exercise 7.3 half right.

The right two panels settle Exercise 7.4 with the plainest possible ingredients, x2x^2 and exe^{-x}, both convex. Their sum and maximum survive, with minimum second derivatives +2.0498+2.0498 and +0.4951+0.4951 and no chord violation in 300,000300{,}000 draws. Their difference and product do not, reaching 5.3872-5.3872 and 0.4120-0.4120 with violations of 0.577640.57764 and 0.280170.28017. Those violations are four orders of magnitude larger than the third figure’s — when convexity fails badly, sampling finds it instantly. It is the near misses that hide.

testneedsproves convexitycatches a kinkcost
chord, Definition 7.3nothingonly if checked for all pairsyes, applies anywayinfinite if done honestly
chord, samplednothingno, neveryescheap, and misleading
tangent, Equation 7.31one derivativeif checked for all pairsno, undefined at a kinkmoderate
Hessian PSDtwo derivativesyes, and locally checkableno, undefined at a kinkcheap where it applies
closure rules, Example 7.4knowing the piecesyesyesone line
operationpreserves convexitymeasured on x2x^2 and exe^{-x}
f1+f2f_1 + f_2yesmin f=+2.0498f'' = +2.0498, no violation
αf\alpha f for α0\alpha \geq 0yesscaling cannot flip an inequality
max(f1,f2)\max(f_1, f_2)yesmin f=+0.4951f'' = +0.4951, no violation
f1f2f_1 - f_2nomin f=5.3872f'' = -5.3872, violation 0.577640.57764
f1f2f_1 \cdot f_2nomin f=0.4120f'' = -0.4120, violation 0.280170.28017
ABA \cap B (sets)yes, any numbercontiguous in every case tested
ABA \cup B (sets)no[1,1][3,4][-1,1] \cup [3,4] is two pieces
ABA \setminus B (sets)no[2,2][1,1][-2,2] \setminus [-1,1] is two pieces
pch.quizTag Can you tell what convexity does and does not give you?
  1. You draw a million random chords on a function and none dips below it. What have you established?

    pch.quizShowAnswer

    B — Nothing about convexity: measured on x^4 - 0.02x^2, which is NOT convex, the violation rate is under 0.3 percent and the worst magnitude is 1e-4 — The violation rate sat near 0.3 percent from a thousand draws to a million — it reflects the 2.887 percent of the domain where the function misbehaves, not the sample size. And a worst violation of 1e-4 would be dismissed as floating-point noise by any reasonable tolerance. Sampling can disprove convexity; it can never establish it.

  2. Why is Equation 7.31, the tangent condition, the useful one for optimisation?

    pch.quizShowAnswer

    B — Because the tangent is a GLOBAL lower bound, so a zero gradient at one point immediately proves that point is a global minimum — Substituting grad f(x) = 0 into f(y) >= f(x) + grad f(x) dot (y - x) gives f(y) >= f(x) for every y. That one line is the entire reason convex problems have no local minima to get trapped in — local information became a global guarantee.

  3. f1 and f2 are both convex. Which of these is guaranteed convex?

    pch.quizShowAnswer

    B — max(f1, f2) — Measured with f1 = x^2 and f2 = exp(-x), both perfectly ordinary convex functions: the difference reaches a second derivative of -5.3872 with a chord violation of 0.57764, and the product reaches -0.4120 with 0.28017. Only non-negative weighted sums and maxima are in the closure toolkit — which is why adding a regulariser is safe and subtracting one is not.

  4. What is the relationship between a convex function and a convex set?

    pch.quizShowAnswer

    B — The epigraph — the region on and above the graph — of a convex function is a convex set, and conversely — Filling in a convex function gives a convex set, which is why the two definitions are the same sentence about different objects. Measured: of 200000 random segments with endpoints in the epigraph of x squared, none left it; for the epigraph of x^4 - 3x^2, 3128 did — matching exactly the chords that dip below that curve.

  5. Which counterexample shows that the union of two convex sets need not be convex?

    pch.quizShowAnswer

    B — [-1,1] union [3,4] — The overlapping pair [-1,1] union [0.5,2] is the single interval [-1,2], which IS convex — so it proves nothing. You need them disjoint: with [-1,1] union [3,4], the point x = 2 lies on a chord between the two pieces and in neither. 'Not preserved in general' is not 'never preserved'.

Exercise 1 – Example 7.3, checked both ways

Section titled “Exercise 1 – Example 7.3, checked both ways”

Exercise 3 – Why a chord sample is not a proof

Section titled “Exercise 3 – Why a chord sample is not a proof”

Exercise 4 – Which operations preserve convexity

Section titled “Exercise 4 – Which operations preserve convexity”

Exercise 5 – Which set operations preserve convexity

Section titled “Exercise 5 – Which set operations preserve convexity”
  • Definition 7.2, a convex set: theta x plus (1 minus theta) y stays in the set for every theta in [0,1]. Every chord stays inside.
  • Definition 7.3, a convex function: f of a point on the segment is at most the chord at that point. Every chord stays above the curve.
  • The epigraph links them. Fill in a convex function and you get a convex set, so the two definitions are one statement. Measured: of 200000 random segments in the epigraph of x squared, none left it; for x^4 - 3x^2, 3128 did.
  • The domain must be convex too, or Definition 7.3 evaluates f at a point that may not be in the domain. That is why Definition 7.2 comes first.
  • Three equivalent tests: the chord (no derivatives, survives kinks), Equation 7.31’s tangent (one derivative, gives a GLOBAL lower bound), and a positive semidefinite Hessian (two derivatives, cheapest where it applies).
  • Equation 7.31 is the one that matters. Put a zero gradient into it and you get f(y) at least f(x) for every y — a stationary point of a convex function is global, in one line. That is the whole reason convexity is worth having.
  • Example 7.3 measured: f(3) = 3 log2 3 = 4.754888 against a chord value of 5, slack 0.245112. The tangent at 2 has slope 2.442695 (Equation 7.32) and predicts 6.885390 against a true f(4) = 8, slack 1.114610. Second derivative at 2 is 0.721350, exactly 1/(2 ln 2).
  • Sampling can disprove convexity and can never prove it. x^4 - 0.02x^2 is not convex, min second derivative -0.04, but the failure occupies 2.887% of the domain with a worst chord violation of 1e-4. A million draws catch it 0.2954% of the time, and the rate does not improve with more draws.
  • Example 7.4, the closure rule that does the work: a non-negative weighted sum of convex functions is convex, for any number of terms. That is how every objective in Part II is actually shown convex — nobody uses the definition.
  • Maximum is safe; difference and product are not. With f1 = x squared and f2 = exp(-x), the sum has min second derivative +2.0498 and the max +0.4951, while the difference reaches -5.3872 and the product -0.4120. Adding a regulariser keeps convexity; subtracting one destroys it.
  • Intersection of convex sets is always convex, for any number of them — which is why a feasible region built from many convex constraints stays convex. Union and difference are not, and the counterexample must use DISJOINT sets: [-1,1] union [0.5,2] is convex.
  • A kink defeats the Hessian test but not convexity. max(x squared, exp(-x)) is convex and non-differentiable near x = 0.7035. The hinge loss of Chapter 12 is the same shape, which is why Section 7.3.3 bothers to smooth it.
  • Equation 7.38 defines a convex optimization problem, and for one you get strong duality: the dual optimum equals the primal optimum, so solving either solves both. That is what failed on the nonconvex quartic of Section 7.2, by 1.984123.

Next: the two convex families you will actually meet, and their duals worked out in full. Linear and Quadratic Programming

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading