Author rhettinger
Recipients rhettinger, steven.daprano
Date 2019-09-28.18:28:15
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
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)


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])

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)

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)

    >>> harmonic_mean([2.5]*10_000 + [3]*7_200 + [10]*12_900)


Following the formula at , 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


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.
Date User Action Args
2019-09-28 18:28:15rhettingersetrecipients: + rhettinger, steven.daprano
2019-09-28 18:28:15rhettingersetmessageid: <>
2019-09-28 18:28:15rhettingerlinkissue38308 messages
2019-09-28 18:28:15rhettingercreate