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 |
|
repair it |
|
summarize it |
|
tell it from noise |
|
score a forecast of it |
|
undo the Epps attenuation |
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 thaninf. The round trip throughfrom_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.
Find the nearest correlation matrix to a matrix, by Higham’s alternating projections. |
|
Shrink a correlation matrix toward a structured target, by Ledoit and Wolf. |
|
Apply Fisher’s transform, |
|
Map Fisher’s |
|
Compute the standard error of a correlation’s Fisher |
Structure¶
One correlation for a whole matrix, or one for each block, and the spectrum’s share in each mode.
Compute the mean off-diagonal correlation of a matrix. |
|
Estimate the common correlation from one standardized row, by Engle and Kelly’s closed form. |
|
Compute the Gaussian log-density of a standardized row under a common correlation. |
|
Compute Kritzman, Li, Page and Rigobon’s absorption ratio of a correlation matrix. |
|
Compute Kritzman, Li, Page and Rigobon’s standardized absorption shift. |
|
Compute the top |
|
Rebuild a matrix from eigenpairs, completed to a unit diagonal. |
|
Compute the mean correlation within and between labeled blocks of columns. |
|
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.
Compute the Marchenko-Pastur edges for |
|
Compute the Marchenko-Pastur density at given eigenvalues. |
|
Estimate how much of a correlation’s movement across blocks is not sampling noise. |
|
Score a forecast correlation matrix against the realized one. |
|
Compute the correlation over |