I want to test the various covariance estimators implemented in scikit-learn (for outlier detection). Each of these methods implement a mahalanobis(observations) function. The doc says:

The provided observations are assumed to be centered. 
One may want to center them using a location estimate first.

But in the source code of, for example, EmpiricalCovariance, we have:

def mahalanobis(self, observations):
    centered_obs = observations - self.location_
    mahalanobis_dist = np.sum(
        np.dot(centered_obs, precision) * centered_obs, 1)

As far as I understand, it seems to me that the observations are centered during the computation, so I don't understand why the doc says we need to center the observations? Have I miss something?

Moreover, it seems to me that in this case, the location corresponds to a simple mean. I think that, for MinCovDet estimator, the location really is the reweighted estimator as reweight_covariance() is called at the end of the fit() method, am I right?

I really like the boxplots presented as examples in the doc, but I don't understand why we have the cube root of the distance?

Edit:thanks for the answer for EmpiricalCovariance, but what happen for the other covariance estimators (MincovDet, ShrunkCovariance)? Is it the same? And then, in all these cases, is it the squared Mahalanobis distance that is returned?


2 Answers 2


One can directly work out that centering doesn't matter, at least from a mathematical perspective. Suppose you have two vectors $x_1$ and $x_2$ with finite mean $\mu$. Mean-centering them yields $x_1 - \mu$ and $x_2 - \mu$. The Mahalanobis computation is $d(a,b)=\sqrt{(a-b)^T\Sigma^{-1}(a-b)}$, so the effect is to compute $$ d(x_1 - \mu, x_2 - \mu)= \sqrt{(x_1 - \mu - x_2 + \mu)^T\Sigma^{-1}(x_1 - \mu - x_2 + \mu)} = \sqrt{(x_1 - x_2)^T\Sigma^{-1}(x_1 - x_2)} =d(x_1, x_2)$$

Purely from the perspective of the axioms of distance and linear algebra, any positive definite $\Sigma$ is suitable.


This is a known bug in the documentation. Data don't need to be centered.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.