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, usenearest().Parameters
rThe matrix
R: ak x kcorrelation or covariance matrix, or a Gram or a closed group’s row, read asrheon.gram.correlation()reads it, without the intercept (the module’s docstring says why).targetThe target
F."constant", the default, is Ledoit and Wolf’s constant-correlation target (“Honey, I shrunk the sample covariance matrix”):f_ii = r_iiandf_ij = rbar * sqrt(r_ii r_jj), withr_ijthe entries ofRandrbarthe mean off-diagonal correlation. On a correlation matrix it is the equicorrelation matrix atrbar."identity"is their scaled identity (“A well-conditioned estimator for large-dimensional covariance matrices”):f = mu I, withmuthe mean of the diagonal. On a correlation matrix it is the identity.alphaThe intensity, from 0 (no shrinkage) to 1 (the target alone). Default
None, Ledoit and Wolf’s optimal intensity, which needsx(Notes). One ofalphaandxis required.xThe
T x ksample 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). Passalphayourself when the two cannot be the same sample.
Returns
(shrunk, alpha): thek x kshrunk matrix, and the intensity used. The result is positive definite whenever the target is andalphais above 0.Raises
ValueErrorA
targetthat is neither word;alphaoutside[0, 1]; neitherxnoralpha; anxof the wrong shape or with fewer than two rows;ran 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(), therbarof the constant-correlation target.
Notes
The optimal intensity is Ledoit and Wolf’s (2004),
alpha = max(0, min(kappa / T, 1)), withkappa = (pi - rho) / gamma. Withx_itthe value of columniin rowtofx, andxbar_ithat 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
murather thans_ii:rho = 0
which is
sklearn.covariance.ledoit_wolf’s intensity.piandthetaare sums of fourth moments, so they cannot be recovered fromRandT: that is why the optimal intensity needsx.The sample has to be the one the matrix is of.
s_ijabove is the plain covariance ofx, dividing byT, andgammacompares the target with it. So anrthat is another estimate of the same pairs, such as a decayed one out of a bank, makeskappaa 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 therpassed.Shrinkage and
nearest()answer different questions: shrinkage trades bias for variance, and Higham’s projection changes the matrix as little as possible.