Section 11.16

Quantile and percentile

Definition

A quantile at level pp, a fraction between 0 and 1, is the smallest value that splits a distribution so that at least a fraction pp of it lies at or below the value and at least 1−p1 - p lies at or above it. A percentile is a quantile with the level in percent: the 95th percentile, written p95, is the quantile at level 0.95. The median §11.13 is the quantile at level 0.5. A sample quantile needs a stated rule, because the requested position usually falls between two sorted values.

The idea

A service answers most requests in 5 milliseconds, but one request in twenty waits behind a slow disk and takes 200 milliseconds. The mean, 0.95×5+0.05×200=14.750.95 \times 5 + 0.05 \times 200 = 14.75 milliseconds, describes no actual request, and it hides the slow requests that users notice. A percentile asks a direct question instead: within what time do 95% of requests finish? Latency targets are therefore often written as percentiles, for example “p99 below 50 milliseconds”.

The smallest-value convention resolves ties in the definition. With equal probabilities at 5 and 200 milliseconds, every value between them meets both half-probability conditions, and the lower median is 5. A sample of nn values raises a practical problem: the position 95% of the way up the sorted list is rarely a whole position. With 5 values, 95% of 5 is 4.75. A rule must say what to report: the 5th value, the 4th, or something in between. Different software uses different rules, and in small samples with large gaps between the sorted values, the rules give very different numbers.

How it works

Sort the nn values from smallest to largest and write x(j)x_{(j)} for the value at sorted position jj. The parentheses mark a sorted position, so x(1)x_{(1)} is the smallest value and x(n)x_{(n)} the largest; these sorted values are called order statistics. Two rules are common.

Hyndman and Fan (1996) cataloged nine sample-quantile rules found in statistical software and numbered them [469]. Nearest rank is their type 1, and the linear rule above is their type 7. Type 7 is the default of the quantile function of the R language and of the numpy.percentile function of the NumPy §12.29 library for Python §12.23. Type 6 uses the position h=(n+1)ph = (n + 1) p; for pp near 1 and small nn that position lies beyond the largest value, and software then either reports the largest value or refuses. Some rules report the average of two neighboring values when npnp is a whole number.

Worked example

Five response times §9.32, already sorted, are 1, 2, 3, 4 and 100 milliseconds, so n=5n = 5. For p95, p=0.95p = 0.95.

The same five values give 100 or 80.8 milliseconds. No request took 80.8 milliseconds; that value lies in the gap between the two largest observations. For the median, p=0.5p = 0.5, the two rules agree: nearest rank gives j=⌈2.5⌉=3j = \lceil 2.5 \rceil = 3, interpolation gives h=1+4×0.5=3h = 1 + 4 \times 0.5 = 3, and both report x(3)=3x_{(3)} = 3 milliseconds.

Even with many values the gap between adjacent observations matters. For n=1000n = 1000 and p=0.95p = 0.95, nearest rank reports x(950)x_{(950)}, and interpolation computes h=1+999×0.95=950.05h = 1 + 999 \times 0.95 = 950.05, which gives x(950)x_{(950)} plus 5% of the gap to x(951)x_{(951)}. Sample size limits what a percentile can mean. Under nearest rank, the 99th percentile of fewer than 100 values is the largest value, because ⌈0.99n⌉=n\lceil 0.99 n \rceil = n whenever n<100n < 100: a p99 from 50 measurements is just their maximum.

Stated precisely

Let XX be a random value, such as the duration of one request, and let F(x)=Pr(X≤x)F(x) = \Pr(X \le x) be its cumulative distribution function: the probability that XX is at most xx. For 0<p<10 < p < 1, the quantile at level pp is qp=inf{x:F(x)≥p},q_p = \inf\{x : F(x) \ge p\}, the smallest xx at which FF reaches pp. Here inf\inf, the infimum, is the greatest lower bound §19.2.6 of the set. Because a cumulative distribution function is right-continuous, the bound is attained, so qpq_p is a minimum. It satisfies Pr(X≤qp)≥p\Pr(X \le q_p) \ge p and Pr(X≥qp)≥1−p\Pr(X \ge q_p) \ge 1 - p. When FF is continuous and strictly increasing, qpq_p is simply the xx with F(x)=pF(x) = p.

Replacing FF by the empirical cumulative distribution function F̂n(x)\hat F_n(x), the fraction of the nn sample values that are at most xx, gives the nearest-rank rule. Since F̂n(x(j))≥j/n\hat F_n(x_{(j)}) \ge j/n, the smallest sample value at which F̂n\hat F_n reaches pp is x(⌈np⌉)x_{(\lceil np \rceil)}. The linear rule instead joins the points (x(j),(j−1)/(n−1))(x_{(j)},\ (j - 1)/(n - 1)) for j=1,…,nj = 1, \dots, n by straight lines and reports where the line reaches height pp. Both estimate the population quantile. For a single observation, both return that value.

The uncertainty of a sample quantile follows from counting. For independent values from a continuous distribution, the number of sample values below the true qpq_p follows a binomial distribution, the distribution of the number of successes in nn independent trials that each succeed with probability pp. For n=20n = 20 and p=0.95p = 0.95, all 20 values lie below the true p95 with probability 0.9520≈0.3580.95^{20} \approx 0.358. So in about 36% of such samples even the largest of the 20 values is below the true p95.

Limits and common mistakes

A sample p95 is not a bound. It does not promise that at most 5% of future requests will exceed it, and it says nothing about the worst case.

Percentiles do not add or average. The p95 of a total time is not in general the sum of the p95s of its parts, and the average of the p95s of ten machines is not in general the p95 of all their requests together. For a pooled percentile, combine the raw observations or their exact value counts. Fixed-width bins generally identify only the bin containing the desired rank.

Tail percentiles need many observations. A p99 rests on the largest 1% of the values; with 200 measurements that is two values. Report nn, and if precision matters, an interval for the percentile.

With a common observation limit, count all attempts when finding the nearest-rank percentile. If 19 of 20 requests finish by the limit and the last is still running, rank 19 gives an exact sample p95. With different limits, a censored request may finish before a completion observed on another request.

Name the rule. Two reports of p95 from the same data can differ by the gap between neighboring sorted values, so a large tail gap makes the convention especially important.

Questions answered

Which interpolation convention defines a sample p95?

No single interpolation convention is built into the name p95: a sample p95, the value below which about 95% of the sorted measurements lie, is defined only once a rule for a fractional position is stated. The two most common rules are these. Nearest rank reports the sorted value at position ⌈0.95n⌉\lceil 0.95 n \rceil, meaning 0.95 times the sample size nn, rounded up, and so always reports an observed value. Linear interpolation computes the position h=1+0.95(n−1)h = 1 + 0.95 (n - 1) and interpolates between the two sorted values around hh; it is the default of R’s quantile function and of NumPy’s numpy.percentile.

For the five values 1, 2, 3, 4 and 100 milliseconds, nearest rank gives position ⌈4.75⌉=5\lceil 4.75 \rceil = 5 and reports 100 milliseconds, while linear interpolation gives position 4.8 and reports 4+0.8×96=80.84 + 0.8 \times 96 = 80.8 milliseconds. A report should therefore state the rule, the software and its setting, and the number of measurements. Neither rule makes a p95 from a small sample precise.

Related entries: Empirical cumulative distribution function (ECDF) (§11.17), Confidence interval (§11.18), Censoring (§11.28), Latency distribution and tail (§11.36), Median (§11.13). Further reading: [469].