Search

Search papers and lessons

Convex Function

Level 2 · 9 min read

A convex function is shaped like a bowl: every chord between two points lies on or above the graph, so any local minimum is also the global minimum.

On this page

  • Colour = topic mix
  • Optimization
  • Linear algebra
  • Sign in to see your progress

TL;DR

A function is convex when its graph curves like a bowl. Pick any two points on the graph and join them with a straight segment: the segment never dips below the curve. This one shape property matters because a convex function has no false valleys. Any point where it stops going downhill is the lowest point anywhere.

Where you'll see it: in the squared-error loss of linear regression, in the negative log-likelihood of logistic regression, and in Jensen's inequality, which bounds averages of functions in probability and information theory.

Intuition

A bowl has one bottom

Picture a marble placed on a curve. On a bowl, it rolls to the single lowest point wherever it starts. On a mountain range with several valleys, it can settle into a shallow one and stay there. A convex function is a bowl: start walking downhill from any point and you reach the same bottom.

This matters because training a model means finding the parameters that minimise a loss. If the loss is a bowl, a simple procedure like gradient descent (repeatedly step downhill) can find the best answer. If the loss has many valleys, the procedure may stop at a poor one and never know it.

−2−1012302468xf(x)
  • f(x) = x^2
  • chord from x = -1 to x = 2
The chord between two points on the graph lies on or above the curve. For x squared it does this for every pair of points, so x squared is convex.

The math

Definition: Convex function

Let ff be a function defined on a convex set CC. Then ff is convex if, for every x,y∈Cx, y \in C and every λ∈[0,1]\lambda \in [0, 1],

f(λx+(1−λ)y)≤λf(x)+(1−λ)f(y).f(\lambda x + (1 - \lambda) y) \le \lambda f(x) + (1 - \lambda) f(y).

It is strictly convex if the inequality is strict whenever x≠yx \ne y and 0<λ<10 \lt \lambda \lt 1. A function is concave when −f-f is convex, which reverses the inequality.

In words: the value of ff at a blend of two points is at most the same blend of the two values. The weight λ\lambda runs from 0 to 1. At λ=0\lambda = 0 the blended point is yy and both sides equal f(y)f(y). At λ=1\lambda = 1 it is xx and both sides equal f(x)f(x). In between, the blended point slides along the segment from yy to xx, and the right side traces the chord above it.

Convex functions lie above their tangents

