Section 11.13

Median

Definition

The median is the middle value of a set of numbers after sorting: at least half of the values are at or below it, and at least half are at or above it. For an odd number of values it is the middle sorted value; for an even number a convention is needed, most often the average of the two middle values. The median is also called the 50th percentile §11.16. It depends on the order of the values, not on their sizes, so one extreme value cannot move it far.

The idea

Five requests take 1, 2, 3, 4 and 100 milliseconds (ms). The sample mean §11.12, the sum divided by the count, is (1+2+3+4+100)/5=110/5=22(1 + 2 + 3 + 4 + 100)/5 = 110/5 = 22 ms, larger than four of the five values. The middle sorted value is 3 ms, with two requests below it and two above. For the question of how long a typical request takes, 3 ms is the better answer. If the 100 ms request becomes 100,000 ms, the mean jumps to 100,010/5=20,002100{,}010/5 = 20{,}002 ms and the median stays at 3 ms.

Line people up by height and pick the person in the middle. The tallest person could be ten meters tall without changing who stands in the middle. The picture stops at an even number of people: there is no single middle person, and a convention must decide.

How it works

  1. Sort the nn values from smallest to largest and number the sorted positions from 1.

  2. If nn is odd, the median is the value at position (n+1)/2(n + 1)/2. For n=5n = 5 that is position (5+1)/2=3(5 + 1)/2 = 3.

  3. If nn is even, the two middle positions are n/2n/2 and n/2+1n/2 + 1, and the common convention reports the average of their values. For 1, 2, 4 and 100 ms the middle values are 2 and 4 ms, and the median is (2+4)/2=3(2 + 4)/2 = 3 ms, a value that was never observed. Other conventions report the lower or the upper of the two middle values, which is always an observed value; the statistics module of the Python §12.23 language, for example, offers median, median_low and median_high.

  4. State how missing values are treated. Some functions skip them; others return an undefined result when any value is missing.

Worked example

Each of three requests passes through two stages, A and B. The times, in ms, are:

Request Stage A Stage B Total
1 0 100 100
2 0 0 0
3 100 0 100

The median of stage A is the middle of 0, 0 and 100, which is 0 ms. The median of stage B is also 0 ms. Their sum is 0 ms. The totals, sorted, are 0, 100 and 100 ms, and their median is 100 ms. Adding the stage medians misses the median total by 100 ms.

Pair the same stage values differently, with both long stages in one request. The totals become 0+0=00 + 0 = 0, 0+0=00 + 0 = 0 and 100+100=200100 + 100 = 200 ms, and their median is 0 ms. Each stage’s list of values is unchanged, so the stage medians cannot tell the two cases apart. The means can: in both cases the mean total is 200/3≈66.7200/3 \approx 66.7 ms, which equals the stage A mean of 100/3≈33.3100/3 \approx 33.3 ms plus the stage B mean of 100/3≈33.3100/3 \approx 33.3 ms. The mean adds up whatever the pairing; the median does not. The correct order is to add each request’s own stage times first, then take the median of the totals.

Stated precisely

For a sample x1,…,xnx_1, \dots, x_n, write x(1)≤x(2)≤…≤x(n)x_{(1)} \le x_{(2)} \le \dots \le x_{(n)} for the same values sorted; x(k)x_{(k)} is called the kk-th order statistic. The sample median is x((n+1)/2)x_{((n+1)/2)} when nn is odd and, under the averaging convention, (x(n/2)+x(n/2+1))/2(x_{(n/2)} + x_{(n/2+1)})/2 when nn is even.

For a random quantity XX, a population median is any number mm with P(X≤m)≥1/2P(X \le m) \ge 1/2 and P(X≥m)≥1/2P(X \ge m) \ge 1/2, where PP denotes probability. The population median need not be unique. If XX is 0 or 10 with probability one half each, every mm from 0 to 10 satisfies both conditions, and a convention must choose one, for example the midpoint, 5. If P(X≤x)P(X \le x), as a function of xx, is continuous and strictly increasing where it passes 1/21/2, the median is the one value mm with P(X≤m)=1/2P(X \le m) = 1/2.

Medians add in one special case. Call two stages comonotone when they rank the requests in the same order: the request with the kk-th smallest stage A time also has the kk-th smallest stage B time, for every kk. Then the kk-th smallest total is a(k)+b(k)a_{(k)} + b_{(k)}, and for an odd nn the median of the totals equals the sum of the two medians. In the first table the long stage A time and the long stage B time belong to different requests, the stages are not comonotone, and the equality fails. The mean needs no such condition: the mean of the totals always equals the sum of the means.

Limits and common mistakes

The median says nothing about the tail §11.36, the rare large values. A system whose median is 3 ms can have one request in a hundred that takes a minute. When slow requests matter, report high percentiles, such as the 99th, or the empirical cumulative distribution function §11.17.

The median of differences differs from the difference of medians §18.1.14. In a comparison of matched measurements, compute each pair’s difference first and then take the median of the differences.

The median is the wrong summary when totals matter. Total processor time, total energy and throughput follow the mean, whatever the median shows.

A median of per-board medians weights each board equally; the median of all requests weights each request equally. They answer different questions.

In a small sample the median is coarse. It is one observed value, or the average of two, and it jumps when a single value crosses the middle. Its uncertainty can be estimated with the bootstrap §11.19, or with an interval whose ends are order statistics.

Resistance to extreme values is not resistance to everything. If enough values cross the middle, for example when a third of the requests, previously faster than the median, become slower than it, the median moves too.

Questions answered

Why is the median of sums generally different from the sum of medians?

The median of sums differs from the sum of medians because the median is chosen by position in sorted order, not computed by adding values, so it does not add: the middle total depends on which stage values occur together in the same request, and the separate stage medians do not record that. Take three requests with stage A times of 0, 0 and 100 milliseconds (ms) and stage B times of 100, 0 and 0 ms, in request order. Each stage’s median is 0 ms, so the sum of the medians is 0 ms. The request totals are 100, 0 and 100 ms, and their median is 100 ms. Pairing the same values differently, with both long stages in one request, gives totals of 0, 0 and 200 ms and a median total of 0 ms. So even knowing every value of each stage does not determine the median total; how the values are combined within requests does. The two agree when both stages rank the requests in the same order. The mean, by contrast, always adds: the mean of the totals equals the sum of the stage means. To summarize totals, form each request’s total first, then take the median.

Related entries: Sample mean (§11.12), Quantile and percentile (§11.16), Latency distribution and tail (§11.36), Empirical cumulative distribution function (ECDF) (§11.17), Paired design (§11.7).