Definition
A quantile at level , a fraction between 0 and 1, is the smallest value that splits a distribution so that at least a fraction of it lies at or below the value and at least 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, 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 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 values from smallest to largest and write for the value at sorted position . The parentheses mark a sorted position, so is the smallest value and the largest; these sorted values are called order statistics. Two rules are common.
Nearest rank, also called the inverse of the empirical distribution function §11.17: report with , where means rounded up to a whole number. The result is always one of the observed values.
Linear interpolation: compute the position . If is a whole number, report . Otherwise let be rounded down, and report , the point that lies the fraction of the way from to .
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 ; for near 1 and small 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 is a whole number.
Worked example
Five response times §9.32, already sorted, are 1, 2, 3, 4 and 100 milliseconds, so . For p95, .
Nearest rank: , rounded up to , so the p95 is milliseconds.
Linear interpolation: , so and the fraction is . The p95 is milliseconds.
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, , the two rules agree: nearest rank gives , interpolation gives , and both report milliseconds.
Even with many values the gap between adjacent observations matters. For and , nearest rank reports , and interpolation computes , which gives plus 5% of the gap to . Sample size limits what a percentile can mean. Under nearest rank, the 99th percentile of fewer than 100 values is the largest value, because whenever : a p99 from 50 measurements is just their maximum.
Stated precisely
Let be a random value, such as the duration of one request, and let be its cumulative distribution function: the probability that is at most . For , the quantile at level is the smallest at which reaches . Here , 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 is a minimum. It satisfies and . When is continuous and strictly increasing, is simply the with .
Replacing by the empirical cumulative distribution function , the fraction of the sample values that are at most , gives the nearest-rank rule. Since , the smallest sample value at which reaches is . The linear rule instead joins the points for by straight lines and reports where the line reaches height . 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 follows a binomial distribution, the distribution of the number of successes in independent trials that each succeed with probability . For and , all 20 values lie below the true p95 with probability . 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 , 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 , meaning 0.95 times the sample size , rounded up, and so always reports an observed value. Linear interpolation computes the position and interpolates between the two sorted values around ; 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 and reports 100 milliseconds, while linear interpolation gives position 4.8 and reports 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].