Elliptic Envelope

Fit a robust Gaussian to the data and flag points whose Mahalanobis distance exceeds a threshold.

16 min read1499 wordscore0 viewsupdated 2026-10-08

Definition

An elliptic envelope is an outlier detector that assumes the normal data come from a single multivariate Gaussian. It estimates the centre μ\mu and covariance Σ\Sigma robustly (so the outliers cannot drag them), measures every point's Mahalanobis distance from the centre, and flags the points outside an ellipse whose size is set by the expected fraction of outliers, the contamination.

At a glance

Task
Unsupervised outlier / novelty detection
Input → output
x∈Rpx\in\mathbb{R}^p → a score −d2(x)-d^2(x) and a label: +1+1 inlier, −1-1 outlier
Learns
a centre μ\mu, a covariance Σ\Sigma (via the Minimum Covariance Determinant) and a threshold τ\tau
Decision rule
outlier if d2(x)=(x−μ)⊤Σ−1(x−μ)>τd^2(x)=(x-\mu)^\top\Sigma^{-1}(x-\mu)>\tau
Training
FastMCD: roughly O(n p2)O(n\,p^2) per C-step over a few hundred random starts
Prediction
O(p2)O(p^2) per point: one quadratic form
Hyperparameters
contamination (sets τ), support_fraction (size of the MCD subset)
Assumes
inliers are roughly Gaussian, so one ellipsoid describes them; n comfortably larger than p

The idea

Picture a scatter of measurements from a healthy machine: temperature and vibration, say. They form a tilted, elongated cloud because the two rise together. A faulty reading is not necessarily far from the centre in plain distance; it is far relative to the shape of the cloud, for example high vibration at low temperature. The elliptic envelope captures that shape with a Gaussian, whose density contours are ellipses, and draws one ellipse that should contain the normal points. Anything outside it is an outlier.

Three questions make up the method: how to measure distance relative to the cloud (the Mahalanobis distance), how big to make the ellipse (the threshold), and how to estimate the cloud's shape when the outliers you are hunting are mixed into the data (a robust estimate).

Mahalanobis distance

d2(x)=(x−μ)⊤ Σ−1 (x−μ)d^2(x)=(x-\mu)^\top\,\Sigma^{-1}\,(x-\mu)
μ\mu
centre of the inlier cloud (mean vector)
Σ\Sigma
p×pp\times p covariance matrix of the inliers
d2(x)d^2(x)
squared Mahalanobis distance of x from the centre

With Σ=I\Sigma=I this is the ordinary squared Euclidean distance. In general Σ−1\Sigma^{-1} rescales and de-correlates: a step along a direction in which the data vary a lot costs little, and a step along a direction in which they barely vary costs a lot. Because of that, the distance is affine invariant: rescale a feature, change units or rotate the axes, and every d2d^2 stays the same. Feature scaling makes no difference to it.

The geometry: ellipses from eigenvectors

Every set {x:d2(x)=τ}\{x: d^2(x)=\tau\} is an ellipse (an ellipsoid in pp dimensions). To see its shape, diagonalize the covariance:

Σ=QΛQ⊤  ⇒  d2(x)=∑i=1p(qi⊤(x−μ))2λi\Sigma=Q\Lambda Q^\top \;\Rightarrow\; d^2(x)=\sum_{i=1}^{p}\frac{\big(q_i^\top(x-\mu)\big)^2}{\lambda_i}
qiq_i
i-th eigenvector of Σ: the direction of an axis of the ellipse
λi\lambda_i
i-th eigenvalue: the variance of the data along that axis

In the rotated coordinates ui=qi⊤(x−μ)u_i=q_i^\top(x-\mu) the contour d2=τd^2=\tau is ∑iui2/(τλi)=1\sum_i u_i^2/(\tau\lambda_i)=1: an axis-aligned ellipse with semi-axis τ λi\sqrt{\tau\,\lambda_i} along qiq_i. Large variance gives a long axis. Its area (volume in pp dimensions) grows with det⁡Σ\sqrt{\det\Sigma}; in 2-D it is π τdet⁡Σ\pi\,\tau\sqrt{\det\Sigma}. Raising τ\tau scales the whole ellipse up without changing its shape or tilt.

Choosing the threshold: contamination

