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

Keith Goodman kwgoodman@gmail....
Thu Jun 5 20:06:28 CDT 2008

On Thu, Jun 5, 2008 at 4:54 PM, Christopher Marshall
<christopherlmarshall@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()

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

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

But at least you get a warning.

More information about the Numpy-discussion mailing list