简体   繁体   English

Python statsmodels ARIMA预测

[英]Python statsmodels ARIMA Forecast

I am trying to do out of sample forecasting using python statsmodels. 我正在尝试使用python statsmodels进行样本预测。 I do not want to just forecast the next x number of values from the end of the training set but I want to forecast one value at a time and take in consideration the actual values when forecasting. 我不想仅仅预测训练集末尾的下一个x值,但我想一次预测一个值,并在预测时考虑实际值。 In other words I want to do rolling 1-period forecasts, but I don't want to recalibrate the model every time. 换句话说,我想做滚动1期预测,但我不想每次都重新校准模型。 The closest post I could find was here: 我能找到的最近的帖子是:

ARMA out-of-sample prediction with statsmodels 使用statsmodels进行ARMA样本外预测

However, this uses ARMA not ARIMA. 但是,这使用ARMA而不是ARIMA。 How can I achieve this with ARIMA or is there a better method? 如何使用ARIMA实现这一目标还是有更好的方法? I know I could actually pull the coefficients and apply a function myself but in my code the ARIMA model I am using is dynamic over time, therefore the number of coefficients and lagged values used is not constant. 我知道我实际上可以拉动系数并自己应用函数但是在我的代码中我使用的ARIMA模型随着时间的推移是动态的,因此系数和滞后值的使用数量不是恒定的。 Any help would be greatly appreciated. 任何帮助将不胜感激。

If I am right, I had the very similar problem: basically I wanted to split my time series into training and test set, train the model, and then predict arbitrarily any element of the test set given its past history. 如果我是对的,我有类似的问题:基本上我想将我的时间序列分成训练和测试集,训练模型,然后根据过去的历史任意预测测试集的任何元素。 I did not manage to achieve it using the ARIMA statsmodels class though. 我没有设法使用ARIMA statsmodels类实现它。

That's how I did it using statsmodels: I've applied a first order difference to the series to achieve stationarity, and computed an arma model: 这就是我使用statsmodel做的方式:我已经将一阶差异应用于系列以实现平稳性,并计算了一个arma模型:

model = sm.tsa.ARMA(fitting_data, order=(p, q), dates=fitting_dates).fit()

I've converted the arma model into a pure-ar one: 我已经将arma模型转换为纯粹的ar模型:

ar_params = model.arparams
ma_params = model.maparams

ar_coefficients = arma2ar(ar_params, ma_params, nobs=final_ar_coeff)

The nobs parameters influences the number of auto-regressive coefficients you will get. nobs参数会影响您将获得的自回归系数的数量。 I tried several values, increasing it until no significant change in the predictions was observed. 我尝试了几个值,增加它直到观察到预测没有显着变化。 Once you get your predictions wrt the differenced series, you want to bring back them to the original one. 一旦你得到了不同系列的预测,你想把它们带回原来的系列。 I implemented a method which, given one or a chain of predictions and the last known element before your forecasts, computes the predictions in the original series: 我实现了一种方法,在预测之前给出一个或一组预测和最后一个已知元素,计算原始系列中的预测:

def differenced_series_to_original(values, starting_value):

    original_series = [starting_value]
    [original_series.append(original_series[-1]+i) for i in values]

    return original_series[1:]

Obviously values is the list of your predictions, starting_value the last known element. 显然, 是您的预测列表, starting_value是最后一个已知元素。 Hope it helps with your problem. 希望它有助于解决您的问题。

From what i can understand is that you dont want to run the model everytime, there can be two solutions to this problem 从我可以理解的是,你不想每次运行模型,这个问题可以有两个解决方案

  1. Extract model in pickle format and then use the same model everytime to create forecasts. 以pickle格式提取模型,然后每次使用相同的模型来创建预测。
  2. Extract the coefficients from the model and use it for your calculations. 从模型中提取系数并将其用于计算。

Code for both the options are below. 两个选项的代码如下。

  1. Pickle creation and using it further. 泡菜创造并进一步使用它。

     import pmdarima as pm model = pm.auto_arima(train, exogenous=exogenous_train, start_p=1, start_q=1, test='adf', # use adftest to find optimal 'd' max_p=5, max_q=5, # maximum p and q m=12, # frequency of series d=None, # let model determine 'd' seasonal=True, # No Seasonality start_P=0, D=1, trace=True, error_action='ignore', suppress_warnings=True, stepwise=True) filename = 'ARIMA_Model.sav' pickle.dump(model, open(filename, 'wb')) ## This will create a pickle file ## Load Model model = pickle.load(open(filename, 'rb')) ## Forecast fc, confint = model.predict(n_periods=1, exogenous=exogenous_test_df, return_conf_int=True) 
  2. Extract the model coefficients, I have used pmdarima for ARIMA so this is how you can extract the coefficients. 提取模型系数,我已经使用了pmdarima用于ARIMA,所以这就是你如何提取系数。 I guess it should be same in other ARIMA libraries. 我猜它在其他ARIMA库中应该是相同的。

     Model_dict = model.to_dict() Model_Order = Model_dict['order'] Model_seasonal_order = Model_dict['seasonal_order'][1] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM