Outlier detection by projection outlyingness and depth

Detectors that flag multivariate outliers by how far each observation lies from the bulk of the data along many one-dimensional projections, in robustmodels.outliers.

Choosing a detector

Class

Data

Notes

StahelDonohoOutlyingness

Roughly symmetric

Median and MAD of each projection; fast

AdjustedOutlyingness

Skewed

Adjusted boxplot of each projection; finds outliers that skewness masks

DirectionalOutlyingness

Skewed

Side-specific one-step robust scales; no medcouple required

All three need more observations than variables, are affine invariant, and make no assumption of elliptical shape. covariance.MCD with robust distances is an alternative for elliptical data.

Use

The detectors follow scikit-learn’s outlier detection API, as sklearn.covariance.EllipticEnvelope:

>>> import numpy as np
>>> from robustmodels.outliers import StahelDonohoOutlyingness
>>> X = np.random.default_rng(0).normal(size=(100, 2))
>>> X[:5] += 6  # five outliers
>>> sdo = StahelDonohoOutlyingness(random_state=0).fit(X)
>>> np.flatnonzero(sdo.predict(X) == -1).tolist()
[0, 1, 2, 3, 4]
>>> sdo.score_samples([[0, 0], [6, 6]]).round(2).tolist()  # projection depth
[0.77, 0.12]

Attribute or method

Meaning

outlyingness(X), outlyingness_

Outlyingness of new and training observations

score_samples(X)

Projection depth 1 / (1 + outlyingness), in [0, 1]; higher is more central, zero for infinite outlyingness

cutoff_, offset_

Outlyingness cutoff and the depth at the cutoff

decision_function(X)

score_samples(X) - offset_; negative for outliers

predict(X), fit_predict(X)

-1 for outliers, 1 for inliers

directions_

The unit directions searched

New observations are scored against the fitted data, which keeps the directions and the summaries of each projection.

Use plots.outlyingness_plot to inspect the scores, cutoff and flagged counts, including infinite outlyingness.

Skewed data

On a skewed distribution the long tail inflates a symmetric scale, so Stahel-Donoho outlyingness can miss outliers on the short side and flag regular points in the tail. Adjusted outlyingness measures each side with the whisker of the skewness-adjusted boxplot:

>>> from robustmodels.outliers import AdjustedOutlyingness
>>> X = np.random.default_rng(0).lognormal(size=(100, 2))
>>> X[:5] = [-3, -3]  # five outliers on the short side
>>> ao = AdjustedOutlyingness(random_state=0).fit(X)
>>> np.flatnonzero(ao.predict(X) == -1).tolist()
[0, 1, 2, 3, 4]
>>> sdo = StahelDonohoOutlyingness(random_state=0).fit(X)
>>> np.flatnonzero(sdo.predict(X) == -1).tolist()
[37, 39, 82]

Reproducibility

With at most n_directions hyperplanes through p observations, all of them are used and the result is deterministic. Otherwise directions are random: pass random_state to get the same result on every run.

Numerical limits and sources

The outlyingness is the largest standardized projection over a finite set of directions, normal to hyperplanes through p observations (250 times the number of variables by default, as R’s mrfDepth); it can only underestimate the supremum over all directions. In the tests, on two-dimensional data, it reaches at least 95% of the supremum found by searching 100,000 directions. Stahel–Donoho and the log-scale cutoff share the calculation used by mad. Both use the normal consistency constant, with the finite-sample correction disabled.

Observations are flagged when their outlyingness exceeds exp(median(L) + mad(L) z) - 0.1, with L = log(0.1 + outlyingness) and z the 0.995 normal quantile. This is an approximate adaptive rule, not an exact 0.5% false-positive guarantee. The directional-outlyingness source derives this log-scale rule in equation (7).

Adjusted outlyingness computes a medcouple for every direction. Fitting 1000 observations of five variables (1250 directions) takes a few seconds, against a fraction of a second for Stahel-Donoho outlyingness. Quartiles use the (k - 0.5) / n rule (R’s type 5), as mrfDepth.

The observations that define a direction, and any others on the same hyperplane, have equal projections. Rounding can break those ties, which the medcouple treats specially, so projections that differ only by rounding are merged. Exact-arithmetic projection checks verify the selected tie convention; software that uses another convention can produce different scores. If more than half of the projections on a direction coincide, the scale is zero and the other observations would be infinitely outlying on that direction. As robustbase’s adjOutlyingness, such ratios are ignored and each observation takes its largest finite ratio, so discrete data (Likert items, counts) are not mostly flagged. An observation with no finite ratio, as when more than half the sample is one point, keeps infinite outlyingness and is flagged.

Sources: Stahel-Donoho outlyingness, projection depth, adjusted outlyingness and the adjusted boxplot.

Directional outlyingness

DirectionalOutlyingness uses a separate one-step robust scale above and below each projection median (Rousseeuw, Raymaekers and Hubert, 2018). It needs no medcouple and uses the same fitted depth, cutoff and prediction interface as the other detectors. Use it for skewed data where a single symmetric scale hides short-side outliers. plots.outlyingness_plot accepts all three detector classes.

