Linear algebra utilities for portfolio optimization, part of the jebel-quant ecosystem.
pip install cvx-linalgfrom cvx.linalg import (
a_norm, cholesky,
inv, inv_a_norm, is_positive_definite, lstsq, pca, rand_cov, solve, valid,
)
from cvx.linalg.ewm_cov import ewm_covariance # requires polarsa_norm(vector, matrix=None)— Euclidean norm or NaN-aware matrix normcholesky(cov, rhs=None)— Upper triangular Cholesky factor R such that R.T @ R = cov; when rhs is given, solves cov @ x = rhs (falls back to LU for non-positive-definite matrices)cond(matrix, p=None)— Condition number of a matrix (NaN-aware); accepts the samepnorm values asnumpy.linalg.conddet(matrix, cond_threshold=1e12)— Determinant of a square matrix with NaN-aware submatrix handling; emitsIllConditionedMatrixWarningwhen near-singulareigvals(matrix)— Eigenvalues of a general square matrix (supports complex output for non-symmetric matrices)eigh(matrix)— Eigenvalues/eigenvectors of the valid symmetric/Hermitian submatrix in ascending eigenvalue ordereigvalsh(matrix)— Eigenvalues-only convenience wrapper aroundeighewm_covariance(data, assets, index_col, window, is_halflife, warmup)— Exponentially weighted covariance matrices from a Polars DataFrameinv(matrix, cond_threshold=1e12)— Invert a matrix restricted to valid rows/columns; NaN rows/columns are returned for invalid positionsinv_a_norm(vector, matrix=None)— Euclidean norm or inverse NaN-aware matrix normlstsq(matrix, rhs, cond_threshold=1e12)— Solve a least-squares system with NaN-aware row filtering; returns(x, residuals, rank, sv)consistent withnumpy.linalg.lstsqis_positive_definite(matrix)— Return True if the matrix is symmetric positive-definitepca(returns, n_components)— Principal Component Analysis via SVDqr(matrix)— Reduced QR decomposition, matchingnp.linalg.qr(mode='reduced')rand_cov(n, seed)— Random positive semi-definite covariance matrixsolve(matrix, rhs, cond_threshold=1e12)— Solve a linear system restricted to valid rows/columns; NaN entries are returned for invalid positionssvd(matrix)— Raw compact singular value decomposition vianp.linalg.svd(full_matrices=False)valid(matrix)— Return a boolean mask and valid submatrix by removing rows/columns with non-finite diagonal entries
DimensionMismatchError— Raised when vector length does not match matrix dimensionIllConditionedMatrixWarning— Emitted when the condition number exceeds a configurable thresholdNegativeWarmupError— Raised when a negative warmup period is passed toewm_covarianceNonSquareMatrixError— Raised when a square matrix is required but the input is not squareSingularMatrixError— Raised when a matrix is numerically singularcheck_and_warn_condition(matrix, threshold)— EmitIllConditionedMatrixWarningwhen the condition number exceeds the threshold
Matrix— Type alias fornumpy.ndarraywithfloat64dtype