Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

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

  • To: mathgroup at smc.vnet.net
  • Subject: [mg98588] Re: [mg98556] Re: Replace Table[Sum...]...] with Functional code
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Mon, 13 Apr 2009 03:36:06 -0400 (EDT)
  • References: <grpilf$ltq$1@smc.vnet.net> <200904120749.DAA27315@smc.vnet.net>
  • Reply-to: drmajorbob at bigfoot.com

Here's a simpler version (I think):

functionalMR2[data_, period_] :=
  ListConvolve[Rest@# - Most@# &@Sqrt@Range[0, period],
   Log[Rest@data/Most@data]]

Comparing it with yours:

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]]

data = Array[a, 10];
Table[functionalMR[data, p] == functionalMR2[data, p], {p, 0, 10}]

{True, True, True, True, True, True, True, True, True, True, True}

I haven't looked at Ray Koopman's code; it may be the same as mine, for  
all I know.

Bobby

On Sun, 12 Apr 2009 02:49:46 -0500, <mike.honeychurch at gmail.com> wrote:

> 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
>



-- 
DrMajorBob at bigfoot.com


  • Prev by Date: Re: Re: Change in NSolve algorithm
  • Next by Date: Re: Re: Help with ScriptSource in GUIKit
  • Previous by thread: Re: Replace Table[Sum...]...] with Functional code
  • Next by thread: Re: Replace Table[Sum...]...] with Functional code