Efficient computation of "variable (number of points included)" moving average in R -
i'm trying implement variable exponential moving average on time series of intraday data (i.e 10 seconds). variable, mean size of window included in moving average depends on factor (i.e. volatility). thinking of following:
ma(t)=alpha(t)*price(t) + (1-alpha(t))ma(t-1),
where alpha corresponds example changing volatility index.
in backtest on huge series (more 100000) points, computation causes me "troubles". have complete vectors alpha , price, current values of ma need value calculated before. thus, far not see vectorized solution????
another idea, had, trying directly apply implemented ema(..,n=f()) function every data point, having different value f(). not find fast solution neither far.
would kind if me problem??? other suggestions of how constructing variable moving average great.
thx lot in advance martin
a efficient moving average operation possible via filter()
:
## create weight vector -- 1 has equal weights, other schemes possible weights <- rep(1/nobs, nobs) ## , apply one-sided moving average calculations, see help(filter) movavg <- as.vector(filter(somevector, weights, method="convolution", side=1))
that left-sided only, other choices possible.
Comments
Post a Comment