Targeting the Right Quantile When Errors Have Unequal Costs
Statistics & ExperimentsModel answerLast updated
Derive which statistic each loss function optimizes, then translate an asymmetric stockout-vs-overstock cost ratio into a concrete quantile target.
Setting the Scene
You are building a demand forecasting model for a retail chain's inventory system. Each day the model produces a prediction for how many units of a product will sell. You are deciding between two styles of output:
- A single-number forecast (a point prediction of expected demand).
- A distributional forecast that reports specific percentiles of the predicted demand, such as the 10th, 50th, and 90th.
What You Need to Work Through
Part 1 — Define the loss
Write out the quantile loss function (also called pinball loss) for a quantile level tau, a predicted value y_hat, and an observed value y:
If y >= y_hat: L = tau * (y - y_hat)
If y < y_hat: L = (1 - tau) * (y_hat - y)
Explain intuitively what this expression is doing and how the slope changes depending on which side of y_hat the observation falls.
Part 2 — What does each loss function actually estimate?
Explain, with a brief derivation, why:
- A model trained to minimize MSE / RMSE converges to the conditional mean of the target distribution.
- A model trained to minimize quantile loss at tau = 0.5 converges to the conditional median.
Why does this distinction matter in practice, especially for demand data?
Part 3 — Turning a cost ratio into a quantile target
The business has estimated that running out of stock (under-predicting demand) is five times more costly per unit than holding excess inventory (over-predicting demand). Using the quantile loss framework, derive the optimal quantile to target when setting your inventory level, and explain the reasoning behind the formula.
Hints
Hint 1
For Part 2, try minimizing the expected loss over a single free parameter c. Take the derivative with respect to c and set it to zero — the CDF will appear naturally in the pinball case.
Hint 2
For Part 3, think about the marginal cost of adding one more unit of stock: it saves you from a stockout with some probability and causes overstock with the complementary probability. Where is the breakeven point?
Hint 3
The formula that falls out of Part 3 is tau* = cost_under / (cost_under + cost_over). Check that the direction makes sense: when under-predicting is more expensive, tau* should be above 0.5, meaning you stock high.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem connects three ideas: the geometry of a loss function, the statistical quantity that loss function targets, and the translation of business cost asymmetry into a precise model objective. A strong answer derives each result rather than just stating it.
Part 1 — The pinball loss and its geometry
For quantile level τ ∈ (0, 1), the loss penalizes under-predictions and over-predictions at different rates:
L(y, ŷ, τ) = τ · (y − ŷ) when y ≥ ŷ (we guessed too low)
L(y, ŷ, τ) = (1 − τ) · (ŷ − y) when y < ŷ (we guessed too high)
A compact way to write this is max(τ(y − ŷ), (τ − 1)(y − ŷ)).
The key property is that the slopes are asymmetric: the function rises at rate τ as ŷ moves below y, and at rate (1 − τ) as ŷ moves above y. When τ is large (close to 1), being below the observation is very expensive; when τ is small, being above it is. At τ = 0.5 the two slopes are equal and the loss reduces to half the absolute error.
Part 2 — Which statistic does each loss estimate?
Fix the features and let Y have CDF F and density f. Treat the prediction as a free constant c and minimize expected loss.
Squared error (MSE/RMSE)
Expected squared error: R(c) = E[(Y − c)²]
Differentiating: R'(c) = −2·E[Y − c] = 0, so c* = E[Y].
RMSE is a monotone transformation of MSE and has the same minimizer. The gradient magnitude changes, but the optimal point does not.
Pinball loss at level τ
R(c) = τ · ∫_{y > c} (y − c) f(y) dy + (1−τ) · ∫_{y < c} (c − y) f(y) dy
Differentiating (the boundary terms vanish because the integrand is zero at y = c):
R'(c) = −τ · P(Y > c) + (1−τ) · P(Y ≤ c)
= −τ(1 − F(c)) + (1−τ)F(c)
= F(c) − τ
Setting to zero: F(c*) = τ, meaning c* = F⁻¹(τ) — the τ-th quantile of Y.
At τ = 0.5, this is the median.
Why the intuition differs
The gradient of squared error is proportional to the size of the residual, so every observation pulls the estimate proportionally toward itself. The balance point is the mean.
The gradient of pinball loss depends only on the sign of the residual, not its size. The optimal point is where the probability mass above, weighted by τ, exactly balances the mass below, weighted by (1 − τ). Outliers do not pull harder; they just vote.
Why it matters for demand data
Retail demand is right-skewed — most days are near the baseline, but promotional events and supply disruptions create a heavy right tail. For skewed distributions the mean exceeds the median. An RMSE-trained model is dragged upward by those spikes even on ordinary days, leading to systematic overstock the majority of the time. The difference is not academic: on a product with daily demand typically around 100 units but occasional spikes to 400, the RMSE model might predict 120 on a typical day while the median model predicts 95.
Part 3 — Translating cost asymmetry into a quantile target
Define:
c_u= cost per unit of under-stocking (stockout)c_o= cost per unit of over-stocking (excess inventory)
For a stock level q, the total expected cost is:
C(q) = c_u · E[(Y − q)⁺] + c_o · E[(q − Y)⁺]
This expression is proportional to E[L_τ(Y, q)] where τ = c_u / (c_u + c_o). Minimizing C(q) is therefore equivalent to minimizing pinball loss at that τ. From Part 2, the minimizer is:
F(q*) = c_u / (c_u + c_o)
This result is the classical newsvendor critical ratio.
Marginal analysis check: adding one more unit to inventory saves c_u if demand exceeds q (probability 1 − F(q)) and wastes c_o if it does not (probability F(q)). Stop adding stock when c_u · (1 − F(q)) = c_o · F(q), which rearranges to the same formula.
Applying the numbers: with c_u = 5 and c_o = 1:
τ* = 5 / (5 + 1) = 5/6 ≈ 0.833
Stock at the 83rd percentile of forecast demand. If demand is approximately Normal(100, 20²), the corresponding z-score is about 0.97, giving a target stock level of roughly 100 + 0.97 × 20 ≈ 119 units. A model targeting the mean (τ = 0.5) would stock 100 units and face a stockout roughly half the time — a costly policy.
Implementation notes
- Train a quantile model directly at τ = 0.833. LightGBM supports this with
objective="quantile", alpha=0.833; a neural net can use pinball loss in the output layer. - For low-volume, integer-valued demand, choose the smallest integer q such that
F(q) ≥ 5/6. - Validate with two metrics: pinball loss at τ = 0.833 (primary), and empirical coverage — the fraction of days where actual demand fell at or below the predicted level should be close to 0.833.
- Be precise about what
c_uandc_ocapture.c_uis lost margin plus goodwill or substitution effects.c_ois holding cost plus the gap between original price and markdown price. The ratio drives τ; the absolute values do not.
Common mistakes
- Inverting the ratio: writing τ = 1/6 targets the 17th percentile — the cheap side — which makes stockouts worse. Under-prediction is the expensive error, so τ must be above 0.5.
- Claiming RMSE and MSE have different minimizers: they share the same argmin.
- Assuming mean ≈ median for demand data: skewness makes this assumption break down exactly where it hurts most.
- Adding a fixed safety stock to the mean forecast: fixed offsets ignore the fact that demand variance changes with context (day of week, promotions, season). Quantile regression captures that variation directly.
- Evaluating a τ = 0.833 model with RMSE: RMSE penalizes distance from the mean, not coverage accuracy. Always evaluate a quantile model with pinball loss at the same τ and with a calibration check.