python中的滚动窗口预测
几天前我问了这个问题,但没有得到任何回应。所以我决定自己手动做滚动窗口。我对回归预测的掌握有限,这有点阻碍了我的进步 但我想做的是根据时间t-255(周期开始)之前的可用信息预测n天,然后根据时间t-255+n(周期结束)的信息进行另一个n天的预测,例如,如果n=5,窗口应产生255到250的预测,下一个窗口是250到245,以此类推python中的滚动窗口预测,python,regression,forecasting,Python,Regression,Forecasting,几天前我问了这个问题,但没有得到任何回应。所以我决定自己手动做滚动窗口。我对回归预测的掌握有限,这有点阻碍了我的进步 但我想做的是根据时间t-255(周期开始)之前的可用信息预测n天,然后根据时间t-255+n(周期结束)的信息进行另一个n天的预测,例如,如果n=5,窗口应产生255到250的预测,下一个窗口是250到245,以此类推 from harrv import mdl, df import scipy as sp import pandas as pd import numpy as
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模型,参数为params,我不希望随机噪声有太大的影响,所以我将其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
predi=窗口(rv1,4)
---------------------------------------------------------------------------
KeyError回溯(最近一次呼叫最后一次)
在()
---->1 predi=窗口(rv1,4)
/窗口中的Users/NiklasLindeke/Python/window_analysis.py(seq,n)
17对于反向中的i(X范围(0,长度(顺序)):
对于范围(1,n)内的j为18:
--->19 pre=b0+rv1[i]*b1**j+rv5[i]*b2**j+rv22[i]*b3**j+walk[i]
下午20:00(预科)
21 i=i+n
/Users/NiklasLindeke/Library/enthught/Canopy_64bit/User/lib/python2.7/site-packages/pandas/core/series.pyc in uuuuuu getitem(self,key)
512 def_uuugetItem_uuuuu(自身,密钥):
513尝试:
-->514结果=self.index.get_值(self,key)
515
516如果不是np.isscalar(结果):
/Users/NiklasLindeke/Library/enthught/Canopy_64bit/User/lib/python2.7/site-packages/pandas/core/index.pyc in get_value(self、series、key)
1458
1459尝试:
->1460返回自引擎。获取值(s,k)
1461除键错误为e1外:
1462如果len(self)>0且self.u键入['integer','boolean']:
/Users/NiklasLindeke/Library/enthund/Canopy_64bit/User/lib/python2.7/site-packages/pandas/index.so in pandas.index.IndexEngine.get_value(pandas/index.c:3113)()
/用户/NiklasLindeke/Library/enthund/Canopy_64bit/User/lib/python2.7/site-packages/pandas/index.so in pandas.index.IndexEngine.get_value(pandas/index.c:2844)()
/在pandas.index.IndexEngine.get_loc(pandas/index.c:3704)()中的Users/NiklasLindeke/Library/enthund/Canopy_64bit/User/lib/python2.7/site-packages/pandas/index.so
/在pandas.hashtable.Int64HashTable.get_项(pandas/hashtable.c:7255)()中的Users/NiklasLindeke/Library/enthught/Canopy_64bit/User/lib/python2.7/site-packages/pandas/hashtable.so
/在pandas.hashtable.Int64HashTable.get_项(pandas/hashtable.c:7193)()中的Users/NiklasLindeke/Library/enthught/candoy_64bit/User/lib/python2.7/site-packages/pandas/hashtable.so
关键字错误:259
然而,当我在第一个for循环的末尾使用I=I+n时,错误消失了,但是当我这样做时,在p中附加了更多
我现在想不出怎么做了。也许是这样的
for i in reversed(xrange(n, len(seq), n)):
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 - 1
你到底想在这里干什么?如果你想计算每一天、每一周的预测值,不管怎样,你都应该在外循环中将I增加1(这就是你试图做的)。当然,当你每天计算未来的所有预测时,你会得到很多P。现在,当I+N位于内部循环中时,您将指向其中一个变量RVx或walk的大小。请参阅“编辑”,并对我的目标进行更详细的描述。是否尝试创建一个带有“pre=…”行的数组?我不知道您拥有的数据类型,但在我看来,您似乎创建了一个整数。这看起来很奇怪,因为您从definine pre=[]开始,但基本上以后会重新定义它。此外,您的索引已关闭。您的内循环向I添加了不同的值,这些值在外循环中持续存在,这可能会导致索引出错(过高)。那么b0第一个预测、rv1[I]*b1**j第二个预测也是如此吗?我不熟悉您提到的HAC xx方法,但对我来说,在所有预测中使用相同的I值(为什么不是I+j?)似乎有些奇怪。第一个预测是方程式加上一个噪声项,因为j从1开始。我不能让它假设任何附加信息。当它开始以i的某个级别进行预测时,它需要根据到时间i为止的可用信息提前生成n个预测,并沿着序列移动,直到下一个适当的步进点,即i+n。您可能还希望将范围设置为(1,n+1)。否则,如果N=5,则会得到值1-4、6-9等等。外部循环可能有相同的问题,具体取决于您的情况。它无法处理n>2的问题仍然存在,并且出于某种原因,它只返回128行数据。但是,当我在第一个循环中取出n时,它返回的是适当数量的行。您希望有多少行?255/5等于51。所以我想我还是不太理解你。如果索引太大,是否仍会出现相同的错误?不,我预计会有255行,因为“I”将数据集从模型进行预测的位置移动,而n天之前的数据集将在其中附加p,并一直移动到255行。但是循环中的第三个参数是skip,当它已经有了i-1时,它不应该像那样跳过。如果我们取seq=255和n=5,第一行将创建一个列表[250245240235,…]。这意味着结果列表将包含51个值,其中第一个值是i=250249248247246的数组。内部i-1与外部环路内的249248247246一样。你想要不同的吗?