Skip to content
LeetQuant
E05 Advanced Optimisation

E05 · Advanced Optimisation · Lesson 1 of 4

Convexity

Convex sets and functions, the operations that preserve convexity, strong convexity and smoothness with the convergence rates they buy, and how to recognise (or reformulate) portfolio and regression problems as convex programmes.

18 min

Why convexity is the dividing line

The earlier lesson on gradients and Hessians showed the headline fact: for a convex function every local minimum is global and a zero gradient certifies optimality. This lesson is about using that fact in practice. In optimisation the useful split is not linear versus nonlinear but convex versus nonconvex: convex problems with thousands of variables solve reliably in milliseconds with a certificate of optimality, while nonconvex ones (even a quadratic with a cardinality constraint) are NP-hard in general.

So the practical skill is recognition: given a portfolio or estimation problem, can you show it is convex by composing known pieces, and if it is not, can you reformulate it so that it is?

Objective: build convex functions and sets from a small set of rules, use strong convexity and smoothness to predict convergence and certify accuracy, and convert standard quant problems (tracking error, CVaR, maximum Sharpe, risk parity) into convex form.

Convex sets

A set CC is convex if it contains the segment between any two of its points: x,y∈Cx, y \in C and θ∈[0,1]\theta \in [0, 1] imply θx+(1−θ)y∈C\theta x + (1 - \theta)y \in C.

ConvexWhy
Hyperplane a⊤x=ba^\top x = b, half-space a⊤x≤ba^\top x \le blinear
Polyhedron {x:Ax≤b, Cx=d}\{x : Ax \le b,\ Cx = d\}intersection of half-spaces
Norm ball {x:∥x−x0∥≤r}\{x : \|x - x_0\| \le r\}triangle inequality
Ellipsoid {x:x⊤Σx≤c}\{x : x^\top\Sigma x \le c\}, Σ⪰0\Sigma \succeq 0a norm ball in disguise
PSD cone {S:S⪰0}\{S : S \succeq 0\}v⊤(θA+(1−θ)B)v≥0v^\top(\theta A + (1 - \theta)B)v \ge 0
Sublevel set {x:f(x)≤t}\{x : f(x) \le t\} of a convex fffrom the definition of convex ff

Intersections of convex sets are convex, which is why stacking linear constraints, norm bounds and risk limits keeps a problem convex. The long-only, fully invested set {w:w≥0, 1⊤w=1}\{w : w \ge 0,\ \mathbf 1^\top w = 1\} is the simplex; adding sector caps, a leverage bound ∥w∥1≤2\|w\|_1 \le 2 or a risk limit w⊤Σw≤σmax⁡2w^\top\Sigma w \le \sigma^2_{\max} keeps it convex.

The constraints that break convexity are the ones a portfolio manager asks for most often:

  • Cardinality, "at most 20 names": the set of vectors with at most kk nonzeros is a union of subspaces. The midpoint of (1,0)(1, 0) and (0,1)(0, 1) has two nonzeros.
  • Minimum position size, "wi=0w_i = 0 or wi≥1%w_i \ge 1\%": a gap in the middle.
  • A risk target as an equality, w⊤Σw=σ2w^\top\Sigma w = \sigma^2: the surface of an ellipsoid. The midpoint of ww and −w-w is 00. Replace it by ≤\le (usually binding anyway if you maximise return).
  • A lower bound on a convex function, f(w)≥cf(w) \ge c: a superlevel set of a convex function is generally not convex.

These are handled by mixed-integer solvers, heuristics (solve the convex relaxation, then round and re-solve), or by moving them into the objective.

Convex functions and the rules that preserve them

