shrunk_covariance#
- sklearn.covariance.shrunk_covariance(emp_cov,shrinkage=0.1)[source]#
Calculate covariance matrices shrunk on the diagonal.
Read more in theUser Guide.
- Parameters:
- emp_covarray-like of shape (…, n_features, n_features)
Covariance matrices to be shrunk, at least 2D ndarray.
- shrinkagefloat, default=0.1
Coefficient in the convex combination used for the computationof the shrunk estimate. Range is [0, 1].
- Returns:
- shrunk_covndarray of shape (…, n_features, n_features)
Shrunk covariance matrices.
Notes
The regularized (shrunk) covariance is given by:
(1-shrinkage)*cov+shrinkage*mu*np.identity(n_features)
where
mu=trace(cov)/n_features.Examples
>>>importnumpyasnp>>>fromsklearn.datasetsimportmake_gaussian_quantiles>>>fromsklearn.covarianceimportempirical_covariance,shrunk_covariance>>>real_cov=np.array([[.8,.3],[.3,.4]])>>>rng=np.random.RandomState(0)>>>X=rng.multivariate_normal(mean=[0,0],cov=real_cov,size=500)>>>shrunk_covariance(empirical_covariance(X))array([[0.739, 0.254], [0.254, 0.411]])
On this page
