title: Rolling window REVISITED - Adding window rolling quantity as a parameter- Walk Forward Analysis
body: <p>I have been searching the web for methods that could create <strong>rolling windows</strong> so that I can perform a cross-validation technique known as Walk Forward Analysis for time series in a generalized manner. </p>
  
  <p>However, I have not get around to any solution that incorporates flexibility in terms of 1) the window size (almost all methods have this; for example, <code>pandas</code> <a href="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rolling.html" rel="nofollow noreferrer">rolling</a> or a bit different <a href="https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.roll.html" rel="nofollow noreferrer">np.roll</a>) and 2) window rolling quantity, understood as how many indexes do we want to roll the window (i.e. haven't found any that incorporates this).</p>
  
  <p>I have been trying to optimize and make concise code, with the help of <strong>@coldspeed</strong> in <a href="https://stackoverflow.com/questions/45645511/rolling-window-in-python">this answer</a> (I'm unable to comment there because I don't reach the needed reputation; hope to get there soon!), but I haven't been able to incorporate the window rolling quantity. </p>
  
  <p>My thinkings: </p>
  
  <ol>
  <li><p>I have tried with <code>np.roll</code> together with my below example, with no success.</p></li>
  <li><p>I have also tried to modify the code below multiplying the <code>ith</code> value, but I don't get to fit it within the list comprehension, which I would like to maintain. </p></li>
  </ol>
  
  <p><strong>3. The example below does great for any window size, BUT, it only "rolls" the window one step ahead and I would like that it could be generalized to any step.</strong> </p>
  
  <p>So, <strong>¿is there any way to have this two parameters available within the list comprehension approach? or, ¿is there any other resource which I did not find that makes this easier?</strong> All the help is very much appreciated. My example code is the following: </p>
  
  <pre><code>In [1]: import numpy as np
  In [2]: arr = np.random.random((10,3))
  
  In [3]: arr
  
  Out[3]: array([[0.38020065, 0.22656515, 0.25926935],
     [0.13446667, 0.04386083, 0.47210474],
     [0.4374763 , 0.20024762, 0.50494097],
     [0.49770835, 0.16381492, 0.6410294 ],
     [0.9711233 , 0.2004874 , 0.71186102],
     [0.61729025, 0.72601898, 0.18970222],
     [0.99308981, 0.80017134, 0.64955358],
     [0.46632326, 0.37341677, 0.49950571],
     [0.45753235, 0.55642914, 0.31972887],
     [0.4371343 , 0.08905587, 0.74511753]])
  
  In [4]: inSamplePercentage = 0.4
  In [5]: outSamplePercentage = 0.3 * inSamplePercentage
  
  In [6]: windowSizeTrain = round(inSamplePercentage * arr.shape[0])
  In [7]: windowSizeTest = round(outSamplePercentage * arr.shape[0])
  In [8]: windowTrPlusTs = windowSizeTrain + windowSizeTest
  
  In [9]: sliceListX = [arr[i: i + windowTrPlusTs] for i in range(len(arr) - (windowTrPlusTs-1))]
  </code></pre>
  
  <p>Given a window length of 5 and a window roll qty of 2, I could spec something like this: </p>
  
  <pre><code>Out [15]: 
  
  [array([[0.38020065, 0.22656515, 0.25926935],
      [0.13446667, 0.04386083, 0.47210474],
      [0.4374763 , 0.20024762, 0.50494097],
      [0.49770835, 0.16381492, 0.6410294 ],
      [0.9711233 , 0.2004874 , 0.71186102]]),
   array([[0.4374763 , 0.20024762, 0.50494097],
      [0.49770835, 0.16381492, 0.6410294 ],
      [0.9711233 , 0.2004874 , 0.71186102],
      [0.61729025, 0.72601898, 0.18970222],
      [0.99308981, 0.80017134, 0.64955358]]),
   array([[0.9711233 , 0.2004874 , 0.71186102],
      [0.61729025, 0.72601898, 0.18970222],
      [0.99308981, 0.80017134, 0.64955358],
      [0.46632326, 0.37341677, 0.49950571],
      [0.45753235, 0.55642914, 0.31972887]]),
   array([[0.99308981, 0.80017134, 0.64955358],
     [0.46632326, 0.37341677, 0.49950571],
     [0.45753235, 0.55642914, 0.31972887],
     [0.4371343 , 0.08905587, 0.74511753]])]
  </code></pre>
  
  <p>(This incorporates the last array, although its lenght is less than 5).</p>
  
  <p>OR: </p>
  
  <pre><code>Out [16]: 
  
  [array([[0.38020065, 0.22656515, 0.25926935],
      [0.13446667, 0.04386083, 0.47210474],
      [0.4374763 , 0.20024762, 0.50494097],
      [0.49770835, 0.16381492, 0.6410294 ],
      [0.9711233 , 0.2004874 , 0.71186102]]),
   array([[0.4374763 , 0.20024762, 0.50494097],
      [0.49770835, 0.16381492, 0.6410294 ],
      [0.9711233 , 0.2004874 , 0.71186102],
      [0.61729025, 0.72601898, 0.18970222],
      [0.99308981, 0.80017134, 0.64955358]]),
   array([[0.9711233 , 0.2004874 , 0.71186102],
      [0.61729025, 0.72601898, 0.18970222],
      [0.99308981, 0.80017134, 0.64955358],
      [0.46632326, 0.37341677, 0.49950571],
      [0.45753235, 0.55642914, 0.31972887]])]
  </code></pre>
  
  <p>(Only the arrays with lenght == 5 -> However, this could be derived from the one above with a simple mask).</p>
  
  <p>EDIT: Forgot to mention <a href="https://github.com/pandas-dev/pandas/issues/11704" rel="nofollow noreferrer">this also</a> -- Something could be done if pandas rolling objects support <strong>iter</strong> metho. </p>