CCalclabhub

Statistics

How To Calculate A Harmonic Mean

The harmonic mean is the average for rates. Instead of adding the values and dividing, you flip each one, average the flipped numbers, then flip the answer back.

Quick Answer

H = n / (1/x1 + 1/x2 + ... + 1/xn)

H
The harmonic mean of the values
n
How many values there are
x1 ... xn
The positive values being averaged
1/xi
The reciprocal of each value

Add the reciprocals of every value, then divide the count by that sum. For 30, 60 and 90 the reciprocals add to 0.061111, and 3 divided by 0.061111 is 49.0909, which sits below the arithmetic mean of 60.

What Is A Harmonic Mean?

The harmonic mean of n positive numbers is n divided by the sum of their reciprocals. It is the third of the classical Pythagorean means, sitting alongside the arithmetic mean you get by adding and the geometric mean you get by multiplying.

It is the average you need when the numbers are rates. A rate has something in the denominator, and when that denominator is what actually adds up, the plain average gives the wrong answer. Speed is the textbook case: kilometres per hour, where hours are what accumulate.

Drive 60 miles out at 30 miles per hour and 60 miles back at 60 miles per hour. The arithmetic mean of the two speeds is 45, but the trip takes two hours out and one hour back, so the real average is 120 miles over 3 hours, which is 40. The harmonic mean of 30 and 60 is 40.

The harmonic mean is never larger than the geometric mean, which is never larger than the arithmetic mean. For 30, 60 and 90 the three averages line up as 49.0909, 54.5136 and 60, and the gap grows as the values spread apart.

It always lands between the smallest and the largest value, and it leans towards the small end. One slow leg of a journey drags the average down far more than one fast leg lifts it, because slow legs eat the time.

For two numbers there is a shortcut: the harmonic mean is twice the product divided by the sum. That is the same shape as the formula for two resistors in parallel, which is why electricians meet this average without calling it that.

The same pattern shows up whenever two things work together. If one painter finishes a wall in 3 hours and another in 6, the combined rate is the harmonic mean of their rates, and the wall takes 2 hours rather than the 4.5 the arithmetic mean suggests.

Investors meet it through dollar cost averaging. Spend a fixed amount each month and the average price you pay is the harmonic mean of the prices, not the arithmetic mean. Buying at 10, 20 and 5 gives an average cost of 8.5714 rather than 11.6667, which is exactly why the strategy works.

Machine learning uses it for the F1 score. Precision and recall are both rates over the same set of predictions, so a single number has to be combined harmonically; the F1 score is the harmonic mean of the two and collapses to zero if either one does.

It demands positive numbers. A zero in the list forces a division by zero and the whole calculation dies, and a negative value can produce a denominator that cancels to something meaningless or a mean that falls outside the data.

The weighted version handles unequal legs. Divide the total distance by the total time rather than averaging the speeds, which is the honest way to report an average speed over a trip with legs of different lengths.

Do not reach for it with ordinary measurements. Averaging the heights of a class, the price of a basket of goods or a set of test scores is additive, and the harmonic mean would understate every one of them.

A single tiny value dominates the result. Because the smallest number contributes the biggest reciprocal, one value near zero pulls the harmonic mean down towards it, which is a feature for rates and a bug for anything else.

Compute it in three moves: flip every value, add the flips, and divide the count by that sum. On a calculator the reciprocal key does the flipping, and one more press flips the answer back.

A useful sanity check is the ordering. If your harmonic mean comes out above the arithmetic mean of the same numbers, either a value was mistyped or the reciprocals were averaged instead of summed.

Formula

H = n / (1/x1 + 1/x2 + ... + 1/xn)

Formula page →

Add the reciprocals of all values, then divide the count by that sum.

SymbolMeaning
HHarmonic mean
nCount
xiValues

H = 2 x a x b / (a + b)

Formula page →

The two value shortcut, identical in shape to two parallel resistors.

SymbolMeaning
HHarmonic mean
aFirst value
bSecond value

H = (d1 + d2 + ... + dn) / (d1/x1 + d2/x2 + ... + dn/xn)

Formula page →

Total distance over total time, the correct average speed for unequal legs.

SymbolMeaning
HAverage rate
diLeg size
xiRate

How To Calculate A Harmonic Mean

  1. 1

    Check every value is positive

    The harmonic mean divides by each value, so a zero breaks it outright and a negative value gives an answer that no longer describes the data. Speeds, prices and rates are all positive by nature.

  2. 2

    Flip each value into a reciprocal

    One over 30 is 0.033333, one over 60 is 0.016667 and one over 90 is 0.011111. Notice how the slowest speed contributes the largest reciprocal, which is what pulls the mean downwards.

  3. 3

    Add the reciprocals

    The three reciprocals add to 0.061111. This sum is the total time per unit of distance for the three legs, and it is the quantity that actually accumulates.

  4. 4

    Divide the count by the sum

    3 divided by 0.061111 is 49.0909. Compare it with the arithmetic mean of 60: the difference of 10.909 is exactly the penalty for treating a rate as if it were an ordinary number.

