I would like to reproduce this code in vectorized notation
getEMA2<-function(x,win){
k<-2/(win+1)
v<-vector()
for (i in 1:length(x)){
if (i==1){
v[i]<-x[i]
}
else{
v[i]<-k*x[i]+(1-k)*v[i-1]
}
}
return (v)
}
testOutput<-getEMA2(rnorm(100,0,1),5)
I’ve tried using the filter function, but it doesn’t appear that recursive / convolution methods can achieve this
Thanks for responses,
Since
filtercomputesyou need to rescale the result.
Most of those filters are already defined in the
TTRpackage.