If ff is differentiable (it has a Derivative f′f' everywhere), the tangent line at any point xx lies below the whole graph:

f(y)≥f(x)+f′(x) (y−x)for all x,y∈C.f(y) \ge f(x) + f'(x)\,(y - x) \quad \text{for all } x, y \in C.

The left side is the true height at yy. The right side is the height of the tangent line at xx, evaluated at yy. A tangent line is therefore a linear model that never overestimates the function. Gradient descent relies on exactly this property.

The second-derivative test

If ff is twice differentiable on an interval, then ff is convex on that interval if and only if

f′′(x)≥0for every x in the interval.f''(x) \ge 0 \quad \text{for every } x \text{ in the interval}.

Why this works: f′′f'' measures how the slope changes as you move right. If the slope never decreases, the curve keeps bending upward, which is the bowl shape. If f′′(x)>0f''(x) \gt 0 everywhere, the function is strictly convex. The converse fails: x4x^4 is strictly convex, yet its second derivative is zero at x=0x = 0.

Local minima are global minima

This is the key result for optimisation. If ff is convex and x⋆x^\star is a local minimum, then x⋆x^\star is a global minimum.

Proof sketch. Suppose some point yy has f(y)<f(x⋆)f(y) \lt f(x^\star). For λ\lambda close to 1, the blended point zλ=λx⋆+(1−λ)yz_\lambda = \lambda x^\star + (1 - \lambda) y is close to x⋆x^\star. By convexity,

f(zλ)≤λf(x⋆)+(1−λ)f(y)<λf(x⋆)+(1−λ)f(x⋆)=f(x⋆).f(z_\lambda) \le \lambda f(x^\star) + (1 - \lambda) f(y) \lt \lambda f(x^\star) + (1 - \lambda) f(x^\star) = f(x^\star).

So points arbitrarily close to x⋆x^\star have lower values than x⋆x^\star. That contradicts x⋆x^\star being a local minimum.

Building convex functions

Convexity is preserved by these operations, which lets you check a complicated function by splitting it into simple parts:

  • A sum of convex functions is convex.
  • A non-negative multiple of a convex function is convex.
  • The pointwise maximum of convex functions is convex.

So (w−3)2+∣w∣(w - 3)^2 + |w| is convex, because both terms are convex.

Worked examples

Example
A tiny check with f(x) = x squared

Use x=1x = 1, y=3y = 3 and λ=12\lambda = \tfrac{1}{2}.

  1. Blended point: 12⋅1+12⋅3=2\tfrac{1}{2} \cdot 1 + \tfrac{1}{2} \cdot 3 = 2.
  2. Left side: f(2)=22=4f(2) = 2^2 = 4.
  3. Right side: 12f(1)+12f(3)=12⋅1+12⋅9=5\tfrac{1}{2} f(1) + \tfrac{1}{2} f(3) = \tfrac{1}{2} \cdot 1 + \tfrac{1}{2} \cdot 9 = 5.
  4. Check: 4≤54 \le 5, so the inequality holds for this pair.

One pair is not a proof. The chord picture above shows why it holds for every pair. The gap of 1 is the vertical distance between the chord and the curve at the midpoint.

Example
Exponential: the gap between chord and curve

Use x=0x = 0, y=2y = 2 and λ=14\lambda = \tfrac{1}{4}, so the blend puts weight 14\tfrac{1}{4} on xx and 34\tfrac{3}{4} on yy.

  1. Blended point: 14⋅0+34⋅2=1.5\tfrac{1}{4} \cdot 0 + \tfrac{3}{4} \cdot 2 = 1.5.
  2. Left side: f(1.5)=e1.5≈4.482f(1.5) = e^{1.5} \approx 4.482.
  3. Right side: 14e0+34e2≈0.25+0.75⋅7.389≈5.792\tfrac{1}{4} e^0 + \tfrac{3}{4} e^2 \approx 0.25 + 0.75 \cdot 7.389 \approx 5.792.
  4. Check: 4.482≤5.7924.482 \le 5.792, so the inequality holds.

The gap is 5.792−4.482≈1.315.792 - 4.482 \approx 1.31. This is how far the chord sits above the curve at that blend.

Example
Averaging predictions in a log loss

Let f(p)=−ln⁡pf(p) = -\ln p for p∈(0,1]p \in (0, 1]. This is the loss for a single prediction that assigns probability pp to the correct answer. It is convex. Take two predictions p1=0.9p_1 = 0.9 and p2=0.1p_2 = 0.1, with λ=12\lambda = \tfrac{1}{2}.

  1. Blended prediction: 12⋅0.9+12⋅0.1=0.5\tfrac{1}{2} \cdot 0.9 + \tfrac{1}{2} \cdot 0.1 = 0.5.
  2. Loss of the blend: f(0.5)=ln⁡2≈0.693f(0.5) = \ln 2 \approx 0.693.
  3. Average of the two losses: 12(−ln⁡0.9)+12(−ln⁡0.1)≈0.053+1.151≈1.204\tfrac{1}{2} (-\ln 0.9) + \tfrac{1}{2} (-\ln 0.1) \approx 0.053 + 1.151 \approx 1.204.
  4. Check: 0.693≤1.2040.693 \le 1.204.

Averaging the predictions first gives a lower loss than averaging the losses. This is Jensen's inequality for a convex function, and it is why mixing forecasts tends to score well.

Tradeoffs and failure modes

Convexity gives strong guarantees, and those guarantees only hold when the function really is convex.

SituationWhat goes wrongBetter choice
A deep neural network lossMany local minima and saddle points. The "local is global" guarantee is gone.Accept a good local minimum, use several restarts, or tune the optimiser
A non-smooth convex function such as ∣x∣\lvert x \rvertNo derivative at the kink, so plain gradient descent can zigzagSubgradient methods, or smooth the function slightly
Checking convexity only near one pointA local bowl is mistaken for a global oneCheck f′′f'' over the whole domain, or check the definition directly
Using a convex function where the domain is not convexThe definition does not apply, and the inequality can failRestrict to a convex subset first
Expecting uniqueness of the minimiserConvex functions can have a flat bottom with many minimisersUse strict convexity when a unique answer matters

When you design a model, prefer a convex loss if the problem allows it. Logistic regression, linear regression and lasso all have convex losses, so their training has no false valleys. Use a non-convex model only when its extra expressive power is worth losing those guarantees.

History

The inequality that makes convexity useful for averages was published by the Danish mathematician Johan Jensen in 1906, in a paper on convex functions and mean values in Acta Mathematica. Earlier inequalities of Hölder and Minkowski from the late nineteenth century already relied on similar ideas.

Convexity became a field of its own in the middle of the twentieth century. Werner Fenchel's early-1950s lecture notes helped organise it, and R. T. Rockafellar's book Convex Analysis (1970) became the standard reference. Convexity moved into machine learning and engineering mainly in the 2000s, when convex optimisation became practical for large problems. Boyd and Vandenberghe's textbook (2004) is the most widely used introduction.

Check yourself

QuestionRecall: what does the convexity inequality compare on each side?Show answer

The left side is the value of ff at a blend λx+(1−λ)y\lambda x + (1 - \lambda) y of two points. The right side is the same blend of the two values, λf(x)+(1−λ)f(y)\lambda f(x) + (1 - \lambda) f(y). Convexity says the function at the blend is at most the blend of the function values, so the graph never rises above the chord.

QuestionCompute: let f(x) = 2x squared + 3x. Take x = 0, y = 4 and lambda = 1/2. Find both sides of the inequality.Show answer

The blended point is 12⋅0+12⋅4=2\tfrac{1}{2} \cdot 0 + \tfrac{1}{2} \cdot 4 = 2, and f(2)=2⋅4+6=14f(2) = 2 \cdot 4 + 6 = 14. The right side is 12f(0)+12f(4)=12⋅0+12⋅(32+12)=22\tfrac{1}{2} f(0) + \tfrac{1}{2} f(4) = \tfrac{1}{2} \cdot 0 + \tfrac{1}{2} \cdot (32 + 12) = 22. So 14≤2214 \le 22, as convexity requires.

QuestionWhy must a local minimum of a convex function also be a global minimum?Show answer

Suppose a local minimum x⋆x^\star is beaten by some point yy. Blending yy with x⋆x^\star gives points near x⋆x^\star whose values, by convexity, are below f(x⋆)f(x^\star). Those points would then be lower than x⋆x^\star in a neighbourhood of it, which contradicts x⋆x^\star being a local minimum. So no lower point can exist, and the local minimum is the global one.

QuestionApplied: is the loss f(w) = (w - 3) squared + |w| convex, and why does that matter for gradient descent?Show answer

Yes. (w−3)2(w - 3)^2 is convex, ∣w∣|w| is convex, and a sum of convex functions is convex. Because the loss is convex, gradient descent cannot get trapped in a false valley: any local minimum is the global one. The kink at w=0w = 0 means you need a subgradient rather than an ordinary derivative there.

QuestionSecond derivative: is f(x) = x cubed convex on the interval [0, infinity)? On all of the real line?Show answer

On [0,∞)[0, \infty), f′′(x)=6x≥0f''(x) = 6x \ge 0, so ff is convex there. On the whole real line it is not convex, because f′′(−1)=−6<0f''(-1) = -6 \lt 0, so the curve bends downward for negative xx.

Sources