使用 R 的指数加权移动标准差的矢量化实现? [英] Vectorized implementation of exponentially weighted moving standard deviation using R?

查看:39
本文介绍了使用 R 的指数加权移动标准差的矢量化实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to implement a vectorized exponentially weighted moving standard deviation using R. Is this the correct approach?

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() 函数不同.

I'm guessing it's not, since its output is different from Python's pandas.Series.ewm.std() function.

当我跑步时

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

推荐答案

根据 Pandas 文档pandas.Series.ewm() 函数接收一个 adjust 参数,默认为 <代码>真.当 adjust == TRUE 时,pandas.Series.ewm.mean() 的指数加权移动平均值是通过权重计算的,而不是递归计算.当然,这也会影响标准偏差输出.请参阅此 Github 问题这个问题 了解更多信息.

According to the 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 for more info.

这是 R 中的矢量化解决方案:

Here's a vectorized solution in 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
      )
    }

这篇关于使用 R 的指数加权移动标准差的矢量化实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