在python的滚动窗口预测

时间:2015-05-08 16:48:23

标签: python regression forecasting

几天前我问this问题,但没有得到任何回复。所以我自己动手做了滚动窗口。我对回归预测的有限把握使我的进步有点困难。

但我想要做的是根据截至时间t-255(期间开始)的可用信息预测未来n天,然后根据来自的信息进行另一个提前n天的预测。 t - 255 + n(直到期末),例如如果n = 5,则窗口应产生255到250的预测,下一个窗口应产生250到245的预测,依此类推。

from harrv import mdl, df
import scipy as sp
import pandas as pd
import numpy as np

b0, b1, b2, b3 = mdl.params[0], mdl.params[1], mdl.params[2], mdl.params[3]
rv1, rv5, rv22, mu = df.RV1, df.RV5, df.RV22, b0
walk = sp.stats.invgauss.rvs(mu, size = len(rv1))
pre, p = [], []

def window(seq, n):
    """
    seq -> the sequence over which prediction should be performed

    a rolling window function to calculate n-step ahead prediction"""
    for i in reversed(xrange(0, len(seq))):
        for j in range(1, n):
            pre = b0 + rv1[i] * b1**j + rv5[i] * b2**j + rv22[i] * b3**j + walk[i]           
            p.append(pre)
            i = i + n

    return p

其中mdl是我的HAC-OLS估计的具有参数参数的HAR-RV模型,我不希望随机噪声产生太大的影响,因此我将其MU设置为截距,这是非常小的。 我不确定我现在是否正在做,而且更紧迫,我不能选择n超过2的值,因为当我这样做时,这表明:

predi = window(rv1, 4)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-480-bbee880e0682> in <module>()
----> 1 predi = window(rv1, 4)

/Users/NiklasLindeke/Python/window_analysis.py in window(seq, n)
     17     for i in reversed(xrange(0, len(seq))):
     18         for j in range(1, n):
---> 19             pre = b0 + rv1[i] * b1**j + rv5[i] * b2**j + rv22[i] * b3**j + walk[i]
     20             p.append(pre)
     21             i = i + n

/Users/NiklasLindeke/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/core/series.pyc in __getitem__(self, key)
    512     def __getitem__(self, key):
    513         try:
--> 514             result = self.index.get_value(self, key)
    515 
    516             if not np.isscalar(result):

/Users/NiklasLindeke/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/core/index.pyc in get_value(self, series, key)
   1458 
   1459         try:
-> 1460             return self._engine.get_value(s, k)
   1461         except KeyError as e1:
   1462             if len(self) > 0 and self.inferred_type in ['integer','boolean']:

/Users/NiklasLindeke/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/index.so in pandas.index.IndexEngine.get_value (pandas/index.c:3113)()

/Users/NiklasLindeke/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/index.so in pandas.index.IndexEngine.get_value (pandas/index.c:2844)()

/Users/NiklasLindeke/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/index.so in pandas.index.IndexEngine.get_loc (pandas/index.c:3704)()

/Users/NiklasLindeke/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/hashtable.so in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:7255)()

/Users/NiklasLindeke/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/hashtable.so in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:7193)()

KeyError: 259

然而,当我在第一个for循环的结尾处有i = i + n时,错误消失了,但是当我这样做时,p中还有更多的附加内容。

我现在虽然没有关于如何做到这一点的想法。

1 个答案:

答案 0 :(得分:0)

也许是这样的?

{{1}}