Re: Replace Table[Sum...]...] with Functional code
- To: mathgroup at smc.vnet.net
- Subject: [mg98751] Re: Replace Table[Sum...]...] with Functional code
- From: green tea <cetech07 at gmail.com>
- Date: Sat, 18 Apr 2009 03:36:20 -0400 (EDT)
- References: <grn1l3$n0t$1@smc.vnet.net>
On Apr 10, 5:57 pm, 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. ListCorrelate is very fast, but generalized ListCorrelate like "ListCorrelate[{1,1},data,{1,-1}, 1,Times,Log[#2/#1]&]" is terribly slow. I don't know why. instead use these codes. Log[data/RotateRight[data]] Log[Rest[data]/Most[data]] Log[Ratios[data]] etc. ListConvolve's computational compexity is O(n log(n)), and it does not create intermediate list structure like Partition and Dot combination. so it is more suitable for this time series analysis. mm[data_, period_] := With[{kernel = (period Sqrt[Range[1., period]])^-1, list = Log [data]}, list[[period + 1 ;;]] Total[kernel] - ListConvolve[kernel, list[[;; -2]]]] mr[data_, period_] := With[{kernel = Differences[Sqrt[Range[0., period]]], list = Log[Ratios [data]]}, ListConvolve[kernel, list[[;; -2]]]]