Message Boards Message Boards

0
|
3848 Views
|
4 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Overflow in Sum?

Posted 4 years ago

I am using Windows 10 with 32GB memory and Mathematica 12.1 To be sure if the following is a bug, I would be glad if somebody can verify the following unexpected result.

sa := Total[RandomVariate[NormalDistribution[0, 1], 10]]^2/10;
n = 10^6;
Sum[sa, {i, n}]/n
n = 10^6 + 1;
Sum[sa, {i, n}]/n

We consider a sample of 10 from a Normal- distribution and calculate the expectation value of the variance using n samples. This gives reasonable results near 1 for n upto 10^6. But for n>10^6 the results are definitively wrong. It looks like an overflow in "Sum" to me.

Interestingly, I also get correct results if I calculate the data separately before summing up, so it seems more complicated than a simple overflow:

sa := Total[RandomVariate[NormalDistribution[0, 1], 10]]^2/10;
n = 10^6 + 1;
d = Table[sa, n];
Sum[d[[i]], {i, n}]/n

If I replace "Sum" by "Total" everything is o.k.

POSTED BY: Daniel Huber
4 Replies
Posted 4 years ago

Ah. I see what you mean. Sometimes I can just not see colons.

Then I think you might be right. Sum was built to have a variable in the expression.

Total is probably the way to go.

However, I thought I'd try putting the variable in:

sa[i_] := Total[RandomVariate[NormalDistribution[0, 1], 10]]^2/10;
n = 10^6;
Sum[sa[i], {i, n}]/n
n = 10^6 + 1;
Sum[sa[i], {i, n}]/n

This still works for me.

I have 12.1 on a Mac with 64GB.

POSTED BY: Mike Besso
Posted 4 years ago

Daniel, this one has me a bit confused.

sa := Total[RandomVariate[NormalDistribution[0, 1], 10]]^2/10;

gives sa a constant value that does not change (unless executed again).

So, regardless of what n is,

Sum[sa, {i, n}]/n

should just just return sa.

Am I missing something?

As an FYI, I was able to run the code you provided on a Mac with 64GB without any issues.

THANKS

POSTED BY: Mike Besso

No, sa is evaluated anew each time the iterator i is given a new value, as you can see this way:

a := RandomInteger[{1, 10}];
Sum[a, {i, 3}] // Trace
POSTED BY: Gianluca Gorni
POSTED BY: Gianluca Gorni
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