.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/covariance/plot_lw_vs_oas.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_covariance_plot_lw_vs_oas.py: ============================= Ledoit-Wolf 与 OAS 估计 ============================= 通常的协方差最大似然估计可以通过收缩进行正则化。Ledoit 和 Wolf 提出了一个计算渐近最优收缩参数(最小化 MSE 标准)的闭合公式,从而得到了 Ledoit-Wolf 协方差估计。 Chen 等人提出了对 Ledoit-Wolf 收缩参数的改进,即 OAS 系数,在假设数据为高斯分布的情况下,其收敛性显著更好。 这个例子借鉴了 Chen 的出版物 [1],展示了使用高斯分布数据时 LW 和 OAS 方法估计的 MSE 的比较。 [1] "Shrinkage Algorithms for MMSE Covariance Estimation" Chen 等人,IEEE 信号处理汇刊,2010 年 10 月,第 58 卷,第 10 期。 .. GENERATED FROM PYTHON SOURCE LINES 16-24 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from scipy.linalg import cholesky, toeplitz from sklearn.covariance import OAS, LedoitWolf np.random.seed(0) .. GENERATED FROM PYTHON SOURCE LINES 25-99 .. code-block:: Python n_features = 100 # 模拟协方差矩阵(AR(1)过程) r = 0.1 real_cov = toeplitz(r ** np.arange(n_features)) coloring_matrix = cholesky(real_cov) n_samples_range = np.arange(6, 31, 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] = lw.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_ # plot 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.0 + (plt.ylim()[1] - plt.ylim()[0]) / 10.0) plt.xlim(5, 31) plt.show() .. image-sg:: /auto_examples/covariance/images/sphx_glr_plot_lw_vs_oas_001.png :alt: Comparison of covariance estimators :srcset: /auto_examples/covariance/images/sphx_glr_plot_lw_vs_oas_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.190 seconds) .. _sphx_glr_download_auto_examples_covariance_plot_lw_vs_oas.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/main?urlpath=lab/tree/notebooks/auto_examples/covariance/plot_lw_vs_oas.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_lw_vs_oas.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_lw_vs_oas.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_lw_vs_oas.zip ` .. include:: plot_lw_vs_oas.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_