gpt4 book ai didi

variables - R中 "variable (number of points included)"移动平均线的高效计算

转载 作者:行者123 更新时间:2023-12-04 06:44:21 25 4
gpt4 key购买 nike

我正在尝试对日内数据的时间序列(即 10 秒)实现可变指数移动平均线。通过变量,我的意思是移动平均线中包含的窗口大小取决于另一个因素(即波动性)。我在想以下几点:

MA(t)=alpha(t)*price(t) + (1-alpha(t))MA(t-1),

例如,其中 alpha 对应于不断变化的波动率指数。

在对巨大系列(超过 100000)点的回测中,这种计算给我带来了“麻烦”。我有完整的向量 alpha 和 price,但是对于 MA 的当前值,我总是需要之前刚刚计算的值。因此,到目前为止我还没有看到矢量化的解决方案????

我的另一个想法是尝试将实现的 EMA(..,n=f()) 函数直接应用于每个数据点,方法是始终为 f() 设置不同的值。但到目前为止,我也没有找到一个快速的解决方案。

如果有人能帮我解决我的问题,我会很友好吗???甚至关于如何构建可变移动平均线的其他建议也会很棒。

提前很多
马丁

最佳答案

通过 filter() 也可以进行非常有效的移动平均操作:

  ## create a weight vector -- this one has equal weights, other schemes possible
weights <- rep(1/nobs, nobs)

## and apply it as a one-sided moving average calculations, see help(filter)
movavg <- as.vector(filter(somevector, weights, method="convolution", side=1))

那只是左侧,其他选择也是可能的。

关于variables - R中 "variable (number of points included)"移动平均线的高效计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3879520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com