The fitted model gives every training point a distance. The threshold τ\tau is set so that a chosen fraction of training points falls outside. That fraction is the contamination cc, your prior guess of how much of the data is anomalous:

τ=quantile1−c{d2(x1),…,d2(xn)},f(x)=τ−d2(x)\tau=\text{quantile}_{1-c}\big\{d^2(x_1),\dots,d^2(x_n)\big\},\qquad f(x)=\tau-d^2(x)
cc
contamination: expected outlier fraction (scikit-learn default 0.1)
f(x)f(x)
decision function: positive inside the envelope, negative outside, zero on it

Prediction is the sign: f(x)≥0f(x)\ge 0 means inlier (+1+1), f(x)<0f(x)<0 means outlier (−1-1). In scikit-learn, score_samples(x) returns −d2(x)-d^2(x) (higher means more normal), offset_ stores −τ-\tau, and decision_function is score_samples − offset_ =τ−d2=\tau-d^2. Note that mahalanobis() returns the squared distance.

Why the plain mean and covariance fail: masking

The obvious estimates are the sample mean and covariance of all points. But the outliers are part of "all points". A cluster of outliers pulls μ\mu toward itself and stretches Σ\Sigma in its own direction, so the ellipse grows to accommodate them, and their distances shrink. This is masking: the outliers hide themselves. The side effect is swamping: the distorted ellipse no longer follows the inliers, so some perfectly normal points end up looking extreme.

A single far-away point is enough to wreck the classical estimates: their breakdown point is 0%0\%. The fix is to estimate μ\mu and Σ\Sigma from the points that look most like a tight Gaussian core. Scikit-learn's EllipticEnvelope uses the Minimum Covariance Determinant (MCD): among all subsets of h≈(n+p+1)/2h\approx(n+p+1)/2 points, find the one whose covariance has the smallest determinant, which is the smallest-volume ellipse holding half the data. How MCD is computed (FastMCD and its C-steps) and why it survives up to about 50% contamination is the topic of the Robust Covariance article. Here we use it as a black box.

EllipticEnvelope.fit(X), as scikit-learn does it

  1. 1Robust core. Run MCD with h=⌈(n+p+1)/2⌉h=\lceil(n+p+1)/2\rceil (or support_fraction·nn). This gives a raw centre and covariance from the hh most concentrated points.
  2. 2Consistency correction. The covariance of the tightest half underestimates the spread, so rescale it until the median d2d^2 of all points equals the χp2\chi^2_p median (1.3861.386 for p=2p=2).
  3. 3Reweight. Keep every point with corrected d2<χp,0.9752d^2<\chi^2_{p,0.975} and recompute an ordinary mean and covariance from those points. This step recovers efficiency: good points left out of the half-sample come back in.
  4. 4Threshold. Compute d2d^2 for every training point under the reweighted fit, then set τ\tau to their (1−c)(1-c) quantile.
  5. 5Predict. For any xx, return the sign of f(x)=τ−d2(x)f(x)=\tau-d^2(x).

Worked example: twelve points, two outliers

Ten inliers lie along a rising diagonal; two outliers, P11 and P12, sit together at the bottom right, off the trend. Every number below was checked against scikit-learn's EllipticEnvelope(contamination=2/12, random_state=0) and NumPy.

Pointx₁x₂classical d²robust d²classical flag (τ = 4.46)robust flag (τ = 10.76)
P11.01.52.5403.004
P21.51.02.3052.246
P32.02.50.9101.814
P42.52.00.2520.403
P53.03.51.5382.543
P63.52.50.0761.814
P73.02.50.0730.202
P84.54.54.5713.887outlier (swamped)
P94.53.51.7643.004
P102.01.50.9971.084
P115.01.04.52345.777outlieroutlier
P125.51.54.45145.154inlier (masked)outlier
The data, and the squared Mahalanobis distance of every point under each fit. Highlighted: the outliers, plus P8, the honest point that the classical fit gets wrong.
QuantityClassical (all 12 points)Robust (EllipticEnvelope)
μ(3.167, 2.292)(2.750, 2.500)
Σ[[1.931, 0.410], [0.410, 1.102]][[1.263, 1.000], [1.000, 1.050]]
det Σ1.9600.326
Σ⁻¹[[0.562, −0.209], [−0.209, 0.985]][[3.225, −3.071], [−3.071, 3.877]]
correlation0.280.87
eigenvalues λ₁, λ₂2.099, 0.9342.162, 0.151
major axis angle22.3°42.0°
97.5% ellipse semi-axes3.94 × 2.63 (area 32.5)3.99 × 1.05 (area 13.2)
The two fits. The outliers drag the classical centre to the right and down, inflate the spread of x₁, and destroy most of the correlation (0.87 → 0.28).

