Foundations to Frontier
Tests
D. Classical Machine Learning › D10 Anomaly and Outlier Detection

Statistical Outlier Tests: z-score, IQR, Grubbs, Generalized ESD

The oldest outlier detectors are simple univariate rules: flag values more than three standard deviations from the mean, values beyond Tukey's quartile fences, or values whose robust z-score, based on the median and the median absolute deviation, exceeds 3.5. Formal tests attach a significance level: Grubbs' test asks whether the single most extreme value is too extreme for a normal sample of that size, and Rosner's generalized extreme Studentized deviate (ESD) test extends it to up to r outliers. Their two classic failure modes are masking, where a group of outliers inflates the mean and standard deviation enough to hide itself, and swamping, where ordinary points are flagged because the data are skewed or the outliers drag the centre. This page covers each rule and its threshold, the Samuelson bound on how large a z-score can be, the Grubbs and ESD critical values, masking and swamping, and when to transform data or switch to robust and multivariate methods. In the exercises you implement all five rules; then you measure masking with clusters of outliers and false alarms on skewed data.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Which readings don’t belong?

A temperature sensor reports 21, 22, 20, 21, 87, 22 degrees. The 87 is almost certainly a fault. Outlier tests make that judgement automatic: they measure how far each value sits from the bulk of the data and flag the ones that are too far. The simplest rule says “more than three standard deviations from the average”. It works well for one bad reading, but a group of bad readings can fool it, because together they drag the average and the spread toward themselves.

Outlier tests: when outliers hide each other
z-score > 3 (0)modified z > 3.5 (6)IQR fences (1.5) (6)repeated Grubbs (0)generalized ESD (r = 15) (6)0
sample s.d. with the outliers 1.67 (true 1) · largest |z| 2.74 against the Grubbs critical value 3.24
Orange rings are the planted outliers; filled = flagged (black fill = a false alarm). One or two outliers are caught by every rule. As the cluster grows, the outliers inflate the mean and standard deviation, so their own z-scores shrink: the z-score rule and repeated Grubbs both miss all of them at once (masking). The median/MAD and quartile rules resist this, and generalized ESD looks ahead through 15 candidates, so it still finds the cluster. IQR fences also flag the odd ordinary point.
Why engineers care

Telemetry validation, sensor fault flags, test-data screening and quality control all start with univariate outlier rules, because they are cheap, explainable and easy to certify. Knowing when they fail, through masking by clustered faults, swamping on skewed signals, and their normality assumptions, decides whether a ground-station alarm is trustworthy.

📝 Quick check (Level 0)
0/3 answered
  1. 1. The classic z-score rule flags values more than … standard deviations from the mean.
  2. 2. Several outliers together can hide because they…
  3. 3. The median and the median absolute deviation are used because they are…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

The rules by hand

z-score. zᵢ = (xᵢ − x̄)/s, using the sample standard deviation. Flag |z| > 3. A value 5 standard deviations out has z = 5.

Modified (robust) z-score. Mᵢ = 0.6745 (xᵢ − median)/MAD, with MAD = median |xᵢ − median|; Iglewicz and Hoaglin suggest flagging |M| > 3.5. The factor 0.6745 makes MAD consistent with σ for normal data. For 1, 2, 3, 4, 100: median 3, MAD 1, so 100 gets M = 0.6745 · 97 = 65.4.

Tukey’s fences. Flag values below Q1 − 1.5 IQR or above Q3 + 1.5 IQR. For 1 … 9, Q1 = 3 and Q3 = 7, so the fences are −3 and 13.

Grubbs’ test (one outlier, normal data). G = max |xᵢ − x̄|/s, compared with the critical value

G_crit = ((n − 1)/√n) · √(t²/(n − 2 + t²)), where t is the t quantile with n − 2 degrees of freedom at α/(2n).

  • At α = 0.05, n = 10 gives 2.290, n = 20 gives 2.708 and n = 100 gives 3.384.
  • For the classic sample 2, 4, 4, 4, 5, 5, 7, 9: mean 5, s = 2.138, G = 4/2.138 = 1.871, not an outlier.

