markowitz.core.linalg¶
markowitz.core.linalg
¶
Numerically stable linear algebra primitives.
The module is intentionally narrow: every covariance-related solve in the
rest of the package is funnelled through :func:cholesky_solve so that
no caller is ever tempted to use numpy.linalg.inv. Inversion via
inv is forbidden because it is both slower and numerically inferior
to a Cholesky-based solve for symmetric positive definite systems.
All public functions accept array-likes and return numpy arrays with
dtype=float64; inputs are validated for finiteness and shape.
cholesky_solve(Sigma: npt.ArrayLike, b: npt.ArrayLike, *, check_finite: bool = True, overwrite_b: bool = False) -> FloatArray
¶
Solve Sigma x = b for a symmetric positive definite Sigma.
The Cholesky factorization is preferred over forming the explicit inverse: it is twice as fast for SPD systems and avoids the catastrophic cancellation associated with inverting near-singular matrices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Sigma
|
ArrayLike
|
Symmetric positive definite matrix of shape |
required |
b
|
ArrayLike
|
Right-hand side of shape |
required |
check_finite
|
bool
|
Forwarded to SciPy; when |
True
|
overwrite_b
|
bool
|
Permit SciPy to overwrite |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
x |
FloatArray
|
Solution array with the same shape as |
Raises:
| Type | Description |
|---|---|
SingularCovarianceError
|
If |
NumericalError
|
If |
Source code in src/markowitz/core/linalg.py
condition_number(Sigma: npt.ArrayLike) -> float
¶
Return lambda_max / lambda_min for Sigma.
Returns float('inf') if the matrix is not positive definite,
contains non-finite entries, or has a non-positive smallest
eigenvalue. No exception is raised.
Source code in src/markowitz/core/linalg.py
psd_check(Sigma: npt.ArrayLike, *, tol: float = 1e-10, require_symmetric: bool = True, symmetry_tol: float = 1e-10) -> None
¶
Validate that Sigma is symmetric positive definite.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Sigma
|
ArrayLike
|
Candidate covariance matrix. |
required |
tol
|
float
|
Lower bound for the smallest eigenvalue. Matrices whose
minimum eigenvalue is below |
1e-10
|
require_symmetric
|
bool
|
Reject matrices that are not symmetric within |
True
|
symmetry_tol
|
float
|
Absolute tolerance for the |
1e-10
|
Raises:
| Type | Description |
|---|---|
SingularCovarianceError
|
If the matrix is not symmetric (when required), not positive definite, or contains non-finite entries. |
ValueError
|
If the matrix is not 2-D and square. |
Source code in src/markowitz/core/linalg.py
regularize(Sigma: npt.ArrayLike, *, eps: float | Literal['auto'] = 'auto', method: Literal['ridge'] = 'ridge') -> FloatArray
¶
Return a regularized copy of Sigma that is safely SPD.
The default ridge method adds eps * I to a symmetrized copy
of Sigma. When eps='auto', a scale-aware value is chosen
based on the trace of Sigma and machine precision.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Sigma
|
ArrayLike
|
Input matrix. |
required |
eps
|
float | Literal['auto']
|
Either an explicit non-negative scalar or the string |
'auto'
|
method
|
Literal['ridge']
|
Currently only |
'ridge'
|
Returns:
| Type | Description |
|---|---|
A new ``(n, n)`` ``float64`` array.
|
|