Quant interview toolkit · nonparametric inference

Nine observations and an exact interval for the median

Nine independent replay jobs take 8, 9, 11, 12, 14, 18, 23, 31 and 70 milliseconds. The sample median is 14. How uncertain is that estimate if the latency distribution is skewed and its shape is unknown? Start by counting how many jobs finish below the true median.

Sign-test coverage changes with rank

Coverage of [X(k), X(10−k)] (%)

Nine independent continuous observations; median coverage follows a Binomial(9, 1/2) count.

Hide the unknown median behind a count

Suppose latencies are independent draws from the same continuous distribution with unique median mm. Each draw falls below mm with probability one half. Write BB for the number of draws below it. Before observing the data, B∼Binomial⁡(9,1/2)B\sim\operatorname{Binomial}(9,1/2), whatever the distribution’s shape.

Write X(j)X_{(j)} for the jjth fastest observed time. The event X(2)≤m≤X(8)X_{(2)}\le m\le X_{(8)} means at least two observations are below mm and at most seven are below it (equality with mm has probability zero). The coverage is

P(2≤B≤7)=1−2(90)+(91)29=1−20512=96.09375%.P(2\le B\le7)=1-2\frac{\binom90+\binom91}{2^9} =1-\frac{20}{512}=96.09375\%.

For these measurements the resulting interval is 9 to 31 ms. A single extreme run could rise from 70 to 700 without changing either endpoint. Tightening both ends by one rank changes coverage substantially. The third through seventh observations, 11 to 23 ms, cover only 1−2(1+9+36)/512≈82.03%1-2(1+9+36)/512\approx82.03\%. At small sample sizes the available coverage levels come in steps.

Why 96% coverage does not mean 96% of future jobs fit inside

Coverage describes how often newly constructed intervals contain one fixed population median. A future job is a different target. With a tenth independent observation and no ties, its rank among all ten is uniform. It falls between the second and eighth observations of the original nine if its rank in the combined ten is 3 through 8. That probability is 6/10=60%6/10=60\%.

The same random endpoints can answer both questions. Their guarantees differ because the target is different. The 60% figure also averages over the original sample and the future observation. It is not a conditional guarantee for the specific observed limits of 9 and 31 ms.

How much data does a tail percentile require?

For a population quantile qpq_p with P(X<qp)=pP(X<q_p)=p, replace the fair coin by a Bernoulli draw with success probability pp. In particular, the sample maximum misses the true 99th percentile on the low side if every draw lands below it. With nn observations the chance is 0.99n0.99^n. To use the maximum as a one-sided upper confidence bound with at least 95% coverage, solve

1−0.99n≥0.95,n≥log⁡(0.05)log⁡(0.99).1-0.99^n\ge0.95,\qquad n\ge\frac{\log(0.05)}{\log(0.99)}.

Rounding up gives 299 independent jobs. With nine jobs the coverage would be only 1−0.999≈8.65%1-0.99^9\approx8.65\%. Even the slowest observed run is usually below the true 99th percentile in such a small sample.

from math import comb

def coverage(n, lo, hi, p=0.5):
    """Coverage of the closed interval between two one-based ranks."""
    if not (1 <= lo < hi <= n and 0 < p < 1):
        raise ValueError("invalid ranks or quantile")
    return sum(comb(n, b) * p**b * (1-p)**(n-b)
               for b in range(lo, hi))

print(coverage(9, 2, 8))  # 0.9609375

Practice

DependenceAll nine measurements came from back-to-back calls during one system stall. What step is in doubt?
Show answer

The indicators for being below the population median may be correlated. Even if each has probability one half of being below, their sum need not be binomial. Sampling independent replay sessions can be more informative about across-session latency than increasing the burst length.

RoundingWhat changes if the clock rounds all times to integer milliseconds?
Show answer

Ties with a population median can have positive probability. The continuous-model equalities for ranks then need adjustment. For a fixed discrete median, the probabilities of being strictly below and strictly above are each at most one half. Bounding the two failure tails separately still makes the closed, symmetric median interval conservative for independent draws. Uniform future ranks need a tie convention.