title: Improving runtime of weighted moving average filter function?
body: <p>I have a weighted moving average function which smooths a curve by averaging 3*width values to the left and to the right of each point using a gaussian weighting mechanism. I am only worried about smoothing a region bounded by [start, end]. The following code works, but the problem is runtime with large arrays.</p>
  
  <pre><code>import numpy as np
  def weighted_moving_average(x, y, start, end, width = 3):
      def gaussian(x, a, m, s):
          return a*exp(-(x-m)**2/(2*s**2))
      cut = (x&gt;=start-3*width)*(x&lt;=end+3*width)
      x, y = x[cut], y[cut]
      x_avg = x[(x&gt;=start)*(x&lt;=end)]
      y_avg = np.zeros(len(x_avg))
      bin_vals = np.arange(-3*width,3*width+1)
      weights = gaussian(bin_vals, 1, 0, width)
      for i in range(len(x_avg)):
          y_vals = y[i:i+6*width+1]
          y_avg[i] = np.average(y_vals, weights = weights)
      return x_avg, y_avg
  </code></pre>
  
  <p>From my understanding, it is generally inefficient to loop through a NumPy array. I was wondering if anyone had an idea to replace the for loop with something more runtime efficient.</p>
  
  <p>Thanks</p>