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 .
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.
What you’ll learn
Section titled “What you’ll learn”- 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: against , and against a tangent bound of .
- Why sampling cannot prove convexity. Measured: is not convex, but a chord test finds a violation on of a million random draws, with a worst magnitude of .
- 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.
Intuition: stretch a rubber band
Section titled “Intuition: stretch a rubber band”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.
flowchart TD D2["Def 7.2: convex SET
chord stays inside"] D3["Def 7.3: convex FUNCTION
chord stays above"] D3 -->|"epigraph: fill it in"| D2 D3 --> T1["test 1: the chord
needs no derivatives"] D3 --> T2["test 2: Eq 7.31 tangent
gives a global lower bound"] D3 --> T3["test 3: Hessian PSD
needs two derivatives"] T1 --> W["no second valley"] T2 --> W T3 --> W W --> G1["every local min is GLOBAL"] W --> G2["duality gap is ZERO
Section 7.3"] CL["closure rules, Example 7.4:
sum, max, nonneg scaling"] -->|"how convexity is proved in practice"| D3 SAMP["sampling chords"] -.->|"can only DISPROVE,
never establish"| D3 style SAMP stroke-dasharray: 4 3
The math
Section titled “The math”Definition 7.2: a convex set
Section titled “Definition 7.2: a convex set”A set is convex if for any and any scalar with ,
The expression traces the straight segment from (at ) to (at ). 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.
Definition 7.3: a convex function
Section titled “Definition 7.3: a convex function”Let have a convex set as its domain. Then is convex if for all in the domain and all ,
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.
The epigraph
Section titled “The epigraph”Take a convex function and “fill it in” — the region on and above the graph:
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 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.
Equation 7.31: the first-order condition
Section titled “Equation 7.31: the first-order condition”If is differentiable, convexity is equivalent to: for any two points ,
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 and the inequality reads for every — a stationary point of a convex function is a global minimum, in one line.
The second-order condition
Section titled “The second-order condition”If is twice differentiable, it is convex if and only if is positive semidefinite everywhere on the domain. In one dimension that is just .
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.
Example 7.3: the negative entropy
Section titled “Example 7.3: the negative entropy”is convex for . The book checks it at and ; here is the same check with every digit.
By Definition 7.3, with :
, with slack . The book quotes and .
By Equation 7.31, we need the derivative:
At that is . So the tangent at , evaluated at :
and , with slack . The book quotes .
By the second-order condition: , which is positive for every . At it is , and a measured second difference gives .
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 is convex and then is convex — multiply both sides of Definition 7.3 by , and a non-negative multiplier preserves the inequality direction. And if are convex,
Add them and regroup the right-hand side:
which is Definition 7.3 for . Combining, is convex for , 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 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”where all and are convex functions and all 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.”
Worked example by hand
Section titled “Worked example by hand”Show that is convex, without touching the definition.
Step 1. is convex: everywhere.
Step 2. is convex: everywhere.
Step 3. The maximum of two convex functions is convex. Here is the one-line argument — for any ,
by applying Definition 7.3 inside each branch, and then
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 — near — 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 random chords found no violation, and the minimum second derivative away from the kink was .
And the counterexample worth carrying: is not convex, with a minimum second derivative of on and a worst chord violation of . Subtraction is not in the toolkit.
See it move
Section titled “See it move”The chord test is the definition, so drag a chord around and watch it pass or fail:
And the closure rules, which is how convexity is actually established:
From scratch
Section titled “From scratch”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.")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.On real data
Section titled “On real data”Reading the plot
Section titled “Reading the plot”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 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 random segments whose endpoints lay in the epigraph, had a midpoint outside it — while for ‘s epigraph, none of did. So the correspondence is not a metaphor.
The second figure is Example 7.3 with nothing left implicit. The green chord from to sits above the curve, and the amber bar at measures by how much: . The dashed purple line is the tangent at with slope from Equation 7.32, and it sits below the curve everywhere — that is Equation 7.31, and the red bar at measures the slack, .
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 , Equation 7.31 immediately gives for all , 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 and it looks like a textbook convex bowl. It is not. The inset zoom shows a dimple, and the middle panel shows why: , which is negative for . That band is wide out of a domain of , or .
The right panel prices what that means for testing. Drawing a million random chords, the violation rate is and the worst violation found is . Two consequences, and both matter:
- The rate does not improve with effort. It sits near 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 . Any sane numerical tolerance — , 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 — 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 is on a chord between two members of and on a chord between two members of , 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: is convex, because the intervals overlap into one run. The counterexample needs them disjoint: puts on a chord and in neither set. Same for difference — is convex, while 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, and , both convex. Their sum and maximum survive, with minimum second derivatives and and no chord violation in draws. Their difference and product do not, reaching and with violations of and . 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.
Pitfalls
Section titled “Pitfalls”Compare
Section titled “Compare”| test | needs | proves convexity | catches a kink | cost |
|---|---|---|---|---|
| chord, Definition 7.3 | nothing | only if checked for all pairs | yes, applies anyway | infinite if done honestly |
| chord, sampled | nothing | no, never | yes | cheap, and misleading |
| tangent, Equation 7.31 | one derivative | if checked for all pairs | no, undefined at a kink | moderate |
| Hessian PSD | two derivatives | yes, and locally checkable | no, undefined at a kink | cheap where it applies |
| closure rules, Example 7.4 | knowing the pieces | yes | yes | one line |
| operation | preserves convexity | measured on and |
|---|---|---|
| yes | min , no violation | |
| for | yes | scaling cannot flip an inequality |
| yes | min , no violation | |
| no | min , violation | |
| no | min , violation | |
| (sets) | yes, any number | contiguous in every case tested |
| (sets) | no | is two pieces |
| (sets) | no | is two pieces |
-
You draw a million random chords on a function and none dips below it. What have you established?
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.
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.
-
Why is Equation 7.31, the tangent condition, the useful one for optimisation?
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.
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.
-
f1 and f2 are both convex. Which of these is guaranteed convex?
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.
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.
-
What is the relationship between a convex function and a convex set?
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.
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.
-
Which counterexample shows that the union of two convex sets need not be convex?
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'.
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'.
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – Example 7.3, checked both ways
Section titled “Exercise 1 – Example 7.3, checked both ways”Exercise 2 – The second-derivative test
Section titled “Exercise 2 – The second-derivative test”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”Recall card
Section titled “Recall card”- 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.coffeeCtapch.feedbackHeading
pch.feedbackSubheading