Sub topic 12
Quartiles and the Interquartile Range
This chapter assumes you can already find a median, including for lists with an even number of values. It also assumes you know that the range is the largest value minus the smallest. Quartiles extend the median idea. They give you a measure of spread that outliers do not distort.
Why quartiles exist
The range uses only two values, the smallest and the largest. One extreme value can change it a lot. The standard deviation uses every value, and an outlier inflates it too. You need a spread measure that describes the typical values and ignores the extremes.
Quartiles do this. They cut a sorted data set into four parts that each hold about the same number of values.
- Q1, the first quartile, has about 25% of the values at or below it.
- Q2, the second quartile, is the median. About 50% of the values are at or below it.
- Q3, the third quartile, has about 75% of the values at or below it.
The interquartile range (IQR) is Q3 − Q1. It is the width of the middle half of the data.
Finding quartiles by hand
Use the median-of-halves method:
- Sort the list from least to greatest.
- Find the median. This is Q2.
- Split the list into a lower half and an upper half. If the count is odd, leave the median out of both halves. If the count is even, the split falls between the two middle values and no value is dropped.
- Q1 is the median of the lower half. Q3 is the median of the upper half.
- Subtract: IQR = Q3 − Q1.
Worked example: an even count
Use the data set from earlier chapters: 2, 4, 4, 4, 5, 5, 7, 9. It is already sorted and has 8 values.
- The median is the average of the 4th and 5th values: (4 + 5) / 2 = 4.5.
- The lower half is 2, 4, 4, 4. Its median is (4 + 4) / 2 = 4, so Q1 = 4.
- The upper half is 5, 5, 7, 9. Its median is (5 + 7) / 2 = 6, so Q3 = 6.
- IQR = 6 − 4 = 2.
The range of this set is 7, but the IQR is only 2. The middle half of the values sits in a narrow band from 4 to 6.
Worked example: an odd count
Take 3, 5, 6, 8, 10, 12, 15. There are 7 values.
- The median is the 4th value, 8.
- Leave the 8 out. The lower half is 3, 5, 6, so Q1 = 5.
- The upper half is 10, 12, 15, so Q3 = 12.
- IQR = 12 − 5 = 7.
Leaving the median out of the halves is the step people most often skip. If you keep the 8 in both halves, you get different quartiles.
Checking the method in code
The function below follows the five steps exactly.
def median(values):
s = sorted(values)
n = len(s)
mid = n // 2
if n % 2 == 1:
return s[mid]
return (s[mid - 1] + s[mid]) / 2
def quartiles(values):
s = sorted(values)
n = len(s)
mid = n // 2
lower = s[:mid]
upper = s[mid + 1:] if n % 2 == 1 else s[mid:]
return median(lower), median(s), median(upper)
even_set = [2, 4, 4, 4, 5, 5, 7, 9]
odd_set = [3, 5, 6, 8, 10, 12, 15]
q1, q2, q3 = quartiles(even_set)
print(q1, q2, q3, q3 - q1) # 4.0 4.5 6.0 2.0
q1, q2, q3 = quartiles(odd_set)
print(q1, q2, q3, q3 - q1) # 5 8 12 7Both results match the hand calculations.
The five-number summary and box plots
The five-number summary lists the minimum, Q1, the median, Q3, and the maximum. For the even set above, it is 2, 4, 4.5, 6, 9.
A box plot draws these five numbers. The box runs from Q1 to Q3, with a line at the median. Lines called whiskers extend toward the minimum and maximum. On the GRE you may be asked to read a box plot in a data interpretation question. Each of the four sections, whisker, lower box, upper box, and whisker, holds roughly a quarter of the values. A long whisker does not mean many values are out there. It means the values in that quarter are spread widely.
The widths of the sections also show skew. If the median line sits near the left edge of the box and the right whisker is long, the data stretch toward larger values.
Why the IQR resists outliers
Quartiles depend on positions in the sorted list, not on how large the extreme values are. Changing the maximum from 11 to 50 does not move any middle position.
Take the odd-count list 1, 3, 4, 5, 6, 7, 8, 9, 11. Then replace the 11 with 50.
def median(values):
s = sorted(values)
n = len(s)
mid = n // 2
if n % 2 == 1:
return s[mid]
return (s[mid - 1] + s[mid]) / 2
def quartiles(values):
s = sorted(values)
n = len(s)
mid = n // 2
lower = s[:mid]
upper = s[mid + 1:] if n % 2 == 1 else s[mid:]
return median(lower), median(s), median(upper)
original = [1, 3, 4, 5, 6, 7, 8, 9, 11]
with_outlier = [1, 3, 4, 5, 6, 7, 8, 9, 50]
for data in (original, with_outlier):
q1, q2, q3 = quartiles(data)
print("IQR", q3 - q1, "range", max(data) - min(data),
"mean", round(sum(data) / len(data), 2))
# IQR 5.0 range 10 mean 6.0
# IQR 5.0 range 49 mean 10.33The IQR stays at 5. The range jumps from 10 to 49, and the mean rises from 6 to about 10.33. The standard deviation would also increase.
| Statistic | Affected by one extreme value? |
|---|---|
| Mean | Yes |
| Median | Barely |
| Range | Yes, directly |
| Standard deviation | Yes |
| IQR | No, unless the extreme value changes a middle position |
A rule for flagging outliers
A common convention calls a value an outlier if it is more than 1.5 × IQR below Q1 or above Q3. For the second list, Q1 = 3.5, Q3 = 8.5, and IQR = 5, so the fences are 3.5 − 7.5 = −4 and 8.5 + 7.5 = 16. The value 50 is above 16, so it is flagged. The GRE does not usually require this rule, but it helps you check your judgment about which values are extreme.
Transformations and the IQR
The IQR is a spread measure, so it behaves like the range and the standard deviation.
- Adding a constant to every value shifts Q1 and Q3 by the same amount. The IQR does not change.
- Multiplying every value by k multiplies the IQR by |k|.
- Multiplying by a negative number reverses the order of the list. The old Q3 becomes the new Q1. The IQR is still positive because it uses the absolute value of k.
Apply 3x + 10 to the even set. Q1 becomes 3(4) + 10 = 22, and Q3 becomes 3(6) + 10 = 28. The IQR is 6, which is 3 × 2. The +10 had no effect on the spread.
Common traps
- Not sorting first. Quartiles of an unsorted list are meaningless. Sort before you split.
- Keeping the median in both halves when the count is odd.
- Treating Q1 as a value from the middle of the original list. Q1 is the median of the lower half. It can be a value that is not in the data, such as 3.5.
- Assuming exactly 25% of values lie in each quarter. With small or tied data, the counts can differ. The statement "about a quarter" is safe. "Exactly a quarter" is not.
- Mixing methods. Software often computes quartiles by interpolating between positions. For the even set, NumPy's default gives Q3 = 5.5, not 6. If a GRE question gives you quartile values, use them as given. If it asks you to compute them, use the median-of-halves method from this chapter.
Summary of what to remember
- Q1, Q2, and Q3 split a sorted list into quarters. Q2 is the median.
- Q1 and Q3 are the medians of the lower and upper halves. For an odd count, leave the median out.
- IQR = Q3 − Q1 measures the spread of the middle half of the data.
- The IQR resists outliers. The range, mean, and standard deviation do not.
- Adding a constant leaves the IQR unchanged. Multiplying by k multiplies it by |k|.
Exercise
Find Q1, Q3, and the IQR of 12, 15, 15, 18, 20, 22, 25, 31.
Show answer
The list is sorted and has 8 values, so split it into 12, 15, 15, 18 and 20, 22, 25, 31. Q1 = (15 + 15) / 2 = 15. Q3 = (22 + 25) / 2 = 23.5. IQR = 23.5 − 15 = 8.5.
Exercise
A data set has an IQR of 10. Every value is replaced by 7 − 2x. What is the IQR of the new data set?
Show answer
The IQR is 20. Adding 7 does not change the spread. Multiplying by −2 multiplies the spread by |−2| = 2. The negative sign reverses the order, so the old Q3 becomes the new Q1, but the IQR stays positive.
Exercise
A sorted list has 11 values. Which positions hold Q1, the median, and Q3 under the median-of-halves method?
Show answer
The median is the 6th value. The lower half is the 1st through 5th values, so Q1 is the 3rd value. The upper half is the 7th through 11th values, so Q3 is the 9th value.
Exercise
In the list 4, 6, 7, 9, 10, 12, 14, the largest value is changed from 14 to 140. Which of these change: the median, the IQR, the range, the mean?
Show answer
The median (9) and the IQR do not change. The IQR is 12 − 6 = 6 both before and after, because Q1 = 6 and Q3 = 12 come from positions that are not the maximum. The range changes from 10 to 136, and the mean increases.