Sub topic 3
The median: the middle value
This chapter assumes you can sort a list of numbers, do arithmetic with fractions, decimals, and negatives, and compute a mean. It covers the median in depth: how to find it, where students make errors, and how it behaves when data changes.
What the median is
The median is the middle value of a data set after the values are arranged in order from least to greatest. Half of the values are at or below it, and half are at or above it.
The median answers a different question than the mean does. The mean asks, "What would each value be if the total were shared equally?" The median asks, "Which value sits in the middle of the ordered list?"
The procedure
- Sort the list from least to greatest. This step is required, because the median depends on position.
- Count the values. Call the count n.
- If n is odd, the median is the single middle value, at position (n + 1) / 2.
- If n is even, the median is the mean of the two middle values, at positions n / 2 and n / 2 + 1.
Odd count
Find the median of 9, 3, 7, 1, 5.
Sorted: 1, 3, 5, 7, 9. There are 5 values, so the middle position is (5 + 1) / 2 = 3. The third value is 5. The median is 5.
Even count
Find the median of 2, 4, 4, 4, 5, 5, 7, 9. This list is already sorted and has 8 values. The middle positions are 4 and 5. The 4th value is 4 and the 5th value is 5, so the median is (4 + 5) / 2 = 4.5.
Notice that 4.5 does not appear in the list. With an even count, the median can be a value that is not in the data set.
import statistics
data = [2, 4, 4, 4, 5, 5, 7, 9]
print(statistics.mean(data)) # 5
print(statistics.median(data)) # 4.5
print(statistics.mode(data)) # 4
print(max(data) - min(data)) # 7
print(statistics.pstdev(data)) # 2.0The mean is 5 and the median is 4.5, so the two measures of center differ even in this small, fairly balanced list.
Sorting is not optional
A frequent GRE trap is a list written out of order. Taking the middle entry of an unsorted list gives the wrong answer.
Consider 12, 3, 8, 20, 5. The middle entry as written is 8, which happens to be correct only by luck. Sorted, the list is 3, 5, 8, 12, 20, and the median is 8. Now consider 12, 3, 20, 8, 5. The entry in the middle position is 20, but the sorted list is again 3, 5, 8, 12, 20, so the median is still 8.
Always sort first. Duplicates stay in the list, and each copy counts as its own position.
import statistics
unsorted = [12, 3, 20, 8, 5]
print(unsorted[len(unsorted) // 2]) # 20, the middle entry as written (wrong)
print(statistics.median(unsorted)) # 8, the true medianDuplicates count as separate values
Suppose the list is 1, 2, 2, 2, 2, 10. There are 6 values. The middle positions are 3 and 4, and both hold 2. The median is (2 + 2) / 2 = 2.
Do not collapse repeated values into one. If you did, you would count only 3 distinct values (1, 2, 10) and wrongly pick 2 for a different reason, or pick a wrong value in other lists.
Where the median sits in a frequency table
GRE problems often give counts instead of a long list. For example:
| Value | Frequency |
|---|---|
| 1 | 3 |
| 2 | 5 |
| 3 | 4 |
| 4 | 2 |
The total count is 3 + 5 + 4 + 2 = 14. This is even, so the median is the mean of the 7th and 8th values. Build a running total:
- Values at positions 1 to 3 are 1.
- Values at positions 4 to 8 are 2.
- Values at positions 9 to 12 are 3.
- Values at positions 13 to 14 are 4.
The 7th and 8th values are both 2, so the median is 2. You never need to write out all 14 numbers.
import statistics
values = [1] * 3 + [2] * 5 + [3] * 4 + [4] * 2
print(len(values)) # 14
print(statistics.median(values)) # 2.0How the median responds to changes
The median depends on the order of the values and on the middle of the list. It does not depend on how far the extreme values are from the center. This makes it resistant to outliers, which are values far from the rest.
Take 3, 5, 8, 12, 20. The median is 8. If you change 20 to 2,000, the sorted list is 3, 5, 8, 12, 2000 and the median is still 8. The mean, by contrast, jumps from 9.6 to 405.6.
Moving a value changes the median only if it crosses the middle. Changing 3 to 7 in the original list gives 5, 7, 8, 12, 20, and the median is still 8. Changing 3 to 15 gives 5, 8, 12, 15, 20, and the median becomes 12, because the changed value moved from below the middle to above it.
Adding or removing values
Adding one value to a list shifts the median by half a step at most, in position terms, toward the side where the new value falls.
- If the new value is above the current median, the median moves up or stays the same.
- If the new value is below the current median, the median moves down or stays the same.
- If n was odd, the new list has an even count, so the median becomes the mean of two values.
- If n was even, the new list has an odd count, so the median becomes a single value.
Example: the list 2, 4, 4, 4, 5, 5, 7, 9 has median 4.5. Add 100. The sorted list is 2, 4, 4, 4, 5, 5, 7, 9, 100, with 9 values. The middle position is 5, and the 5th value is 5. The median moved from 4.5 to 5, even though the added value was huge. The size of 100 did not matter. Only the fact that it landed above the middle did.
import statistics
data = [2, 4, 4, 4, 5, 5, 7, 9]
print(statistics.median(data)) # 4.5
print(statistics.median(data + [100])) # 5
print(statistics.median(data + [-100])) # 4Adding a constant or scaling
If you add a constant to every value, the median increases by that constant. If you multiply every value by a constant, the median is multiplied by it. Both follow because the order of the values is preserved (for a positive multiplier).
Using the list above, 3x + 10 gives a median of 3(4.5) + 10 = 23.5.
import statistics
data = [2, 4, 4, 4, 5, 5, 7, 9]
transformed = [3 * x + 10 for x in data]
print(statistics.mean(transformed)) # 25
print(statistics.median(transformed)) # 23.5
print(max(transformed) - min(transformed)) # 21
print(statistics.pstdev(transformed)) # 6.0Be careful with negative multipliers. Multiplying by -1 reverses the order, but the middle position is still the middle position, so the median is still multiplied by -1. The sorted list reverses, but the median formula gives the same result.
Mean versus median
The two measures agree when the data is symmetric and separate when it is lopsided. A few large values pull the mean above the median (a right skew). A few very small values pull it below (a left skew). A later chapter develops this fully. For now, remember a quick test:
- If the mean is greater than the median, the high values are stretching the data out.
- If the median is greater than the mean, the low values are doing so.
The median can also be found from a diagram of positions:
Common errors
- Skipping the sort. The median of the list as written is not the median.
- Taking the average position instead of the average value. For n = 8 the middle is between positions 4 and 5, but the median is the mean of the values at those positions, not the value at position 4.5 of anything.
- Dropping duplicates. Each repeated value occupies its own position.
- Confusing the median with the mean. They are equal only for symmetric data.
- Forgetting that an even count can give a median not in the list.
- Using the middle of the range, (min + max) / 2, as the median. That is the midrange, a different quantity.
Missing-value problems with the median
Sometimes a problem gives the median and asks about an unknown value. Suppose the list is 4, 7, x, 12, 15 with x unknown, and the median is 10. With 5 values, the median is the 3rd value in sorted order. For the median to be 10, x must be 10, because 10 sits between 7 and 12 and takes the middle position.
Now change the list to 4, 7, x, 12 with median 9. There are 4 values, so the median is the mean of the 2nd and 3rd values. If x is between 7 and 12, then 7 and x are the middle values, and (7 + x) / 2 = 9 gives x = 11. This is between 7 and 12, so it is consistent. Always verify that your answer places x where you assumed it would.
import statistics
print(statistics.median([4, 7, 11, 12])) # 9.0Summary of key facts
| Situation | Effect on the median |
|---|---|
| Add k to every value | Median increases by k |
| Multiply every value by a positive c | Median is multiplied by c |
| Change the largest value to something larger | Median unchanged |
| Add a value above the median | Median stays or moves up |
| Add a value below the median | Median stays or moves down |
Exercise
A list contains the values 14, 6, 9, 6, 20, 11. (a) Find the median. (b) The value 20 is replaced with 200. What is the new median? (c) The value 6 (one copy) is replaced with 30. What is the new median of the original list?
Show answer
(a) Sorted: 6, 6, 9, 11, 14, 20. There are 6 values, so the median is the mean of the 3rd and 4th values: (9 + 11) / 2 = 10. (b) The sorted list is 6, 6, 9, 11, 14, 200. The middle values are still 9 and 11, so the median is 10. (c) Replace one 6 with 30 in the original list: 14, 30, 9, 6, 20, 11. Sorted: 6, 9, 11, 14, 20, 30. The 3rd and 4th values are 11 and 14, so the median is 12.5.
Exercise
A frequency table shows the value 0 with frequency 4, the value 1 with frequency 6, the value 2 with frequency 3, and the value 3 with frequency 7. What is the median?
Show answer
The total is 4 + 6 + 3 + 7 = 20, which is even, so the median is the mean of the 10th and 11th values. Positions 1 to 4 are 0, positions 5 to 10 are 1, positions 11 to 13 are 2, and positions 14 to 20 are 3. The 10th value is 1 and the 11th value is 2, so the median is (1 + 2) / 2 = 1.5.
Exercise
The list 3, 8, x, 15, 22, 30 has a median of 14. Find x.
Show answer
There are 6 values, so the median is the mean of the 3rd and 4th sorted values. Assume x lies between 8 and 15, so the middle values are x and 15. Then (x + 15) / 2 = 14, so x = 13. This is between 8 and 15, so the assumption holds. The answer is x = 13.