Where the robust numbers come from

  1. 1MCD subset. h=⌈(12+2+1)/2⌉=8h=\lceil(12+2+1)/2\rceil=8. Of the (128)=495\binom{12}{8}=495 subsets, the one with the smallest covariance determinant (0.14260.1426) is {P1, P2, P3, P4, P6, P7, P9, P10}. Raw centre (2.500,2.125)(2.500, 2.125).
  2. 2Correction. The median raw d2d^2 over all 12 points is 3.3563.356; dividing by the χ22\chi^2_2 median 1.3861.386 gives a factor of 2.4212.421, which inflates the raw covariance.
  3. 3Reweight. With the corrected fit, P5 (d2=3.82d^2=3.82) and P8 (5.805.80) are under 7.3787.378 and come back in; P11 and P12 (≈25\approx 25 and 2323) stay out. The final μ\mu and Σ\Sigma are the plain mean and covariance of P1 to P10: exactly the ten real inliers.
  4. 4Threshold. The sorted robust d2d^2 end …,3.004,3.887,45.154,45.777\dots, 3.004, 3.887, 45.154, 45.777. The 1−2121-\tfrac{2}{12} quantile sits at position 1012⋅11=9.17\tfrac{10}{12}\cdot 11=9.17, so τ=3.887+0.17 (45.154−3.887)=10.765\tau=3.887+0.17\,(45.154-3.887)=10.765. Exactly P11 and P12 exceed it.

One distance by hand: P11 = (5.0, 1.0)

  1. 1Robust. x−μ=(2.25, −1.50)x-\mu=(2.25,\,-1.50). Then d2=3.225(2.25)2+2(−3.071)(2.25)(−1.50)+3.877(−1.50)2=16.32+20.73+8.72=45.78d^2=3.225(2.25)^2+2(-3.071)(2.25)(-1.50)+3.877(-1.50)^2=16.32+20.73+8.72=45.78. The cross term is large and positive because P11 moves against the strong positive correlation: right, but down.
  2. 2Classical. x−μ=(1.833, −1.292)x-\mu=(1.833,\,-1.292). Then d2=0.562(1.833)2+2(−0.209)(1.833)(−1.292)+0.985(−1.292)2=1.89+0.99+1.64=4.52d^2=0.562(1.833)^2+2(-0.209)(1.833)(-1.292)+0.985(-1.292)^2=1.89+0.99+1.64=4.52. With the correlation mostly gone, moving against it barely costs anything.
  3. 3Verdict. Robustly, P11 is 45.78=6.8\sqrt{45.78}=6.8 "standard deviations" out, with a χ22\chi^2_2 tail probability of about 10−1010^{-10}. Classically it is unremarkable: below the 97.5% cut-off of 7.3787.378, and in fact the classical fit flags no point at all at that cut-off. That is masking in its purest form.

Now score two new points with the robust model. (3,3)(3, 3) has d2=0.40d^2=0.40 and f=10.765−0.40=10.36f=10.765-0.40=10.36: deep inside. (4,2)(4, 2) is only 1.281.28 away from μ\mu in plain distance, but it lies off the diagonal: d2=9.85d^2=9.85 and f=0.92f=0.92. It is still an inlier, but only just, and it would be flagged under the stricter χ2,0.9752=7.378\chi^2_{2,0.975}=7.378 cut-off. The decision function tells you more than the label does: how far inside or outside the boundary a point is.

▸ Elliptic envelope · 12 points · 2-Dcontamination 0.167 · τ = 10.8
00112233445566123456789101112Mahalanobis d = √d² (robust)01234567P1P2P3P4P5P6P7P8P9P10P11P12√τχ²₂ 97.5%
robust (MCD + reweight)
μ = (2.75, 2.50)
Σ = [1.26, 1.00; 1.00, 1.05]
τ = 10.8 · flags P11, P12
classical (all points)
μ = (3.17, 2.29)
Σ = [1.93, 0.41; 0.41, 1.10]
τ = 4.46 · flags P8, P11
◆ probe (4.00, 2.00)
d² = 9.85
f(x) = τ − d² = 0.92
inlier (+1)

