Quantile loss (also called pinball loss) is the loss function you minimise when you want a model to predict a chosen percentile of the target instead of its average. This page gives the formula, a free calculator for single predictions or whole batches, a worked example with real numbers, and the mistakes that trip up beginners.
What is quantile loss? The formula
Pick a quantile level \( \tau \) between 0 and 1 (for example \( \tau = 0.9 \) for the 90th percentile). Write the residual as \( r = y – \hat{y} \), the actual value minus the prediction. The pinball loss for one example is:
\[ L_\tau(y, \hat{y}) = \begin{cases} \tau \,(y – \hat{y}) & \text{if } y \ge \hat{y} \\ (1 – \tau)\,(\hat{y} – y) & \text{if } y < \hat{y} \end{cases} \]
The same thing in one line is \( L_\tau = \max\big(\tau r,\ (\tau – 1) r\big) \). Over a dataset of \( n \) points you take the mean:
\[ \text{Quantile loss} = \frac{1}{n} \sum_{i=1}^{n} L_\tau(y_i, \hat{y}_i) \]
The idea is an asymmetric penalty. With \( \tau = 0.9 \), predicting too low costs 0.9 per unit of error, while predicting too high costs only 0.1. The model is pushed upwards until roughly 90% of the actual values fall below its predictions, which is exactly what the 90th percentile means. With \( \tau = 0.5 \) both sides cost 0.5, and the loss is half the mean absolute error, so the model learns the median.
Quantile loss calculator
Enter the actual value, the prediction and \( \tau \), or paste two lists to average the pinball loss over a batch.
Worked example: forecasting demand at the 90th percentile
A shop wants to stock enough of a product to cover demand on most days, so it trains a model at \( \tau = 0.9 \). Tomorrow’s actual demand turns out to be \( y = 100 \).
- Prediction too low, \( \hat{y} = 90 \): the residual is \( r = 100 – 90 = 10 \ge 0 \), so \( L = 0.9 \times 10 = 9 \).
- Prediction too high, \( \hat{y} = 110 \): the residual is \( r = -10 \), so \( L = (1 – 0.9) \times 10 = 1 \).
Both predictions miss by 10 units, but under-predicting costs nine times more. That is the asymmetry that drags a \( \tau = 0.9 \) model towards high values. At \( \tau = 0.5 \), the same under-prediction would cost \( 0.5 \times 10 = 5 \), the same as an over-prediction.
Now a batch of four days, still with \( \tau = 0.9 \):
| Day | Actual \( y \) | Prediction \( \hat{y} \) | Residual \( r \) | Pinball loss |
|---|---|---|---|---|
| 1 | 12 | 10 | 2 | \( 0.9 \times 2 = 1.8 \) |
| 2 | 15 | 16 | −1 | \( 0.1 \times 1 = 0.1 \) |
| 3 | 9 | 9 | 0 | 0 |
| 4 | 20 | 17 | 3 | \( 0.9 \times 3 = 2.7 \) |
The sum is \( 1.8 + 0.1 + 0 + 2.7 = 4.6 \), so the mean quantile loss is \( 4.6 / 4 = 1.15 \). Those are the default lists in the calculator. Switch \( \tau \) to 0.5 and the batch gives 0.75, which is exactly half the MAE of 1.5 for these four days.
When to use quantile loss, and when not to
Use it when the cost of an error depends on its direction or when you need a range rather than one number:
- Prediction intervals. Train one model at \( \tau = 0.05 \) and another at \( \tau = 0.95 \) to get a 90% interval for each prediction, without assuming the errors are normal. The idea is related to a confidence interval, but it describes where individual outcomes fall, not where a mean lies.
- Asymmetric business costs. Running out of stock, under-provisioning servers or under-estimating delivery times usually hurt more than the opposite mistake.
- Skewed or heavy-tailed targets. Like MAE, the penalty grows linearly, so a few extreme outliers do not dominate training the way they do with squared error.
Most libraries support it directly. In scikit-learn, GradientBoostingRegressor(loss="quantile", alpha=0.9) trains a gradient boosting model at the 90th percentile, and mean_pinball_loss evaluates it. Note that scikit-learn calls the quantile level alpha, not \( \tau \), and default values differ between functions and libraries, so set it explicitly.
Skip it when you only need the average outcome (use mean squared error), when errors cost the same in both directions, or when you need a smooth second derivative: the loss has a kink at \( r = 0 \), which some optimisers handle less gracefully than smooth losses such as Huber loss or log-cosh loss.
Quantile loss vs mean absolute error
The nearest relative is MAE. Pinball loss is a tilted MAE: at \( \tau = 0.5 \) it equals exactly half of it, and both are minimised by the median.
| Quantile (pinball) loss | Mean absolute error | |
|---|---|---|
| Formula per point | \( \max(\tau r, (\tau – 1) r) \) | \( |r| \) |
| Penalty shape | Asymmetric V (slopes \( \tau \) and \( 1 – \tau \)) | Symmetric V (slope 1 on both sides) |
| What the best constant prediction is | The \( \tau \)-quantile of \( y \) | The median of \( y \) |
| Extra settings | \( \tau \) | None |
| Gradient w.r.t. \( \hat{y} \) | \( -\tau \) or \( 1 – \tau \) | \( -1 \) or \( +1 \) |
| Typical use | Percentiles, prediction intervals, asymmetric costs | Robust point forecasts |
Because the gradient only takes two values, the size of an error does not change how hard each point pulls during gradient descent; only its sign does. That makes the loss robust but can slow convergence near the optimum. For a broader overview see the guide to loss functions.
Common mistakes with quantile loss
- Flipping the residual. Some sources define \( r = \hat{y} – y \). If you do that with the formula above, a \( \tau = 0.9 \) model will learn the 10th percentile. Check which convention your code uses.
- Confusing \( \tau \) with the interval width. A 90% interval needs \( \tau = 0.05 \) and \( \tau = 0.95 \), not \( \tau = 0.9 \) on its own.
- Judging the model with RMSE. A good 90th-percentile model is deliberately biased upwards, so its RMSE looks poor. Evaluate it with pinball loss at the same \( \tau \) and check coverage: about 90% of actual values should sit below the predictions on held-out data, ideally across several cross-validation folds.
- Comparing losses across different \( \tau \). Values at \( \tau = 0.5 \) and \( \tau = 0.9 \) are on different scales. Compare models at the same quantile level only.
- Quantile crossing. Separately trained models can produce a 10th percentile above the 90th for some inputs. Sort the predictions or use a method that fits several quantiles jointly.
Frequently asked questions
Why is it called pinball loss?
Plotted against the residual, the loss looks like a tilted V, similar to the path of a ball bouncing off a pinball flipper. The names quantile loss, pinball loss and check loss all refer to the same function.
Can the loss be negative?
No. For \( \tau \) between 0 and 1, both branches are zero or positive, and it is zero only when the prediction equals the actual value.
What happens at tau = 0 or tau = 1?
At \( \tau = 0 \) under-predictions cost nothing, and at \( \tau = 1 \) over-predictions cost nothing, so the model is pushed towards the minimum or maximum of the data. In practice \( \tau \) is chosen strictly between 0 and 1.
Who introduced quantile regression?
Roger Koenker and Gilbert Bassett introduced regression quantiles in a 1978 paper in Econometrica. The Wikipedia article on quantile regression summarises the method and its history.
Does linear regression work with pinball loss?
Yes. Replacing squared error with pinball loss in linear regression gives linear quantile regression, which fits a straight line through the chosen percentile instead of the mean.