title: NumPy version of "Exponential weighted moving average", equivalent to pandas.ewm().mean()
body: <p>How do I get the exponential weighted moving average in NumPy just like the following in <a href="http://pandas.pydata.org/pandas-docs/stable/computation.html#exponentially-weighted-windows" rel="noreferrer">pandas</a>?</p>
  
  <pre><code>import pandas as pd
  import pandas_datareader as pdr
  from datetime import datetime
  
  # Declare variables
  ibm = pdr.get_data_yahoo(symbols='IBM', start=datetime(2000, 1, 1), end=datetime(2012, 1, 1)).reset_index(drop=True)['Adj Close']
  windowSize = 20
  
  # Get PANDAS exponential weighted moving average
  ewm_pd = pd.DataFrame(ibm).ewm(span=windowSize, min_periods=windowSize).mean().as_matrix()
  
  print(ewm_pd)
  </code></pre>
  
  <p>I tried the following with NumPy</p>
  
  <pre><code>import numpy as np
  import pandas_datareader as pdr
  from datetime import datetime
  
  # From this post: http://stackoverflow.com/a/40085052/3293881 by @Divakar
  def strided_app(a, L, S): # Window len = L, Stride len/stepsize = S
      nrows = ((a.size - L) // S) + 1
      n = a.strides[0]
      return np.lib.stride_tricks.as_strided(a, shape=(nrows, L), strides=(S * n, n))
  
  def numpyEWMA(price, windowSize):
      weights = np.exp(np.linspace(-1., 0., windowSize))
      weights /= weights.sum()
  
      a2D = strided_app(price, windowSize, 1)
  
      returnArray = np.empty((price.shape[0]))
      returnArray.fill(np.nan)
      for index in (range(a2D.shape[0])):
          returnArray[index + windowSize-1] = np.convolve(weights, a2D[index])[windowSize - 1:-windowSize + 1]
      return np.reshape(returnArray, (-1, 1))
  
  # Declare variables
  ibm = pdr.get_data_yahoo(symbols='IBM', start=datetime(2000, 1, 1), end=datetime(2012, 1, 1)).reset_index(drop=True)['Adj Close']
  windowSize = 20
  
  # Get NumPy exponential weighted moving average
  ewma_np = numpyEWMA(ibm, windowSize)
  
  print(ewma_np)
  </code></pre>
  
  <p>But the results are not similar as the ones in pandas.</p>
  
  <p>Is there maybe a better approach to calculate the exponential weighted moving average directly in NumPy and get the exact same result as the <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.ewm.html#pandas.DataFrame.ewm" rel="noreferrer"><code>pandas.ewm().mean()</code></a>?</p>
  
  <p>At 60,000 requests on pandas solution, I get about 230 seconds. I am sure that with a pure NumPy, this can be decreased significantly.</p>