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.
- f(x) = x^2
- chord from x = -1 to x = 2
The math
Let be a function defined on a convex set . Then is convex if, for every and every ,
It is strictly convex if the inequality is strict whenever and . A function is concave when is convex, which reverses the inequality.
In words: the value of at a blend of two points is at most the same blend of the two values. The weight runs from 0 to 1. At the blended point is and both sides equal . At it is and both sides equal . In between, the blended point slides along the segment from to , and the right side traces the chord above it.
Convex functions lie above their tangents
If is differentiable (it has a Derivative everywhere), the tangent line at any point lies below the whole graph:
The left side is the true height at . The right side is the height of the tangent line at , evaluated at . 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 is twice differentiable on an interval, then is convex on that interval if and only if
Why this works: 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 everywhere, the function is strictly convex. The converse fails: is strictly convex, yet its second derivative is zero at .
Local minima are global minima
This is the key result for optimisation. If is convex and is a local minimum, then is a global minimum.
Proof sketch. Suppose some point has . For close to 1, the blended point is close to . By convexity,
So points arbitrarily close to have lower values than . That contradicts 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 is convex, because both terms are convex.
Worked examples
Use , and .
- Blended point: .
- Left side: .
- Right side: .
- Check: , 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.
Use , and , so the blend puts weight on and on .
- Blended point: .
- Left side: .
- Right side: .
- Check: , so the inequality holds.
The gap is . This is how far the chord sits above the curve at that blend.
Let for . This is the loss for a single prediction that assigns probability to the correct answer. It is convex. Take two predictions and , with .
- Blended prediction: .
- Loss of the blend: .
- Average of the two losses: .
- Check: .
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.
| Situation | What goes wrong | Better choice |
|---|---|---|
| A deep neural network loss | Many 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 | No derivative at the kink, so plain gradient descent can zigzag | Subgradient methods, or smooth the function slightly |
| Checking convexity only near one point | A local bowl is mistaken for a global one | Check over the whole domain, or check the definition directly |
| Using a convex function where the domain is not convex | The definition does not apply, and the inequality can fail | Restrict to a convex subset first |
| Expecting uniqueness of the minimiser | Convex functions can have a flat bottom with many minimisers | Use 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 at a blend of two points. The right side is the same blend of the two values, . 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 , and . The right side is . So , as convexity requires.
QuestionWhy must a local minimum of a convex function also be a global minimum?Show answer
Suppose a local minimum is beaten by some point . Blending with gives points near whose values, by convexity, are below . Those points would then be lower than in a neighbourhood of it, which contradicts 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. is convex, 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 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 , , so is convex there. On the whole real line it is not convex, because , so the curve bends downward for negative .