[issue27181] Add geometric mean to `statistics` module
Steven D'Aprano
report at bugs.python.org
Thu Mar 28 19:37:04 EDT 2019
Steven D'Aprano <steve+python at pearwood.info> added the comment:
> In the spirit of "perfect is the enemy of good", would it be
> reasonable to start with a simple, fast implementation using
> exp-mean-log? Then if someone wants to make it more accurate later,
> they can do so.
I think that is a reasonable idea. On the basis that something is better
than nothing, go ahead. We can discuss accuracy and speed issues later.
Getting some tricky cases down for reference:
# older (removed) implementation
py> geometric_mean([7]*2)
7.0
py> geometric_mean([7]*15)
7.0
# Raymond's newer (faster) implementation
py> exp(fmean(map(log, [7]*2)))
6.999999999999999
py> exp(fmean(map(log, [7]*15)))
6.999999999999999
py> geometric_mean([3,27])
9.0
py> geometric_mean([3,27]*5)
9.0
py> exp(fmean(map(log, [3,27])))
9.000000000000002
py> exp(fmean(map(log, [3,27]*5)))
8.999999999999998
py> x = 2.5e15
py> geometric_mean([x]*100)
2500000000000000.0
py> exp(fmean(map(log, [x]*100)))
2499999999999999.5
On the other hand, sometimes rounding errors work in our favour:
py> geometric_mean([1e50, 1e-50]) # people might expect 1.0
0.9999999999999998
py> 1e-50 == 1/(1e50) # even though they aren't quite inverses
False
py> exp(fmean(map(log, [1e50, 1e-50])))
1.0
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue27181>
_______________________________________
More information about the Python-bugs-list
mailing list