How big can a z-score be? However extreme one value is, |z| ≤ (n − 1)/√n, the Samuelson bound. For n = 10 that is 2.846, so with 10 or fewer points the “|z| > 3” rule can never fire.

Generalized ESD (Rosner, 1983), for up to r outliers:

  1. For i = 1 … r, compute Rᵢ, the largest |z| among the remaining points, then remove that point.
  2. Compute λᵢ, the Grubbs-type critical value for the current sample size.
  3. The number of outliers is the largest i with Rᵢ > λᵢ.

The look-ahead in step 3 is what defeats masking: R₁ may fall below λ₁ even when R₃ exceeds λ₃.

📝 Quick check (Level 1)
0/3 answered
  1. 1. Tukey's upper fence is…
  2. 2. Grubbs' test assumes the data are…
  3. 3. With n = 8, can any value have |z| > 3?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.

Masking, swamping and robustness

Masking. With k outliers at distance a, the sample mean shifts by about ka/n, and the variance grows by about ka²/n. Every outlier’s z-score falls; once k is large enough, none exceeds 3. Repeated Grubbs (test, remove, repeat) fails the same way: its first test sees a deflated maximum, so it never starts.

Swamping. Ordinary points are flagged because the model of “normal” is wrong: skewed or heavy-tailed data have long tails by nature, or outliers on one side pull the centre so that the other side looks extreme.

Robustness. The median and MAD have a 50% breakdown point: up to half the data can be arbitrary before they become arbitrary. The mean and standard deviation break down with a single point. Quartiles have a 25% breakdown point.

Exercise 1 (100 values from N(10, 1) with outliers 16, 15.5 and 4 planted):

  • all five rules flagged exactly the three outliers;
  • Grubbs’ G = 4.42 against a critical value of 3.384;
  • generalized ESD had R₁ … R₄ = 4.42, 4.64, 4.84, 2.49 against λ ≈ 3.38, so it found 3 outliers.

R grew from step 1 to step 3: removing one outlier made the next look more extreme, a small sign of masking.

from scipy import stats
z = stats.zscore(x, ddof=1); flags = np.abs(z) > 3
from statsmodels.robust.scale import mad                   # MAD scaled to σ; use 0.6745·|x − med|/MAD_raw for Iglewicz–Hoaglin
🐍 Exercise 1: five outlier rules
Implement the z-score, modified z-score, IQR fences, Grubbs' test with its t-based critical value, and the generalized ESD test; check them on normal data with three planted outliers.
📝 Quick check (Level 2)
0/3 answered
  1. 1. The breakdown point of the median is…
  2. 2. Repeated Grubbs fails against a cluster of outliers because…
  3. 3. Generalized ESD decides the number of outliers by…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Samuelson’s inequality. For any sample, |xᵢ − x̄| ≤ s(n − 1)/√n. Equality holds when all other points coincide. It caps every z-score and explains why small samples can never trigger large-z rules.

Grubbs’ critical value. If the data are normal, the Studentized deviation of a given point, transformed as t = G√(n(n − 2)/((n − 1)² − nG²)), follows a t distribution with n − 2 degrees of freedom. A Bonferroni correction over n candidates (α/n, split over two tails) gives the critical value above. Rosner’s λᵢ is the same formula applied to the shrinking sample. Rosner’s simulations showed the approximation is very accurate for n ≥ 25 and reasonable for n ≥ 15.

Multiple testing. Flagging with |z| > 3 in a clean normal sample of 10 000 values gives about 27 false alarms on average (two-sided tail probability 0.0027). Grubbs and ESD control the probability of any false alarm in the whole sample; fixed-threshold rules do not.