Examples

Example 1: Three equal legs driven at 30, 60 and 90 miles per hour

First value
30
Second value
60
Third value
90
StepCalculationResult
Reciprocals of the three speeds1/30 + 1/60 + 1/900.061111
Total distance covered60 + 60 + 60180
Total time taken60/30 + 60/60 + 60/903.666667
Average speed from distance over time180 / 3.66666749.0909

Result: Three 60 mile legs at 30, 60 and 90 miles per hour cover 180 miles in 3.666667 hours, so the true average speed is 49.0909 miles per hour. The harmonic mean gives the same 49.0909, while the arithmetic mean of 60 overstates it by 10.909.

Example 2: Dollar cost averaging at prices of 10, 20 and 5

First value
10
Second value
20
Third value
5
StepCalculationResult
Shares bought with 100 each month100/10 + 100/20 + 100/535
Total money invested100 x 3300
Average price paid per share300 / 358.5714
Arithmetic mean of the prices for contrast(10 + 20 + 5) / 311.6667

Result: Spending 100 a month at prices of 10, 20 and 5 buys 35 shares for 300, an average cost of 8.5714 per share. That is the harmonic mean of the three prices, and it is 3.0952 below the arithmetic mean of 11.6667.

Calculator

Harmonic mean

49.0909

Arithmetic mean
60
Geometric mean
54.5136
Sum of the reciprocals
0.0611
Harmonic over arithmetic
0.8182

Values update as you type. This calculator covers the single scenario its formula assumes — see Common Mistakes for what it leaves out.

Prefer a full-width tool? Open the A Harmonic Mean calculator page.

Common Mistakes

  • Averaging speeds with the arithmetic mean

    Equal distances at 30 and 60 miles per hour average to 40, not 45. The trip spends twice as long at the slow speed, and the arithmetic mean ignores that.

  • Feeding in a zero

    One over zero is undefined, so a single zero in the list makes the whole mean undefined. Filter zeros out or switch to a different average.

  • Using negative values

    A negative reciprocal can cancel a positive one and leave a denominator near zero, which produces an absurd answer. The harmonic mean is defined for positive values only.

  • Forgetting to divide the count

    The numerator is n, not 1. Reporting 1 divided by 0.061111 gives 16.3636 instead of 49.0909, and the result no longer sits between the smallest and largest values.

  • Averaging the reciprocals instead of summing them

    The reciprocals are added, then the count divides that sum. Dividing the sum by n first gives 0.02037, whose reciprocal has no meaning as an average of the original values.

  • Reaching for it on plain measurements

    Heights, weights and prices are additive. The harmonic mean of 30, 60 and 90 is 49.0909, which badly understates three numbers that plainly average 60.

FAQ

What is the harmonic mean formula?

Divide the number of values by the sum of their reciprocals. For three values that is 3 divided by one over x1 plus one over x2 plus one over x3.

When should I use the harmonic mean instead of the arithmetic mean?

Whenever the values are rates and the denominator is what adds up, such as speeds over equal distances, prices per share when you invest a fixed amount, or precision and recall.

Why is the harmonic mean always the smallest of the three averages?

Because taking reciprocals gives the small values the most weight. The arithmetic mean of the reciprocals is at least the reciprocal of the arithmetic mean, so flipping back lands below it.

What is the harmonic mean of two numbers?

Twice the product divided by the sum. For 30 and 60 that is 3600 divided by 90, which is 40, the true average speed of a round trip at those two speeds.

Can the harmonic mean handle zero or negative values?

No. A zero makes a reciprocal undefined, and negative values can cancel in the denominator and give a meaningless result. Restrict it to strictly positive numbers.

How is the harmonic mean different from the geometric mean?

The geometric mean multiplies and takes a root, which suits growth factors. The harmonic mean uses reciprocals, which suits rates. For 30, 60 and 90 they give 54.5136 and 49.0909 respectively.

References

  1. [1]Wikipedia, Harmonic mean — https://en.wikipedia.org/wiki/Harmonic_mean
  2. [2]Wikipedia, F-score — https://en.wikipedia.org/wiki/F-score
  3. [3]Wikipedia, Dollar cost averaging — https://en.wikipedia.org/wiki/Dollar_cost_averaging