稳健与经验协方差估计#

通常的协方差最大似然估计对数据集中是否存在异常值非常敏感。在这种情况下,最好使用稳健的协方差估计器来确保估计对数据集中的“错误”观测值具有抵抗力。[1], [2]

最小协方差行列式估计器#

最小协方差行列式(Minimum Covariance Determinant,MCD)估计器是一种稳健的、高击穿点(high-breakdown point)(即它可以用于估计高度污染的数据集的协方差矩阵,最多可容纳 \(\frac{n_\text{samples} - n_\text{features}-1}{2}\) 个异常值)协方差估计器。其思想是找到 \(\frac{n_\text{samples} + n_\text{features}+1}{2}\) 个观测值,这些观测值的经验协方差具有最小的行列式,从而产生一个“纯净”的观测值子集,从中计算标准的位置和协方差估计。经过校正步骤,旨在补偿估计仅从初始数据的一部分学习这一事实,我们最终得到数据集位置和协方差的稳健估计。

最小协方差行列式估计器(MCD)由 P.J.Rousseuw 在 [3] 中引入。

评估#

在此示例中,我们比较了在受污染的高斯分布数据集上使用各种类型的位置和协方差估计时所产生的估计误差:

  • 完整数据集的均值和经验协方差,一旦数据集中存在异常值,它们就会失效。

  • 稳健的 MCD,在 \(n_\text{samples} > 5n_\text{features}\) 的情况下,误差较低。

  • 已知为良好观测值的观测值的均值和经验协方差。这可以被视为“完美”的 MCD 估计,因此可以通过与这种情况进行比较来信任我们的实现。

参考文献#

Influence of outliers on the location estimation, Influence of outliers on the covariance estimation
# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause

import matplotlib.font_manager
import matplotlib.pyplot as plt
import numpy as np

from sklearn.covariance import EmpiricalCovariance, MinCovDet

# example settings
n_samples = 80
n_features = 5
repeat = 10

range_n_outliers = np.concatenate(
    (
        np.linspace(0, n_samples / 8, 5),
        np.linspace(n_samples / 8, n_samples / 2, 5)[1:-1],
    )
).astype(int)

# definition of arrays to store results
err_loc_mcd = np.zeros((range_n_outliers.size, repeat))
err_cov_mcd = np.zeros((range_n_outliers.size, repeat))
err_loc_emp_full = np.zeros((range_n_outliers.size, repeat))
err_cov_emp_full = np.zeros((range_n_outliers.size, repeat))
err_loc_emp_pure = np.zeros((range_n_outliers.size, repeat))
err_cov_emp_pure = np.zeros((range_n_outliers.size, repeat))

# computation
for i, n_outliers in enumerate(range_n_outliers):
    for j in range(repeat):
        rng = np.random.RandomState(i * j)

        # generate data
        X = rng.randn(n_samples, n_features)
        # add some outliers
        outliers_index = rng.permutation(n_samples)[:n_outliers]
        outliers_offset = 10.0 * (
            np.random.randint(2, size=(n_outliers, n_features)) - 0.5
        )
        X[outliers_index] += outliers_offset
        inliers_mask = np.ones(n_samples).astype(bool)
        inliers_mask[outliers_index] = False

        # fit a Minimum Covariance Determinant (MCD) robust estimator to data
        mcd = MinCovDet().fit(X)
        # compare raw robust estimates with the true location and covariance
        err_loc_mcd[i, j] = np.sum(mcd.location_**2)
        err_cov_mcd[i, j] = mcd.error_norm(np.eye(n_features))

        # compare estimators learned from the full data set with true
        # parameters
        err_loc_emp_full[i, j] = np.sum(X.mean(0) ** 2)
        err_cov_emp_full[i, j] = (
            EmpiricalCovariance().fit(X).error_norm(np.eye(n_features))
        )

        # compare with an empirical covariance learned from a pure data set
        # (i.e. "perfect" mcd)
        pure_X = X[inliers_mask]
        pure_location = pure_X.mean(0)
        pure_emp_cov = EmpiricalCovariance().fit(pure_X)
        err_loc_emp_pure[i, j] = np.sum(pure_location**2)
        err_cov_emp_pure[i, j] = pure_emp_cov.error_norm(np.eye(n_features))

# Display results
font_prop = matplotlib.font_manager.FontProperties(size=11)
plt.subplot(2, 1, 1)
lw = 2
plt.errorbar(
    range_n_outliers,
    err_loc_mcd.mean(1),
    yerr=err_loc_mcd.std(1) / np.sqrt(repeat),
    label="Robust location",
    lw=lw,
    color="m",
)
plt.errorbar(
    range_n_outliers,
    err_loc_emp_full.mean(1),
    yerr=err_loc_emp_full.std(1) / np.sqrt(repeat),
    label="Full data set mean",
    lw=lw,
    color="green",
)
plt.errorbar(
    range_n_outliers,
    err_loc_emp_pure.mean(1),
    yerr=err_loc_emp_pure.std(1) / np.sqrt(repeat),
    label="Pure data set mean",
    lw=lw,
    color="black",
)
plt.title("Influence of outliers on the location estimation")
plt.ylabel(r"Error ($||\mu - \hat{\mu}||_2^2$)")
plt.legend(loc="upper left", prop=font_prop)

plt.subplot(2, 1, 2)
x_size = range_n_outliers.size
plt.errorbar(
    range_n_outliers,
    err_cov_mcd.mean(1),
    yerr=err_cov_mcd.std(1),
    label="Robust covariance (mcd)",
    color="m",
)
plt.errorbar(
    range_n_outliers[: (x_size // 5 + 1)],
    err_cov_emp_full.mean(1)[: (x_size // 5 + 1)],
    yerr=err_cov_emp_full.std(1)[: (x_size // 5 + 1)],
    label="Full data set empirical covariance",
    color="green",
)
plt.plot(
    range_n_outliers[(x_size // 5) : (x_size // 2 - 1)],
    err_cov_emp_full.mean(1)[(x_size // 5) : (x_size // 2 - 1)],
    color="green",
    ls="--",
)
plt.errorbar(
    range_n_outliers,
    err_cov_emp_pure.mean(1),
    yerr=err_cov_emp_pure.std(1),
    label="Pure data set empirical covariance",
    color="black",
)
plt.title("Influence of outliers on the covariance estimation")
plt.xlabel("Amount of contamination (%)")
plt.ylabel("RMSE")
plt.legend(loc="center", prop=font_prop)

plt.tight_layout()
plt.show()

脚本总运行时间: (0 分钟 2.515 秒)

相关示例

鲁棒协方差估计和马哈拉诺比斯距离相关性

鲁棒协方差估计和马哈拉诺比斯距离相关性

真实数据集上的离群点检测

真实数据集上的离群点检测

Ledoit-Wolf vs OAS 估计

Ledoit-Wolf vs OAS 估计

具有协方差椭球的线性和二次判别分析

具有协方差椭球的线性和二次判别分析

由 Sphinx-Gallery 生成的图库