Table of contents
The problem is a solve
Many portfolio calculations are written with an inverse. For example, the unconstrained minimum-variance direction is proportional to . That notation describes the mathematics, but computing an explicit inverse is usually the wrong numerical instruction.
What we actually need is the solution to
If is symmetric positive definite, factor it once as , then solve two triangular systems.
import numpy as np
def minimum_variance_weights(cov: np.ndarray) -> np.ndarray:
ones = np.ones(cov.shape[0])
chol = np.linalg.cholesky(cov)
y = np.linalg.solve(chol, ones)
direction = np.linalg.solve(chol.T, y)
return direction / direction.sum()
Why this is better
Cholesky uses the structure we already believe the covariance matrix has. It does less work than a generic inverse and avoids materializing a matrix we never needed. The code also says what the calculation means: solve for a direction, then normalize it.
The diagnostic benefit
A failed factorization is useful information. It tells us the estimated covariance matrix is not positive definite at the precision we are using. Silently applying a generic inverse can conceal that modeling problem behind unstable weights.