Convex Sets and Convex Functions
Convexity is the property that guarantees a local best answer is the global best answer — it is the single dividing line between optimisation problems you can solve reliably and ones you generally can't.
Prerequisites: Derivatives and Differentiation Rules, The Hessian and Second-Order Behavior
Ask an optimiser to find the minimum-risk portfolio and it will confidently return a number. Ask a different optimiser to find the best set of weights for a neural network and it will also confidently return a number — but only the first answer comes with a guarantee that it's genuinely the best possible one. The difference isn't the algorithm; it's a property of the problem itself, called convexity, and it is the single most important dividing line in all of optimisation: convex problems can be solved with certainty, non-convex ones generally can't be.
The analogy: a bowl versus an egg carton
Drop a marble into a mixing bowl and, wherever you drop it, it rolls down to the same single lowest point — the bottom of the bowl. There's exactly one "best" spot, and any local downhill-rolling strategy finds it. Now drop the same marble onto an egg carton. It rolls into whichever dimple it happens to land nearest, and that dimple might be far from shallowest one on the whole carton — the marble has no way of knowing, from where it sits, that a better dimple exists three cups over. A bowl is convex; an egg carton is not. Every optimisation algorithm is a marble rolling downhill, and convexity is exactly the guarantee that the shape it's rolling on has only one dimple — so "roll downhill until you stop" and "find the global best" become the same instruction.
Writing it down
A set is convex if, for any two points in it, the entire straight line segment between them is also in it:
In words: pick any two allowed points and walk straight between them — you never step outside the set. A disk is convex (any chord stays inside); a crescent moon shape is not (a chord across the tips cuts outside the boundary).
A function is convex if the straight line connecting any two points on its graph never dips below the graph itself:
In words: mixing two inputs and evaluating gives a result no worse (no higher) than mixing the two corresponding outputs — the function always curves upward or stays flat, never caves inward to hide a lower point off the straight-line path. When is twice differentiable, this has a simple test: everywhere (in higher dimensions, the Hessian matrix must be positive semidefinite — every direction curves upward, never downward).
The payoff is the single most important theorem in optimisation: for a convex function over a convex set, any local minimum is automatically the global minimum. There is no separate proof needed for each problem — convexity itself is the guarantee, which is exactly why the marble in the bowl never needs to worry about a better dimple existing elsewhere.
A parabola like the one here is the simplest convex function there is — one bottom, and moving away from it in either direction only ever increases the value. Contrast that mentally with a wavy curve that has several dips: that's the "egg carton" shape, non-convex, where a marble can get stuck in the wrong dip.
Worked example 1: checking convexity of a portfolio risk function, by hand
Portfolio variance as a function of weight in a two-asset portfolio (weight in the second asset) is . With , , : expand and collect terms in . The coefficient of works out to . So , a positive constant — convex everywhere, confirming portfolio variance is a convex (in fact quadratic, bowl-shaped) function of the weight, for any correlation strictly less than 1.
Test two candidate weights directly: and , and the midpoint , to see the defining inequality hold. . . Average of these: . Now . Since , the chord lies above the curve at the midpoint — exactly the convexity inequality, confirmed numerically.
Worked example 2: a non-convex counter-example, by hand
Take over — deliberately egg-carton shaped, with two humps. , which is positive near (convex-looking locally) but negative for — the sign flips, so is convex in no region wider than that, and definitely not convex over the whole interval.
Concretely: , , and the midpoint . The chord's average is , and — right on the boundary here, not a violation. But try and : , , chord average ; the midpoint is , . Since , the curve sits above the chord — violating the convexity inequality outright, confirming is genuinely non-convex, with two separate local maxima and a dip between them that a naive downhill search could get stuck straddling incorrectly, or a downhill search from the wrong start could miss entirely.
What this means in practice
- It's why mean-variance portfolio optimisation is trusted. Portfolio variance is a convex (quadratic) function of weights, and typical constraints (weights summing to 1, within bounds) form a convex set — so the standard optimiser reliably finds the true global minimum-risk portfolio, no restarts or luck required.
- It's why deep learning training has no such guarantee. A neural network's loss surface is famously non-convex, riddled with many local minima and saddle points — which is why training uses tricks like random restarts, momentum, and careful initialisation instead of trusting a single downhill run.
- It's the property duality and interior-point methods exploit. Both Lagrangian duality and efficient large-scale solvers depend on convexity to guarantee correctness and speed — without it, those tools lose their guarantees entirely.
A convex set never lets a straight line between two allowed points leave the set; a convex function's graph never dips below the straight line joining any two of its points. Together they guarantee the single most valuable property in optimisation: any local minimum found is automatically the global minimum — no other dimple to worry about.
The classic confusion is assuming a function "looks smooth and bowl-shaped near where I'm standing" means it's convex everywhere. Convexity is a global property — it must hold between every pair of points in the domain, not just locally around a current guess. The worked non-convex example above is locally convex-looking right near (positive second derivative there) while being globally non-convex a bit further out. The practical trap: running gradient descent, seeing it settle into a stable point, and declaring "found the optimum" — when in reality gradient descent only ever certifies a local minimum, and without an independent convexity check on the whole problem, there is no way to know from the algorithm's behaviour alone whether a better solution exists elsewhere on the surface.
Related concepts
Practice in interviews
Further reading
- Boyd & Vandenberghe, Convex Optimization (ch. 2-3)
- Rockafellar, Convex Analysis (ch. 1)