我正在尝试在R中复制ewm python (https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.ewm.html)函数,但没有成功。
以下是python代码:
import pandas as pd
df = pd.DataFrame({'B': [0:100]})
df.ewm(span=100).std()我不能在R中得到相同(或相似)的结果。
发布于 2021-01-18 16:10:38
我更改了Vectorized implementation of exponentially weighted moving standard deviation using R?中的代码以使其更快:
f <- function(y, m, alpha) {
weights <- (1 - alpha)^((m - 1):0)
ewma <- sum(weights * y) / sum(weights)
bias <- sum(weights)^2 / (sum(weights)^2 - sum(weights^2))
ewmsd <- sqrt(bias * sum(weights * (y - ewma)^2) / sum(weights))
ewmsd
}
test <- frollapply(df0, 1000, function(x) f(x, 1000, alpha))其中df0是具有一列或向量的df。
结果与python函数生成的结果相同(小数点后5位)。
https://stackoverflow.com/questions/65760795
复制相似问题