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, so cmatrix must 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_signs is given

  • eigen_signs (ndarray) – explicit sign per eigenvector, shape (n,), entry j for the j-th eigenvector in descending order. Eigenvector j is flipped when its first loading (asset 0) has the opposite sign to eigen_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_signs does not have one entry per eigenvector

Return type:

(<class ‘numpy.ndarray’>, <class ‘numpy.ndarray’>)