[Numpy-discussion] calculating the mean and variance of a large float vector

Keith Goodman kwgoodman at gmail.com
Thu Jun 5 21:06:28 EDT 2008


On Thu, Jun 5, 2008 at 4:54 PM, Christopher Marshall
<christopherlmarshall at yahoo.com> wrote:
> I will be calculating the mean and variance of a vector with millions of elements.
>
> I was wondering how well numpy's mean and variance functions handle the numerical stability of such a calculation.

How's this for stability?

>> import numpy as np
>> x = 1e10 * np.ones(100000000)
>> x.mean()
   10000000000.0

Are you worried that the mean might overflow on the intermediate sum?
Yeah, that could be a problem if you are counting stars or bugs in my
code:

>> x = 1e305 * np.ones(10000000)
>> y = x.mean()
Warning: overflow encountered in reduce
>> y
   inf

But at least you get a warning.



More information about the NumPy-Discussion mailing list