Re: Replace Table[Sum...]...] with Functional code
- To: mathgroup at smc.vnet.net
- Subject: [mg98590] Re: Replace Table[Sum...]...] with Functional code
- From: Raffy <raffy at mac.com>
- Date: Mon, 13 Apr 2009 03:36:28 -0400 (EDT)
- References: <grn1l3$n0t$1@smc.vnet.net> <grs6cs$qk4$1@smc.vnet.net>
On Apr 12, 12:48 am, Ray Koopman <koop... at sfu.ca> wrote:
> 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 fourth version of mm. Why I transposed things in the third
> version is beyond me! This one is both faster and more elegant.
>
> mm[data_, period_] := Partition[Log@data,period+1,1].
> (Append[-#,Tr@#]&[1/(Sqrt@Range[period,1,-1]*period)])
These eek out a tiny bit more speed:
mm3[v_List, n_Integer] := With[{u = Log[v]}, Dot[Drop[u, n] - Partition
[Most[u], n, 1], 1/Sqrt@Range[n, 1, -1]] / n];
mr3[v_List, n_Integer] := Dot[Partition[Log[Take[v, {2, -2}]/Drop[v,
-2]], n, 1], Sqrt@Range[n, 1, -1] - Sqrt@Range[n - 1, 0, -1]];