Re: Moving average type process
- To: mathgroup at smc.vnet.net
- Subject: [mg18231] Re: [mg18221] Moving average type process
- From: "Wolf, Hartmut" <hwolf at debis.com>
- Date: Thu, 24 Jun 1999 14:24:15 -0400
- Organization: debis Systemhaus
- References: <199906230041.UAA04390@smc.vnet.net.>
- Sender: owner-wri-mathgroup at wolfram.com
Hello Virgil,
Virgil Stokes schrieb:
>
> I wish to perform the following "moving average" type process on
> a list to generate a new list:
>
> Inputs:
> wtlist = {w1,w2,w3} -- weight list
> inlist = {a,b,c,d,e,f} -- any other list (>= 3 elements)
>
> Output:
> outlist = {w1*a+w2*b+w3*c, w1*b+w2*c+w3*d, w1*c+w2*d+w3*e, w1*d+w2*e+w3*f}
>
> Note, outlist will always contain 2 less (Length[wtlist]/2) elements
> than in the input list (inlist).
>
> If w1=w2=w3=x, then
> the following works fine:
>
> outlist = x*Drop[Plus@@NestList[RotateRight,inlist,2],2]
>
> This is a weighted (from wtlist) sum over another list of arbitrary
> length (inlist). I would like to get a "fast" function for doing this when
> the weights are not equal.
>
Look at:
In[1]:= wtlist = {w1,w2,w3};
inlist = {a,b,c,d,e,f};
In[2]:= Partition[inlist,3,1]
Out[2]= {{a,b,c},{b,c,d},{c,d,e},{d,e,f}}
In[4]:= %.wtlist
Out[4]= {a w1+b w2+c w3,b w1+c w2+d w3,c w1+d w2+e w3,d w1+e w2+f w3}
So you'll get your weighted average with
Partition[inlist,Length[wtlist],1].wtlist
quite fast:
In[28]:= wtlist=With[{n=100},Table[Binomial[n,m]/2^n,{m,0,n}]];
In[30]:= inlist=Table[Random[],{10000}];
In[31]:= Partition[inlist,Length[wtlist],1].wtlist//Short//Timing
Out[31]= {3.875 Second,{0.503121,<<9898>>,0.58417}}
this is slightly better than the method you missed only nearly:
In[37]:= With[{ov=Length[wtlist]-1},
Drop[Transpose at NestList[RotateRight,inlist,ov],ov].wtlist]//Short//
Timing
Out[37]= {5.629 Second,{0.503121,<<9898>>,0.58417}}
kind regards, hw
- References:
- Moving average type process
- From: Virgil Stokes <virgil.stokes@neuro.ki.se>
- Moving average type process