Sub topic 5
The Interquartile Range
This chapter assumes you can order a list of numbers, find a median, and find Q1 and Q3 by splitting an ordered list into halves. It also assumes you can do arithmetic with fractions and percents. The earlier chapters on the median, range, and quartiles cover these.
What the Interquartile Range Measures
The interquartile range (IQR) is the distance between the third quartile and the first quartile:
IQR = Q3 − Q1
It measures spread, which is how far apart the values are. It describes the middle half of the data. The lowest quarter and the highest quarter are ignored. Three facts follow from the definition:
- The IQR is a single number, not an interval. If Q1 = 6 and Q3 = 16, the IQR is 10, not "6 to 16".
- It is in the same units as the data. If the data are heights in centimeters, the IQR is in centimeters.
- At least about half of the values lie between Q1 and Q3. This holds even when the values are bunched up unevenly inside that stretch.
The range is the maximum minus the minimum. It depends entirely on the two most extreme values. The IQR depends on the middle of the list, so one unusual value rarely changes it much.
Computing the IQR Step by Step
The splitting rule depends on whether the list has an odd or even number of values.
- Even n: the median falls between two values. The lower half is the first n/2 values and the upper half is the last n/2 values.
- Odd n: the median is one of the values. Leave it out. Each half then has (n − 1)/2 values.
Worked Example with an Odd Count
Take the list 3, 5, 7, 8, 12, 13, 14, 18, 21, which has 9 values and is already sorted.
- The median is the 5th value, 12. Leave it out.
- The lower half is 3, 5, 7, 8. Its median is (5 + 7) / 2 = 6, so Q1 = 6.
- The upper half is 13, 14, 18, 21. Its median is (14 + 18) / 2 = 16, so Q3 = 16.
- IQR = 16 − 6 = 10.
The range is 21 − 3 = 18.
Worked Example with an Even Count
Take 2, 4, 4, 6, 9, 10, 12, 15, which has 8 values.
- The median is (6 + 9) / 2 = 7.5. No value is left out.
- The lower half is 2, 4, 4, 6, so Q1 = (4 + 4) / 2 = 4.
- The upper half is 9, 10, 12, 15, so Q3 = (10 + 12) / 2 = 11.
- IQR = 11 − 4 = 7.
The range is 15 − 2 = 13.
Computing It in Code
The program below applies the same splitting rule. It runs as shown.
def median(values):
n = len(values)
mid = n // 2
if n % 2 == 1:
return values[mid]
return (values[mid - 1] + values[mid]) / 2
def quartiles(data):
s = sorted(data)
half = len(s) // 2
lower = s[:half]
upper = s[half + len(s) % 2:] # skip the middle value when n is odd
return median(lower), median(s), median(upper)
def spread(data):
q1, _, q3 = quartiles(data)
return {"range": max(data) - min(data), "iqr": q3 - q1}
a = [3, 5, 7, 8, 12, 13, 14, 18, 21]
b = a + [90] # same list with one extreme value added
print(spread(a)) # {'range': 18, 'iqr': 10.0}
print(spread(b)) # {'range': 87, 'iqr': 11}Adding the single value 90 multiplies the range by almost five, from 18 to 87. The IQR moves only from 10 to 11. For the second list (10 values), the lower half is 3, 5, 7, 8, 12 with median 7, and the upper half is 13, 14, 18, 21, 90 with median 18. The extreme value shifted which values count as the halves, but it did not stretch the middle. This is why the IQR is called resistant to outliers, while the range is not.
Range and IQR Compared
| Feature | Range | Interquartile range |
|---|---|---|
| Formula | Maximum − minimum | Q3 − Q1 |
| Values it uses | Only the two extremes | The middle half of the ordered data |
| Effect of one extreme value | Can change a lot | Usually changes little |
| Always at least as large as the other? | Yes | No. It is never larger than the range. |
| Shown on a boxplot as | Distance from end of one whisker to the end of the other | Length of the box |
The range is never smaller than the IQR, because Q1 and Q3 both lie between the minimum and the maximum. The two can be equal. For example, in the list 1, 5, 5, 9, the range is 8 and the IQR is 5 − 5 = 0 for the middle values, while Q1 = 3 and Q3 = 7 give an IQR of 4. They match only in unusual cases, such as a list whose two halves each have their median at the extremes.
How the IQR Responds to Changes in the Data
Questions on the GRE often ask what happens to a measure after every value is changed in the same way. The IQR follows simple rules.
- Adding a constant to every value does not change the IQR. Q1 and Q3 both shift by the same amount, so their difference stays the same.
- Multiplying every value by a positive number k multiplies the IQR by k. Q1 and Q3 are both scaled by k.
- Multiplying by a negative number multiplies the IQR by the absolute value of that number. The order of the data reverses, but the distance between the quartiles is still positive.
If a data set has an IQR of 8 and every value is tripled and then raised by 10, the new IQR is 3 × 8 = 24. The added 10 has no effect. The range follows the same rules, and so does the median. The median shifts with the added constant, while the IQR does not.
Connection to Percentiles and Boxplots
Q1 is the 25th percentile and Q3 is the 75th percentile, so the IQR is the 75th percentile minus the 25th percentile. This is the same quantity described in terms of percentiles. On a boxplot, the left and right edges of the box sit at Q1 and Q3. The IQR is the width of the box, read directly off the axis.
The IQR also sets the outlier fences under the 1.5 × IQR rule:
- Lower fence: Q1 − 1.5 × IQR
- Upper fence: Q3 + 1.5 × IQR
For the first list, Q1 = 6, Q3 = 16, and IQR = 10. The fences are 6 − 15 = −9 and 16 + 15 = 31. No value in the list is outside them, so the list has no outliers. For the list with 90 added, Q1 = 7 and Q3 = 18, so the IQR is 11 and the fences are 7 − 16.5 = −9.5 and 18 + 16.5 = 34.5. The value 90 is far above 34.5, so it is flagged as an outlier. A later chapter covers this rule in full.
Common Errors and Edge Cases
Quartile Method Differences
Some textbooks keep the median in both halves when the list has an odd number of values. For the list 3, 5, 7, 8, 12, 13, 14, 18, 21, that method gives a lower half of 3, 5, 7, 8, 12 with Q1 = 7, and an upper half of 12, 13, 14, 18, 21 with Q3 = 14. The IQR would be 7 instead of 10. Software packages use still other interpolation rules. GRE questions avoid this problem. They usually give the quartiles, show them on a boxplot, or use data where the methods agree. If you must compute quartiles yourself, use the exclude-the-middle method shown in this chapter and keep the same method throughout a problem.
Mistakes to Avoid
- Reporting Q3 or Q1 instead of their difference. The IQR is a length, not a position on the axis.
- Finding quartiles without sorting first. Medians of unsorted halves give wrong values.
- Treating the IQR as half the range. The IQR can be a small fraction of the range or a large one, depending on how the data are spread.
- Thinking the IQR holds exactly half of the values in all cases. With ties and small lists, the count between Q1 and Q3 can differ slightly from half.
- Assuming a longer whisker means more data. Each quarter of the data lies in its own section of the boxplot, whatever the length of that section. The IQR measures spread in the middle, not how many values there are.
- Mixing up spread and center. Two data sets can have the same median and very different IQRs.
Using the IQR to Compare Two Data Sets
Suppose two classes take the same quiz. Class A has Q1 = 62 and Q3 = 78. Class B has Q1 = 55 and Q3 = 85. Then Class A has an IQR of 16 and Class B has an IQR of 30. The scores in Class B are more spread out in the middle half, even if both classes have the same median. If a question asks which class has more consistent middle scores, the answer is the class with the smaller IQR, Class A.
Exercise
Find the range and the interquartile range of 4, 6, 9, 11, 15, 16, 20, 22, 25, 30.
Show answer
The list has 10 values and is sorted. The lower half is 4, 6, 9, 11, 15, so Q1 = 9. The upper half is 16, 20, 22, 25, 30, so Q3 = 22. The IQR is 22 − 9 = 13. The range is 30 − 4 = 26.
Exercise
A boxplot shows a minimum of 31, Q1 = 40, Q3 = 52, and a maximum of 80. What is the IQR, and is 80 an outlier under the 1.5 × IQR rule?
Show answer
The IQR is 52 − 40 = 12. The upper fence is 52 + 1.5 × 12 = 70. Since 80 is greater than 70, it is an outlier. The lower fence is 40 − 18 = 22, and the minimum of 31 is above it, so it is not an outlier.
Exercise
A data set has an IQR of 8. Every value is multiplied by 3, and then 10 is added to every value. What is the IQR of the new data set?
Show answer
Multiplying by 3 multiplies the IQR by 3, giving 24. Adding 10 does not change the IQR. The new IQR is 24.