TATECHATLAS
◎ English
Math & models / Tip

Aligning Loss and Evaluation Metrics in Time Series

Using the same strictly consistent scoring function for both training and evaluation in time series forecasting ensures models are optimized for and accurately assessed against the desired outcome.

On this page

For time series forecasting, it is crucial to use the same strictly consistent scoring function for both training a model and evaluating its performance. This alignment ensures that the model's learning process is directly optimized for the metric that matters most for the specific business objective. When a model is trained using a loss function that perfectly aligns with the evaluation metric, it is incentivized to predict the true underlying functional of the target variable. If different functions are used, the model might minimize a surrogate loss during training, leading to a model that appears to perform well but fails to meet the actual requirements in practice. This is particularly relevant in probabilistic forecasting where specific quantiles are of interest.

Aligning Loss and Evaluation Metrics

To ensure that a model's learning process is directly optimized for the desired evaluation outcome, it is critical to use the same strictly consistent scoring function for both training (as a loss function) and evaluation (as a metric). This alignment prevents situations where a model performs well on a surrogate loss function but poorly on the actual metric of interest, leading to suboptimal or misleading results. Statistical decision theory suggests that if the scoring function is not dictated by external constraints, one should consider the ultimate goal and application of the prediction. If the goal is to predict a specific functional of a distribution, such as the mean, median, or a quantile, a strictly consistent scoring function should be chosen for that target functional. This consistent function should then be used for both model training and evaluation.

A strictly consistent scoring function acts as a 'truth serum,' incentivizing the model to predict the true underlying functional of the target variable. When the scoring function is aligned with the ultimate goal of the prediction (e.g., predicting a specific quantile), using it for both training and evaluation ensures that the model is optimized for and accurately assessed against that goal. This is particularly important in time series forecasting where specific quantiles might be critical for decision-making, such as risk management or resource allocation.

from sklearn.metrics import mean_absolute_error, mean_pinball_loss
from sklearn.ensemble import HistGradientBoostingRegressor

# Example of using a consistent scoring function (pinball loss for quantile)
# Assume y_true are the actual observed values and y_pred_quantile are the predicted 99th quantiles
y_true = [10, 12, 15, 11, 13]
y_pred_quantile = [11, 13, 14, 12, 14]

# Using pinball loss for evaluation
mae_loss = mean_absolute_error(y_true, y_pred_quantile)
pinball_loss = mean_pinball_loss(y_true, y_pred_quantile, alpha=0.99)

print(f"Mean Absolute Error: {mae_loss}")
print(f"Mean Pinball Loss (alpha=0.99): {pinball_loss}")

# If training a model, use the corresponding loss function
# model = HistGradientBoostingRegressor(loss='quantile', quantile=0.99)
# model.fit(X_train, y_train)

Consequences of Misalignment

If different functions are used for training and evaluation, the model might learn to minimize a loss function that doesn't perfectly reflect the desired outcome. For instance, training with Mean Squared Error (MSE) optimizes for the mean of the distribution. However, if the business objective is to predict a specific quantile (e.g., the 99th percentile for risk management), evaluating with MSE would not accurately capture the model's performance on that quantile. This misalignment can lead to a model that appears to perform well during training or initial validation but fails to meet critical business objectives in practice because its predictions are not optimized for the target functional.

For example, if a model is trained to minimize Mean Absolute Error (MAE), it is optimized to predict the median. If this model is then evaluated using Mean Squared Error (MSE), its performance might seem acceptable. However, if the true goal was to predict the median, evaluating with MSE would not be the most informative. Conversely, training with MSE (optimizing for the mean) and evaluating with MAE (assessing median prediction accuracy) would also lead to a mismatch, as the model's learned behavior is not directly aligned with the evaluation criterion.

from sklearn.metrics import mean_absolute_error, mean_squared_error

# Scenario: Model trained to minimize MAE (optimizing for median)
y_true = [10, 12, 15, 11, 13]
y_pred_median_optimized = [11, 12, 14, 12, 13] # Predictions optimized for median

# Evaluating this model using MSE
mae_eval = mean_absolute_error(y_true, y_pred_median_optimized)
mse_eval = mean_squared_error(y_true, y_pred_median_optimized)

print(f"Model trained for MAE, evaluated with MAE: {mae_eval}")
print(f"Model trained for MAE, evaluated with MSE: {mse_eval}")

# Scenario: Model trained to minimize MSE (optimizing for mean)
y_pred_mean_optimized = [10.5, 12.5, 14.5, 11.5, 13.5] # Predictions optimized for mean

# Evaluating this model using MAE
mae_eval_2 = mean_absolute_error(y_true, y_pred_mean_optimized)
mse_eval_2 = mean_squared_error(y_true, y_pred_mean_optimized)

print(f"Model trained for MSE, evaluated with MAE: {mae_eval_2}")
print(f"Model trained for MSE, evaluated with MSE: {mse_eval_2}")

Example: Quantile Forecasting with Pinball Loss

