Currently, harmonic_mean() is difficult to use in real applications because it assumes equal weighting. While that is sometimes true, the API precludes a broad class of applications where the weights are uneven.
That is easily remedied with an optional *weights* argument modeled after the API for random.choices():
harmonic_mean(data, weights=None)
Examples
--------
Suppose a car travels 40 km/hr for 5 km, and when traffic clears, speeds-up to 60 km/hr for the remaining 30 km of the journey. What is the average speed?
>>> harmonic_mean([40, 60], weights=[5, 30])
56.0
Suppose an investor owns shares in each of three companies, with P/E (price/earning) ratios of 2.5, 3 and 10, and with market values of 10,000, 7,200, and 12,900 respectively. What is the weighted average P/E ratio for the investorâ€™s portfolio?
>>> avg_pe = harmonic_mean([2.5, 3, 10], weights=[10_000, 7_200, 12_900])
>>> round(avg_pe, 1)
3.9
Existing workarounds
--------------------
It is possible to use the current API for theses tasks, but it is inconvenient, awkward, slow, and only works with integer ratios:
>>> harmonic_mean([40]*5 + [60]*30)
56.0
>>> harmonic_mean([2.5]*10_000 + [3]*7_200 + [10]*12_900)
3.9141742522756826
Algorithm
---------
Following the formula at https://en.wikipedia.org/wiki/Harmonic_mean#Weighted_harmonic_mean , the algorithm is straight forward:
def weighted_harmonic_mean(data, weights):
num = den = 0
for x, w in zip(data, weights):
num += w
den += w / x
return num / den
PR
--
If you're open to this suggestion, I'll work-up a PR modeled after the existing code and that uses _sum() and _fail_neg() for exactness and data validity checks. |