Skip to main content

Constructing minimum variance portfolios

Project description

fast-minimum-variance: Solving Minimum Variance Portfolios Fast

Python License Rhiza

Overview

fast-minimum-variance is a Python library for computing long-only minimum variance portfolios without ever forming the sample covariance matrix. By operating directly on the returns matrix $R \in \mathbb{R}^{T \times N}$, it exposes a clean hierarchy of solvers — from an exact direct KKT solve to matrix-free Krylov methods — that scale gracefully as $N$ grows.

The core insight is that minimising portfolio variance is equivalent to minimising $|Rw|^2$, which can be evaluated using two matrix-vector products $w \mapsto R^\top(Rw)$ without constructing $R^\top R$ explicitly. This reframing connects the portfolio optimisation literature directly to Krylov subspace methods.

The long-only constraint $w \geq 0$ is handled throughout via an active-set method: solve the unconstrained problem on the current active set, drop assets with negative weights, and repeat. The process terminates in at most $N$ iterations.

Solvers

Solver Module Method Notes
solve_kkt kkt Direct KKT via numpy.linalg.solve Exact; baseline for accuracy comparisons
solve_minres krylov MINRES on the indefinite KKT system Matrix-free capable; handles indefiniteness correctly
solve_cg krylov CG in the constraint-reduced space Positive-definite reduced system; no indefinite solver needed
solve_cvxpy cvx General-purpose convex solver via CVXPY Reference implementation; slowest but most flexible

All solvers return a weight vector $w \in \mathbb{R}^N$ satisfying $\sum_i w_i = 1$ and $w_i \geq 0$.

Quick Start

from fast_minimum_variance.random import make_returns
from fast_minimum_variance.kkt import solve_kkt
from fast_minimum_variance.krylov import solve_cg, solve_minres
from fast_minimum_variance.cvx import solve_cvxpy

# Generate a synthetic return matrix: 500 daily returns, 20 assets
R = make_returns(T=500, N=20, seed=42)

# Solve with any of the available solvers
w_kkt = solve_kkt(R)  # exact KKT solve
w_minres = solve_minres(R)  # MINRES on the indefinite KKT system
w_cg = solve_cg(R)  # CG in the constraint-reduced space
w_cvxpy = solve_cvxpy(R)  # CVXPY reference

# All solutions satisfy the portfolio constraints
assert abs(w_kkt.sum() - 1.0) < 1e-8
assert (w_kkt >= 0).all()

The KKT System

The equality-constrained minimum variance problem yields the $(N+1) \times (N+1)$ KKT system:

$$\begin{pmatrix} 2R^\top R & \mathbf{1} \cr \mathbf{1}^\top & 0 \end{pmatrix} \begin{pmatrix} w \cr \lambda \end{pmatrix} = \begin{pmatrix} \mathbf{0} \cr 1 \end{pmatrix}$$

This system is symmetric but indefinite — the zero in the bottom-right corner of the KKT matrix introduces a negative eigenvalue. This rules out standard CG on the full system, but it opens the door to MINRES. Alternatively, the CG solver eliminates the constraint entirely by parameterising $w = w_0 + Pv$ where $P$ spans the null space of $\mathbf{1}^\top$, yielding a positive-definite reduced system of size $(N-1) \times (N-1)$.

Installation

pip install fast-minimum-variance

For development:

git clone https://github.com/Jebel-Quant/fast_minimum_variance
cd fast_minimum_variance
make install

Requirements

  • Python 3.11+
  • numpy
  • scipy
  • cvxpy

Citing

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}
}

License

MIT License — see LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fast_minimum_variance-0.3.0.tar.gz (5.3 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fast_minimum_variance-0.3.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file fast_minimum_variance-0.3.0.tar.gz.

File metadata

  • Download URL: fast_minimum_variance-0.3.0.tar.gz
  • Upload date:
  • Size: 5.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fast_minimum_variance-0.3.0.tar.gz
Algorithm Hash digest
SHA256 e2b61cbd96c9e58bfb8cc1c463013f3aaccaa4ed384810c4b17bcccf6a320ab1
MD5 f5c75f30b7e56775c63db31e39bfcb35
BLAKE2b-256 1c1c85c13cc692dbdde5ba05b2376464a0b40a45f4485338f6831b0bb0841977

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_minimum_variance-0.3.0.tar.gz:

Publisher: rhiza_release.yml on Jebel-Quant/fast_minimum_variance

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_minimum_variance-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fast_minimum_variance-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 69b4c13d1ceaac29ee28facb0ab818f60b113db9574a2d84aac61998705f4c0b
MD5 68fb495cde8094993382f284873d3c2a
BLAKE2b-256 358e3f9c5dd0a41c26ee249c13ad3ec2853cdd98b672523e78937cef84ee0b08

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_minimum_variance-0.3.0-py3-none-any.whl:

Publisher: rhiza_release.yml on Jebel-Quant/fast_minimum_variance

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page