Outlier Detection Methods

Learn IQR, z-score, MAD, and isolation forest methods for detecting outliers in data

Outlier Detection Methods

Outliers are data points that differ significantly from the rest. Move the test point and switch the detection method below to see how each technique draws the line on the same data.

The Data

Every method below works on the same sample of 30 measurements: 29 fixed values, roughly bell-shaped around 50 with one mildly unusual value at 72, plus a test point you control.

Test point xx: 60

From these 30 values: mean μ\mu = 51.07, standard deviation σ\sigma = 6.59, median = 50.50, Q1Q_1 = 47, Q3Q_3 = 54, and MAD (the median distance from the median) = 3.50.

Detection method:

The plot shows the data as dots along the bottom row and, above them, each method's normal range computed from those same 30 values. A value outside a band is flagged by that method; the markers on each row turn red for flagged and green for normal, for both xx and the value 72.

One Dataset, Three Outlier Fences

72x30405060700123ValueRow (0 = the data)Row 3: z-score fence, mean +/- z SDRow 2: IQR fence, Q1 - k IQR to Q3 + k IQRRow 1: MAD fence, median +/- t MAD / 0.6745

Right now the z-score rule flags 1 of the 30 values, the IQR rule 1, and the MAD rule 1. Drag xx out past 90 and watch the top band: the outlier inflates σ\sigma, the z-score fence stretches past 72, and 72 is no longer flagged. This is masking: one outlier hides another from mean-and-SD rules, while the median-based IQR and MAD fences barely move.

IQR (Interquartile Range) Method

The IQR method uses quartiles instead of μ\mu and σ\sigma, making it robust to existing outliers.

Q1Q_1 (25th percentile): 47

Q3Q_3 (75th percentile): 54

Multiplier kk: 1.5

IQR=Q3−Q1=0−0=0\text{IQR} = Q_3 - Q_1 = {\cmv{computed}{dataQ3}} - {\cmv{computed}{dataQ1}} = {\cmv{computed}{iqr}} Lower=Q1−k⋅IQR=0\text{Lower} = Q_1 - k \cdot \text{IQR} = {\cmv{computed}{lowerBoundIQR}} Upper=Q3+k⋅IQR=0\text{Upper} = Q_3 + k \cdot \text{IQR} = {\cmv{computed}{upperBoundIQR}}

Point x=x = 60 is NORMAL.

Drag kk (1.5) from 1.5 (standard) to 3.0 (extreme only) to see the bounds widen and the classification change. Then move xx (60) across the boundary to watch the status flip.

Comparing the Methods

On this sample the three fences agree while xx stays near the others, and disagree once it moves away: the z-score fence moves with every value, the IQR and MAD fences mostly ignore the extremes. Raising any threshold (kk, zthrz_{\text{thr}}, or the MAD threshold) widens that method's band and flags fewer values. Switch methods () to read the formulas for each, and move x=x = 60 to see where each method draws the line.

Try it in Code

# Outlier detection: Z-score, IQR, and MAD methods
import numpy as np

mu, sigma = 51.06666666666667, 6.591181848570068
x = 60
q1, q3 = 47, 54

# Z-Score method
z = (x - mu) / sigma
print(f"Z-Score: {z:.3f} -> {'OUTLIER' if abs(z) > 3 else 'NORMAL'}")

# IQR method
iqr = q3 - q1
lower, upper = q1 - 1.5 * iqr, q3 + 1.5 * iqr
print(f"IQR Fences: [{lower:.2f}, {upper:.2f}] -> {'OUTLIER' if x < lower or x > upper else 'NORMAL'}")

# MAD method
med, mad_val = 50.5, 3.5
mod_z = 0.6745 * (x - med) / mad_val
print(f"Modified Z-Score: {mod_z:.3f} -> {'OUTLIER' if abs(mod_z) > 3.5 else 'NORMAL'}")

What to explore next

You might also like