Re: Replace Table[Sum...]...] with Functional code
- To: mathgroup at smc.vnet.net
- Subject: [mg98550] Re: Replace Table[Sum...]...] with Functional code
- From: Ray Koopman <koopman at sfu.ca>
- Date: Sun, 12 Apr 2009 03:48:42 -0400 (EDT)
- References: <grn1l3$n0t$1@smc.vnet.net>
On Apr 10, 1:57 am, Andreas <aa... at ix.netcom.com> wrote: > I use the following two functions to analyze time series: > > mm[data_,period_] := Table[Sum[(Log[data[[t]]] - Log[data[[t-i]]])/ > Sqrt[i], {i,1,period}] / period, {t, period+1, Length[data]}] > > mr[data_,period_] := Table[Sum[Log[data[[t-i]]/data[[-i + t-1]]] * > (Sqrt[i] - Sqrt[i-1]), {i,1,period}],{t, period+2, Length[data]}] > > They have a similar structure, Table[Sum[...]...], which sums a > bunch of stuff from the time series at each point in the series. > > I'd like to convert them into functional programing constructs, but > as the increments in the Table function come into play in the Sum, > I haven't been able to figure out a way to do it. > > I've tried Map with Partition and a bunch of other things, > but I could use some pointers. > > Any help much appreciated. Here is a third pair of suggestions. This mm is about as fast as the second version but is perhaps a little more elegant. mm[data_, period_] := (Append[-#,Tr@#]&[1/(Sqrt@Range[period,1,-1]*period)]). Transpose@Partition[Log@data,period+1,1] This mr is faster than the second version. mr[data_, period_] := Partition[Rest@#-Most@#&@Log@data,period,1]. (Reverse[Rest@#-Most@#]&@Sqrt@Range[0,period])