Recall the definition and its smooth equivalents: f(θx+(1−θ)y)≤θf(x)+(1−θ)f(y)f(\theta x + (1 - \theta)y) \le \theta f(x) + (1 - \theta)f(y); equivalently f(y)≥f(x)+∇f(x)⊤(y−x)f(y) \ge f(x) + \nabla f(x)^\top(y - x); equivalently ∇2f⪰0\nabla^2 f \succeq 0 everywhere. Checking a Hessian is often painful. Instead, build ff from atoms with rules that preserve convexity:

  1. Non-negative weighted sums: ∑iaifi\sum_i a_i f_i with ai≥0a_i \ge 0.
  2. Affine pre-composition: f(Ax+b)f(Ax + b) is convex if ff is. So ∥Ax−b∥\|Ax - b\|, ∥Σ1/2(w−wb)∥2\|\Sigma^{1/2}(w - w_b)\|_2 and log⁡(1+e−y x⊤β)\log(1 + e^{-y\,x^\top\beta}) are convex.
  3. Pointwise maximum or supremum: max⁡ifi(x)\max_i f_i(x), or sup⁡sf(x,s)\sup_{s} f(x, s) over any index set. A worst case over scenarios of a convex loss is convex.
  4. Composition: h(g(x))h(g(x)) is convex if hh is convex and non-decreasing and gg is convex (so ege^{g} and max⁡(0,g)2\max(0, g)^2 are convex). With hh convex non-increasing, gg must be concave.
  5. Partial minimisation: if f(x,t)f(x, t) is jointly convex in (x,t)(x, t), then min⁡tf(x,t)\min_t f(x, t) is convex in xx.

Atoms: affine functions, x⊤Axx^\top Ax with A⪰0A \succeq 0, any norm, exe^x, −log⁡x-\log x, xlog⁡xx\log x, log⁡∑iexi\log\sum_i e^{x_i}, max⁡(0,x)\max(0, x), and x2/yx^2/y for y>0y > 0.

Two quant-relevant applications:

  • Tracking error (w−wb)⊤Σ(w−wb)=∥Σ1/2(w−wb)∥2\sqrt{(w - w_b)^\top\Sigma(w - w_b)} = \|\Sigma^{1/2}(w - w_b)\|_2 is a norm of an affine map, so "tracking error at most 2%" is a convex (second-order cone) constraint.
  • CVaR at level α\alpha over scenarios r1,…,rSr_1, \dots, r_S is (Rockafellar–Uryasev) CVaR⁡α(w)=min⁡t {t+1(1−α)S∑s=1Smax⁡(0, −rs⊤w−t)}.\operatorname{CVaR}_\alpha(w) = \min_t\ \Big\{ t + \frac{1}{(1 - \alpha)S}\sum_{s=1}^S \max\big(0,\ -r_s^\top w - t\big) \Big\} . The bracket is jointly convex in (w,t)(w, t) (rules 1 to 3), so by rule 5 CVaR is convex in ww, and minimising it is a linear programme after introducing one slack per scenario. VaR, a quantile, is not convex in ww and not even subadditive; that is a structural reason risk desks optimise CVaR rather than VaR.

Worked example

convex or not?

Decide whether each is convex in ww (or β\beta), naming the rule. (a) f(w)=γ2w⊤Σw−μ⊤w+κ∥w−w0∥1f(w) = \tfrac{\gamma}{2}w^\top\Sigma w - \mu^\top w + \kappa\|w - w_0\|_1 with γ,κ≥0\gamma, \kappa \ge 0 and Σ⪰0\Sigma \succeq 0. (b) The logistic loss ∑ilog⁡(1+e−yixi⊤β)\sum_i \log(1 + e^{-y_i x_i^\top\beta}). (c) Portfolio volatility w⊤Σw\sqrt{w^\top\Sigma w}. (d) The constraint w⊤Σw≥5%\sqrt{w^\top\Sigma w} \ge 5\%. (e) f(w)=−w⊤Σwf(w) = -\sqrt{w^\top\Sigma w}.

Show solution

(a) Convex. A PSD quadratic, plus a linear term, plus κ\kappa times a norm of an affine map (rules 1 and 2). This is mean–variance with linear transaction costs.