Interview questions.

  • Mean/s.d. rule or median/MAD? Median/MAD for anything that might contain several bad points, and always for automated pipelines. Mean/s.d. only when at most one outlier is plausible and the data are close to normal.
  • The data are skewed (durations, counts, power). What now? Transform first (log or Box–Cox), or use a skew-aware rule such as adjusted box plots (medcouple), or a model of the actual distribution. Untransformed normal rules will swamp.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Expected false alarms from |z| > 3 in 10 000 clean normal values?
  2. 2. Grubbs' critical value includes a correction for…
  3. 3. For skewed positive data, a first step is to…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

How many clustered outliers break each rule?

History.

  • W. Chauvenet (1863) and B. Peirce (1852) proposed early rejection criteria.
  • F. Grubbs (1950, 1969) introduced the test that bears his name.
  • J. Tukey (1977) introduced box-plot fences.
  • B. Rosner (1983) introduced the generalized ESD.
  • B. Iglewicz and D. Hoaglin (1993) wrote the standard guide and the modified z-score.

Exercise 2. 100 standard normal values plus k outliers tightly clustered at 5σ, 40 datasets per k. Each cell shows recall (fraction of outliers flagged), then mean false alarms per dataset.

k z-score modified z IQR repeated Grubbs generalized ESD
1 1.00, 0.0 1.00, 0.03 1.00, 0.85 1.00, 0.0 1.00, 0.0
3 1.00, 0.0 1.00, 0.05 1.00, 0.82 1.00, 0.05 1.00, 0.05
8 0.00, 0.0 1.00, 0.03 1.00, 0.30 0.00, 0.0 1.00, 0.07
15 0.00, 0.0 0.81, 0.03 0.98, 0.20 0.00, 0.0 1.00, 0.05

On lognormal data with no outliers, the mean false alarms per 100 values were: z-score 2.2, modified z 8.38, IQR 7.92, repeated Grubbs 5.53, generalized ESD 6.92.

  • Masking is abrupt. With 8 outliers out of 108 (7%), the z-score rule and repeated Grubbs went from catching every outlier to catching none.
  • Generalized ESD caught all 15 (14% contamination) with few false alarms, because its look-ahead finds the step at which removing the cluster makes the remaining tests significant.
  • The median/MAD rule degraded only at 15 (recall 0.81): the cluster shifts the median and inflates the MAD a little. IQR stayed above 0.98, but always raised more false alarms.
  • On skewed data, every normal-theory rule swamped, with 2–8 false alarms per 100 clean points. The robust rules were the worst offenders, because the long right tail is real data that they correctly measure as far from the median. Skewness has to be handled before outlier rules are applied.
🐍 Exercise 2 (research): masking and swamping
For 100 normal values plus k = 1, 3, 8, 15 clustered outliers at 5σ, measure recall and false alarms of the z-score, modified z, IQR, repeated Grubbs and generalized ESD (r = 20); then count false alarms on lognormal data with no outliers.

Outlier tests in AI, robotics and aerospace

  • Telemetry limit checks: robust z-scores on each channel with MAD estimated over a sliding window catch spikes without being masked by bursts.
  • Test-stand data screening: generalized ESD on repeated measurements flags bad runs with a stated false-alarm level.
  • Sensor voting: with three or more redundant sensors, the median and MAD across sensors identify a faulty unit at each time step.

References

  1. F. E. Grubbs (1969). “Procedures for detecting outlying observations in samples.” Technometrics 11(1).
  2. B. Rosner (1983). “Percentage points for a generalized ESD many-outlier procedure.” Technometrics 25(2).
  3. B. Iglewicz, D. C. Hoaglin (1993). How to Detect and Handle Outliers. ASQC Quality Press.
  4. J. W. Tukey (1977). Exploratory Data Analysis. Addison-Wesley.
  5. P. A. Samuelson (1968). “How deviant can you be?” Journal of the American Statistical Association 63(324).