TATECHATLAS
◎ 简体中文
数学与模型 / 建议

时间序列预测中损失函数与评估指标的一致性

在时间序列预测中,为模型训练和评估使用相同且严格一致的评分函数,可以确保模型针对最关键的业务目标进行优化,并对其进行准确评估。

本文内容

对于时间序列预测,至关重要的是在训练模型和评估其性能时使用相同且严格一致的评分函数。这种一致性确保了模型的学习过程直接针对特定业务目标最重要的指标进行优化。当模型使用与评估指标完美匹配的损失函数进行训练时,它会被激励去预测目标变量的真实潜在函数。如果使用不同的函数,模型可能会在训练过程中最小化一个代理损失函数,导致模型在表面上表现良好,但在实践中未能满足实际要求。这在概率预测中尤其重要,因为此时对特定分位数感兴趣。

对齐损失函数与评估指标

为了确保模型的学习过程直接针对期望的评估结果进行优化,在训练(作为损失函数)和评估(作为指标)时使用相同且严格一致的评分函数至关重要。这种对齐可以避免模型在代理损失函数上表现良好,但在实际关注的指标上表现不佳的情况,从而导致次优或误导性的结果。统计决策理论表明,如果评分函数不受外部约束的限制,则应考虑预测的最终目标和应用。如果目标是预测分布的特定函数,例如均值、中位数或分位数,则应为该目标函数选择一个严格一致的评分函数。然后,应将此一致的函数用于模型训练和评估。

严格一致的评分函数充当“真相剂”,激励模型预测目标变量的真实潜在函数。当评分函数与预测的最终目标(例如,预测特定分位数)一致时,将其用于训练和评估可确保模型针对该目标进行优化并准确评估。这在时间序列预测中尤为重要,因为特定分位数可能对决策至关重要,例如风险管理或资源分配。

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)

不一致的后果

如果在训练和评估中使用不同的函数,模型可能会学习最小化一个不能完美反映期望结果的损失函数。例如,使用均方误差(MSE)进行训练会优化分布的均值。但是,如果业务目标是预测特定分位数(例如,风险管理的第 99 百分位数),则使用 MSE 进行评估将无法准确地捕捉模型在该分位数上的性能。这种不一致可能导致模型在训练或初步验证期间表现良好,但在实践中未能满足关键业务目标,因为其预测并未针对目标函数进行优化。

例如,如果一个模型被训练来最小化平均绝对误差(MAE),它就被优化为预测中位数。如果随后使用均方误差(MSE)来评估此模型,其性能可能看起来是可以接受的。但是,如果真正的目标是预测中位数,那么使用 MSE 进行评估就不是最有信息量的。反之,使用 MSE(优化均值)进行训练并使用 MAE(评估中位数预测准确性)进行评估,也会导致不匹配,因为模型学习到的行为并未直接与评估标准保持一致。

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}")

示例:使用 Pinball Loss 进行分位数预测

考虑网络可靠性工程中的一个场景,目标是预测每天连接中断时长的第 99 百分位数。目标函数正是这个第 99 百分位数。分位数预测的严格一致评分函数是 pinball loss。因此,在训练模型时应使用与 pinball loss 对应的损失函数,例如 HistGradientBoostingRegressor(loss='quantile', quantile=0.99)。对于评估、超参数调整和模型比较,必须使用具有相同分位数(alpha=0.99)的指标,如 mean_pinball_loss。使用其他指标,如平均绝对误差(MAE),进行评估,即使 MAE 本身很低,也无法准确评估模型预测第 99 百分位数的能力。

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}")

权衡与适用性限制

当目标是预测目标变量分布的特定函数(如分位数)时,使用严格一致评分函数的原则最为有效。在这种情况下,使用相应的损失和评估指标可确保直接优化和准确评估。然而,这一原则存在局限性。它主要适用于以特定函数为目标的概率预测。对于主要目标不是直接函数预测的任务,或者当存在多个可能冲突的目标时,评分函数的选择可能涉及更复杂的权衡和折衷。

此外,严格一致评分函数的可获得性和实现方式可能因不同的机器学习库和特定的预测任务而异。虽然常见的分位数和均值得到了很好的支持,但更复杂或小众的目标函数可能没有现成的、严格一致的评分函数。在这种情况下,从业者可能需要依赖代理指标或开发自定义评分函数,并仔细考虑不一致的可能性及其后果。

# 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.

适用条件

  • 目标是预测目标变量分布的特定函数(例如,均值、中位数、分位数)吗?
  • 模型训练(作为损失)和评估(作为指标)是否使用了相同且严格一致的评分函数?
  • 如果预测的是分位数,训练是否使用了 pinball loss,评估是否使用了具有相同 alpha 的 mean_pinball_loss?
  • 是否考虑了训练损失与评估指标之间潜在的不一致性?
  • 是否确认了针对特定任务存在严格一致的评分函数?

严格一致评分函数的原则主要适用于以分布的特定函数为目标的概率预测。对于主要目标不是直接函数预测的任务,或者当存在多个可能冲突的目标时,评分函数的选择可能涉及更复杂的权衡。严格一致评分函数的可获得性和实现方式可能因不同的机器学习库和特定的预测任务而异。

参考来源

  1. scikit-learn: model evaluation ↗
  2. scikit-learn: mean_absolute_error ↗
返回顶部 ↑