(b) Convex. log⁡(1+ez)=log⁡(e0+ez)\log(1 + e^{z}) = \log(e^0 + e^z) is log-sum-exp of (0,z)(0, z), an atom; z=−yixi⊤βz = -y_i x_i^\top\beta is affine in β\beta (rule 2); sum over ii (rule 1).

(c) Convex. w⊤Σw=∥Σ1/2w∥2\sqrt{w^\top\Sigma w} = \|\Sigma^{1/2}w\|_2, a norm of a linear map. Note that w⊤Σww^\top\Sigma w is convex too, but the square root of a general convex function need not be (x\sqrt{x} is concave and increasing, so rule 4 does not apply); here it works only because the result is a norm.

(d) Not convex. It is a superlevel set of a convex function: the complement of an open ellipsoid. Both ww and −w-w satisfy it, their midpoint 00 does not.

(e) Concave, not convex. It is the negative of a norm. Minimising it means maximising volatility, which is unbounded or ends up at a vertex of the feasible set.

The working method is to name the atoms and the rule for each operation. If you cannot, try a counterexample with a midpoint.

Strong convexity and smoothness

Convexity says local equals global; it says nothing about speed. Two constants do:

  • ff is mm-strongly convex if f(y)≥f(x)+∇f(x)⊤(y−x)+m2∥y−x∥2f(y) \ge f(x) + \nabla f(x)^\top(y - x) + \tfrac m2\|y - x\|^2: it curves up at least as fast as a quadratic of curvature mm.
  • ff is LL-smooth if ∇f\nabla f is LL-Lipschitz, equivalently f(y)≤f(x)+∇f(x)⊤(y−x)+L2∥y−x∥2f(y) \le f(x) + \nabla f(x)^\top(y - x) + \tfrac L2\|y - x\|^2.

For twice-differentiable ff both together mean mI⪯∇2f(x)⪯LImI \preceq \nabla^2 f(x) \preceq LI everywhere, and κ=L/m\kappa = L/m is the condition number.

Three consequences, each a two-line derivation.

  1. Suboptimality from the gradient. Minimise the right side of the strong-convexity inequality over yy (the minimiser is y=x−∇f(x)/my = x - \nabla f(x)/m): f∗≥f(x)−∥∇f(x)∥2/(2m)f^* \ge f(x) - \|\nabla f(x)\|^2/(2m). So f(x)−f∗≤∥∇f(x)∥22m,∥x−x∗∥≤∥∇f(x)∥m.f(x) - f^* \le \frac{\|\nabla f(x)\|^2}{2m}, \qquad \|x - x^*\| \le \frac{\|\nabla f(x)\|}{m} . A small gradient is a certificate of near-optimality only when m>0m > 0.
  2. Descent lemma. Put y=x−∇f(x)/Ly = x - \nabla f(x)/L in the smoothness inequality: one gradient step of size 1/L1/L lowers ff by at least ∥∇f(x)∥2/(2L)\|\nabla f(x)\|^2/(2L).
  3. Linear convergence. Combine the two: f(xk+1)−f∗≤f(xk)−f∗−∥∇f(xk)∥2/(2L)≤(1−m/L)(f(xk)−f∗)f(x_{k+1}) - f^* \le f(x_k) - f^* - \|\nabla f(x_k)\|^2/(2L) \le (1 - m/L)\big(f(x_k) - f^*\big), so f(xk)−f∗≤(1−1κ)k(f(x0)−f∗).f(x_k) - f^* \le \Big(1 - \frac1\kappa\Big)^k\big(f(x_0) - f^*\big) . This is the general (non-quadratic) version of the quadratic rate in the gradient-descent lesson.

