我试图用R实现矢量化指数加权移动标准差。这是正确的方法吗?
ewma <- function (x, alpha) {
c(stats::filter(x * ratio, 1 - ratio, "recursive", init = x[1]))
}
ewmsd <- function(x, alpha) {
sqerror <- na.omit((x - lag(ewma(x, ratio)))^2)
ewmvar <- c(stats::filter(sqerror * ratio, 1 - ratio, "recursive", init = 0))
c(NA, sqrt(ewmvar))
}
我猜它不是,因为它的输出与Python的pandas.Series.ewm.std()
函数不同。
我跑的时候
ewmsd(x = 0:9, alpha = 0.96)
输出是
[1] NA 0.2236068 0.4874679 0.7953500 1.1353903 1.4993855 1.8812961 2.2764708 2.6812160 3.0925367
然而,随着
pd.Series(range(10)).ewm(alpha = 0.96).std()
输出是
0 NaN
1 0.707107
2 0.746729
3 0.750825
4 0.751135
5 0.751155
6 0.751156
7 0.751157
8 0.751157
9 0.751157
根据documentation for Pandas,pandas.Series.ewm()
函数接收adjust
参数,默认为TRUE
。当adjust == TRUE
时,来自pandas.Series.ewm.mean()
的指数加权移动平均线是通过权重计算的,而不是递归的。当然,这也会影响标准偏差输出。有关详细信息,请参阅this Github issue和this question。
这是R中的矢量化解决方案:
ewmsd <- function(x, alpha) {
n <- length(x)
sapply(
1:n,
function(i, x, alpha) {
y <- x[1:i]
m <- length(y)
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))
},
x = x,
alpha = alpha
)
}