Compute the quantile (pinball) loss
Why it matters Latency SLO models predict p95 or p99 time per request with exactly this loss.
To predict the 90th percentile instead of the mean, train with the pinball loss.
Write pinball(y, yhat, q) for arrays y and yhat and a quantile q in (0, 1). For residual r = y - yhat the loss is q * r if r >= 0 else (q - 1) * r. Return the mean over all elements as a float.
def pinball(y, yhat, q)Example
pinball([10, 10], [8, 12], 0.9) is (0.9 * 2 + 0.1 * 2) / 2 = 1.0.
Understand the idea
Mean squared error trains a model to predict the average. To predict a percentile instead, such as the 90th percentile of latency, use the pinball loss. It charges unequal prices: under-predicting costs q per unit, over-predicting costs 1 - q per unit. With q = 0.9, guessing too low is nine times more expensive than guessing too high, so the best prediction moves up to the 90th percentile. Worked example: y = [5, 5, 5], yhat = [4, 5, 8], q = 0.8. Residuals r = y - yhat = [1, 0, -3]. Losses: 0.8 * 1 = 0.8; 0.8 * 0 = 0; (0.8 - 1) * (-3) = 0.6. Mean = 1.4 / 3 = 0.4667. Rule: loss_i = q * r when r >= 0, else (q - 1) * r, which is always non-negative; return the mean as a float. Writing (1 - q) * r for the negative case gives negative losses and rewards overshooting. Used to forecast latency percentiles, capacity and demand ranges, and in quantile regression for prediction intervals.