rheon.corr

Read, repair, summarize and score correlation matrices.

rheon.gram solves and diagnoses the design a Gram holds. This module takes a correlation matrix, from a Gram or from anywhere else, and does the arithmetic that comes after a pass:

what you want

the calls

read one

rheon.gram.correlation(), from a Gram, and rheon.gram.from_row() for a closed group’s row

repair it

nearest(), the nearest correlation matrix; shrink(), toward a structured target

summarize it

equicorr(), equicorr_row() and equicorr_loglik() for one common correlation; block_means() and from_blocks() for blocks; spectral() and from_spectral() for the top eigenpairs; absorption() and absorption_shift() for the share the top eigenvectors take

tell it from noise

mp_edge() and mp_density(), the Marchenko-Pastur edges and curve; signal_share() across blocks; fisher_se() for one correlation, with to_z() and from_z()

score a forecast of it

loss()

undo the Epps attenuation

epps_invert()

Every function is a pure function of its inputs: arrays or numbers, or a Gram or a closed group’s row. Where it takes a correlation matrix, it also takes one of the Grams gram() returns, or a row of closed_groups(), and reads its correlation matrix as rheon.gram.correlation() does, without the intercept’s row and column. The intercept is the constant column a spec’s fit_intercept adds, and a constant has no correlation with anything, so rheon.gram.correlation() gives it nan. Every function here reads the Gram as rheon.gram.subset() would give it without the intercept, and returns one row and column per other column, in their order. A feature the stream found constant has no correlation either, and is refused by name: pass the Gram without it, as rheon.gram.subset() returns it.

The arithmetic is the papers’, named in each docstring, and every function is held against a longhand check in tests/test_corr.py: Higham’s own published examples for nearest(), Ledoit and Wolf’s formulas for shrink(), and the closed forms for the rest. Where a library computes the same thing, tests/test_second_opinion.py holds the function to it too: statsmodels’ corr_nearest for nearest(), and scikit-learn’s ledoit_wolf for shrink() toward the identity.

The module needs numpy, an optional extra of this package (pip install rheon[numpy]) and not a dependency, since it is not one of Polars’ either. Nothing here needs scipy or scikit-learn.

Warning

This module is considered unstable: its functions’ names, keywords and results may change in any release without that counting as a breaking change. Under RHEON_WARN_UNSTABLE=1 each call issues rheon.UnstableWarning, once per call, even when that call runs other functions of this module.

rheon.corr.Z_CLIP = 0.999999

Fisher’s transform is infinite at ±1, and a degenerate block gives exactly that. Inputs are clipped here, so to_z(1.0) is about 7.25 rather than inf. The round trip through from_z() is then not quite the identity at the ends: it gives back ±Z_CLIP, to rounding, for ±1.

Repairing and reading a matrix

A correlation matrix made valid, shrunk, or moved to Fisher’s z and back.

rheon.corr.nearest

Find the nearest correlation matrix to a matrix, by Higham’s alternating projections.

rheon.corr.shrink

Shrink a correlation matrix toward a structured target, by Ledoit and Wolf.

rheon.corr.to_z

Apply Fisher’s transform, z = atanh(rho), to correlations elementwise.

rheon.corr.from_z

Map Fisher’s z back to a correlation, tanh(z), elementwise.

rheon.corr.fisher_se

Compute the standard error of a correlation’s Fisher z, or of the correlation itself.

Structure

One correlation for a whole matrix, or one for each block, and the spectrum’s share in each mode.

rheon.corr.equicorr

Compute the mean off-diagonal correlation of a matrix.

rheon.corr.equicorr_row

Estimate the common correlation from one standardized row, by Engle and Kelly’s closed form.

rheon.corr.equicorr_loglik

Compute the Gaussian log-density of a standardized row under a common correlation.

rheon.corr.absorption

Compute Kritzman, Li, Page and Rigobon’s absorption ratio of a correlation matrix.

rheon.corr.absorption_shift

Compute Kritzman, Li, Page and Rigobon’s standardized absorption shift.

rheon.corr.spectral

Compute the top k eigenpairs of a matrix, largest first.

rheon.corr.from_spectral

Rebuild a matrix from eigenpairs, completed to a unit diagonal.

rheon.corr.block_means

Compute the mean correlation within and between labeled blocks of columns.

rheon.corr.from_blocks

Build the block-equicorrelation matrix that a table of block correlations describes.

Noise

How much of a correlation matrix is noise, and how far one matrix is from another.

rheon.corr.mp_edge

Compute the Marchenko-Pastur edges for n observations of m series.

rheon.corr.mp_density

Compute the Marchenko-Pastur density at given eigenvalues.

rheon.corr.signal_share

Estimate how much of a correlation’s movement across blocks is not sampling noise.

rheon.corr.loss

Score a forecast correlation matrix against the realized one.

rheon.corr.epps_invert

Compute the correlation over L rows from lagged co-moments, undoing the Epps attenuation.