Robust covariance estimation and Mahalanobis distances relevance
where μ and Σ are the location and the covariance of
the underlying Gaussian distributions.
In practice, μ and Σ are replaced by some
estimates. The standard covariance maximum likelihood estimate (MLE) is very
sensitive to the presence of outliers in the data set and therefore,
the downstream Mahalanobis distances also are. It would be better to
use a robust estimator of covariance to guarantee that the estimation is
resistant to “erroneous” observations in the dataset and that the
calculated Mahalanobis distances accurately reflect the true
organization of the observations.
The Minimum Covariance Determinant estimator (MCD) is a robust,
high-breakdown point (i.e. it can be used to estimate the covariance
matrix of highly contaminated datasets, up to
2nsamples−nfeatures−1 outliers)
estimator of covariance. The idea behind the MCD is to find
2nsamples+nfeatures+1
observations whose empirical covariance has the smallest determinant,
yielding a “pure” subset of observations from which to compute
standards estimates of location and covariance. The MCD was introduced by
P.J.Rousseuw in [1].
This example illustrates how the Mahalanobis distances are affected by
outlying data. Observations drawn from a contaminating distribution
are not distinguishable from the observations coming from the real,
Gaussian distribution when using standard covariance MLE based Mahalanobis
distances. Using MCD-based
Mahalanobis distances, the two populations become
distinguishable. Associated applications include outlier detection,
observation ranking and clustering.
References
1
],
color
=
"black"
,
label
=
"inliers"
)
outlier_plot
=
ax
.
scatter
(
X
[:,
0
][
-
n_outliers
:],
X
[:,
1
][
-
n_outliers
:],
color
=
"red"
,
label
=
"outliers"
)
ax
.
set_xlim
(
ax
.
get_xlim
()[
0
],
10.0
)
ax
.
set_title
(
"Mahalanobis distances of a contaminated data set"
)
xx
,
yy
=
(
(
()[
0
],
()[
1
],
100
),
(
()[
0
],
()[
1
],
100
),
)
zz
=
[
xx
.
ravel
(),
yy
.
ravel
()]
mahal_emp_cov
=
emp_cov
.
mahalanobis
(
zz
)
mahal_emp_cov
=
mahal_emp_cov
.
reshape
(
xx
.
shape
)
emp_cov_contour
=
(
xx
,
yy
,
(
mahal_emp_cov
),
cmap
=
plt
.
cm
.
PuBu_r
,
linestyles
=
"dashed"
)
mahal_robust_cov
=
robust_cov
.
mahalanobis
(
zz
)
mahal_robust_cov
=
mahal_robust_cov
.
reshape
(
xx
.
shape
)
robust_contour
=
ax
.
contour
(
xx
,
yy
,
(
mahal_robust_cov
),
cmap
=
plt
.
cm
.
YlOrBr_r
,
linestyles
=
"dotted"
)
ax
.
legend
(
[
([],
[],
color
=
"tab:blue"
,
linestyle
=
"dashed"
),
([],
[],
color
=
"tab:orange"
,
linestyle
=
"dotted"
),
inlier_plot
,
outlier_plot
,
],
[
"MLE dist"
,
"MCD dist"
,
"inliers"
,
"outliers"
],
loc
=
"upper right"
,
borderaxespad
=
0
,
)
()
(
X
-
(
X
,
0
))
**
(
0.33
)
ax1
.
boxplot
([
emp_mahal
[:
-
n_outliers
],
emp_mahal
[
-
n_outliers
:]],
widths
=
0.25
)
ax1
.
plot
(
(
n_samples
-
n_outliers
,
1.26
),
emp_mahal
[:
-
n_outliers
],
"+k"
,
markeredgewidth
=
1
,
)
ax1
.
plot
(
(
n_outliers
,
2.26
),
emp_mahal
[
-
n_outliers
:],
"+k"
,
markeredgewidth
=
1
)
ax1
.
axes
.
set_xticklabels
((
"inliers"
,
"outliers"
),
size
=
15
)
ax1
.
set_ylabel
(
r
"$\sqrt[3]{\rm{(Mahal. dist.)}}$"
,
size
=
16
)
ax1
.
set_title
(
"Using non-robust estimates
\n
(Maximum Likelihood)"
)
robust_mahal
=
robust_cov
.
mahalanobis
(
X
-
robust_cov
.
location_
)
**
(
0.33
)
ax2
.
boxplot
([
robust_mahal
[:
-
n_outliers
],
robust_mahal
[
-
n_outliers
:]],
widths
=
0.25
)
ax2
.
plot
(
(
n_samples
-
n_outliers
,
1.26
),
robust_mahal
[:
-
n_outliers
],
"+k"
,
markeredgewidth
=
1
,
)
ax2
.
plot
(
(
n_outliers
,
2.26
),
robust_mahal
[
-
n_outliers
:],
"+k"
,
markeredgewidth
=
1
)
ax2
.
axes
.
set_xticklabels
((
"inliers"
,
"outliers"
),
size
=
15
)
ax2
.
set_ylabel
(
r
"$\sqrt[3]{\rm{(Mahal. dist.)}}$"
,
size
=
16
)
ax2
.
set_title
(
"Using robust estimates
\n
(Minimum Covariance Determinant)"
)
()
Download zipped: plot_mahalanobis_distances.zip