1
我正在浏览本网站以了解有关指数平滑平均值的更多信息,但不确定关于代码的1部分。指数平滑平均值
import pandas, numpy as np
ewma = pandas.stats.moments.ewma
# make a hat function, and add noise
x = np.linspace(0,1,100)
x = np.hstack((x,x[::-1]))
x += np.random.normal(loc=0, scale=0.1, size=200)
plot(x, alpha=0.4, label='Raw')
# take EWMA in both directions with a smaller span term
fwd = ewma(x, span=15) # take EWMA in fwd direction
bwd = ewma(x[::-1], span=15) # take EWMA in bwd direction
c = np.vstack((fwd, bwd[::-1])) # lump fwd and bwd together
c = np.mean(c, axis=0) # average
# regular EWMA, with bias against trend
plot(ewma(x, span=20), 'b', label='EWMA, span=20')
# "corrected" (?) EWMA
plot(c, 'r', label='Reversed-Recombined')
我不明白的是本节
# take EWMA in both directions with a smaller span term
fwd = ewma(x, span=15) # take EWMA in fwd direction
bwd = ewma(x[::-1], span=15) # take EWMA in bwd direction
c = np.vstack((fwd, bwd[::-1])) # lump fwd and bwd together
c = np.mean(c, axis=0) # average
可能有人请您解释一下这到底是怎么回事呢?
该网站的完整的源代码是:http://connor-johnson.com/2014/02/01/smoothing-with-exponentially-weighted-moving-averages/
谢谢!是的,我遇到了'bwd [:: - 1]'的问题'但是很好的解释! –
我还有一个问题。如果我想复制这个,我的下面的代码fwd和bwd是否正确? 'fwd = pd.ewma(df ['Close'],span = 20) bwd = pd.ewma(df ['Close'] [:: - 1],span = 20)' –