Foundations to Frontier
Tests
E. Deep Learning › E2 Loss Functions (one page each)

Quantile (pinball) loss

The quantile or pinball loss, max(τr, (τ − 1)r) for residual r, makes a model predict the τ-quantile of the target instead of its mean: under-predictions cost τ per unit and over-predictions 1 − τ. Fitting several quantiles gives prediction intervals that need no assumption about the shape of the noise, so they stay correct for skewed and heteroscedastic data. This page covers the loss, why its minimiser is a quantile, multi-quantile models, quantile crossing, and interval scoring. In the exercises you implement the loss and the interval score and confirm that the best constant is the τ-quantile, then show that quantile regression put close to 5% of points beyond each bound of a 90% interval in both quiet and noisy regions, where a symmetric Gaussian interval was wrong on both sides.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Predicting a bound, not an average

Sometimes the useful prediction is not “how long will the battery last on average” but “how long will it last in the worst 10% of cases”. The quantile loss trains a model to predict such a bound. It charges more for errors on one side: for the 90th percentile, predicting too low costs 0.9 per unit and too high only 0.1, so the model settles where 90% of outcomes fall below its prediction. Two quantiles, say the 5th and the 95th, give an interval that adapts to how noisy and how lopsided the data are.

Regression losses: how hard does each point pull?
residual r from −4 to 4
quantile (pinball): ŷ = 2.43 + 0.86x · least squares ŷ = 1.08 + 0.52x · true line 1.00 + 0.50x · share of points below the line 0.90 (target τ = 0.90)
Orange points are outliers. Solid line: fit under the chosen loss; dashed: least squares. Right: the loss (solid) and its derivative (dashed), the pull a point exerts at each residual. Squared error's pull grows without limit, so a few outliers drag its line upward; absolute error, Huber and log-cosh cap the pull and stay near the true line. The quantile loss pulls with τ above the line and 1 − τ below it, so its line settles where a share τ of the points lies below; with noise that grows with x, the quantile lines fan out.
Why engineers care

Engineering decisions run on bounds: worst-case descent time, the 95th percentile of wind gusts, the 5th percentile of battery capacity, demand forecasts for spares. Real noise is often skewed (delays can be long but not negative) and its spread changes with conditions. Quantile regression gives intervals that stay honest under both, without assuming Gaussian noise.

📝 Quick check (Level 0)
0/3 answered
  1. 1. The 90% quantile loss charges under-predictions…
  2. 2. The τ = 0.5 quantile loss targets…
  3. 3. Quantile intervals require Gaussian noise?

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Pinball loss: ρ_τ(r) = τ·r if r ≥ 0, and (τ − 1)·r if r < 0, with r = y − q.
  • Examples with τ = 0.9: y = 5, q = 3 costs 0.9 × 2 = 1.8; y = 1, q = 3 costs 0.1 × 2 = 0.2.
  • τ = 0.5: ρ = ½|r|, half the absolute error, so its minimiser is the median.
  • Best constant: the τ-quantile of the data. Its subgradient is −τ·#(above) + (1 − τ)·#(below), which is zero when a share τ of the points lies below.
  • Interval: fit τ = α/2 and 1 − α/2. For a 90% interval, τ = 0.05 and 0.95.
  • Interval (Winkler) score: (hi − lo) + (2/α)·(lo − y) if y < lo, + (2/α)·(y − hi) if y > hi. It rewards narrow intervals and penalises misses. A proper scoring rule for intervals.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Pinball loss (τ = 0.25) for y = 2, q = 0?
  2. 2. For a 80% central interval you fit τ =…
  3. 3. The interval score penalises a miss by…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Quantiles in code

Exercise 1.

  • The pinball loss at τ = 0.5 is half the MAE.
  • On exponential samples, the best constant found by search put exactly a share τ of the data below it, matching the true quantiles −log(1 − τ): 0.105, 0.693 and 2.303.
  • The interval score and a quantile-crossing check are implemented.

Practice.

  • Multi-quantile heads: one network outputs several quantiles at once (τ = 0.1, 0.5, 0.9), trained with the sum of pinball losses.
  • Crossing: separately fitted quantiles can cross (q₀.₁ > q₀.₉ at some x). Fix it by sorting the outputs, or by predicting positive increments between quantiles.
  • Optimisation: the loss is piecewise linear, so plain subgradient descent converges slowly, especially for extreme τ. Adam or a smoothed (Huberised) pinball loss help; linear quantile regression can be solved exactly by linear programming.
  • Conformal calibration: adjusting the bounds on a held-out set guarantees finite-sample coverage.
