factor_cov¶
Defined in fynance.portfolio.covariance
- factor_cov(X, n_factors=3)[source]
Statistical factor-model covariance (low-rank + diagonal).
Eigendecomposes the (population) sample covariance and keeps the
k = min(n_factors, N)largest eigenpairs as a common (systematic) component \(B B^\top\) with loadings \(B = V_k \text{diag}(\sqrt{l_k})\); the remainder of each asset’s variance is kept as an idiosyncratic diagonal term. The result is positive semi-definite by construction and matches the sample covariance’s diagonal (total variance) exactly.- Parameters:
- Xarray_like
Returns panel, shape
(T,)or(T, N).- n_factorsint, optional
Number of factors to keep, clipped to
N. Default 3.
- Returns:
- np.ndarray
Symmetric
(N, N)covariance matrix,B @ B.T + diag(idio).
Examples
>>> import numpy as np >>> rng = np.random.default_rng(0) >>> X = rng.standard_normal((100, 5)) >>> S = factor_cov(X, n_factors=2) >>> S.shape (5, 5) >>> bool(np.all(np.linalg.eigvalsh(S) >= -1e-10)) True