Re: exponential moving average with a varying exponents.
- To: mathgroup at smc.vnet.net
- Subject: [mg98619] Re: exponential moving average with a varying exponents.
- From: Ray Koopman <koopman at sfu.ca>
- Date: Tue, 14 Apr 2009 06:18:26 -0400 (EDT)
- References: <grkgrs$75h$1@smc.vnet.net>
On Apr 9, 2:58 am, Andreas <aa... at ix.netcom.com> wrote: > I have a function with calculates an exponential moving average > with a varying exponent. > > EMAVariableExponent[data_, exponents_]:=Module[{ema}, > ema = {data[[1]]}; > Do[ema =Append[ema,ema[[t-1]] + exponents[[t]] * > (data[[t]] - ema[[t-1]])], {t,2,Length[data]}] > ]; > > You supply the function with a list of data and with a > list of exponents used at each step in the calculations. > > This seems to work OK but I'd like to replace it with a clearer > and more elegant functional construct. > > I wish ExponentialMovingAverage[] could take a list of exponents > rather than a constant, something like: > > ExponentialMovingAverage[list, exponentList] > > I tried the following to try to Map an ExponentialMovingAverage[] > across the list of exponents. > > ExponentialMovingAverage[data, #]&/@ exponents > > but it gives me a list of lists, each of which has > mapped each exponent in exponents to the list of data. > > Any suggestions much appreciated. Here's a first attempt. As a side comment, what you call "exponents" I tend to think of as "multipliers" or "smoothing constants". emav[data_, c_] := FoldList[#1 + (#2[[1]]-#1)#2[[2]]&, data[[1]], Rest@Transpose@{data,c}]