Ledoit-Wolf收缩

2022-05-29 10:20:43 浏览数 (1)

代码语言:javascript复制
import numpy as np
import matplotlib.pyplot as plt
from scipy.linalg import toeplitz, cholesky
from sklearn.covariance import LedoitWolf, OAS
np.random.seed(0)
n_features = 100
#模拟协方差矩阵
r=0.1
real_cov=toeplitz(r**np.arange(n_features))
coloring_matrix=cholesky(real_cov)
n_samples_range=np.arange(6,30,1)
repeat=100
lw_mse=np.zeros((n_samples_range.size,repeat))
oa_mse=np.zeros((n_samples_range.size,repeat))
lw_shrinkage=np.zeros((n_samples_range.size,repeat))
oa_shrinkage=np.zeros((n_samples_range.size,repeat))
for i, n_samples in enumerate(n_samples_range):
    for j in range(repeat):
        X=np.dot(
            np.random.normal(size=(n_samples,n_features)),coloring_matrix.T)
        lw=LedoitWolf(store_precision=False,assume_centered=True)
        lw.fit(X)
        lw_mse[i,j]=lw.error_norm(real_cov,scaling=False)
        lw_shrinkage[i,j]= w.shrinkage_
        oa=OAS(store_precision=False,assume_centered=True)
        oa.fit(X)
        oa_mse[i,j]=oa.error_norm(real_cov,scaling=False)
        oa_shrinkage[i,j]=oa.shrinkage_
#绘制MSE
plt.subplot(2,1,1)
plt.errorbar(n_samples_range,lw_mse.mean(1),yerr=lw_mse.std(1),label='Ledoit-Wolf',color='navy',lw=2)
plt.errorbar(n_samples_range,oa_mse.mean(1),yerr=oa_mse.std(1),label='OAS',color='darkorange',lw=2)
plt.ylabel("Squared error")
plt.legend(loc="upper right")
plt.title("Comparison of covariance estimators")
plt.xlim(5,31)
#绘图收缩系数
plt.subplot(2,1,2)
plt.errorbar(n_samples_range,lw_shrinkage.mean(1),yerr=lw_shrinkage.std(1),label='Ledoit-Wolf',color='navy',lw=2)
plt.errorbar(n_samples_range,oa_shrinkage.mean(1),yerr=oa_shrinkage.std(1),label='OAS',color='darkorange',lw=2)
plt.xlabel("n_samples")
plt.ylabel("Shrinkage")
plt.legend(loc="lower right")
plt.ylim(plt.ylim()[0],1. (plt.ylim()[1]-plt.ylim()[0])/10.)
plt.xlim(5,31)
plt.show()

算法:Ledoit-Wolf收缩是通过最小化MSE准则计算渐近最优收缩参数。

文献:Ledoit, O. , & Wolf, M. . (2004). A well-conditioned estimator for large-dimensional covariance matrices. Journal of Multivariate Analysis, 88(2), 365-411.

0 人点赞