1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//! Robust Mahalanobis distances and the multivariate outlier map, over *any*
//! robust location/scatter pair `(μ̂, Σ̂)`, plus the classical (non-robust)
//! mean/covariance baseline they are meant to replace.
//!
//! A classical Mahalanobis distance uses the sample mean and covariance, both of
//! which have breakdown point `0`: a few outliers inflate the covariance and
//! shrink their own distances (the *masking* effect), so they hide from the very
//! statistic meant to reveal them. Feeding a robust, `χ²`-calibrated `(μ̂, Σ̂)`
//! (from [`Mcd`], [`Ogk`] or [`MScatter`]) into the same formula fixes this: the
//! distances of the clean majority stay small and the outliers stand out.
//! (Tyler's estimator identifies only *shape*, so its distances are not
//! `χ²`-calibrated for this cutoff; see [`TylerFit`](crate::multivariate::TylerFit).)
//!
//! [`Mcd`]: crate::multivariate::Mcd
//! [`Ogk`]: crate::multivariate::Ogk
//! [`MScatter`]: crate::multivariate::MScatter
use ;
use RobustError;
use chi2_quantile;
use mean_covariance;
/// Robust Mahalanobis distances `dᵢ = √((xᵢ − loc)ᵀ Σ⁻¹ (xᵢ − loc))` for every
/// row of `x`, given a location `loc` (`p`-vector) and a symmetric positive-
/// definite scatter `Σ` (`p × p`). Errors on a dimension mismatch or a
/// non-positive-definite scatter.
/// The classical (non-robust) mean and unbiased sample covariance of `x`, the
/// breakdown-`0` baseline the robust estimators replace. Handy for
/// side-by-side comparison and for a maximum-likelihood starting point.
/// The distance cutoff `√χ²_{p, quantile}` for flagging outliers on `p`
/// variables (e.g. `quantile = 0.975`).
/// Flag distances exceeding `distance_cutoff(p, quantile)`. Under a `p`-variate
/// Gaussian the squared robust distances are approximately `χ²_p`, so a
/// `quantile` of `0.975` flags ≈ 2.5% of clean observations.