rheon.corr.from_spectral¶
- rheon.corr.from_spectral(vals: Any, vecs: Any, *, unit_diag: bool = True) Any[source]¶
Rebuild a matrix from eigenpairs, completed to a unit diagonal.
Use it to turn the leading factors
spectral()keeps into a correlation matrix, such as a denoised one that keeps only the eigenvalues abovemp_edge()’s noise edge.Parameters
valsThe eigenvalues, a vector of
k.vecsThe eigenvectors as the rows of a
k x narray, asspectral()returns them.unit_diagTrue, the default, adds1 - diag(V' diag(vals) V), withVthe rows ofvecs, to the diagonal, so the result is a correlation matrix.FalsereturnsV' diag(vals) Vas it is.
Returns
An
n x nfloat array,V' diag(vals) VwithVthe rows ofvecs, its diagonal set to 1 underunit_diag.Raises
ValueErrorvecswithout one row per entry ofvals.
Examples
r = np.array([[1.0, 0.8, 0.7], [0.8, 1.0, 0.75], [0.7, 0.75, 1.0]]) top = rp.corr.from_spectral(*rp.corr.spectral(r, 1)) # the one-factor matrix print(top.round(3))
[[1. 0.852 0.815] [0.852 1. 0.832] [0.815 0.832 1. ]]
See also
spectral(), the eigenpairs.mp_edge(), the noise edge to keep eigenvalues above.
Notes
The remainder
1 - diag(V' diag(vals) V)is not negative whenvalsare a correlation matrix’s leading eigenvalues, because the dropped components are positive semidefinite. So adding it to the diagonal leaves a correlation matrix, not something that only looks like one.