rheon.corr.shrink

rheon.corr.shrink(r: Any, target: str = 'constant', alpha: float | None = None, x: Any = None) → tuple[Any, float][source]

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

Use it to make a noisy sample correlation matrix better conditioned, trading bias for variance: (1 - alpha) R + alpha F. It repairs a sample matrix from fewer rows than columns, which is singular, in one step. To change the matrix as little as possible instead, use nearest().

Parameters

r

The matrix R: a k x k correlation or covariance matrix, or a Gram or a closed group’s row, read as rheon.gram.correlation() reads it, without the intercept (the module’s docstring says why).

target

The target F. "constant", the default, is Ledoit and Wolf’s constant-correlation target (“Honey, I shrunk the sample covariance matrix”): f_ii = r_ii and f_ij = rbar * sqrt(r_ii r_jj), with r_ij the entries of R and rbar the mean off-diagonal correlation. On a correlation matrix it is the equicorrelation matrix at rbar. "identity" is their scaled identity (“A well-conditioned estimator for large-dimensional covariance matrices”): f = mu I, with mu the mean of the diagonal. On a correlation matrix it is the identity.

alpha

The intensity, from 0 (no shrinkage) to 1 (the target alone). Default None, Ledoit and Wolf’s optimal intensity, which needs x (Notes). One of alpha and x is required.

x

The T x k sample the matrix came from, at least two rows, for the optimal intensity: the rows standardized for a correlation matrix, and as they are for a covariance matrix. The sample has to be the one the matrix is of (Notes). Pass alpha yourself when the two cannot be the same sample.

Returns

(shrunk, alpha): the k x k shrunk matrix, and the intensity used. The result is positive definite whenever the target is and alpha is above 0.

Raises

ValueError

A target that is neither word; alpha outside [0, 1]; neither x nor alpha; an x of the wrong shape or with fewer than two rows; r an array that is not square; a Gram column other than the intercept with no variance.

Examples

rng = np.random.default_rng(0)
x = rng.standard_normal((20, 30))                 # fewer rows than columns
r = np.corrcoef(x, rowvar=False)                  # singular
shrunk, alpha = rp.corr.shrink(r, x=(x - x.mean(0)) / x.std(0))
print(round(alpha, 3), np.linalg.eigvalsh(shrunk).min() > 0)
0.867 True

See also

  • nearest(), the repair that changes the matrix least.

  • equicorr(), the rbar of the constant-correlation target.

Notes

The optimal intensity is Ledoit and Wolf’s (2004), alpha = max(0, min(kappa / T, 1)), with kappa = (pi - rho) / gamma. With x_it the value of column i in row t of x, and xbar_i that column’s mean, for both targets:

pi_ij    = (1/T) sum_t ((x_it - xbar_i)(x_jt - xbar_j) - s_ij)**2
pi       = sum_ij pi_ij
gamma    = sum_ij (f_ij - s_ij)**2

and rho, the target’s own term, for the constant correlation:

theta_ii,ij = (1/T) sum_t ((x_it - xbar_i)**2 - s_ii)
                      * ((x_it - xbar_i)(x_jt - xbar_j) - s_ij)
rho      = sum_i pi_ii + sum_{i!=j} (rbar/2)
             * (sqrt(s_jj/s_ii) theta_ii,ij + sqrt(s_ii/s_jj) theta_jj,ij)

and for the identity, whose diagonal is mu rather than s_ii:

rho      = 0

which is sklearn.covariance.ledoit_wolf’s intensity. pi and theta are sums of fourth moments, so they cannot be recovered from R and T: that is why the optimal intensity needs x.

The sample has to be the one the matrix is of. s_ij above is the plain covariance of x, dividing by T, and gamma compares the target with it. So an r that is another estimate of the same pairs, such as a decayed one out of a bank, makes kappa a comparison of two different matrices. The intensity is still in [0, 1] and the result still a shrunk matrix, but it is not the optimal intensity for the r passed.

Shrinkage and nearest() answer different questions: shrinkage trades bias for variance, and Higham’s projection changes the matrix as little as possible.