TExcelTAD
Home › Statistics › Interquartile Range

Interquartile Range

The interquartile range (IQR) is the spread of the middle half of the data:

text
IQR = Q3 − Q1

Because it ignores the lowest 25% and the highest 25% of the values, the IQR is robust: outliers and long tails barely affect it. It is the natural measure of spread to report next to the median.

Computing the IQR

Monthly phone bills (in euros) of 12 customers:

python · try it yourself
import numpy as np

bills = np.array([28, 35, 31, 42, 25, 39, 33, 30, 47, 36, 29, 120])

q1, q3 = np.percentile(bills, [25, 75])
print("Q1 =", q1)
print("Q3 =", q3)
print("IQR =", q3 - q1)
print("range =", np.ptp(bills))
Q1 = 29.75
Q3 = 39.75
IQR = 10.0
range = 95

The middle half of the customers pay bills that lie within about 10 euros of each other, while the range is dominated by one customer with a 120-euro bill.

SciPy has a ready-made function, which uses the same interpolation as NumPy by default:

python · try it yourself
import numpy as np
from scipy import stats

bills = np.array([28, 35, 31, 42, 25, 39, 33, 30, 47, 36, 29, 120])
print("scipy IQR:", stats.iqr(bills))
scipy IQR: 10.0

Robustness compared

Let's add a more and more extreme customer and compare the IQR with the range and the standard deviation:

python · try it yourself
import numpy as np

base = [28, 35, 31, 42, 25, 39, 33, 30, 47, 36, 29]
for extreme in [40, 120, 500, 2000]:
    x = np.array(base + [extreme])
    q1, q3 = np.percentile(x, [25, 75])
    print(f"extreme={extreme:5d}  IQR={q3 - q1:6.2f}  range={np.ptp(x):5d}  std={x.std(ddof=1):7.2f}")
extreme=   40  IQR=  9.50  range=   22  std=   6.49
extreme=  120  IQR= 10.00  range=   95  std=  25.58
extreme=  500  IQR= 10.00  range=  475  std= 134.64
extreme= 2000  IQR= 10.00  range= 1975  std= 567.54

The IQR barely moves, while the range and the standard deviation explode.

The 1.5 × IQR rule for outliers

John Tukey's rule flags a value as a potential outlier if it lies outside the fences:

text
lower fence = Q1 − 1.5 × IQR
upper fence = Q3 + 1.5 × IQR

Some analysts additionally call values beyond 3 × IQR "extreme outliers".

python · try it yourself
import numpy as np

bills = np.array([28, 35, 31, 42, 25, 39, 33, 30, 47, 36, 29, 120])
q1, q3 = np.percentile(bills, [25, 75])
iqr = q3 - q1
low, high = q1 - 1.5 * iqr, q3 + 1.5 * iqr

print(f"fences: [{low}, {high}]")
print("outliers:", bills[(bills < low) | (bills > high)])
print("kept:", np.sort(bills[(bills >= low) & (bills <= high)]))
fences: [14.75, 54.75]
outliers: [120]
kept: [25 28 29 30 31 33 35 36 39 42 47]

The 120-euro bill is flagged. Remember: "flagged" means look at it, not delete it. That customer may simply have made many international calls.

Outliers per group with pandas

Applying the rule within each group catches values that are unusual for their group, even if they would look normal overall:

python · try it yourself
import pandas as pd

df = pd.DataFrame({
    "plan": ["basic"] * 7 + ["premium"] * 7,
    "bill": [22, 25, 24, 27, 23, 26, 49, 60, 64, 58, 62, 61, 59, 30],
})

def flag(group):
    q1, q3 = group.quantile([0.25, 0.75])
    iqr = q3 - q1
    return (group < q1 - 1.5 * iqr) | (group > q3 + 1.5 * iqr)

df["outlier"] = df.groupby("plan")["bill"].transform(flag)
print(df[df["outlier"]])
       plan  bill  outlier
6     basic    49     True
13  premium    30     True

A 49-euro basic bill and a 30-euro premium bill both sit inside the overall spread of bills, but each is unusual for its own plan.

IQR and the normal distribution

For data that follows a normal distribution, the IQR is about 1.349 × σ. That lets you estimate the standard deviation robustly as IQR / 1.349, a useful trick when a few outliers would distort the ordinary standard deviation.

python · try it yourself
import numpy as np
from scipy import stats

rng = np.random.default_rng(8)
x = rng.normal(100, 15, size=5000)
x[:25] = 900                                    # corrupt 0.5% of the values

print(f"ordinary std     : {x.std(ddof=1):6.2f}")
print(f"robust IQR/1.349 : {stats.iqr(x) / 1.349:6.2f}   (true sigma = 15)")
ordinary std     :  58.42
robust IQR/1.349 :  15.18   (true sigma = 15)

Test yourself

Q1 = 14 and Q3 = 26. What is the IQR?

  1. 12
  2. 20
  3. 40
  4. 6

With Q1 = 14 and Q3 = 26, where is the upper Tukey fence?

  1. 32
  2. 38
  3. 44
  4. 50

Why is the IQR robust to outliers?

  1. It uses the mean
  2. It only depends on the middle 50% of the data
  3. It squares the deviations
  4. It removes outliers automatically