Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid Mean of Floating Point Error

When I calculate the mean of a list of floats the following way

def mean(x):
    sum(x) / len(x)

then I usually do not care about tiny errors in floating point operations. Though, I am currently facing an issue where I want to get all elements in a list that are equal or above the list's average.

Again, this is usually no issue but when I face cases where all elements in the list are equal floating point numbers than the mean value calculated by the function above actually returns a value above all the elements. That, in my case, obviously is an issue.

I need a workaround to that involving no reliability on python3.x libraries (like e.g. statistics).

Edit: It has been suggested in the comments to use rounding. This interestingly resulted in errors being rarer, but they still occur, as e.g. in this case:

[0.024484987, 0.024484987, 0.024484987, 0.024484987, ...]  # x
0.024485  # mean
[]  # numbers above mean
like image 545
weidler Avatar asked Dec 19 '25 09:12

weidler


1 Answers

I believe you should be using math.fsum() instead of sum. For example:

>>> a = [0.024484987, 0.024484987, 0.024484987, 0.024484987] * 1360001
>>> math.fsum(a) / len(a)
0.024484987

This is, I believe, the answer you are looking for. It produces more consistent results, irrespective of the length of a, than the equivalent using sum().

>>> sum(a) / len(a)
0.024484987003073517
like image 66
BoarGules Avatar answered Dec 20 '25 23:12

BoarGules



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!