Sub topic 2
The Median as the Middle Value
This chapter assumes you can order numbers from least to greatest, compute a mean, and do arithmetic with fractions and decimals. The previous chapter covered sorting a data set. Here you will use the sorted list to find the median, the value that all the later ideas (quartiles, percentiles, boxplots) are built on.
What the Median Measures
The median is the middle value of a data set that has been sorted. Half of the values lie at or below it, and half lie at or above it. It describes the center of the data, so it is a measure of center, like the mean.
The median depends on position in the sorted list, not on how large the values are. That makes it different from the mean, and many GRE questions test exactly this difference.
Finding the Median
Always sort first. The median of an unsorted list is not the value in the middle of the list as written.
- Sort the values from least to greatest.
- 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
Take the sorted list 3, 5, 7, 8, 12, 13, 14, 18, 21. It has 9 values, so the median is at position (9 + 1) / 2 = 5. The fifth value is 12. Four values lie below it and four lie above it.
Even count
Remove 21 to get 3, 5, 7, 8, 12, 13, 14, 18. Now n = 8, so the middle values are at positions 4 and 5. They are 8 and 12, and the median is (8 + 12) / 2 = 10.
Notice that 10 is not in the data set. With an even count, the median is often a value that does not appear in the list. This is correct and common on the GRE.
The same procedure in code
This function sorts its input itself, so you can pass the data in any order.
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
print(median([14, 3, 21, 5, 12, 7, 18, 13, 8])) # 12
print(median([14, 3, 5, 12, 7, 18, 13, 8])) # 10.0Python indexes from 0, so for n = 9 the middle value is s[4], which is the fifth value. Position (n + 1) / 2 in the formula counts from 1. Keep that difference in mind if you move between the formula and code.
Edge Cases
Repeated values
Repeated values count separately. For the list 2, 4, 4, 4, 9, 10 (n = 6), the middle values are the third and fourth, which are both 4. The median is (4 + 4) / 2 = 4. Do not collapse repeated values into one entry before counting.
A one-value or two-value list
A list with one value has that value as its median. A list with two values has their mean as its median. Both follow from the rules above.
Negative numbers and decimals
Sort by actual value, not by size without the sign. The list -7, -2, 0, 3, 5 is already sorted, and its median is 0. A common slip is ordering -7 after -2 because 7 is larger than 2.
The Median Versus the Mean
The mean uses every value's size. The median uses only the order. This gives the median a property called resistance: extreme values barely move it.
Start from the 9-value list above. Its mean is 101 / 9, about 11.2, and its median is 12. Replace 21 with 210.
data = [3, 5, 7, 8, 12, 13, 14, 18, 21]
changed = [3, 5, 7, 8, 12, 13, 14, 18, 210]
print(sum(data) / len(data), median(data)) # 11.22... 12
print(sum(changed) / len(changed), median(changed)) # 32.22... 12The mean jumped from about 11.2 to about 32.2. The median stayed at 12, because 210 is still the largest value and the middle position did not change.
This gives a quick way to compare the two on the GRE:
| Situation | What happens |
|---|---|
| Data are roughly symmetric | Mean and median are close |
| A few very large values (skewed right) | Mean is pulled above the median |
| A few very small values (skewed left) | Mean is pulled below the median |
| You change the largest value but keep it the largest | Median is unchanged, mean changes |
Medians from Frequency Tables
GRE data often appear as a table of values and how many times each occurs, not as a long list. You do not need to write out every value. Find the middle position, then use cumulative counts to see which value occupies that position.
Suppose 20 students gave ratings from 1 to 4:
| Rating | Frequency | Cumulative frequency |
|---|---|---|
| 1 | 4 | 4 |
| 2 | 6 | 10 |
| 3 | 3 | 13 |
| 4 | 7 | 20 |
With n = 20, the middle values are at positions 10 and 11. The cumulative count reaches 10 at rating 2, so the 10th value is 2. The 11th value falls in the next group, so it is 3. The median is (2 + 3) / 2 = 2.5.
Here is the same method in code:
def value_at_position(table, position):
running = 0
for value in sorted(table):
running += table[value]
if position <= running:
return value
def median_from_table(table):
n = sum(table.values())
low = value_at_position(table, (n + 1) // 2)
high = value_at_position(table, n // 2 + 1)
return (low + high) / 2
ratings = {1: 4, 2: 6, 3: 3, 4: 7}
print(median_from_table(ratings)) # 2.5For odd n, the two positions are the same, so the function returns that single value. For n = 20 the positions are 10 and 11, as in the worked example.
A frequent error is taking the middle row of the table (here, between ratings 2 and 3) or the middle frequency. The median is the middle of the values counted with their frequencies, not the middle of the table rows.
How Adding or Removing Values Changes the Median
The median moves only when the middle position moves or the values around it change.
- Adding one value at or above the old median shifts the middle up by half a position. Adding one at or below it shifts the middle down.
- Adding a value equal to the median of an odd-length list keeps the median unchanged.
- Adding one very large value does not change the median much, because it only shifts the middle position by half a step.
For example, take 4, 6, 9 with median 6. Add 100 to get 4, 6, 9, 100. The median is now (6 + 9) / 2 = 7.5, a modest change, while the mean jumps from about 6.3 to 29.75.
The Median Does Not Combine Simply
You cannot get the median of a combined data set by averaging the medians of its parts. Let set A be 1, 2, 9 with median 2, and set B be 3, 4, 5 with median 4. The average of the two medians is 3. Combine and sort to get 1, 2, 3, 4, 5, 9. The combined median is (3 + 4) / 2 = 3.5. To find the median of a union, merge and sort the actual values.
Connection to What Comes Next
The median is also called the second quartile, Q2, and the 50th percentile. The next chapters split the data further, into quarters, using the same idea: find the middle of an ordered list, then repeat on each half.
Common Mistakes
- Skipping the sort, then taking the middle of the list as written.
- Averaging the wrong pair when n is even, for example the 4th and 6th values of an 8-value list instead of the 4th and 5th.
- Dropping repeated values before counting.
- Assuming the median must be a member of the data set.
- Using the middle row of a frequency table instead of cumulative counts.
- Assuming the median equals the mean. They are equal only for special cases, such as symmetric data.
Exercise
Find the median of 17, 4, 9, 22, 4, 13, 30, 9. Then say what happens to the median if 30 is replaced by 300.
Show answer
Sorted: 4, 4, 9, 9, 13, 17, 22, 30. There are 8 values, so average the 4th and 5th: (9 + 13) / 2 = 11. Replacing 30 with 300 keeps it the largest value, so the order is unchanged and the median stays 11.
Exercise
A table shows 15 households by number of cars: 0 cars (3 households), 1 car (5), 2 cars (4), 3 cars (3). What is the median number of cars?
Show answer
With n = 15 the median is the 8th value. Cumulative counts are 3 (for 0), 8 (for 1), 12 (for 2), 15 (for 3). The 8th value falls in the 1-car group, so the median is 1.
Exercise
Set A is 2, 6, 10 and set B is 1, 3, 20, 30. Is the median of the combined set equal to the average of the two medians? Show your work.
Show answer
The medians are 6 and (3 + 20) / 2 = 11.5, and their average is 8.75. The combined sorted list is 1, 2, 3, 6, 10, 20, 30 with 7 values, so the median is the 4th value, 6. Since 6 is not 8.75, the answer is no.