title: How to calculate slope in numpy
body: <p>If I have an array of 50 elements,  how would I calculate a 3 period slope and a 5 period slope? 
  The docs dont add much.....</p>
  
  <pre><code>&gt;&gt;&gt; from scipy import stats
  &gt;&gt;&gt; import numpy as np
  &gt;&gt;&gt; x = np.random.random(10)
  &gt;&gt;&gt; y = np.random.random(10)
  &gt;&gt;&gt; slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
  </code></pre>
  
  <p>Would this work?</p>
  
  <pre><code>def slope(x, n): 
     if i&lt;len(x)-n: 
          slope = stats.linregress(x[i:i+n],y[i:i+n])[0]
          return slope 
  </code></pre>
  
  <p>but the would the arrays be the same length</p>
  
  <p>@joe:::</p>
  
  <pre><code>xx = [2.0 ,4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]
  x  = np.asarray(xx, np.float)
  s  = np.diff(x[::3])/3
  
  window  = [1, 0, 0, 0,  -1]
  window2 = [1, 0,  -1]
  slope   = np.convolve(x, window, mode='same') / (len(window) - 1)
  slope2  = np.convolve(x, window2, mode='same') / (len(window2) - 1)
  
  print x
  print s
  
  print slope
  print slope2
  </code></pre>
  
  <p>Results.....</p>
  
  <pre><code>[  2.   4.   6.   8.  10.  12.  14.  16.  18.  20.  22.  24.  26.  28.  30.]
  [ 2.  2.  2.  2.]
  [ 1.5  2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.  -6.  -6.5]
  [  2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2. -14.]
  </code></pre>
  
  <p>The slope and slope2 are what Im after except the  -6, -6.5 and -14 arent the results I am  looking for.  </p>
  
  <p>this worked.......</p>
  
  <pre><code>window    = [1, 0, 0, -1]
  slope     = np.convolve(xx, window, mode='valid') / float(len(window) - 1)
  padlength = len(window) -1
  slope     = np.hstack([np.ones(padlength), slope])
  print slope
  </code></pre>