The half samples include the median for odd sample sizes, as in the paper, and the Gaussian calibration is evaluated analytically. mrfDepth 1.0.17 excludes the odd median and rounds the calibration constant to 1.54, so its scores use a different numerical convention. Tests integrate the scale expectation independently, check normal quantiles and affine transformations, and run scikit-learn’s estimator checks. Equations (2)–(3) of Rousseeuw, Raymaekers and Hubert define the scales. Their 25% implosion/explosion breakdown results for the side scales assume a continuous symmetric unimodal model with density strictly decreasing away from its center. They are not a 50% breakdown guarantee for these scales. Zero side scales are skipped as zero scales above. The log cutoff uses only finite training scores, and observations with infinite scores are flagged.

Reference

class StahelDonohoOutlyingness(*, n_directions=None, random_state=None)[source]

Outlier detection by Stahel-Donoho outlyingness (Stahel 1981; Donoho 1982).

The outlyingness of x is the largest |u'x - med(u'X)| / mad(u'X) over directions u, with the MAD consistent at the normal model; the projection depth is 1 / (1 + outlyingness) (Zuo 2003). Directions are normal to hyperplanes through p observations, which makes the result affine invariant; with at most n_directions such hyperplanes all are used, otherwise a random selection, as R’s mrfDepth. Observations with outlyingness above cutoff_ are outliers. For skewed data use AdjustedOutlyingness.

Parameters:
  • n_directions (int, default None) – Number of directions; 250 * n_features by default.

  • random_state (int, numpy.random.Generator, numpy.random.RandomState or None) – Seed for the random directions.

outlyingness_

Outlyingness of the training data.

Type:

ndarray of shape (n_samples,)

cutoff_

exp(median(L) + mad(L) * z) - 0.1, with L = log(0.1 + outlyingness_) and z the 0.995 normal quantile, as mrfDepth.

Type:

float

offset_

Depth at the cutoff, 1 / (1 + cutoff_).

Type:

float

directions_

Unit directions.

Type:

ndarray of shape (n_features, n_directions)

centers_, scales_

Median and MAD of the training projections on each direction.

Type:

ndarray of shape (n_directions,)

References

Stahel, W. A. (1981). Robuste Schätzungen: infinitesimale Optimalität und Schätzungen von Kovarianzmatrizen. PhD thesis, ETH Zürich.

Donoho, D. L. (1982). Breakdown properties of multivariate location estimators. Qualifying paper, Harvard University.

Zuo, Y. (2003). Projection-based depth functions and associated medians. The Annals of Statistics, 31(5), 1460-1490.

Segaert, P., Hubert, M., Rousseeuw, P. J. and Raymaekers, J. (2018). mrfDepth: Depth measures in multivariate, regression and functional settings. R package.

Examples

>>> import numpy as np
>>> from robustmodels.outliers import StahelDonohoOutlyingness
>>> X = np.random.default_rng(0).normal(size=(100, 2))
>>> X[:5] += 6  # five outliers
>>> sdo = StahelDonohoOutlyingness(random_state=0).fit(X)
>>> np.flatnonzero(sdo.predict(X) == -1).tolist()
[0, 1, 2, 3, 4]
decision_function(X)

score_samples(X) - offset_; negative for outliers.

fit(X, y=None)

Fit to X of shape (n_samples, n_features); y is ignored. Returns self.

outlyingness(X)

Outlyingness of each row of X: the largest standardized projection.

predict(X)

-1 for outliers (outlyingness above cutoff_) and 1 for inliers.

score_samples(X)

Projection depth in [0, 1]; higher is more central, 0 for infinite outlyingness.

class AdjustedOutlyingness(*, n_directions=None, random_state=None)[source]

Outlier detection for skewed data by adjusted outlyingness (Hubert & Van der Veeken 2008).

On each direction u, the distance of u'x to the median of the projections is divided by the distance from the median to the whisker of the skewness-adjusted boxplot on the same side (Hubert & Vandervieren 2008), which uses the medcouple MC: the fences are Q3 + 1.5 exp(3 MC) IQR and Q1 - 1.5 exp(-4 MC) IQR for MC >= 0 (exp(4 MC) and exp(-3 MC) otherwise). The outlyingness is the largest ratio over the directions, and 1 / (1 + outlyingness) is the skew-adjusted projection depth. Because the scale on each side follows the skewness, outliers on the short side of a skewed distribution are not masked by the long tail, as they are for StahelDonohoOutlyingness. The medcouple follows its definition, as robustbase; mrfDepth’s differs when projections are tied, which affine directions always produce. Quartiles use the (k - 0.5) / n rule (R’s type 5), as mrfDepth; directions and cutoff as in StahelDonohoOutlyingness.

Parameters:
  • n_directions (int, default None) – Number of directions; 250 * n_features by default.

  • random_state (int, numpy.random.Generator, numpy.random.RandomState or None) – Seed for the random directions.

outlyingness_

Adjusted outlyingness of the training data.

Type:

ndarray of shape (n_samples,)

cutoff_

exp(median(L) + mad(L) * z) - 0.1, with L = log(0.1 + outlyingness_) and z the 0.995 normal quantile, as mrfDepth.

Type:

float

offset_

Depth at the cutoff, 1 / (1 + cutoff_).

Type:

float

directions_

Unit directions.

Type:

ndarray of shape (n_features, n_directions)

centers_

Median of the training projections on each direction.

Type:

ndarray of shape (n_directions,)

lower_whiskers_, upper_whiskers_

Distances from the median to the adjusted boxplot whiskers.

Type:

ndarray of shape (n_directions,)

References

Hubert, M. and Van der Veeken, S. (2008). Outlier detection for skewed data. Journal of Chemometrics, 22(3-4), 235-246.

Hubert, M. and Vandervieren, E. (2008). An adjusted boxplot for skewed distributions. Computational Statistics & Data Analysis, 52(12), 5186-5201.

Examples

>>> import numpy as np
>>> from robustmodels.outliers import AdjustedOutlyingness
>>> X = np.random.default_rng(0).lognormal(size=(100, 2))
>>> X[:5] = [-3, -3]  # five outliers on the short side
>>> ao = AdjustedOutlyingness(random_state=0).fit(X)
>>> np.flatnonzero(ao.predict(X) == -1).tolist()
[0, 1, 2, 3, 4]
decision_function(X)

score_samples(X) - offset_; negative for outliers.

fit(X, y=None)

Fit to X of shape (n_samples, n_features); y is ignored. Returns self.

outlyingness(X)

Outlyingness of each row of X: the largest standardized projection.

predict(X)

-1 for outliers (outlyingness above cutoff_) and 1 for inliers.

score_samples(X)

Projection depth in [0, 1]; higher is more central, 0 for infinite outlyingness.

class DirectionalOutlyingness(*, n_directions=None, random_state=None)[source]

Outlier detection for skewed data by directional outlyingness (Rousseeuw et al. 2018).

On each direction u, the distance of u'x to the median of the projections is divided by a scale of the half sample on the same side: s_a above the median and s_b below. Each is a one-step M-estimator of scale of the deviations from the median, s = s_o sqrt(sum(rho(z_i / s_o)) / (2 alpha h)), with s_o the median of the deviations divided by the 0.75 normal quantile, rho(t) = min((t / 2.1)^2, 1) and h = (n + 1) // 2 deviations per side; alpha makes both scales consistent at the normal model. The outlyingness is the largest ratio over the directions. Unlike AdjustedOutlyingness it needs no medcouple, so it is faster and its scales cannot be inflated by a few far points. R’s mrfDepth uses 1.54 for 1 / (2 sqrt(alpha)) = 1.534 and leaves the median out of the half samples when n is odd; this class follows the paper. Directions and cutoff as in StahelDonohoOutlyingness.

Parameters:
  • n_directions (int, default None) – Number of directions; 250 * n_features by default.

  • random_state (int, numpy.random.Generator, numpy.random.RandomState or None) – Seed for the random directions.

outlyingness_

Directional outlyingness of the training data.

Type:

ndarray of shape (n_samples,)

cutoff_

exp(median(L) + mad(L) * z) - 0.1, with L = log(0.1 + outlyingness_) and z the 0.995 normal quantile.

Type:

float

offset_

Depth at the cutoff, 1 / (1 + cutoff_).

Type:

float

directions_

Unit directions.

Type:

ndarray of shape (n_features, n_directions)

centers_

Median of the training projections on each direction.

Type:

ndarray of shape (n_directions,)

lower_scales_, upper_scales_

Scales below and above the median.

Type:

ndarray of shape (n_directions,)

References

Rousseeuw, P. J., Raymaekers, J. and Hubert, M. (2018). A measure of directional outlyingness with applications to image data and video. Journal of Computational and Graphical Statistics, 27(2), 345-359.

Examples

>>> import numpy as np
>>> from robustmodels.outliers import DirectionalOutlyingness
>>> X = np.random.default_rng(0).lognormal(size=(100, 2))
>>> X[:5] = [-3, -3]  # five outliers on the short side
>>> do = DirectionalOutlyingness(random_state=0).fit(X)
>>> np.flatnonzero(do.predict(X) == -1).tolist()
[0, 1, 2, 3, 4]
decision_function(X)

score_samples(X) - offset_; negative for outliers.

fit(X, y=None)

Fit to X of shape (n_samples, n_features); y is ignored. Returns self.

outlyingness(X)

Outlyingness of each row of X: the largest standardized projection.

predict(X)

-1 for outliers (outlyingness above cutoff_) and 1 for inliers.

score_samples(X)

Projection depth in [0, 1]; higher is more central, 0 for infinite outlyingness.