Quantile (Pinball) loss function
loss_quantile.Rd
A generalized version of the quantile loss. The model can predict multiple quantiles at once.
Details
Loss value for a single sample-timestep-quantile is computed as: $$QL(y_t, \hat{y}_t, q) = max(q(y_t - \hat{y}_t), (q - 1)(y_t - \hat{y}_t))$$ or equivalently as : $$QL(y_t, \hat{y}_t, q) = max(q(y_t - \hat{y}_t), 0) + max((1 - q)(\hat{y}_t - y_t), 0)$$
When multiple quantiles are defined, the generalized, averaged loss is computed according to the equation: $$\mathcal{L}(\Omega, W) = \Sigma_{y_t \in \Omega}\Sigma_{q \in \mathcal{Q}}\Sigma^{\tau_{max}}_{\tau=1} \frac{QL(y_t, \hat{y}(q, t - \tau, \tau), q)}{M_{\tau_{max}}}$$
The loss function is computed as above when reduction = 'auto
or reduction = 'mean'
.
Note
For the moment, you can only use loss_quantile
to instantiate a class
and not call directly like other loss in keras
. Please see:
#1342 issue
References
B. Lim, S.O. Arik, N. Loeff, T. Pfiste, Temporal Fusion Transformers for Interpretable Multi-horizon Time Series Forecasting(2020)
Examples
y_pred <- array(runif(60), c(2, 10, 3))
y_true <- array(runif(20), c(2, 10, 1))
loss_quantile(quantiles = c(0.1, 0.5, 0.9), reduction = 'auto')(y_true, y_pred)
#> tf.Tensor(0.15113433, shape=(), dtype=float32)
loss_quantile(quantiles = c(0.1, 0.5, 0.9), reduction = 'sum')(y_true, y_pred)
#> tf.Tensor(9.06806, shape=(), dtype=float32)