title: Rolling window forecast in python
body: <p>i asked <a href="https://stackoverflow.com/questions/30055520/moving-window-forecasting-with-python">this</a> question some days ago but haven't got any response. So I've taken it to myself to do the rolling window manually. My limited grasp on regression forecasting has stumped my progress a bit. </p>
  
  <p>But what I am trying to do is a forecast of n-days ahead based on the information available up to time t-255 (start of the period), and then do another n-day ahead forecasts based on the information from t - 255 + n (until the end of the period), e.g. if n = 5, the window should produce forecasts from 255 to 250, and next window at 250 to 245 and so on. </p>
  
  <pre><code>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 -&gt; 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
  </code></pre>
  
  <p>Where mdl is my HAC-OLS estimated HAR-RV model with the parameters params, I didnt want the random noise to have too much effect, so I set its MU to be the intercept, which is quite small. 
  Im not sure if im doing it right now either, and more pressing, I cant choose a value of n more than 2, cause when I do, this shows:</p>
  
  <pre><code>predi = window(rv1, 4)
  ---------------------------------------------------------------------------
  KeyError                                  Traceback (most recent call last)
  &lt;ipython-input-480-bbee880e0682&gt; in &lt;module&gt;()
  ----&gt; 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):
  ---&gt; 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:
  --&gt; 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:
  -&gt; 1460             return self._engine.get_value(s, k)
     1461         except KeyError as e1:
     1462             if len(self) &gt; 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
  </code></pre>
  
  <p>The error however, dissapeared when I had the i = i + n in the end of the first for loop, but when I did that, there was alot more appended in p.</p>
  
  <p>I am now though running out of ideas on how to do this.</p>