API Reference¶
The public interface of fast_minimum_variance:
fast_minimum_variance
¶
fast_minimum_variance — fast solvers for the minimum-variance portfolio.
Problem(X, target=None, A=None, b=None, C=None, d=None, alpha=0.0, rho=0.0, mu=None, target_lr=None, pcg_lr=None)
¶
Create a portfolio optimisation problem.
Returns a :class:_MinVarProblem (shrinking active-set) when no custom
constraints are supplied, or a :class:_Problem (growing active-set) when
any of A, b, C, d are provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Returns matrix of shape |
required |
target
|
ndarray | None
|
Optional |
None
|
A
|
ndarray | None
|
Equality constraint matrix |
None
|
b
|
ndarray | None
|
Equality RHS |
None
|
C
|
ndarray | None
|
Inequality constraint matrix |
None
|
d
|
ndarray | None
|
Inequality RHS |
None
|
alpha
|
float
|
Shrinkage intensity; only active when |
0.0
|
rho
|
float
|
Return tilt strength (Markowitz mean-variance). |
0.0
|
mu
|
ndarray | None
|
Expected returns vector |
None
|
target_lr
|
tuple[float, ndarray, ndarray] | None
|
Low-rank factored target |
None
|
pcg_lr
|
tuple[float, ndarray, ndarray] | None
|
RMT preconditioner |
None
|
Returns:
| Type | Description |
|---|---|
_MinVarProblem | _Problem
|
A solver instance with |
_MinVarProblem | _Problem
|
|
_MinVarProblem | _Problem
|
|
Examples:
>>> import numpy as np
>>> X = np.random.default_rng(42).standard_normal((500, 20))
>>> w, _ = Problem(X).solve_kkt()
>>> float(round(w.sum(), 8))
1.0
>>> bool((w >= 0).all())
True
Source code in src/fast_minimum_variance/__init__.py
simulate_equity_returns(n, T, *, k=None, rng=None)
¶
Simulate a TxN demeaned equity return matrix with latent factor structure.
Returns are generated from the model
X = F @ B.T + E
where F (TxK) are factor returns, B (NxK) are factor loadings, and E (TxN) is idiosyncratic noise. The first factor is a market factor with universally positive loadings and high variance; the remaining k-1 factors are style/industry factors with sparse loadings. This produces a covariance spectrum qualitatively similar to equity universes: a dominant market eigenvalue, a handful of secondary factor eigenvalues, and a long tail of near-equal idiosyncratic eigenvalues.
Parameters¶
n:
Number of assets.
T:
Number of time periods (trading days).
k:
Number of latent factors. Defaults to max(3, n // 10).
rng:
Random state — a :class:numpy.random.Generator, an integer seed,
or None (non-reproducible).
Returns:¶
X : ndarray of shape (T, n) Demeaned return matrix. Each column has zero mean.
Examples:¶
X = simulate_equity_returns(100, 200, k=5, rng=0) X.shape (200, 100) bool(abs(X.mean(axis=0)).max() < 1e-14) True