fusionlab.nn.losses.quantile_loss_multi¶
- fusionlab.nn.losses.quantile_loss_multi(quantiles=[0.1, 0.5, 0.9])[source]¶
Multi-Quantile (Pinball) Loss Function for Quantile Regression.
The
quantile_loss_multifunction computes the average quantile loss across multiple quantiles, allowing for the simultaneous prediction of several quantiles of the target variable’s distribution. This is particularly useful in probabilistic forecasting where a range of possible outcomes is desired.\[L_{ ext{multi}}(Y, \hat{Y}) =\]- rac{1}{Q} sum_{q in
ext{quantiles}} L_q(Y, hat{Y})
Where: - \(L_q(Y, \hat{Y})\) is the quantile loss for a specific quantile
\(q\).
\(Q\) is the total number of quantiles.
Each individual quantile loss is defined as:
\[L_q(Y, \hat{Y}) =\]
rac{1}{N} sum_{i=1}^{N} ho_q(y_i -
hat{y}_i)
And the pinball loss function :math:`
ho_q(u)` is:
\[\]ho_q(u) = u cdot (q - mathbb{I}(u < 0))
Here, \(\mathbb{I}(u < 0)\) is the indicator function that is 1 if \(u < 0\) and 0 otherwise.
- quantileslist of float, default=[0.1, 0.5, 0.9]
A list of quantiles to calculate the loss for. Each value must be between 0 and 1. For example,
quantiles=[0.1, 0.5, 0.9]corresponds to the 10th percentile, median, and 90th percentile respectively.
- losscallable
A loss function that can be used in Keras models. This function takes two arguments,
y_trueandy_pred, and returns the averaged quantile loss across the specified quantiles.
>>> from fusionlab.nn.loss import quantile_loss_multi >>> import tensorflow as tf >>> from tensorflow.keras.models import Sequential >>> from tensorflow.keras.layers import Dense >>> import numpy as np >>> >>> # Create a simple Keras model >>> model = Sequential() >>> model.add(Dense(64, input_dim=10, activation='relu')) >>> model.add(Dense(1)) >>> >>> # Compile the model with multi-quantile loss for the 10th, 50th, >>> # and 90th percentiles >>> model.compile(optimizer='adam', loss=quantile_loss_multi( ... quantiles=[0.1, 0.5, 0.9])) >>> >>> # Generate example data >>> X_train = np.random.rand(100, 10) >>> y_train = np.random.rand(100, 1) >>> >>> # Train the model >>> model.fit(X_train, y_train, epochs=10, batch_size=32)
- Probabilistic Forecasting:
The multi-quantile loss function is essential for probabilistic forecasting, where multiple quantiles provide a comprehensive view of the possible outcomes rather than a single point estimate.
- Model Output Configuration:
When using multiple quantiles, ensure that the model’s output layer is configured to output predictions for each quantile. For example, the output layer should have a number of units equal to the number of quantiles.
- Handling Multiple Quantiles in Predictions:
The model will output a separate prediction for each quantile. It is important to interpret these predictions correctly, understanding that each represents a specific percentile of the target distribution.
- Gradient Computation:
The quantile loss function is differentiable, allowing it to be used seamlessly with gradient-based optimization algorithms in Keras.
- Robustness to Outliers:
Unlike Mean Squared Error (MSE), the quantile loss function is more robust to outliers, especially when predicting lower or higher quantiles.
- tensorflow.keras.lossesA module containing built-in loss functions
in Keras.
- sklearn.metrics.mean_pinball_lossComputes the mean pinball loss,
similar to quantile loss used here.
- statsmodels.regression.quantile_regressionProvides tools for
quantile regression analysis.