fusionlab.nn.losses.quantile_loss¶
- fusionlab.nn.losses.quantile_loss(q)[source]¶
Quantile (Pinball) Loss Function for Quantile Regression.
The
quantile_lossfunction computes the quantile loss, also known as Pinball loss, which is used in quantile regression to predict a specific quantile of the target variable’s distribution. This loss function penalizes over-predictions and under-predictions differently based on the quantile parameter, allowing the model to estimate the desired quantile.\[L_q(y, \hat{y}) =\]rac{1}{N} sum_{i=1}^{N} ho_q(y_i -
hat{y}_i)
Where: - \(y_i\) is the true value. - \(\hat{y}_i\) is the predicted value. - :math:`
ho_q(u)` is the quantile loss function defined as:
\[\]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.
- qfloat
The quantile to calculate the loss for. Must be a value between 0 and 1. For example,
q=0.1corresponds to the 10th percentile,q=0.5is the median, andq=0.9corresponds to the 90th percentile.
- losscallable
A loss function that can be used in Keras models. This function takes two arguments,
y_trueandy_pred, and returns the computed quantile loss.
>>> from fusionlab.nn.losses import quantile_loss >>> 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 quantile loss for the 10th percentile >>> model.compile(optimizer='adam', loss=quantile_loss(q=0.1)) >>> >>> # 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)
- Usage in Probabilistic Forecasting:
The quantile loss function is particularly useful in probabilistic forecasting where multiple quantiles are predicted to provide a distribution of possible outcomes rather than a single point estimate.
- Handling Multiple Quantiles:
To predict multiple quantiles, you can create separate output layers for each quantile and compile the model with a list of quantile loss functions.
- 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.