The classical fit disagrees with the robust one: it misses P12 (masked — the outliers inflated Σ toward themselves) and it wrongly flags P8 (swamped — the shifted μ and weakened correlation make an honest point look extreme).

contamination17%detector

drag the points and the ◆ probe · ringed = raw MCD subset (h = 8) · solid = active envelope d² = τ · dashed = the other fit · faint rings d² = 1, 4

fig.The worked example, live. The solid ellipse is the active detector's envelope d² = τ, with its principal axes (√(τλᵢ) along each eigenvector) and faint rings at d² = 1 and 4; the dashed ellipse is the other fit at its own threshold. Rose points are flagged. The bars show each point's distance d = √d² against √τ and the χ²₂ 97.5% line. Switch to the classical detector and P12 slips inside while P8 is pushed out. Drag P11 and P12 further away: the classical ellipse stretches to follow them, while the robust one does not move. Slide the contamination up and the ellipse shrinks onto the core. Drag the ◆ probe to read the decision function f(x) = τ − d².

In code

python
1import numpy as np
2from scipy.stats import chi2
3from sklearn.covariance import EllipticEnvelope
4
5X = np.array([[1.0, 1.5], [1.5, 1.0], [2.0, 2.5], [2.5, 2.0], [3.0, 3.5], [3.5, 2.5],
6 [3.0, 2.5], [4.5, 4.5], [4.5, 3.5], [2.0, 1.5], [5.0, 1.0], [5.5, 1.5]])
7
8def mahalanobis_sq(X, mu, cov):
9 diff = X - mu
10 return np.einsum("ij,jk,ik->i", diff, np.linalg.inv(cov), diff)
11
12# Classical (non-robust) envelope: sample mean and MLE covariance of all 12 points
13mu, cov = X.mean(axis=0), np.cov(X, rowvar=False, bias=True)
14d2 = mahalanobis_sq(X, mu, cov)
15print(d2.round(2)) # outliers P11, P12 get only 4.52, 4.45
16print((d2 > chi2.ppf(0.975, df=2)).sum()) # 0 -> masked: nothing exceeds 7.38
17
18# Robust envelope (MCD inside), threshold from contamination
19ee = EllipticEnvelope(contamination=2/12, random_state=0).fit(X)
20print(ee.location_, ee.covariance_) # [2.75 2.5] [[1.2625 1.] [1. 1.05]]
21print(ee.mahalanobis(X).round(2)) # squared distances: P11 45.78, P12 45.15
22print(-ee.offset_) # threshold tau = 10.76
23print(ee.predict(X)) # -1 for P11 and P12 only
24print(ee.decision_function([[3, 3], [4, 2]]).round(3)) # tau - d^2: [10.361 0.918]
scikit-learn on the worked example, against the classical fit. The comments show the actual outputs.
python
1import numpy as np
2from itertools import combinations
3from scipy.stats import chi2
4
5class EllipticEnvelopeScratch:
6 def __init__(self, contamination=0.1):
7 self.contamination = contamination
8
9 def fit(self, X):
10 n, p = X.shape
11 h = int(np.ceil((n + p + 1) / 2))
12 # 1. MCD: the h points whose covariance has the smallest determinant
13 # (exhaustive here; FastMCD searches with random starts + C-steps)
14 best = min(combinations(range(n), h),
15 key=lambda s: np.linalg.det(np.cov(X[list(s)], rowvar=False, bias=True)))
16 S = X[list(best)]
17 mu, cov = S.mean(axis=0), np.cov(S, rowvar=False, bias=True)
18 # 2. consistency: rescale so the median d^2 matches the chi2_p median
19 d2 = mahalanobis_sq(X, mu, cov)
20 cov = cov * np.median(d2) / chi2.ppf(0.5, p)
21 # 3. reweight: refit on every point inside the 97.5% chi2 ellipse
22 keep = mahalanobis_sq(X, mu, cov) < chi2.ppf(0.975, p)
23 self.location_ = X[keep].mean(axis=0)
24 self.covariance_ = np.cov(X[keep], rowvar=False, bias=True)
25 # 4. threshold: the (1 - contamination) quantile of the training d^2
26 self.threshold_ = np.percentile(self.mahalanobis(X), 100 * (1 - self.contamination))
27 return self
28
29 def mahalanobis(self, X):
30 return mahalanobis_sq(np.asarray(X, float), self.location_, self.covariance_)
31
32 def decision_function(self, X):
33 return self.threshold_ - self.mahalanobis(X) # > 0 inside, < 0 outside
34
35 def predict(self, X):
36 return np.where(self.decision_function(X) >= 0, 1, -1)
37
38m = EllipticEnvelopeScratch(contamination=2/12).fit(X)
39print(m.location_, m.threshold_.round(3)) # [2.75 2.5] 10.765
40print(m.predict(X)) # [ 1 1 1 1 1 1 1 1 1 1 -1 -1]
From scratch: exhaustive MCD (fine for tiny n), correction, reweighting and the contamination threshold. It reproduces scikit-learn's numbers on this data.

