【发布时间】:2014-09-22 12:40:49
【问题描述】:
我试图预测包含外生变量的 python statsmodels ARIMA 包中的时间序列,但无法找出在预测步骤中插入外生变量的正确方法。有关文档,请参阅 here。
import numpy as np
from scipy import stats
import pandas as pd
import statsmodels.api as sm
vals = np.random.rand(13)
ts = pd.TimeSeries(vals)
df = pd.DataFrame(ts, columns=["test"])
df.index = pd.Index(pd.date_range("2011/01/01", periods = len(vals), freq = 'Q'))
fit1 = sm.tsa.ARIMA(df, (1,0,0)).fit()
#this works fine:
pred1 = fit1.predict(start=12, end = 16)
print(pred1)
Out[32]:
2014-03-31 0.589121
2014-06-30 0.747575
2014-09-30 0.631322
2014-12-31 0.654858
2015-03-31 0.650093
Freq: Q-DEC, dtype: float64
现在添加一个趋势外生变量
exogx = np.array(range(1,14))
#to make this easy, let's look at the ols of the trend (arima(0,0,0))
fit2 = sm.tsa.ARIMA(df, (0,0,0),exog = exogx).fit()
print(fit2.params)
const 0.555226
x1 0.013132
dtype: float64
print(fit2.fittedvalues)
2011-03-31 0.568358
2011-06-30 0.581490
2011-09-30 0.594622
2011-12-31 0.607754
2012-03-31 0.620886
2012-06-30 0.634018
2012-09-30 0.647150
2012-12-31 0.660282
2013-03-31 0.673414
2013-06-30 0.686546
2013-09-30 0.699678
2013-12-31 0.712810
2014-03-31 0.725942
Freq: Q-DEC, dtype: float64
注意,正如我们所料,这是一条趋势线,随着时间的推移每增加一个刻度,增加 0.013132(当然这是随机数据,所以如果你运行它,值会有所不同,但正面或负面的趋势故事将是相同的)。因此,下一个值(时间 = 14)应该是 0.555226 + 0.013132*14 = 0.739074。
#out of sample exog should be (14,15,16)
pred2 = fit2.predict(start = 12, end = 16, exog = np.array(range(13,17)))
print(pred2)
2014-03-31 0.725942
2014-06-30 0.568358
2014-09-30 0.581490
2014-12-31 0.594622
2015-03-31 0.765338
Freq: Q-DEC, dtype: float64
所以,2014-03-31 正确预测(最后一个 insample),但 2014-06-30 从头开始(t = 1),但请注意 2015-03-31(实际上,总是最后一次观察预测,无论水平如何)都取 t = 16(即(值 - 截距)/beta = (0.765338 - 0.555226)/0.013132)。
为了更清楚地说明这一点,请注意当我膨胀 x mat 的值时会发生什么
fit2.predict(start = 12, end = 16, exog = np.array(range(13,17))*10000)
Out[41]:
2014-03-31 0.725942
2014-06-30 0.568358
2014-09-30 0.581490
2014-12-31 0.594622
2015-03-31 2101.680532
Freq: Q-DEC, dtype: float64
看到 2015-03-31 爆炸了,但没有考虑其他 xmat 值?我在这里做错了什么???
我已经尝试过所有我知道如何传递 exog 变量的方法(改变维度,使 exog 成为矩阵,使 exog 只要输入加上地平线,等等,等等)。任何建议将不胜感激。
我正在使用来自 Anaconda2.1 的 2.7 numpy 1.8.1 scipy 0.14.0 熊猫 0.14.0 statsmodels 0.5.0
并已在 windows 7 64 位和 centos 64 位上验证了该问题。
还有一些事情。我将 ARIMA 用于 ARIMA 功能,以上只是为了说明(也就是说,我不能“只使用 OLS ...”,正如我想象的那样)。由于项目的限制(更一般地说,基础 Spark 中缺乏对 R 的支持),我也不能“只使用 R”。
这里是代码中有趣的部分,如果你想自己尝试一下
import numpy as np
from scipy import stats
import pandas as pd
import statsmodels.api as sm
vals = np.random.rand(13)
ts = pd.TimeSeries(vals)
df = pd.DataFrame(ts, columns=["test"])
df.index = pd.Index(pd.date_range("2011/01/01", periods = len(vals), freq = 'Q'))
exogx = np.array(range(1,14))
fit2 = sm.tsa.ARIMA(df, (0,0,0),exog = exogx).fit()
print(fit2.fittedvalues)
pred2 = fit2.predict(start = 12, end = 16, exog = np.array(range(13,17))*10000)
print(pred2)
【问题讨论】:
-
请注意,这些问题在以下文章中有所提及(但未直接讨论):github.com/statsmodels/statsmodels/issues/1076stackoverflow.com/questions/18721547/…
标签: python numpy statsmodels predict