ML0098 Outlier Detection

How do you detect outliers in a dataset, from simple statistical rules to Isolation Forest and beyond?

Answer

Outlier detection ranges from simple statistical rules to model-based methods, and the right choice depends on the data dimensionality, distribution, and whether you have labels. For univariate data, the two standard rules are the z-score method (flag points more than 2 or 3 standard deviations from the mean) and the IQR rule (flag points below Q1 – 1.5*IQR or above Q3 + 1.5*IQR); the IQR rule is more robust because the mean and std are themselves inflated by outliers. For multivariate and high-dimensional data, statistical rules break down, and model-based methods take over: Isolation Forest isolates anomalies by random splits (outliers need fewer splits to isolate), Local Outlier Factor (LOF) compares local density to neighbors, and autoencoders flag points with high reconstruction error. Production data quality tools combine Isolation Forest for anomaly detection with robust statistics (median plus 5 times the robust standard deviation) for per-column outlier flagging, and a 2024 PMLR paper introduced HPOD, the first continuous hyperparameter search method for unsupervised outlier detection, achieving 58% and 66% improvement over default LOF and Isolation Forest hyperparameters.

(1) Statistical (Univariate): z-score flags points beyond 2-3 sigma from the mean; IQR flags points outside Q1 – 1.5*IQR to Q3 + 1.5*IQR; IQR is robust to the outliers themselves, z-score is not.
(2) Model-Based (Multivariate): Isolation Forest isolates anomalies with fewer random splits (O(n log n), scales well); LOF compares local density to k-nearest neighbors (O(n^2) naive, good for local anomalies); autoencoders flag high reconstruction error (good for complex distributions but needs training data).
(3) Production (Data Quality Tools + HPOD): production data quality reports use Isolation Forest with anomaly scores plus robust per-column statistics (median plus or minus 5*RSTD); HPOD (PMLR 2024) capitalizes on prior benchmark performance to tune LOF and Isolation Forest hyperparameters without labels, improving detection by 58-66%.

Four panels: z-score shows a bell curve with the tails beyond plus and minus 3 sigma shaded and labelled flagged; IQR shows a horizontal boxplot with outliers drawn as X markers past the whiskers; Isolation Forest shows a two-feature scatter where far-from-centre points are marked as isolated; LOF shows two dense clusters with a single low-local-density point between them marked

Figure 1: Four outlier detection methods: z-score flags points beyond 3 sigma (sensitive to outliers itself), IQR uses the interquartile range (robust), Isolation Forest isolates anomalies with few random splits, and LOF compares local density to neighbors.

The practical workflow is to start simple and escalate. First, visualize: boxplots per feature and scatter plots of feature pairs reveal obvious outliers. Second, apply the IQR rule per feature for a quick, robust baseline. Third, for multivariate outliers that no single feature reveals, run Isolation Forest (scales to high dimensions, O(n log n), no distributional assumption) and inspect the anomaly score distribution. Fourth, if anomalies are local (dense regions with sparse sub-regions), use LOF. Fifth, for complex nonlinear structure (images, text embeddings), train an autoencoder on the majority class and flag high reconstruction error. Always investigate flagged points before removing them: an outlier may be a genuine rare event (fraud, anomaly) rather than bad data, and domain expertise is the final arbiter. Production data quality tools automate this by generating a Data Quality and Insights Report that combines Isolation Forest anomaly scores with per-column robust statistics and time-series decomposition for temporal anomalies.

Mathematical Formulation:
z_i = \frac{x_i - \mu}{\sigma},\quad |z_i| > 3 \Rightarrow \text{outlier}
\text{IQR} = Q_3 - Q_1,\quad x_i \leq Q_1 - 1.5\,\text{IQR} \;\text{or}\; x_i \geq Q_3 + 1.5\,\text{IQR} \Rightarrow \text{outlier}
s(x, n) = 2^{-\mathbb{E}[h(x)]\,/\,c(n)}

Where:

  • z_i is the z-score of point x_i; \mu and \sigma are the mean and standard deviation of the feature. The z-score method is not robust because outliers inflate \mu and \sigma, masking themselves.
  • Q_1 and Q_3 are the 25th and 75th percentiles; the IQR rule is robust because percentiles are unaffected by extreme values. A related robust rule used in production: flag values outside median plus or minus 5 times the robust standard deviation (RSTD).
  • s(x, n) is the Isolation Forest anomaly score. \mathbb{E}[h(x)] is the average path length needed to isolate x across the trees and c(n) is the expected path length for n points, so outliers isolate in fewer splits, which drives the exponent up and pushes s toward 1, while normal points sit near 0.5 or below.
MethodTypeComplexityBest For
z-scoreUnivariate, parametricO(n)Approximately normal data
IQR ruleUnivariate, robustO(n log n)Skewed data, robust baseline
Isolation ForestMultivariate, model-basedO(n log n)High-dimensional, scalable
LOFMultivariate, density-basedO(n^2) naiveLocal anomalies, varying density
AutoencoderMultivariate, reconstructionTraining + O(nd) inferenceComplex nonlinear (images, embeddings)

Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *