r的指数加权运动差异
我正在尝试编写一个R功能来计算指数加权的移动方差(EWMV)。我仅找到了简单移动方差的R包装(例如runsd
在软件包ttr
,roll_sd
in Package roll_sd
roll 中)。
可以在 paper 。
我努力理解数学并将其转变为工作功能。尤其是,我要注意,如Wiki所建议的那样,如何计算“轻松与移动平均线”计算EWMV。
这是计算指数移动平均值(EMA)的函数(改编,来自此 site ):
EMA <- function (price,n){
ema <- c()
ema[1:(n-1)] <- NA
ema[n]<- mean(price[1:n])
alpha <- 2/(n+1)
for (i in (n+1):length(price)){
ema[i]<-alpha* price[i] +
(1-alpha) * ema[i-1]
}
ema <- reclass(ema,price)
return(ema)
}
我非常感谢如何将EMWV集成到此功能中的帮助和想法。多谢!
更新: 感谢大家的想法和代码。我重读文献,并根据您的评论以以下方式更改了代码:
EMVar <- function(x, n){
alpha <- 2/(n+1)
# exponential moving average
ema <- c()
ema[1:(n-1)] <- NA
ema[n]<- mean(x[1:n])
for (i in (n+1):length(x)){
ema[i]<-alpha* x[i] +
(1-alpha) * ema[i-1]
}
# exponential moving variance
delta <- x - lag(ema)
emvar <- c()
emvar[1:(n-1)] <- NA
emvar[n] <- ifelse(n==1,0,var(x[1:n]))
for(i in (n+1):length(x)){
emvar[i] <- (1-alpha) * (emvar[i-1] + alpha * delta[i]^2)
}
return(emvar)
}
以下是主要更改:
ema [i] = ema [i-1] + alpha * delta
与alpha * x [i] +(1-alpha) * ema [i-1] ,这是计算指数移动平均值的表格。因此,无需再次计算它。另外,可以在软件包
ttr
中使用函数ema
。但是,此功能在这里也计算出EMA,并且不依赖其他软件包。- Wiki上的描述未考虑不同的滑动窗口。相反,它以
x [1]
开始,并正确地说明ema [1] = x [1]
andemvar [1] = 0
。但是,这与Eg 20的闪亮窗口不起作用。对于移动平均,在这种情况下使用简单的平均值(即ema [20] =平均值(x [1:20])
)。因此,我将简单的方差用于第一个值(即,emvar [20] = var(x [1:20])
)。
I am trying to write a R-function to calculate the exponentially weighted moving variance (EWMV). I only found R-packages for the simple moving variance (e.g. runSD
in package TTR
, roll_SD
in package roll
).
A formula to calculate EMWV can be found on wiki which refers to this paper.
I struggle in understanding the mathematics and transform them into a working function. Especially, I am note sure how to compute the EWMV "easily along with the moving average" as wiki suggests.
Here is a function to calculate the exponential moving average (EMA) (adapted, from this site):
EMA <- function (price,n){
ema <- c()
ema[1:(n-1)] <- NA
ema[n]<- mean(price[1:n])
alpha <- 2/(n+1)
for (i in (n+1):length(price)){
ema[i]<-alpha* price[i] +
(1-alpha) * ema[i-1]
}
ema <- reclass(ema,price)
return(ema)
}
I would be very grateful for some help and ideas on how to integrate the EMWV into this function. Thanks a lot!
Update:
Thanks everyone for your ideas and codes. I reread the literature and based on your comments changed the code the following way:
EMVar <- function(x, n){
alpha <- 2/(n+1)
# exponential moving average
ema <- c()
ema[1:(n-1)] <- NA
ema[n]<- mean(x[1:n])
for (i in (n+1):length(x)){
ema[i]<-alpha* x[i] +
(1-alpha) * ema[i-1]
}
# exponential moving variance
delta <- x - lag(ema)
emvar <- c()
emvar[1:(n-1)] <- NA
emvar[n] <- ifelse(n==1,0,var(x[1:n]))
for(i in (n+1):length(x)){
emvar[i] <- (1-alpha) * (emvar[i-1] + alpha * delta[i]^2)
}
return(emvar)
}
Here are the main changes:
EMA[i] = EMA[i-1] + alpha * delta
is the same asalpha * x[i] + (1-alpha) * EMA[i-1]
, which is the formule to calculate the exponential moving average. So there was no need in calculating it again. Also, one could use functionEMA
in packageTTR
. However, this function here calculates EMA as well and does not rely on other packages.- The description on wiki did not take different sliding windows into account. Instead, it started with
x[1]
and stated correctly thatEMA[1] = x[1]
andEMVar[1] = 0
. However, this does not work with a slinding window of e.g. 20. For moving averages the simple mean is used in such situations (i.e.EMA[20] = mean(x[1:20])
). Accordingly, I used the simple variance for the first value (i.e.,EMVar[20] = var(x[1:20])
).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不熟悉这个主题,但是您不能以简单的循环编写方程式吗?
方程是:
而且我们只需要在您的帖子上使用
ema
函数创建ema
,然后在循环的emvar
theemvar
the loop的初始值。对于初始值,我分配了emvar
的第一个n
为na
和n
th + 1 AS(1-Alpha)*(emvar [n] + alpha*delta [n + 1]^2)
处理emvar [n]
as0 < /code>(这可能是错误的,但是正如我所说,我对此主题不熟悉)。
delta
值可以在循环usign usign函数lag
之外计算。我也不知道
reclass
函数来自哪里,但是我最终将其包括在ema
函数下。I'm not familiar with this theme, but can't you write the equations in a simple loop?
The equations are:
And we only need to create
ema
using theEMA
function on your post, and assingn theemvar
initial values for the loop. For the initial values, I assigned the firstn
values of theemvar
asNA
, and then
th + 1 as(1-alpha)*(emvar[n] + alpha*delta[n+1]^2)
treatingemvar[n]
as0
(which might be wrong, but as I said, i'm not familiar with this theme).The
delta
values can be calculated outside the loop usign the functionlag
.I also don't know where the
reclass
function is from, but I included it in the end, following theEMA
function you presented.以下功能将进行一系列观察结果,并返回包含EMA和EMVAR的两列数据框架。它使用一个默认的alpha值,可以根据需要更改,并且可以选择NA值的选项:
我们可以在一些随机泊松分布式数据上进行测试,其中平均值和方差应平均大约为10个:
我们可以绘制绘图这样的结果:
在2022-06-18创建的 reprex软件包(v2.0.1)
The following function will take a series of observations and return a two-column data frame containing the EMA and EMVar. It uses a default alpha value which can be changed as required, and an option to drop NA values:
We can test it out on some random Poisson distributed data, where the mean and variance should both be around 10 on average:
And we can plot the result like this:
Created on 2022-06-18 by the reprex package (v2.0.1)