Skip to content
Module 07 of 1060 min readAdvanced

Quadratic programming and mean-variance

The QP. Markowitz mean-variance as a QP. Adding constraints — long-only, sector caps, turnover. Why the unconstrained MV problem is closed-form.

70%

Listen along

Read “Quadratic programming and mean-variance” aloud

Plays in your browser using on-device text-to-speech — nothing leaves the page.

Quadratic programming is the single most important optimisation family in portfolio finance. Markowitz mean-variance, ridge regression, constrained linear regression, SVMs, transaction-cost optimisation — all reduce to QPs. Modern interior-point QP solvers handle problems with tens of thousands of variables in seconds.

QP standard form

math
minimize (1/2) xᵀ P x + qᵀ x
subject to A x ≤ b (inequalities)
E x = f (equalities)

P is symmetric. If P is PSD, the QP is convex. If P is indefinite, the problem is non-convex and NP-hard in general; finance QPs are almost always convex by construction.

Markowitz as QP

math
min (1/2) wᵀΣw - λ μᵀw
s.t. 1ᵀw = 1
(optional) w ≥ 0 (long-only)
(optional) L ≤ Bw ≤ U (sector / factor exposures)

λ is the risk-aversion parameter. As λ → 0, you minimise variance (minimum-variance portfolio). As λ → ∞, you maximise return (single-asset concentration). Sweeping λ traces the efficient frontier.

Closed-form solution for unconstrained Markowitz

Without inequality constraints, the FOC is Σw* = λ μ + ν 1 (from the budget Lagrangian). Solving:

math
w* = [Σ⁻¹ μ (1ᵀΣ⁻¹μ - Σ-mean adjustments)] / (...)

Specifically, w* = (1/c)[Σ⁻¹ μ - (B/A) Σ⁻¹ 1] + (1/A) Σ⁻¹ 1, where A = 1ᵀΣ⁻¹1, B = 1ᵀΣ⁻¹μ, C = μᵀΣ⁻¹μ, with the parameters varying to trace the frontier. The two-fund theorem follows: every efficient portfolio is a linear combination of two fixed efficient portfolios (e.g., the minimum-variance and the global tangency).

Constrained Markowitz — no closed form

With w ≥ 0 (long-only), sector caps, turnover bounds, etc., no closed form exists. The problem is solved numerically — typically by an active-set or interior-point QP solver. For n in the hundreds, these solve in milliseconds; for n = 10⁴-10⁵ with sparse structure, in seconds.

Transaction-cost models

Define current weights w_0 and target weights w. Linear transaction cost: c|w - w_0|, where c is per-unit-trade cost. Quadratic transaction cost (price impact): (1/2) (w - w_0)ᵀ M (w - w_0). Adding to Markowitz:

math
min (1/2) wᵀΣw - λ μᵀw + (1/2) (w - w_0)ᵀ M (w - w_0)
s.t. 1ᵀw = 1, (other constraints)

Linear t-costs make it a QP-with-L¹-term, often converted to a QP by introducing slack variables. Quadratic t-costs simply add a quadratic to the objective — still a pure QP. Both are standard in production code.

Ridge regression — the textbook QP

math
β̂_ridge = argmin ‖y - Xβ‖² + α ‖β‖² = (XᵀX + αI)⁻¹ Xᵀy

Same first-order structure as Markowitz; the (XᵀX + αI) instead of Σ has the regularising effect. Closed-form, scalable, robust to multicollinearity. The Bayesian interpretation: posterior mean under Gaussian prior with precision α/σ².

Solvers and warm-starting

OSQP (Stellato et al., 2020) is the dominant open-source QP solver: ADMM-based, handles sparse problems, supports warm-starting. For sweeping the efficient frontier (many QPs with slightly different λ), warm-starting from the previous solution typically reduces solve time by 10-100×. Quadprog, qpsolvers, cvxpy all wrap OSQP and similar engines.

Exercise

A 3-asset universe has Σ = diag(0.04, 0.09, 0.16), μ = (8%, 10%, 13%). (1) Compute the minimum-variance portfolio. (2) Compute the maximum-Sharpe portfolio with r_f = 3%. (3) Verify both lie on the efficient frontier.

Loading progress…
LeadAfrikPublic Economics Hub