Use when

  • +The normal data really are one roughly Gaussian blob: sensor readings, measurement errors, process-control variables.
  • +You want a transparent, nearly instant detector. The model is just μ, Σ and τ, and a score is one quadratic form.
  • +Correlations matter: it catches points that are ordinary in every single feature but unusual in combination.
  • +Features have different units: Mahalanobis distance is affine invariant, so no scaling is needed.
  • +The score has a calibrated meaning: under the Gaussian model, d² follows χ²_p, so a cut-off maps to a false-alarm rate.

Avoid when

  • −The inliers are multimodal, curved or heavy-tailed: a single ellipse covers empty space and misses structure.
  • −High dimensions: Σ has p(p+1)/2 parameters, MCD needs n well above p, and the fit becomes slow and unstable.
  • −Features are discrete, binary or highly skewed, which makes the Gaussian assumption a poor fit.
  • −Contamination is unknown, since τ hinges on it; a wrong guess flags too many or too few.
  • −Features are collinear: Σ becomes singular and the distances blow up.

Where it's used

Industrial monitoring

Multivariate statistical process control: a robust Hotelling T² (which is d²) on correlated sensor channels flags a drifting machine early.

Data cleaning

Screen a dataset for gross errors and entry mistakes before fitting a regression or a classifier, especially when the features are correlated.

Finance & fraud

Flag transactions or daily returns whose combination of features (amount, frequency, time) is unusual under the typical joint behaviour.

Novelty detection

Fit on clean reference data, then score incoming points with decision_function, for example to catch out-of-distribution inputs to a deployed model.

Check yourself

In 2-D, a point has squared Mahalanobis distance d² = 9 under a Gaussian model. Roughly what fraction of genuine inliers lie at least this far out?

Check yourself

You refit an EllipticEnvelope with contamination raised from 0.05 to 0.2. What changes?

Check yourself

In the worked example the classical fit gives the outlier P12 a d² of only 4.45, while the robust fit gives 45.15. Why?

Check yourself

Under a fitted envelope, the major axis of the ellipse d2=τd^2=\tau has length (end to end)…

Remember

  • ✓Model the inliers as one Gaussian: score each point by d2=(x−μ)⊤Σ−1(x−μ)d^2=(x-\mu)^\top\Sigma^{-1}(x-\mu) and flag it if d2>τd^2>\tau.
  • ✓For Gaussian inliers d2∼χp2d^2\sim\chi^2_p. Its contours are ellipses with semi-axes τλi\sqrt{\tau\lambda_i} along the eigenvectors of Σ\Sigma.
  • ✓contamination only sets τ\tau, as the (1−c)(1-c) quantile of the training d2d^2. The decision function is f(x)=τ−d2(x)f(x)=\tau-d^2(x), and its sign gives the label.
  • ✓Classical mean and covariance are masked by the outliers they should detect, and they can swamp honest points. EllipticEnvelope fits a robust MCD estimate instead (see Robust Covariance).
  • ✓Works for unimodal, roughly Gaussian data with n well above p. For multimodal, curved or high-dimensional data, use other detectors.
← back to ML Algorithms roadmap