Without strong convexity (m=0m = 0), gradient descent still converges but only sublinearly, f(xk)−f∗≤L∥x0−x∗∥2/(2k)f(x_k) - f^* \le L\|x_0 - x^*\|^2/(2k), and the minimiser may be non-unique or may not exist at all: logistic regression on linearly separable data is convex but its infimum is approached only as ∥β∥→∞\|\beta\| \to \infty.

Regularisation is the standard fix. Adding λ2∥β∥2\tfrac\lambda2\|\beta\|^2 adds λ\lambda to every Hessian eigenvalue, so m→m+λm \to m + \lambda and L→L+λL \to L + \lambda. Lasso's ℓ1\ell_1 penalty does not add strong convexity (it is piecewise linear), which is why lasso solutions can be non-unique when p>np > n and why the elastic net adds a small ridge term.

Worked example

what ridge buys an optimiser

A least-squares problem f(β)=12n∥y−Xβ∥2f(\beta) = \tfrac1{2n}\|y - X\beta\|^2 has Hessian eigenvalues between 0.050.05 and 44. Compare gradient descent with step 1/L1/L on the raw problem and with a ridge penalty 0.152∥β∥2\tfrac{0.15}{2}\|\beta\|^2: how many steps does the bound (1−1/κ)k(1 - 1/\kappa)^k need to reach a 10−610^{-6} reduction in suboptimality? On the ridge problem, a solver stops with ∥∇f∥=10−3\|\nabla f\| = 10^{-3}. What can you guarantee?

Show solution

Raw: m=0.05m = 0.05, L=4L = 4, κ=80\kappa = 80. Steps: k≥ln⁡(10−6)/ln⁡(1−1/80)≈1,099k \ge \ln(10^{-6})/\ln(1 - 1/80) \approx 1{,}099.

Ridge: m=0.2m = 0.2, L=4.15L = 4.15, κ=20.75\kappa = 20.75. Steps: k≥ln⁡(10−6)/ln⁡(1−1/20.75)≈280k \ge \ln(10^{-6})/\ln(1 - 1/20.75) \approx 280, about four times fewer.

Certificate: with m=0.2m = 0.2,

f(β)−f∗≤(10−3)22×0.2=2.5×10−6,∥β−β∗∥≤10−30.2=0.005.f(\beta) - f^* \le \frac{(10^{-3})^2}{2 \times 0.2} = 2.5 \times 10^{-6}, \qquad \|\beta - \beta^*\| \le \frac{10^{-3}}{0.2} = 0.005 .

The same gradient norm on the raw problem only guarantees f−f∗≤10−5f - f^* \le 10^{-5} and a distance of 0.020.02. A stopping rule based on gradient norm is only meaningful once you know (or bound) mm.

The statistical side is separate: ridge changes the estimator (adds bias, cuts variance). The optimisation side is that it makes every algorithm faster and the solution unique.

Recognising convex formulations

ProblemConvex formClass
OLS, ridge∥y−Xβ∥2+λ∥β∥2\|y - X\beta\|^2 + \lambda\|\beta\|^2unconstrained QP
Lasso∥y−Xβ∥2+λ∥β∥1\|y - X\beta\|^2 + \lambda\|\beta\|_1QP after splitting β=β+−β−\beta = \beta^+ - \beta^-
Quantile / LAD regression∑iρτ(yi−xi⊤β)\sum_i \rho_\tau(y_i - x_i^\top\beta)LP
Logistic regressionlog-sum-exp of affine mapssmooth convex
Minimum variance, mean–varianceγ2w⊤Σw−μ⊤w\tfrac\gamma2 w^\top\Sigma w - \mu^\top w, linear constraintsQP
Tracking-error or volatility limit∥Σ1/2(w−wb)∥2≤τ\|\Sigma^{1/2}(w - w_b)\|_2 \le \tauSOCP
Minimum CVaRRockafellar–Uryasev with scenario slacksLP
Maximum Sharpe ratiochange of variables (below)QP
Risk paritymin⁡y12y⊤Σy−∑ibilog⁡yi\min_y \tfrac12 y^\top\Sigma y - \sum_i b_i\log y_i (below)smooth strictly convex
Cardinality, lot sizes, VaRnone in generalmixed-integer / nonconvex

