PinballLoss¶
Defined in fynance.models.loss
- class PinballLoss(taus=(0.1, 0.5, 0.9), **kwargs)[source]
Bases:
BaseLossPinball (quantile / check) loss for multi-quantile regression.
For a target quantile \(\tau \in (0, 1)\) and error \(e = y - \hat{y}_\tau\):
\[\mathcal{L}_\tau(e) = \max(\tau e,\ (\tau - 1) e)\]an asymmetric penalty: under-prediction (\(e > 0\)) is penalized by \(\tau\) and over-prediction (\(e < 0\)) by \(1 - \tau\). At \(\tau = 0.5\) it reduces to \(0.5 |e|\), i.e. half the mean absolute error. Minimizing it over a batch drives
predtoward the \(\tau\)-th conditional quantile oftarget.With several
tausat once,predcarries one column per quantile and the reported loss is the mean over quantiles and samples of the per-quantile pinball loss — the standard way to train a single multi-quantile head such asQuantileModel.- Parameters:
- tausfloat or sequence of float, optional
Target quantile(s) in
(0, 1). Default(0.1, 0.5, 0.9).- **kwargs
Forwarded to
BaseLoss(rf,period,eps) — not used by this loss (it is not a ratio/annualized objective), kept only for API consistency acrossfynance.models.loss.
- Raises:
- ValueError
If
tausis empty or any value is not strictly in(0, 1).
See also
fynance.models.quantile.QuantileModel
Notes
tausare sorted ascending internally (see_validate_taus), sopred’s trailing axis is expected in that same ascending order. This loss does not enforce non-crossing quantiles (q10 <= q50 <= q90) — it is the plain per-column pinball loss; monotonicity is a predict-time concern (seepredict_quantiles).Examples
>>> import torch >>> from fynance.models.loss import PinballLoss >>> target = torch.tensor([1.0, 2.0, 3.0]) >>> pred = torch.tensor([[1.0], [2.0], [3.0]]) # perfect single-tau fit >>> PinballLoss(taus=0.5)(pred, target).item() 0.0 >>> pred_off = pred + 1.0 >>> loss = PinballLoss(taus=0.5)(pred_off, target) >>> loss.item() == 0.5 # tau=0.5 -> half the (constant) absolute error True
- forward(pred, target)[source]
Compute the mean pinball loss across quantiles and samples.
- Parameters:
- predtorch.Tensor
Predicted quantiles, shape
(..., n_taus).- targettorch.Tensor
True values, shape
(...,)—predwithout its trailing quantile axis — broadcast against every quantile column.
- Returns:
- torch.Tensor
Scalar loss: the mean over quantiles and samples of \(\max(\tau e, (\tau - 1) e)\).
- Raises:
- TypeError
If
predortargetis not atorch.Tensor.- ValueError
If
pred’s trailing dimension does not equal the number oftaus, ortarget’s shape does not matchpred’s shape without that trailing dimension.