Percentiles and the IQR
Intuition
The 90th percentile is the value below which 90% of the data falls. Percentiles describe a distribution without assuming it is bell-shaped, and they are robust to outliers.
Precise explanation
There are several percentile definitions. We use linear interpolation between order
statistics (NumPy's default method="linear"): with sorted data ,
(when just return ). is the median.
IQR = , the width of the middle half. Tukey's fences: values below or above () are flagged — the rule behind box-plot whiskers.
Worked example
data [1, 2, 3, 4, 10]: : r = 1 → 2; : r = 3 → 4; IQR = 2; fences [2 − 3, 4 + 3] = [−1, 7] → 10 is an outlier. : r = 1.6 → 2 + 0.6·1 = 2.6.
Common mistakes
- Using instead of (that is a different definition).
- Forgetting to sort.
- Comparing percentiles computed by different definitions across tools.
Why this matters
Robust scaling, latency SLOs ("p99 < 200 ms"), box plots and clipping extreme gradient values all use percentiles.
Library implementation
Linear interpolation between order statistics is NumPy's default
(np.percentile(data, q), method "linear") and the "inclusive" method of
statistics.quantiles(data, n=4, method="inclusive"), which returns the three quartiles at once.
Other methods (such as the default "exclusive") give slightly different answers for small
samples — always check which definition a library uses.
References
- Think Stats (3rd edition) — Allen B. Downey (
S010) - NumPy: the absolute basics for beginners (NumPy v2.5 Manual) — NumPy Developers (
S007)