The classes nest, LP ⊂\subset QP ⊂\subset SOCP ⊂\subset SDP, and each has interior-point methods that solve to high accuracy in a few dozen iterations, each costing a linear solve. When you can name the class, you can name the solver.

Maximum Sharpe. The ratio μ⊤w/w⊤Σw\mu^\top w/\sqrt{w^\top\Sigma w} is not concave, but it is quasi-concave: for s≥0s \ge 0 its superlevel set {w:μ⊤w≥s∥Σ1/2w∥2}\{w : \mu^\top w \ge s\|\Sigma^{1/2}w\|_2\} is a convex cone. It is also scale-invariant, so fix the numerator instead of the denominator: solve

min⁡y y⊤Σys.t.μ⊤y=1  (plus Ay≤b 1⊤y for homogeneous linear constraints, and y≥0 if long-only),\min_y\ y^\top\Sigma y \quad \text{s.t.}\quad \mu^\top y = 1 \ \ (\text{plus } Ay \le b\,\mathbf 1^\top y \text{ for homogeneous linear constraints, and } y \ge 0 \text{ if long-only}),

then set w=y/1⊤yw = y/\mathbf 1^\top y. This is a convex QP, valid whenever some portfolio has positive expected excess return.

Risk parity. Equal risk contributions wi(Σw)i=w⊤Σw/nw_i(\Sigma w)_i = w^\top\Sigma w/n look like a system of nonlinear equations. But the stationarity condition of the strictly convex problem min⁡y>012y⊤Σy−∑ibilog⁡yi\min_{y > 0} \tfrac12 y^\top\Sigma y - \sum_i b_i\log y_i is (Σy)i=bi/yi(\Sigma y)_i = b_i/y_i, i.e. yi(Σy)i=biy_i(\Sigma y)_i = b_i: risk contributions proportional to the budgets bib_i. Normalising yy preserves the proportions, so the unique long-only risk-budgeting portfolio is the solution of a convex problem.

import numpy as np
from scipy.optimize import minimize

vol = np.array([0.10, 0.20, 0.30])
corr = np.array([[1.0, 0.2, 0.4], [0.2, 1.0, 0.5], [0.4, 0.5, 1.0]])
Sigma = np.outer(vol, vol) * corr
b = np.ones(3) / 3  # equal risk budgets

def f(y):
    return 0.5 * y @ Sigma @ y - b @ np.log(y)

def grad(y):
    return Sigma @ y - b / y

res = minimize(f, x0=np.ones(3), jac=grad, method="L-BFGS-B", bounds=[(1e-9, None)] * 3)
w = res.x / res.x.sum()
rc = w * (Sigma @ w) / (w @ Sigma @ w)  # fractional risk contributions
print("weights:      ", np.round(w, 4))
print("risk shares:  ", np.round(rc, 4))
print("inverse vol:  ", np.round((1 / vol) / (1 / vol).sum(), 4))
weights:       [0.5636 0.2715 0.1649]
risk shares:   [0.3333 0.3333 0.3333]
inverse vol:   [0.5455 0.2727 0.1818]

Inverse-volatility weights would be exact risk parity only if all correlations were equal; here asset 3 is more correlated with the others, so it gets less than its inverse-vol share.

Worked example

maximum Sharpe as a QP

Two assets have excess returns μ=(8%,4%)\mu = (8\%, 4\%), volatilities 20%20\% and 10%10\% and correlation 0.30.3. Find the maximum-Sharpe portfolio by the convex reformulation and confirm it beats each asset alone.

Show solution

Σ=(0.040.0060.0060.01)\Sigma = \begin{pmatrix} 0.04 & 0.006 \\ 0.006 & 0.01 \end{pmatrix}, det⁡Σ=0.0004−0.000036=0.000364\det\Sigma = 0.0004 - 0.000036 = 0.000364.

