#355Pinball (Quantile) Regression LossMedium

Pinball (Quantile) Regression Loss

Background

Quantile regression predicts a specific quantile of yxy \mid x rather than the mean. The loss is the asymmetric pinball loss: under-prediction is penalised by τ\tau per unit, over-prediction by (1τ)(1 - \tau) per unit. The ETA-prediction case uses this to train per-quantile heads (p10, p50, p90) for delivery-time confidence intervals.

Problem statement

Implement pinball_loss(y_true, y_pred, tau):

L(y,y^;τ)  =  max(τ(yy^), (τ1)(yy^))\mathcal{L}(y, \hat{y}; \tau) \;=\; \max\bigl(\tau (y - \hat{y}),\ (\tau - 1)(y - \hat{y})\bigr)

Return the mean over all samples.

Input

  • y_true, y_pred - array-like of floats, same length.
  • tau - float in (0,1)(0, 1), the target quantile.

Output

  • float >= 0.

Examples

Input: y_true=[1.0, 2.0], y_pred=[0.0, 0.0], tau=0.5
Output: 0.5 * mean([1.0, 2.0]) = 0.75

Constraints

  • 0<τ<10 < \tau < 1. ValueError otherwise.
  • Lengths must match.

Notes

  • tau = 0.5 -> MAE / 2. The 50%-quantile pinball loss equals half the mean absolute error.
  • Asymmetric penalty. For ETA prediction with τ=0.9\tau = 0.9, under-promising (predicting too high) is 9x cheaper than missing the deadline (predicting too low).
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Reference example: tau=0.5 equals half the MAE
  • Example: pure under-prediction at tau=0.9
  • Sample: mixed over/under predictions at tau=0.3