.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/signal/plot_covariance_estimation_robust.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 .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_signal_plot_covariance_estimation_robust.py: =============================================================================== Robust covariance estimation =============================================================================== Comparison of robustness of different covariance estimators on a corrupted low-dimensional dataset. See also [1]_. .. GENERATED FROM PYTHON SOURCE LINES 9-20 .. code-block:: Python # Author: Quentin Barthélemy # # License: BSD (3-clause) import numpy as np from matplotlib import pyplot as plt from pyriemann.estimation import Covariances from pyriemann.utils.viz import plot_cov_ellipse .. GENERATED FROM PYTHON SOURCE LINES 21-32 .. code-block:: Python def plot_cov_estimators(ax, X, estimators): plot_cov_ellipse(ax, C_ref, edgecolor="C0", label='Reference') for i, est in enumerate(estimators): C = Covariances(estimator=est).transform(X[np.newaxis, ...])[0] plot_cov_ellipse(ax, C, edgecolor=f"C{i+2}", label=est) ax.legend(loc='upper left') return ax .. GENERATED FROM PYTHON SOURCE LINES 33-38 Generate a Gaussian dataset --------------------------- Input samples are generated from a centered 2D Gaussian distribution considered as the reference. .. GENERATED FROM PYTHON SOURCE LINES 38-46 .. code-block:: Python rs = np.random.RandomState(2023) n_channels, n_inliers = 2, 50 C_ref = np.array([[1, 0.6], [0.6, 1.5]]) X = C_ref @ rs.randn(n_channels, n_inliers) .. GENERATED FROM PYTHON SOURCE LINES 47-57 Estimate covariance matrices on dataset --------------------------------------- Compare reference covariance matrix to different estimators: - sample covariance matrix (scm), - Ledoit-Wolf shrunk covariance matrix (lwf), - oracle approximating shrunk covariance matrix (oas), - minimum covariance determinant matrix (mcd), - robust Huber's M-estimator based covariance matrix (hub). .. GENERATED FROM PYTHON SOURCE LINES 57-71 .. code-block:: Python estimators = ["scm", "lwf", "oas", "mcd", "hub"] fig, ax = plt.subplots(figsize=(7, 7)) ax.set_title("Covariance estimations on dataset") ax.scatter(X[0], X[1], c='C0', edgecolors="k", label='Inputs') ax = plot_cov_estimators(ax, X, estimators) xlim, ylim = ax.get_xlim(), ax.get_ylim() min_, max_ = min(xlim[0], ylim[0]), max(xlim[1], ylim[1]) ax.set_xlim(min_, max_) ax.set_ylim(min_, max_) plt.show() .. image-sg:: /auto_examples/signal/images/sphx_glr_plot_covariance_estimation_robust_001.png :alt: Covariance estimations on dataset :srcset: /auto_examples/signal/images/sphx_glr_plot_covariance_estimation_robust_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 72-76 Add outliers to dataset ----------------------- Outliers are added to the dataset. .. GENERATED FROM PYTHON SOURCE LINES 76-83 .. code-block:: Python n_outliers = 7 mu, scale = np.array([15, 1]), 5 Xout = mu[:, np.newaxis] + scale * rs.randn(n_channels, n_outliers) X = np.concatenate((X, Xout), axis=1) .. GENERATED FROM PYTHON SOURCE LINES 84-88 Estimate covariance matrices on corrupted dataset ------------------------------------------------- Compare robustness of the different estimators. .. GENERATED FROM PYTHON SOURCE LINES 88-99 .. code-block:: Python fig, ax = plt.subplots(figsize=(14, 7)) ax.set_title("Covariance estimations on corrupted dataset") ax.scatter(X[0, :n_inliers], X[1, :n_inliers], c='C0', edgecolors="k", label='Inliers') ax.scatter(X[0, n_inliers:], X[1, n_inliers:], c='C1', edgecolors="k", label='Outliers') ax = plot_cov_estimators(ax, X, estimators) plt.show() .. image-sg:: /auto_examples/signal/images/sphx_glr_plot_covariance_estimation_robust_002.png :alt: Covariance estimations on corrupted dataset :srcset: /auto_examples/signal/images/sphx_glr_plot_covariance_estimation_robust_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 100-103 References ---------- .. [1] https://scikit-learn.org/stable/auto_examples/covariance/plot_mahalanobis_distances.html # noqa .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.510 seconds) .. _sphx_glr_download_auto_examples_signal_plot_covariance_estimation_robust.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_covariance_estimation_robust.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_covariance_estimation_robust.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_