Quant Memo
Advanced

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 CC is convex if, for any two points in it, the entire straight line segment between them is also in it:

x,yC    λx+(1λ)yCfor all λ[0,1].x, y \in C \;\Rightarrow\; \lambda x + (1-\lambda) y \in C \quad \text{for all } \lambda \in [0,1] .

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 ff is convex if the straight line connecting any two points on its graph never dips below the graph itself:

f(λx+(1λ)y)λf(x)+(1λ)f(y)for all λ[0,1].f(\lambda x + (1-\lambda)y) \le \lambda f(x) + (1-\lambda) f(y) \quad \text{for all } \lambda \in [0,1] .

In words: mixing two inputs and evaluating ff 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 ff is twice differentiable, this has a simple test: f(x)0f''(x) \ge 0 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.

Function explorer
-2222.0
x = 1.00f(x) = 2.000

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.

convex: chord stays above non-convex: chord dips below
Left: a bowl, where the straight chord between any two points never dips below the curve — the convexity test passes everywhere. Right: an egg-carton curve, where a chord can cut below the curve between its two humps — convexity fails.

Worked example 1: checking convexity of a portfolio risk function, by hand

Portfolio variance as a function of weight ww in a two-asset portfolio (weight 1w1-w in the second asset) is f(w)=w2σ12+(1w)2σ22+2w(1w)ρσ1σ2f(w) = w^2\sigma_1^2 + (1-w)^2\sigma_2^2 + 2w(1-w)\rho\sigma_1\sigma_2. With σ1=0.2\sigma_1=0.2, σ2=0.3\sigma_2=0.3, ρ=0.5\rho=0.5: expand and collect terms in ww. The coefficient of w2w^2 works out to σ12+σ222ρσ1σ2=0.04+0.092(0.5)(0.2)(0.3)=0.130.06=0.07\sigma_1^2+\sigma_2^2-2\rho\sigma_1\sigma_2 = 0.04+0.09-2(0.5)(0.2)(0.3)=0.13-0.06=0.07. So f(w)=2(0.07)=0.14f''(w) = 2(0.07)=0.14, 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: f(0.3)f(0.3) and f(0.7)f(0.7), and the midpoint f(0.5)f(0.5), to see the defining inequality hold. f(0.3)=0.09(0.04)+0.49(0.09)+2(0.3)(0.7)(0.5)(0.2)(0.3)=0.0036+0.0441+0.0126=0.0603f(0.3) = 0.09(0.04)+0.49(0.09)+2(0.3)(0.7)(0.5)(0.2)(0.3) = 0.0036+0.0441+0.0126=0.0603. f(0.7)=0.49(0.04)+0.09(0.09)+2(0.7)(0.3)(0.5)(0.2)(0.3)=0.0196+0.0081+0.0126=0.0403f(0.7)=0.49(0.04)+0.09(0.09)+2(0.7)(0.3)(0.5)(0.2)(0.3)=0.0196+0.0081+0.0126=0.0403. Average of these: (0.0603+0.0403)/2=0.0503(0.0603+0.0403)/2=0.0503. Now f(0.5)=0.25(0.04)+0.25(0.09)+2(0.25)(0.03)=0.01+0.0225+0.015=0.0475f(0.5)=0.25(0.04)+0.25(0.09)+2(0.25)(0.03)=0.01+0.0225+0.015=0.0475. Since 0.04750.05030.0475 \le 0.0503, 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 g(w)=w4+w2g(w) = -w^4 + w^2 over w[1.5,1.5]w\in[-1.5,1.5] — deliberately egg-carton shaped, with two humps. g(w)=12w2+2g''(w) = -12w^2+2, which is positive near w=0w=0 (convex-looking locally) but negative for w>1/60.408|w|>\sqrt{1/6}\approx0.408 — the sign flips, so gg is convex in no region wider than that, and definitely not convex over the whole interval.

Concretely: g(1)=1+1=0g(-1)=-1+1=0, g(1)=1+1=0g(1)=-1+1=0, and the midpoint g(0)=0+0=0g(0) = 0+0=0. The chord's average is (0+0)/2=0(0+0)/2=0, and g(0)=0g(0)=0 — right on the boundary here, not a violation. But try g(1.2)g(-1.2) and g(0.2)g(0.2): g(1.2)=2.0736+1.44=0.6336g(-1.2)=-2.0736+1.44=-0.6336, g(0.2)=0.0016+0.04=0.0384g(0.2)=-0.0016+0.04=0.0384, chord average =0.2976=-0.2976; the midpoint is w=0.5w=-0.5, g(0.5)=0.0625+0.25=0.1875g(-0.5)=-0.0625+0.25=0.1875. Since 0.1875>0.29760.1875 > -0.2976, the curve sits above the chord — violating the convexity inequality outright, confirming gg 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 w=0w=0 (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)
ShareTwitterLinkedIn