Message Boards Message Boards

0
|
7182 Views
|
5 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How to improve efficiency of Table expression

Posted 9 years ago

The following code is correct but is highly inefficient:

data2 = RandomVariate[NormalDistribution[0, 1], 10^6];
MyList2 = Table[Mean[Take[data2, i]], {i, Length[data2]}];

Is there a concise way of doing this more efficiently?

POSTED BY: Gerald Feigin
5 Replies

Seems to me the mean of k+1 terms can be calculated from the mean of k terms and the k+1 term, if you're willing to put up with round off errors. You could do that sequentially starting with the first term and that should be faster.

POSTED BY: Frank Kampas
Posted 9 years ago

If you're wanting to calculate the sequential means of observations 1, 1+2, 1+2+3, etc., then

MyEfficientList = Accumulate[data2]/Table[i, {i, Length[data2]}]

is much faster.

If you're then going to do something with those sequential means, there might be more efficient ways to get to that final product.

POSTED BY: Jim Baldwin

Somewhat faster, by keeping the arrays packed:

myList = Accumulate[data2]/N[Range[Length[data2]]];

myList == MyList2

takes about 0.01 seconds on my machine.

POSTED BY: Ilian Gachevski

Look at this

In[119]:= FoldList[Plus, a, {b, c, d, f}]/Range[5]
Out[119]= {a, (a + b)/2, 1/3 (a + b + c), 1/4 (a + b + c + d), 1/5 (a + b + c + d + f)}

with other words

FoldList[Plus, First[data2], Rest[data2]]/Range[Length[data2]]

should do it.

POSTED BY: Udo Krause
Posted 9 years ago

Thank you all for the helpful responses.

POSTED BY: Gerald Feigin
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract