【发布时间】:2020-01-20 13:22:52
【问题描述】:
我正在尝试使用Pandas ewm function 来计算指数加权移动平均线。但是我注意到信息似乎贯穿了你的整个时间序列。这意味着每个数据点的 MA 取决于不同数量的先前数据点。因此每个数据点的 ewm 函数在数学上是不同的。
我想这里有些人有类似的问题
Does Pandas calculate ewm wrong?
但我确实尝试了他们的方法,但我没有得到我想要的功能。
def EMA(arr, window):
sma = arr.rolling(window=window, min_periods=window).mean()[:window]
rest = arr[window:]
return pd.concat([sma, rest]).ewm(com=window, adjust=False).mean()
a = pd.DataFrame([x for x in range(100)])
print(list(EMA(a, 10)[0])[-1])
print(list(EMA(a[50:], 10)[0])[-1])
在这个例子中,我有一个 1 到 100 的数组。我计算这个数组的移动平均线和 50-100 的数组。最后一个移动平均线应该是相同的,因为我只使用了 10 个窗口。但是当我运行此代码时,我得到两个不同的值,表明 ewm 确实依赖于整个系列。
【问题讨论】:
标签: python pandas moving-average weighted-average