Solve min⁡y⊤Σy\min y^\top\Sigma y s.t. μ⊤y=1\mu^\top y = 1. The Lagrangian condition 2Σy=λμ2\Sigma y = \lambda\mu gives y∝Σ−1μy \propto \Sigma^{-1}\mu:

Σ−1μ=10.000364(0.01×0.08−0.006×0.04−0.006×0.08+0.04×0.04)=10.000364(0.000560.00112)=(1.5383.077).\Sigma^{-1}\mu = \frac{1}{0.000364}\begin{pmatrix} 0.01 \times 0.08 - 0.006 \times 0.04 \\ -0.006 \times 0.08 + 0.04 \times 0.04 \end{pmatrix} = \frac{1}{0.000364}\begin{pmatrix} 0.00056 \\ 0.00112 \end{pmatrix} = \begin{pmatrix} 1.538 \\ 3.077 \end{pmatrix} .

With C=μ⊤Σ−1μ=0.08×1.538+0.04×3.077=16/65≈0.2462C = \mu^\top\Sigma^{-1}\mu = 0.08 \times 1.538 + 0.04 \times 3.077 = 16/65 \approx 0.2462, the constraint fixes y=Σ−1μ/C=(6.25, 12.5)y = \Sigma^{-1}\mu/C = (6.25,\ 12.5), and y⊤Σy=1/C=4.0625y^\top\Sigma y = 1/C = 4.0625.

Normalise: w=y/1⊤y=(1/3, 2/3)w = y/\mathbf 1^\top y = (1/3,\ 2/3). Its expected excess return is 5.33%5.33\%, volatility 10.75%10.75\%, and Sharpe ratio

μ⊤ww⊤Σw=C=16/65≈0.496.\frac{\mu^\top w}{\sqrt{w^\top\Sigma w}} = \sqrt{C} = \sqrt{16/65} \approx 0.496 .

Each asset alone has Sharpe 0.08/0.2=0.04/0.1=0.400.08/0.2 = 0.04/0.1 = 0.40; the 50/50 mix has 0.4820.482. The reformulation works because the Sharpe ratio is unchanged by scaling ww, so we may fix μ⊤y=1\mu^\top y = 1 and minimise risk: the most return per unit of risk is the least risk per unit of return.

Knowledge check

Which of these portfolio constraint sets is NOT convex?

Knowledge check

A loss is 0.20.2-strongly convex. Your solver stops at a point where ∥∇f∥=10−3\|\nabla f\| = 10^{-3}. What is the best guaranteed bound on f(x)−f∗f(x) - f^*?

Knowledge check

Let gg be convex but able to take negative values. Which composition is guaranteed convex?

Knowledge check

Why can maximum-Sharpe be solved as a convex QP even though the Sharpe ratio is not a concave function of the weights?

Key takeaways

  • Prove convexity by composition: non-negative sums, affine pre-composition, pointwise maxima, monotone composition, and partial minimisation. Disprove it with a midpoint.
  • Intersections of convex sets are convex. Cardinality, minimum lot sizes, VaR limits and risk equalities are the usual convexity breakers.
  • Strong convexity mm and smoothness LL give f−f∗≤∥∇f∥2/(2m)f - f^* \le \|\nabla f\|^2/(2m) and linear convergence at rate 1−1/κ1 - 1/\kappa; without m>0m > 0 convergence is sublinear and minimisers may not be unique or exist. Ridge adds λ\lambda to mm.
  • Many quant problems are convex after reformulation: tracking-error limits are SOCP, CVaR is an LP, maximum Sharpe is a QP after fixing μ⊤y=1\mu^\top y = 1, and risk parity is a strictly convex log-barrier problem.

Practice problems

Finished reading?

Answer the 4 knowledge checks to complete this lesson (0 done).