calculate exponential moving average in python

后端 未结 14 2345
囚心锁ツ
囚心锁ツ 2020-12-01 00:59

I have a range of dates and a measurement on each of those dates. I\'d like to calculate an exponential moving average for each of the dates. Does anybody know how to do t

相关标签:
14条回答
  • 2020-12-01 01:54

    A fast way (copy-pasted from here) is the following:

    def ExpMovingAverage(values, window):
        """ Numpy implementation of EMA
        """
        weights = np.exp(np.linspace(-1., 0., window))
        weights /= weights.sum()
        a =  np.convolve(values, weights, mode='full')[:len(values)]
        a[:window] = a[window]
        return a
    
    0 讨论(0)
  • 2020-12-01 01:54

    May be shortest:

    #Specify decay in terms of span
    #data_series should be a DataFrame
    
    ema=data_series.ewm(span=5, adjust=False).mean()
    
    
    0 讨论(0)
提交回复
热议问题