polars_online.corr

Read, repair, summarize and score correlation matrices.

polars_online.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

polars_online.gram.correlation(), from a Gram, and polars_online.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 polars_online.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 polars_online.gram.correlation() gives it nan. Every function here reads the Gram as polars_online.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 polars_online.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 polars-online[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 POLARS_ONLINE_WARN_UNSTABLE=1 each call issues polars_online.UnstableWarning, once per call, even when that call runs other functions of this module.

polars_online.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.

polars_online.corr.nearest

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

polars_online.corr.shrink

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

polars_online.corr.to_z

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

polars_online.corr.from_z

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

polars_online.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.

polars_online.corr.equicorr

Compute the mean off-diagonal correlation of a matrix.

polars_online.corr.equicorr_row

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

polars_online.corr.equicorr_loglik

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

polars_online.corr.absorption

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

polars_online.corr.absorption_shift

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

polars_online.corr.spectral

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

polars_online.corr.from_spectral

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

polars_online.corr.block_means

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

polars_online.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.

polars_online.corr.mp_edge

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

polars_online.corr.mp_density

Compute the Marchenko-Pastur density at given eigenvalues.

polars_online.corr.signal_share

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

polars_online.corr.loss

Score a forecast correlation matrix against the realized one.

polars_online.corr.epps_invert

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