Re: Re: Correlation function and data
- To: mathgroup at smc.vnet.net
- Subject: [mg24777] Re: [mg24748] Re: Correlation function and data
- From: Richard Palmer <mapsinc at bellatlantic.net>
- Date: Thu, 10 Aug 2000 00:32:02 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
on 8/9/00 2:31 AM, Deborah Leddon at Dleddon at students.cas.unt.edu wrote: > Hello, > I am trying to construct an autocorrelation function that can be > applied to a data set as follows; > > n=Length[data]; > c[y_]:= Sum[(data[[i]] - Mean[data] )* ( data[[i+y]] - Mean[data])/ > Variance[data], {i,1,n - y}]; > corrfunction = Array[c, {n - 1}]; > > ListPlot[corrfunction, PlotJoined->True] > > Problem is , this routine works well for data sets up to a length of > 300 points, but gets unusually long for larger data sets , say > around 1000-3000 points in length. I've tried "Map" (using > anonymous function rules), Table-Evaluate, etc.. > > Anyone got any ideas? I would much appreciate them! > > Regards, > Deb L. > > Hi Deb, Mathematica is not an optimizing compiler. Compute the Mean[data] and the Variance[data] outside the loop. I ran your code on a vector with length 300. Timing went from 140 seconds to 3 seconds. Here is the code. n = Length[data]; md = Mean[data]; vd = Variance[data]; c[y_] := Sum[(data[[i]] - md )* ( data[[i + y]] - md)/ vd, {i, 1, n - y}]; corrfunction = Array[c, {n - 1}]; Regards, Richard Palmer ListPlot[corrfunction, PlotJoined -> True]