hmean

mpsci.stats.hmean(x)

Harmonic mean of the values in the sequence x.

If any value in x is 0, the return value is 0.

hmean accepts negative values. Usually the harmonic mean is defined for positive values only, but the formula is well-defined as long as 1/x[0] + 1/x[1] + … + 1/x[-1] is not 0.

If that expression is 0, and the signs of the x values are mixed, nan is returned. If the signs are not mixed, then either all the values are +inf or they are all -inf. For those cases, +inf and -inf are returned, respectively.

Examples

>>> from mpsci.stats import hmean
>>> from mpmath import mp
>>> mp.dps = 25
>>> hmean([1, 3, 3])
mpf('1.8')
>>> hmean([10, 3, -2])
mpf('-45.0')
>>> hmean(range(1, 10))
mpf('3.181371861411137606957497545')
>>> hmean([2, -2])
mpf('nan')
>>> hmean([mp.inf, mp.inf, mp.inf])
mpf('+inf')
>>> hmean([mp.inf, mp.inf, -mp.inf])
mpf('nan')
>>> hmean([-mp.inf, -mp.inf, -mp.inf])
>>> mpf('-inf')