polars_online.corr.from_spectral

polars_online.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 above mp_edge()’s noise edge.

Parameters

vals

The eigenvalues, a vector of k.

vecs

The eigenvectors as the rows of a k x n array, as spectral() returns them.

unit_diag

True, the default, adds 1 - diag(V' diag(vals) V), with V the rows of vecs, to the diagonal, so the result is a correlation matrix. False returns V' diag(vals) V as it is.

Returns

An n x n float array, V' diag(vals) V with V the rows of vecs, its diagonal set to 1 under unit_diag.

Raises

ValueError

vecs without one row per entry of vals.

Examples

r = np.array([[1.0, 0.8, 0.7], [0.8, 1.0, 0.75], [0.7, 0.75, 1.0]])
top = po.corr.from_spectral(*po.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

Notes

The remainder 1 - diag(V' diag(vals) V) is not negative when vals are 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.