qis.apply_pca¶
- qis.apply_pca(cmatrix, is_max_sign_positive=True, eigen_signs=None)[source]¶
eigen decomposition of a symmetric matrix, ordered from the largest eigenvalue down.
Uses
np.linalg.eigh, socmatrixmust be symmetric; only the lower triangle is read. Eigenvectors are columns:cmatrix @ vectors[:, i] == values[i] * vectors[:, i].The sign of an eigenvector is arbitrary, which makes loadings flip between refits. Two conventions are offered to pin it down; each flips whole eigenvectors (columns), so the result remains an eigen-decomposition. Neither can rule out every flip: under the default, a near tie in magnitude between the two largest loadings of opposite sign lets the sign change between refits, and eigenvectors of a repeated eigenvalue are not unique.
- Parameters:
cmatrix (ndarray) – symmetric covariance or correlation matrix, shape (n, n)
is_max_sign_positive (bool) – flip each eigenvector so its largest-magnitude element is positive. Ignored when
eigen_signsis giveneigen_signs (ndarray) – explicit sign per eigenvector, shape (n,), entry
jfor thej-th eigenvector in descending order. Eigenvectorjis flipped when its first loading (asset 0) has the opposite sign toeigen_signs[j]; a zero first loading or a zero sign leaves it unchanged. Use to carry the convention of a previous fit forward so loadings stay comparable across dates
- Returns:
(eigenvalues, eigenvectors), descending by eigenvalue, eigenvectors as columns
- Raises:
ValueError – if
eigen_signsdoes not have one entry per eigenvector- Return type:
(<class ‘numpy.ndarray’>, <class ‘numpy.ndarray’>)