def pinball(y, q, tau): r = y - q; return torch.maximum(tau * r, (tau - 1) * r).mean()
🐍 Exercise 1: the pinball loss and interval scoring
Implement the pinball loss and the interval score, confirm that the best constant is the τ-quantile, and detect quantile crossings.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Quantile crossing means…
  2. 2. Linear quantile regression can be solved exactly by…
  3. 3. Plain subgradient descent on extreme quantiles is…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why the minimiser is a quantile. E[ρ_τ(Y − q)] has derivative −τ·P(Y > q) + (1 − τ)·P(Y < q) = P(Y < q) − τ, which is zero when F(q) = τ. The expected pinball loss is convex in q and minimised at the τ-quantile. Conditionally, a flexible model trained with ρ_τ predicts the conditional quantile q_τ(x).

Distribution-free. Nothing assumes a noise family: skewness and changing spread are captured as long as the model can represent the quantile function. In exercise 2 the true quantiles were linear in x, so linear quantile regression was correctly specified even though the noise was skewed and heteroscedastic.

Consistency and properness. The pinball loss is a consistent scoring function for quantiles (Gneiting, 2011): no other report has lower expected loss than the true quantile. The interval score is proper for central intervals.

Limitations.

  • Intervals from quantile regression have only asymptotic coverage guarantees and can under-cover in small samples or under model misspecification. Conformalised quantile regression (Romano, Patterson and Candès, 2019) fixes finite-sample coverage.
  • Extreme quantiles need lots of data.
📝 Quick check (Level 3)
0/3 answered
  1. 1. The derivative of the expected pinball loss is…
  2. 2. Quantile regression was introduced by…
  3. 3. Finite-sample coverage can be guaranteed by…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

Honest intervals for skewed noise

History.

  • R. Koenker and G. Bassett (1978) introduced regression quantiles.
  • Gradient boosting and neural forecasters (for example the multi-horizon quantile networks of R. Wen et al., 2017) brought quantile losses to large-scale forecasting.
  • Y. Romano, E. Patterson and E. Candès (2019) introduced conformalised quantile regression.
  • T. Gneiting (2011) formalised consistent scoring functions.

Exercise 2. y = 2x + (0.2 + x)(E − 1), with E ~ Exponential(1) and x uniform on [0, 1]: noise that is skewed (long upper tail) and grows with x. A 90% interval is fitted in two ways, on 2000 training points, and evaluated on 20 000 test points in the low-x and high-x halves:

  • linear quantile regression at τ = 0.05 and 0.95, trained with Adam on the pinball loss;
  • least squares with a symmetric mean ± 1.645σ interval of one width.

The fitted 5% line was (−0.185, 1.048), against the true (−0.190, 1.051); the 95% line was (0.38, 4.32), against the true (0.40, 4.00).

interval region below the lower bound (target 0.05) above the upper bound (target 0.05)
quantile regression x < 0.5 0.063 0.046
quantile regression x ≥ 0.5 0.053 0.039
mean ± 1.645σ x < 0.5 0.000 0.025
mean ± 1.645σ x ≥ 0.5 0.000 0.087
  • Quantile regression was close to 5% in every cell (3.9–6.3%), in both regions and on both sides. Its 95% line’s slope was somewhat too steep, as expected for an extreme quantile from 2000 points.
  • The Gaussian interval was wrong in three of four cells. It never missed below, because skewed noise is bounded below, so its lower bound was wasted width. Above, it missed 2.5% of the time where noise was small and 8.7% where it was large.
  • An optimisation lesson: plain subgradient descent with a decaying step had not converged for τ = 0.95 in a first attempt (slope 1.83 against 4.0). Adam fixed it. Always compare fitted quantiles with empirical coverage.
🐍 Exercise 2 (research): honest intervals for skewed noise
Fit 5% and 95% linear quantile regressions to skewed, heteroscedastic data and compare their tail coverage in quiet and noisy regions with a symmetric Gaussian interval.

Quantile losses in AI, robotics and aerospace

  • Planning under uncertainty: quantile predictions of travel time, energy use or wind give risk-aware margins for mission planning.
  • Maintenance and reliability: lower quantiles of remaining useful life set conservative replacement schedules.
  • Forecasting: demand, delay and arrival-time forecasts are reported as quantiles (P10, P50, P90) rather than single numbers.

References

  1. R. Koenker, G. Bassett (1978). “Regression quantiles.” Econometrica 46(1).
  2. R. Koenker (2005). Quantile Regression. Cambridge University Press.
  3. T. Gneiting (2011). “Making and evaluating point forecasts.” Journal of the American Statistical Association 106.
  4. Y. Romano, E. Patterson, E. Candès (2019). “Conformalized quantile regression.” Advances in NeurIPS 32.
  5. R. Wen, K. Torkkola, B. Narayanaswamy, D. Madeka (2017). “A multi-horizon quantile recurrent forecaster.” NeurIPS Time Series Workshop.