Multivariate Outlier Removal With Mahalanobis Distance
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Mahalanobis distance measures how far a point is from the center of a multivariate distribution, accounting for correlations between variables. Unlike Euclidean distance, which treats all directions equally, Mahalanobis distance stretches or compresses axes based on the covariance structure of the data. Points with a large Mahalanobis distance are multivariate outliers — they may look normal in each variable individually but are unusual when considering the relationships between variables.
The Formula
The Mahalanobis distance of a point x from a distribution with mean mu and covariance matrix S is:
xis the observation vectormuis the mean vector of the datasetSis the covariance matrixS^(-1)is the inverse covariance matrix (precision matrix)
The key insight: multiplying by the inverse covariance matrix decorrelates the variables and normalizes their scales, so variables with high variance do not dominate the distance calculation.
Python Implementation
Setting the Threshold with Chi-Square
For multivariate normal data, the squared Mahalanobis distance follows a chi-square distribution with p degrees of freedom (where p is the number of variables):
Common alpha values:
alpha=0.05(95% confidence) — moderate, catches more outliersalpha=0.01(99% confidence) — conservative, fewer false positivesalpha=0.001(99.9% confidence) — very conservative
Complete Outlier Removal Function
With pandas DataFrames
Scikit-Learn Approach
EllipticEnvelope uses the Minimum Covariance Determinant (MCD) estimator, which is more robust to outliers than the standard covariance matrix.
Why Not Just Use Euclidean Distance?
Euclidean distance also fails when variables have different scales (e.g., height in cm vs weight in kg). Mahalanobis distance normalizes scales automatically through the covariance matrix.
Common Pitfalls
- Singular covariance matrix: When features are perfectly correlated or there are more features than samples, the covariance matrix is singular and cannot be inverted. Use
np.linalg.pinv()(pseudo-inverse) or reduce dimensions with PCA first. - Non-normal data: Mahalanobis distance assumes multivariate normality. For skewed or heavy-tailed data, the chi-square threshold produces unreliable results. Transform data (log, Box-Cox) or use robust methods like
EllipticEnvelope. - Outliers contaminating the covariance estimate: The standard covariance is itself affected by outliers, making the distance less reliable. Use the Minimum Covariance Determinant (MCD) estimator from
sklearn.covariance.MinCovDetfor a robust covariance. - High dimensionality: In high dimensions, Mahalanobis distance requires many more samples than features for a stable covariance estimate. As a rule of thumb, you need at least 5-10x more samples than features.
- Removing too many points: With
alpha=0.05, you expect to flag 5% of normal data as outliers even when there are none. Choose alpha conservatively and inspect flagged points before removing them.
Summary
- Mahalanobis distance measures how unusual a point is considering correlations between variables
- Squared Mahalanobis distance follows a chi-square distribution — use it to set a statistical threshold
- Use
scipy.spatial.distance.mahalanobisfor computation andscipy.stats.chi2.ppffor the threshold - Use
sklearn.covariance.EllipticEnvelopefor a robust, ready-made solution - Always check for singular covariance matrices and non-normal data before applying
- Start with
alpha=0.01and inspect flagged points rather than blindly removing them

