Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

python - Is numpy.sum implemented in such a way that numerical errors are avoided?

It is well known that adding up numbers can result in numerical errors (for example, if the first number is really large, whereas there are many other small numbers).

This can be solved adding up the numbers in a non straight-forward way. See for example: https://en.wikipedia.org/wiki/Kahan_summation_algorithm

Is numpy.sum implemented in such a way that numerical errors are avoided?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Searching on numpy kahan turned up a closed bug/issue

https://github.com/numpy/numpy/issues/2448 Numerical-stable sum (similar to math.fsum)

I haven't read it in detail. Note the reference to math.fsum

fsum(iterable)
Return an accurate floating point sum of values in the iterable.
Assumes IEEE-754 floating point arithmetic.
(from the Python math docs)
Return an accurate floating point sum of values in the iterable. Avoids loss of precision by tracking multiple intermediate partial sums

And a SO question, with some discussion, but no real answer:

Is there any documentation of numpy numerical stability?

A simple comparison:

In [320]: x=np.ones(100000)/100000
In [321]: sum(x)-1
Out[321]: -1.9162449405030202e-12
In [322]: np.sum(x)-1
Out[322]: 1.3322676295501878e-15
In [323]: math.fsum(x)-1
Out[323]: 0.0

respective times are 72 ms, 304 μs, 23.8 ms

np.sum is clearly fastest; but fsum is better than sum, probably because of its sepecialized C implementation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...