Quant interview toolkit · quantile forecasts

Which percentile belongs in a latency budget?

A replay scheduler reserves time before the next job can start. Each unused millisecond costs one unit of idle capacity. Each millisecond of overrun costs four units of delay. Given a forecast distribution of runtime, how much time should the scheduler reserve? The answer follows from asking which jobs would benefit from one extra millisecond.

Underestimation costs depend on quantile

90th-percentile pinball loss (ms)

One observed 150 ms latency; pinball loss penalizes being under budget nine times as much as being over.

Move the reservation a little to the right

Let qq be the reservation and YY the runtime. Pay cu(Y−q)c_u(Y-q) if the job overruns and co(q−Y)c_o(q-Y) if it finishes early. Assume positive constant costs, a finite mean runtime, and that all reservations in the relevant range are feasible. For a tiny increase in qq, an early job costs a little more while a late job costs a little less. At a point of continuity of the runtime distribution FF, expected cost has slope

coF(q)−cu[1−F(q)].c_o F(q)-c_u[1-F(q)].

Set the slope to zero. The target cumulative probability is τ=cu/(cu+co)\tau=c_u/(c_u+c_o). Four units of overrun versus one of idle capacity selects the 80th percentile. With atoms in the distribution the slope can jump over zero. Every qq satisfying F(q−)≤τ≤F(q)F(q^-)\le\tau\le F(q) minimizes the convex expected cost.

You can have more than one best reservation

Consider five equally likely workloads taking 2, 3, 4, 8 and 13 ms. Four finish within 8 ms, so 8 meets the 80th-percentile condition. Look at what happens as the reservation moves across the gap before the longest job.

ReservationTotal unused msTotal overrun msMean cost per job
4 ms313(3+4(13))/5=11(3+4(13))/5=11
8 ms155(15+4(5))/5=7(15+4(5))/5=7
10 ms233(23+4(3))/5=7(23+4(3))/5=7
13 ms35035/5=735/5=7

Every extra millisecond from 8 to 13 charges four early jobs one unit each and saves four units on the remaining job. The empirical optimum is a whole interval. Different conventions for reporting a sample quantile can return different points in that interval.

Score a quantile forecast without a full distribution

Divide the cost by cu+coc_u+c_o. The normalized loss for residual u=y−qu=y-q is called pinball loss,

ρτ(u)={τuu≥0,(1−τ)(−u)u<0.\rho_\tau(u)= \begin{cases} \tau u & u\ge0,\\ (1-\tau)(-u) & u<0. \end{cases}

At τ=0.8\tau=0.8, the 8 ms reservation has mean pinball loss 7/5=1.47/5=1.4 ms. The units are milliseconds because the penalty scales linearly with the size of the miss. To evaluate time-varying forecasts, pair each forecast with its own realized runtime before averaging.

Coverage alone can hide a costly assignment. Suppose two jobs last 1 and 10 ms. Forecasts of 2 and 9 cover one of the two and incur mean loss (0.2+0.8)/2=0.5(0.2+0.8)/2=0.5 ms. Swapping the forecasts also covers one of the two, but now the early and late misses are both 8 ms. Mean loss rises to 4 ms. Compare forecasts on the same jobs, at the same quantile level, using a horizon chosen before observing their outcomes.

from fractions import Fraction as F

def pinball(y, q, tau):
    return tau * max(y - q, 0) + (1 - tau) * max(q - y, 0)

times = [2, 3, 4, 8, 13]
loss = lambda q: sum(pinball(y, q, F(4, 5)) for y in times) / len(times)
assert all(loss(q) == F(7, 5) for q in range(8, 14))
assert loss(7) > loss(8) and loss(14) > loss(13)

Practice

Cost ratioWhat cost assumption is implicit in reserving the 99th percentile under this model?
Show answer

The ratio cu/coc_u/c_o must equal 0.99/0.01=990.99/0.01=99. A millisecond of overrun has to cost 99 times a millisecond of idle capacity. A fixed charge triggered by any breach would add a different loss term and change this calculation.

Evaluation targetCan a forecaster always minimize tomorrow's cost by reserving the 80th percentile of the entire historical archive?
Show answer

That single reservation is fitted to the historical mix. If the scheduler knows the incoming workload size, the relevant target is the conditional runtime quantile for that size. Estimate it on earlier runs and score it on subsequent jobs, including the small and large jobs in the proportions the scheduler actually sees.