fast-minimum-variance: Solving Minimum Variance Portfolios Fast
fast-minimum-variance solves the long-only minimum variance portfolio without ever
forming the sample covariance matrix. The key observation is that the KKT stationarity
condition
Working directly with the returns matrix
import numpy as np
from fast_minimum_variance import Problem
# 500 daily returns, 20 assets
X = np.random.default_rng(42).standard_normal((500, 20))
w, outer, inner = Problem(X).solve_cg() # matrix-free CG — recommended
assert abs(w.sum() - 1.0) < 1e-8
assert (w >= 0).all()Ledoit-Wolf shrinkage plays a dual role: statistically it reduces estimation error; numerically
it compresses the eigenvalue spectrum and directly cuts CG iteration counts. Use
alpha = N / (N + T) as a simple analytical estimate of the optimal shrinkage intensity:
T, N = X.shape
w, outer, inner = Problem(X, alpha=N / (N + T)).solve_cg()On S&P 500 equity data (495 assets, 1192 days), shrinkage cuts CG iterations from 685 to 205 and makes the matrix-free solver the fastest option by a wide margin.
All solvers are methods on Problem and return (w, outer_steps, inner_iters) where
| Method | Approach | When to use |
|---|---|---|
solve_cg() |
Matrix-free conjugate gradients on the SPD reduced system | Default — fastest for large |
solve_pcg() |
Matrix-free PCG with an RMT (low-rank) preconditioner | Eigenvalue-cleaned shrinkage targets (requires pcg_lr) |
The inner step builds a LinearOperator that applies
to a vector using two matrix-vector products with the active-asset submatrix
Runs the same matrix-free active-set iteration as solve_cg, but preconditions the
inner CG solve with the RMT low-rank target T0 (applied via the Woodbury identity at
pcg_lr = (bar_lam, U_k, delta_k) from RMT preprocessing
and returns the oracle-LW minimum-variance portfolio in far fewer iterations when the
shrinkage target has been eigenvalue-cleaned.
Build pcg_lr either from the dense rmt_target_and_alpha (full eigh) or, for large
rmt_preconditioner_rsvd, which recovers the top eigenpairs via a randomized
SVD of
from fast_minimum_variance.shrinkage.util import rmt_preconditioner_rsvd
pcg_lr = rmt_preconditioner_rsvd(X, n_components=16)
w, outer, inner = Problem(X, alpha=N / (N + T), pcg_lr=pcg_lr).solve_pcg()Long-only weights are enforced by an outer loop that wraps any inner solver:
-
Primal step. Solve the budget-only equality system over the current active asset
set. Drop any asset with weight below
$-\varepsilon$ (multiple assets at once if violations are large). -
Dual step. Once all active weights are non-negative, compute the gradient
$\nabla_i f(w) = 2[(1-\alpha)(X^\top X w)_i + \gamma w_i] - \rho\mu_i$ for every excluded asset. If any excluded asset has$\nabla_i f(w) < \lambda$ (the budget multiplier), it would decrease variance if added — re-insert the most-violated asset and repeat. - Termination. The loop exits when primal and dual feasibility hold simultaneously. Combined with stationarity from the inner solve, this is sufficient for global optimality.
With Ledoit-Wolf shrinkage at the analytically optimal
The same solver handles a range of portfolio construction problems by choosing
| Problem | alpha |
rho |
mu |
|---|---|---|---|
| Minimum variance | — | ||
| Mean-variance (Markowitz) | any | expected returns | |
| Minimum tracking error to benchmark |
any | X.T @ (X @ b) |
|
| LW-regularised minimum variance | — |
# Mean-variance
mu = np.random.default_rng(0).standard_normal(N) # expected returns, shape (N,)
w, *_ = Problem(X, rho=1.0, mu=mu).solve_cg()
# Minimum tracking error to benchmark b
b = np.ones(N) / N # equal-weight benchmark
mu_te = X.T @ (X @ b)
w, *_ = Problem(X, rho=2.0, mu=mu_te).solve_cg()When rho != 0, two SPD solves are performed per outer step:
To replace the default budget constraint (B, c):
B = np.zeros((2, N)); B[0, :N // 2] = 1.0; B[1, N // 2:] = 1.0 # each half holds...
c = np.array([0.5, 0.5]) # ...half of the budget
w, *_ = Problem(X, B=B, c=c).solve_cg()Long-only (B must have full row rank on every active set
the shrinking loop visits. Use this path only when you need it — the default path (no B,
c) is faster for the standard budget + long-only problem.
All timings on Apple M4 Pro, Python 3.12, NumPy 2.4, SciPy 1.17.
| Universe |
solve_cg time (s) |
||
|---|---|---|---|
| Synthetic i.i.d. Gaussian | 1000 | 2000 | 0.019 |
| S&P 500 (Jul 2021–Apr 2026) | 495 | 1192 | 0.0091 |
Both with Ledoit-Wolf shrinkage ($\alpha = 0.333$ synthetic / $0.293$ S&P), 56 and 205 CG iterations respectively.
pip install fast-minimum-varianceFor development:
git clone https://github.com/Jebel-Quant/fast_minimum_variance
cd fast_minimum_variance
make install- Python 3.11+
- numpy
- scipy
- scikit-learn
- cvx-linalg
If you use this library in academic work or research, please cite:
@software{fast_minimum_variance,
author = {Schmelzer, Thomas},
title = {fast-minimum-variance: Solving Minimum Variance Portfolios Fast},
url = {https://github.com/Jebel-Quant/fast_minimum_variance},
year = {2026},
license = {MIT}
}MIT License — see LICENSE for details.