本文介绍了Python Statsmodels:将SARIMAX与外生回归变量一起使用以获取预测的均值和置信区间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用statsmodels.tsa.SARIMAX()训练带有外生变量的模型.当使用外生变量训练模型时,是否有等效的get_prediction(),以便返回的对象包含预测的均值和置信区间,而不只是预测的均值结果数组?预测()和预测()方法采用外生变量,但只返回预测的平均值.

I'm using statsmodels.tsa.SARIMAX() to train a model with exogenous variables. Is there an equivalent of get_prediction() when a model is trained with exogenous variables so that the object returned contains the predicted mean and confidence interval rather than just an array of predicted mean results? The predict() and forecast() methods take exogenous variables, but only return the predicted mean value.

SARIMA_model = sm.tsa.SARIMAX(endog=y_train.astype('float64'),
                          exog=ExogenousFeature_train.values.astype('float64'), 
                          order=(1,0,0),
                          seasonal_order=(2,1,0,7), 
                          simple_differencing=False)

model_results = SARIMA_model.fit()

pred = model_results.predict(start=train_end_date,
                               end=test_end_date,
                               exog=ExogenousFeature_test.values.astype('float64').reshape(343,1),
                               dynamic=False)

此处的pred是预测值的数组,而不是包含运行get_predict()时将获得的预测平均值和置信区间的对象.请注意,get_predict()不会接受外部变量.

pred here is an array of predicted values rather than an object containing predicted mean values and confidence intervals that you would get if you ran get_predict(). Note, get_predict() does not take exogenous variables.

我的statsmodels版本是0.8

My version of statsmodels is 0.8

推荐答案

存在一些与向后兼容性相关的问题,原因是未公开完整结果(带有间隔等).

There has been some backward compatibility related issues due to which full results (with pred intervals etc) are not being exposed.

要获得您现在想要的东西:使用具有以下所述参数的get_prediction和get_forecast函数

To get you what you want now: Use get_prediction and get_forecast functions with parameters described below

    pred_res = sarimax_model.get_prediction(exog=ExogenousFeature_train.values.astype('float64'), full_results=True,alpha=0.05)
    pred_means = pred_res.predicted_mean
    # Specify your prediction intervals by alpha parameter. alpha=0.05 implies 95% CI
    pred_cis = pred_res.conf_int(alpha=0.05)

    # You can then plot it (import matplotlib first)
    fig = plt.figure(figsize=(12, 8))
    ax = fig.add_subplot(1,1,1)
    #Actual data
    ax.plot(y_train.astype('float64'), '--', color="blue", label='data')
    # Means
    ax.plot(pred_means, lw=1, color="black", alpha=0.5, label='SARIMAX')
    ax.fill_between(pred_means.index, pred_cis.iloc[:, 0], pred_cis.iloc[:, 1], alpha=0.05)
    ax.legend(loc='upper right')
    plt.draw()

有关更多信息,请访问:

For more info, go to:

  • https://github.com/statsmodels/statsmodels/issues/2823
  • Solution by the author: http://www.statsmodels.org/dev/examples/notebooks/generated/statespace_local_linear_trend.html

这篇关于Python Statsmodels:将SARIMAX与外生回归变量一起使用以获取预测的均值和置信区间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 07:40