Consider a scenario in network reliability engineering where the goal is to predict the 99% quantile of connection interruption duration per day. The target functional is precisely this 99% quantile. The strictly consistent scoring function for quantile prediction is the pinball loss. Therefore, one should use a loss function corresponding to pinball loss when training a model, such as HistGradientBoostingRegressor(loss='quantile', quantile=0.99). For evaluation, hyperparameter tuning, and model comparison, a metric like mean_pinball_loss with the same quantile (alpha=0.99) must be used. Using a different metric, such as Mean Absolute Error (MAE), for evaluation would not accurately assess the model's ability to predict the 99th percentile, even if the MAE itself is low.

from sklearn.metrics import mean_pinball_loss
from sklearn.ensemble import HistGradientBoostingRegressor
import numpy as np

# Sample data for demonstration
y_true = np.array([5, 8, 12, 15, 10, 9, 11, 14, 16, 13])
# These predictions are assumed to be optimized for the 99th percentile
y_pred_99th_percentile = np.array([7, 9, 13, 16, 11, 10, 12, 15, 17, 14])

# Training a model that uses pinball loss for the 99th quantile
# In a real scenario, you would fit this model to your training data.
# For illustration, we just show the initialization.
model = HistGradientBoostingRegressor(loss='quantile', quantile=0.99)
# model.fit(X_train, y_train)

# Evaluating the model's performance using the consistent metric
# alpha corresponds to the quantile (0.99 for 99th percentile)
performance_metric = mean_pinball_loss(y_true, y_pred_99th_percentile, alpha=0.99)

print(f"Evaluation Metric (Mean Pinball Loss for 99th percentile): {performance_metric}")

# If you were to evaluate with MAE, it would not directly reflect 99th percentile performance:
# from sklearn.metrics import mean_absolute_error
# mae_evaluation = mean_absolute_error(y_true, y_pred_99th_percentile)
# print(f"Alternative Evaluation (MAE): {mae_evaluation}")

Trade-offs and Applicability Limits

The principle of using strictly consistent scoring functions is most powerful when the objective is to predict a specific functional of the target variable's distribution, such as a quantile. In such cases, using the corresponding loss and evaluation metric ensures direct optimization and accurate assessment. However, this principle has limitations. It primarily applies to probabilistic predictions where a specific functional is targeted. For tasks where a direct functional prediction is not the primary goal, or when multiple, potentially conflicting, objectives exist, the choice of scoring function may involve more complex trade-offs and compromises.

Furthermore, the availability and implementation of strictly consistent scoring functions can vary across different machine learning libraries and specific prediction tasks. While common quantiles and means are well-supported, more complex or niche target functionals might not have readily available, strictly consistent scoring functions. In such scenarios, practitioners may need to rely on surrogate metrics or develop custom scoring functions, carefully considering the potential for misalignment and its consequences.

# Example illustrating a scenario where a single metric might not capture all goals
# Suppose we want to predict both the median (for typical values) and the 95th percentile (for risk)

# Metrics for median and 95th percentile
# from sklearn.metrics import mean_absolute_error # MAE is consistent for median
# from sklearn.metrics import mean_pinball_loss # Pinball loss is consistent for quantiles

# y_true = [10, 12, 15, 11, 13, 25, 30, 28, 26, 29]
# y_pred_median = [11, 12, 14, 12, 13, 26, 27, 27, 27, 28] # Optimized for median
# y_pred_95_percentile = [15, 16, 18, 17, 18, 28, 31, 30, 30, 32] # Optimized for 95th percentile

# Evaluating both predictions against both metrics
# mae_median_pred = mean_absolute_error(y_true, y_pred_median)
# mpl_median_pred = mean_pinball_loss(y_true, y_pred_median, alpha=0.95)

# mae_95_pred = mean_absolute_error(y_true, y_pred_95_percentile)
# mpl_95_pred = mean_pinball_loss(y_true, y_pred_95_percentile, alpha=0.95)

# print(f"Median-optimized model: MAE={mae_median_pred}, MPL(0.95)={mpl_median_pred}")
# print(f"95th-percentile-optimized model: MAE={mae_95_pred}, MPL(0.95)={mpl_95_pred}")
# This shows trade-offs: optimizing for one metric might degrade performance on another.

Applicability

  • Is the goal to predict a specific functional of the target variable's distribution (e.g., mean, median, quantile)?
  • Is the same strictly consistent scoring function used for both model training (as loss) and evaluation (as metric)?
  • If predicting quantiles, is the pinball loss used for training and mean_pinball_loss with the same alpha for evaluation?
  • Are potential misalignments between training loss and evaluation metric considered?
  • Is the availability of strictly consistent scoring functions for the specific task confirmed?

The principle of using strictly consistent scoring functions applies primarily to probabilistic predictions where a specific functional of the distribution is targeted. For tasks where a direct functional prediction is not the primary goal, or where multiple, potentially conflicting, objectives exist, the choice of scoring function may involve more complex trade-offs. The availability and implementation of strictly consistent scoring functions can vary across different machine learning libraries and specific prediction tasks.

Sources

  1. scikit-learn: model evaluation ↗
  2. scikit-learn: mean_absolute_error ↗
Back to top ↑