Re: Replace Table[Sum...]...] with Functional code

*To*: mathgroup at smc.vnet.net*Subject*: [mg98556] Re: Replace Table[Sum...]...] with Functional code*From*: mike.honeychurch at gmail.com*Date*: Sun, 12 Apr 2009 03:49:46 -0400 (EDT)*References*: <grpilf$ltq$1@smc.vnet.net>

On Apr 11, 2:59 am, Bob Hanlon <hanl... at cox.net> wrote: > mm[data_, period_] := > Table[ > Sum[(Log[data[[t]]] - Log[data[[t - i]]])/Sqrt[i], > {i, 1, period}]/period, > {t, period + 1, Length[data]}] > > mm2[data_, period_] := Module[ > {logData = Log[data], d = Sqrt[Range[period, 1, -1]]}, > Mean[#/d] & /@ > (Drop[logData, period] - > Most[Partition[logData, period, 1]])] > > data = Array[x, {500}]; > > period = Random[Integer, {20, 100}] > > 41 > > Timing[r1 = mm[data, period];] > > {0.459393,Null} > > Timing[r2 = mm2[data, period];] > > {0.228474,Null} > > %/%% > > {0.497339,1} > > r1 == r2 > > True > > Bob Hanlon > > ---- 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 th= e increments in the Table function come into play in the Sum, I haven't bee= n able to figure out a way to do it. > > I've tried Map with Partition and a bunch of other things, but I could us= e some pointers. > > Any help much appreciated. By using Table and Sum you are repeating a lot of calculations. Once "period" has been set all the possible values of (Sqrt[i] - Sqrt[i - 1]) and Log[data[[t - i]] / data[[-i + t - 1]]] are set. Therefore: kernel=Table[(Sqrt[i]-Sqrt[i-1]),{i,period}]; ls=ListCorrelate[{1,1},data,{1,-1},1,Times,Log[#2/#1]&]; so now combine them: ListConvolve[kernel,ls,{-1,1},0] functionalMR[data_,period_]:=Module[{kernel,ls}, kernel=Table[(Sqrt[i]-Sqrt[i-1]),{i,period}]; ls=ListCorrelate[{1,1},data,{1,-1},1,Times,Log[#2/#1]&]; ListConvolve[kernel,ls,{-1,1},0] ] This is a little bit slower than Ray Koopman's code but I just like using ListCorrelate and ListConvolve as replacements for Table[Sum [ ...]...] :) Mike

**Follow-Ups**:**Re: Re: Replace Table[Sum...]...] with Functional code***From:*DrMajorBob <btreat1@austin.rr.com>