calculate exponential moving average in python

后端 未结 14 2342
囚心锁ツ
囚心锁ツ 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:38

    You can also use the SciPy filter method because the EMA is an IIR filter. This will have the benefit of being approximately 64 times faster as measured on my system using timeit on large data sets when compared to the enumerate() approach.

    import numpy as np
    from scipy.signal import lfilter
    
    x = np.random.normal(size=1234)
    alpha = .1 # smoothing coefficient
    zi = [x[0]] # seed the filter state with first value
    # filter can process blocks of continuous data if  is maintained
    y, zi = lfilter([1.-alpha], [1., -alpha], x, zi=zi)
    

提交回复
热议问题