这是一个创建于 2186 天前的主题,其中的信息可能已经有所发展或是发生改变。
在 stackoverflow 里找到一个 numpy 版指数移动平均的函数,但实在看不懂,还有当 window 少于等于 7 时不能得到正确的结果。
def numpy_ewma_vectorized_v2(data, window):
alpha = 2 /(window + 1.0)
alpha_rev = 1-alpha
n = data.shape[0]
pows = alpha_rev**(np.arange(n+1))
scale_arr = 1/pows[:-1]
offset = data[0]*pows[1:]
pw0 = alpha*alpha_rev**(n-1)
mult = data*pw0*scale_arr
cumsums = mult.cumsum()
out = offset + cumsums*scale_arr[::-1]
return out
3 条回复 • 2018-10-16 10:48:40 +08:00
|
|
1
yuankui 2018-10-16 10:22:05 +08:00
代码这么乱, 没有看下去的欲望。。
|
|
|
2
Justin13 2018-10-16 10:48:00 +08:00 via Android
类似这种问题,应该先看数学原理。而不是上来看代码,看到懵逼。
|