Tro*_*y D 5 python statsmodels autoregressive-models
我正在尝试测试ARMA模型,并通过此处提供的示例进行工作:
http://www.statsmodels.org/dev/examples/notebooks/generated/tsa_arma_0.html
我无法告诉您是否存在直接的方法来在训练数据集上训练模型然后在测试数据集上对其进行测试。在我看来,您必须使模型适合整个数据集。然后,您可以进行样本内预测,该预测使用与训练模型时相同的数据集。或者,您可以进行样本外预测,但这必须从训练数据集的末尾开始。相反,我想做的是将模型拟合到训练数据集上,然后在不属于训练数据集的完全不同的数据集上运行模型,并获得一系列提前1步的预测。
为了说明这个问题,这里是上面链接的缩写代码。您会看到模型拟合了1700-2008年的数据,然后预测了1990-2012年。我的问题是1990-2008年已经是用于拟合模型的数据的一部分,所以我认为我正在预测和训练相同的数据。我希望能够获得一系列没有前瞻性偏差的第一步预测。
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm
dta = sm.datasets.sunspots.load_pandas().data
dta.index = pandas.Index(sm.tsa.datetools.dates_from_range('1700', '2008'))
dta = dta.drop('YEAR',1)
arma_mod30 = sm.tsa.ARMA(dta, (3, 0)).fit(disp=False)
predict_sunspots = arma_mod30.predict('1990', '2012', dynamic=True)
fig, ax = plt.subplots(figsize=(12, 8))
ax = dta.ix['1950':].plot(ax=ax)
fig = arma_mod30.plot_predict('1990', '2012', dynamic=True, ax=ax, plot_insample=False)
plt.show()
Run Code Online (Sandbox Code Playgroud)
自从我提出这个问题以来的 16 个月里,我在 statsmodels 中了解了更多关于 ARIMA 建模的知识,我认为 ARMA 或 ARIMA 模型不支持我正在寻找的行为,但它在SARIMAX 模型。请参阅下面的代码,基于 statsmodels.org 的示例。绿线表示从 1700 年到 1990 年训练的 ARIMA(10,0,0) 模型(或 AR(10))模型,然后从 1990 年到 2012 年进行动态预测。
https://www.statsmodels.org/dev/examples/notebooks/ generated/statespace_sarimax_stata.html
import pandas
import matplotlib.pyplot as plt
import statsmodels.api as sm
dta = sm.datasets.sunspots.load_pandas().data
dta.index = pandas.Index(sm.tsa.datetools.dates_from_range('1700', '2008'))
dta = dta.drop('YEAR', 1)
arma_mod30 = sm.tsa.ARMA(dta, (3, 0)).fit(disp=False)
predict_sunspots = arma_mod30.predict('1990', '2012', dynamic=True)
fig, ax = plt.subplots(figsize=(12, 8))
ax = dta.ix['1950':].plot(ax=ax)
fig = arma_mod30.plot_predict('1990', '2012', dynamic=True, ax=ax, plot_insample=False)
# Fit the model
mod = sm.tsa.statespace.SARIMAX(dta.loc[:'1990'], order=(10, 0, 0))
fit_res = mod.fit(disp=False)
# Create new model, but instead of fit, copy the params from the first model
mod = sm.tsa.statespace.SARIMAX(dta, order=(10, 0, 0))
res = mod.filter(fit_res.params)
# Dynamic predictions
predict_dy = res.get_prediction(dynamic='1990', end='2012')
predict_dy = predict_dy.predicted_mean
predict_dy['1990':].plot(ax=ax)
plt.show()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
776 次 |
| 最近记录: |