Lekcja 2 z 4
The mean and median tell different stories
One table, two summaries
Our waiting times are 4, 6, 8, 10, and 32 minutes. We want to describe them with one number. The arithmetic mean uses the sum of all the values. The median locates the middle of the ordered dataset. Both are valid, but they serve different purposes.
By the end of this lesson, you will be able to calculate both measures and explain why they differ in this example.
The mean: share out the total
Add all the waiting times, then divide the total by the number of visits:
Włącz JavaScript, aby wyświetlić ten blok. Poniżej znajdziesz jego źródło.
\bar{x}=\frac{1}{n}\sum_{i=1}^{n}x_i=\frac{4+6+8+10+32}{5}=12\;\text{min}
Here, xᵢ is the waiting time for one visit, n is the number of visits, and x̄ is the mean. The total is 60 minutes. Sharing those 60 minutes equally across five visits gives 12 minutes per visit. This does not mean that any visit actually lasted exactly 12 minutes.
The median: find the middle
The middle of five ordered values is the third number: 8 minutes. Two values are smaller and two are larger. With an even number of observations, the median is the mean of the two middle values. For 4, 6, 8, and 10, it would be (6 + 8) / 2 = 7.
| Measure | Result | What does it describe here? |
|---|---|---|
| Mean | 12 min | Total waiting time divided across five visits |
| Median | 8 min | The middle of five ordered values |
The 32-minute visit pulls the mean upwards. The median is less sensitive to the size of an extreme value. If we replaced 32 with 62, the median would still be 8, but the mean would rise to 18 minutes. This resistance is useful, but it also means that the median alone hides the cost of a very long wait.
Check the calculation in Ruby
waiting_minutes = [4, 6, 8, 10, 32]
mean = waiting_minutes.sum.fdiv(waiting_minutes.length)
median = waiting_minutes.sort[waiting_minutes.length / 2]
puts "Mean: #{mean} min"
puts "Median: #{median} min"
The output is Mean: 12.0 min and Median: 8 min. The sum method adds the numbers, and fdiv preserves a fractional result. Ruby arrays start at index 0, so index 2 points to the third item. This median shortcut works only for an odd number of observations. We will write a general version in the final lesson.
Sprawdź się
Replace 32 minutes with 12 minutes. Before calculating, predict which measure will change. Then calculate both.
Porównaj swoją odpowiedź
The mean falls to (4 + 6 + 8 + 10 + 12) / 5 = 8 minutes. The median remains 8 because the middle value has not changed. Equal mean and median values do not imply that every visit took the same amount of time.
Take this forward: name the measure you use. “Eight minutes” and “twelve minutes” can both be true. The question determines which answer is useful.
Code reference: Ruby — Enumerable#sum.
Twój postęp
Oznacz lekcję jako ukończoną, kiedy uznasz, że materiał jest opanowany.