使用 R 的指数加权移动标准偏差的矢量化实现?

Vectorized implementation of exponentially weighted moving standard deviation using R?

我正在尝试使用 R 实现矢量化指数加权移动标准差。这是正确的方法吗?

ewma <- function (x, alpha) {
  c(stats::filter(x * alpha, 1 - alpha, "recursive", init = x[1]))
}
ewmsd <- function(x, alpha) {
  sqerror <- na.omit((x - lag(ewma(x, alpha)))^2)
  ewmvar <- c(stats::filter(sqerror * alpha, 1 - alpha, "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, the pandas.Series.ewm() function receives an adjust parameter, which defaults to TRUE. When adjust == TRUE, the exponentially weighted moving average from pandas.Series.ewm.mean() is calculated through weights, not recursively. Naturally, this affects the standard deviation output as well. See this Github issue and 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
      )
    }