【问题标题】:How to take confidence interval of statsmodels.tsa.holtwinters-ExponentialSmoothing Models in python?如何在 python 中获取 statsmodels.tsa.holtwinters-ExponentialSmoothing 模型的置信区间?
【发布时间】:2022-01-13 12:58:39
【问题描述】:

我在 python 中使用 ExponentialSmoothing 进行时间序列预测分析。我使用了 statsmodels.tsa.holtwinters。

model = ExponentialSmoothing(df, seasonal='mul', seasonal_periods=12).fit()
pred = model.predict(start=df.index[0], end=122)

plt.plot(df_fc.index, df_fc, label='Train')
plt.plot(pred.index, pred, label='Holt-Winters')
plt.legend(loc='best')

我想取模型结果的置信区间。但我在“statsmodels.tsa.holtwinters - ExponentialSmoothing”中找不到任何关于此的功能。我该怎么做?

【问题讨论】:

    标签: python statsmodels forecasting confidence-interval holtwinters


    【解决方案1】:

    来自 GitHub 问题的this 回答,很明显您应该使用新的ETSModel 类,而不是旧的(但仍然存在以保持兼容性)ExponentialSmoothingETSModelExponentialSmoothing 包含更多参数和更多功能。

    要计算置信区间,我建议你使用ETSResultssimulate方法:

    from statsmodels.tsa.exponential_smoothing.ets import ETSModel
    import pandas as pd
    
    
    # Build model.
    ets_model = ETSModel(
        endog=y, # y should be a pd.Series
        seasonal='mul',
        seasonal_periods=12,
    )
    ets_result = ets_model.fit()
    
    # Simulate predictions.
    n_steps_prediction = y.shape[0]
    n_repetitions = 500
    
    df_simul = ets_result.simulate(
        nsimulations=n_steps_prediction,
        repetitions=n_repetitions,
        anchor='start',
    )
    
    # Calculate confidence intervals.
    upper_ci = df_simul.quantile(q=0.9, axis='columns')
    lower_ci = df_simul.quantile(q=0.1, axis='columns')
    

    基本上,调用simulate 方法你会得到一个带有n_repetitions 列和n_steps_prediction 步骤的DataFrame(在这种情况下,你的训练数据集中的相同数量的项目y)。 然后,您使用 DataFrame quantile 方法计算置信区间(请记住 axis='columns' 选项)。 您还可以从df_simul 计算其他统计信息。

    我还查看了源代码:simulate 在内部被forecast 方法调用,以预测未来的步骤。因此,您还可以使用相同的方法预测未来的步骤及其置信区间:只需使用anchor='end',这样模拟将从y 的最后一步开始。

    公平地说,还有一种更直接的方法来计算置信区间:get_prediction 方法(内部使用simulate)。但我不太喜欢它的界面,它对我来说不够灵活,我没有找到指定所需置信区间的方法。在我看来,simulate 方法的方法很容易理解,而且非常灵活。

    如果您想了解有关如何执行此类模拟的更多详细信息,请阅读优秀的预测:原则与实践在线书籍中的this 章节。

    【讨论】:

      猜你喜欢
      • 2019-04-23
      • 1970-01-01
      • 2021-03-10
      • 2022-01-07
      • 1970-01-01
      • 2013-04-21
      • 1970-01-01
      • 2020-05-01
      • 1970-01-01
      相关资源
      最近更新 更多