TL;DR
The harmonic mean of positive numbers is the number you get by taking the reciprocal (1 divided by the number) of each value, averaging those reciprocals, and then taking the reciprocal of that average. It solves the problem that an ordinary average of rates gives the wrong answer when the rates apply to equal amounts of distance, work or items. It matters because it is the average that the F1 score uses to combine precision and recall, and it is the correct way to average speeds over equal distances.
Builds on: Weighted Average · Leads to: the F1 score, HyperLogLog
Where it's used
Scoring classifiers. A spam filter, a supervised classifier, can be right on 90 percent of the messages it flags, yet find only 50 percent of the spam. The first number is its precision, the conditional probability that a flagged message really is spam. The second is its recall, and both are counted from a confusion matrix. Such a filter needs one number to be compared with other filters. The F1 score is the harmonic mean of precision and recall. It stays low unless both numbers are reasonable, so a filter cannot hide a poor recall behind a good precision.
Average speed over equal distances. A driver covers 60 kilometres at 60 km/h and then 60 kilometres back at 30 km/h. The average speed for the whole trip is not the simple average of 60 and 30, which is 45 km/h. The harmonic mean of the two speeds, 40 km/h, is the right answer, because the driver spent more time on the slow leg.
Counting distinct items. Algorithms that estimate how many different items a stream contains, such as the number of unique visitors to a website, need to combine many separate estimates into one. HyperLogLog does this with a harmonic mean, because it is less pulled around by a single large estimate than an ordinary mean would be.
How it works
Take one trip: 60 kilometres at 60 km/h, then 60 kilometres at 30 km/h. We want one speed that describes the whole trip.
- Write the two speeds: 60 and 30 km/h. Each is a rate, distance per hour.
- Take the reciprocal of each speed. and . Check: these are hours per kilometre, the time each kilometre takes.
- Average the reciprocals. . Check: the average time per kilometre is of an hour.
- Take the reciprocal of that average. . The harmonic mean is 40 km/h.
- Check against the trip itself. The first leg takes hour and the second takes hours. The total is 3 hours for 120 kilometres, and km/h. The two agree.
The reason the reciprocals help is that they turn "time per unit" into something that adds up. Averaging the times, not the speeds, is what the trip actually needs.
flowchart LR
A["Speeds: 60 km/h and 30 km/h"] --> B["Reciprocals: 1/60 and 1/30 hours per km"]
B --> C["Average of reciprocals: (1/60 + 1/30) / 2 = 1/40"]
C --> D["Reciprocal of the average: 1 / (1/40) = 40 km/h"]
The math
Let be positive numbers. Their harmonic mean is
The denominator is the sum of the reciprocals, so divided by that sum is the reciprocal of the average reciprocal. This is step 4 of the walkthrough written in symbols.
Step 2 in symbols: the reciprocals
Each reciprocal is . For the trip, and , which are the times per kilometre. The sum is the total time per kilometre across the legs.
Step 3 in symbols: the average of the reciprocals
Dividing the sum by gives . The average reciprocal is the reciprocal of the harmonic mean, which is why the final reciprocal in step 4 gives .
Two values
For two positive numbers and , the formula simplifies to
Check with the trip: .
Weights
If each value has a weight , the weighted harmonic mean is . It is the harmonic version of the weighted average: the weights count the amounts of each part of the job, and the reciprocals turn rates into times. Equal weights give the unweighted formula above. The weights play the same role as probabilities in an expected value: they say how much of the whole each value counts for.
Comparing with the arithmetic mean
For positive numbers that are not all equal, the harmonic mean is less than or equal to the arithmetic mean and to the geometric mean, which averages the logarithms of the values and then converts back. Check: for 60 and 30, the arithmetic mean is 45 and the geometric mean is , and .
Worked examples
Take and .
- Take the reciprocals: and .
- Average them: .
- Take the reciprocal: .
- Check with the two-value formula: .
- Compare: the arithmetic mean is , and .
Answer: the harmonic mean is 3. It is pulled toward the smaller value, 2, because the smaller value has the larger reciprocal.
A cyclist rides three legs of 120 kilometres each, at 40, 60 and 120 km/h.
- Take the reciprocals of the speeds: , and .
- Add them with a common denominator of 120: .
- Average over the three legs: .
- Take the reciprocal: . The harmonic mean is 60 km/h.
- Check with the trip: the legs take , and hours. The total is 6 hours for 360 kilometres, and km/h.
- Compare: the arithmetic mean of the speeds is , which is too high because the fast leg took only one hour.
Answer: the average speed for the whole ride is 60 km/h.
A fraud detector has precision 0.9 and recall 0.5. Its F1 score is the harmonic mean of the two.
- Write the two values: and .
- Take the reciprocals: and .
- Average them: .
- Take the reciprocal: .
- Check with the formula .
- Compare with the arithmetic mean of 0.7. The F1 score is lower, because the detector misses half of the fraud, and the harmonic mean keeps that weakness visible.
Answer: the F1 score is about 0.643.
Try it: a second detector has precision 0.5 and recall 0.2. Compute its F1 score. Answer: . Its arithmetic mean would be 0.35, which makes the poor recall look much less serious than it is.
Common mistakes and tradeoffs
It is tempting to think the harmonic mean is just the average of the reciprocals; in fact it is the reciprocal of that average. Stopping at step 3 gives for the trip, which is a time per kilometre and not a speed.
It is tempting to average speeds with the ordinary mean whenever you have speeds; in fact the ordinary mean is right when the time spent at each speed is equal, and the harmonic mean is right when the distance covered at each speed is equal. The trip in the walkthrough covers equal distances, so the harmonic mean is correct there.
It is tempting to think a single poor value does not matter much in an average. In fact the harmonic mean is pulled strongly toward the smallest value. A precision of 1.0 with a recall of 0.1 gives an F1 score of , not the 0.55 an ordinary average would give.
Use the harmonic mean for rates over equal amounts, and when you want a low value in any input to pull the result down. Do not use it when a value can be zero, because its reciprocal does not exist, or when values are additive quantities such as costs or counts; then the ordinary mean is right. For values that multiply, such as growth factors, the geometric mean fits better. A weighted F-score () gives one side of a classifier more say than the other when recall matters more than precision.
History
The harmonic mean is one of the three classical Pythagorean means, studied in ancient Greece alongside the arithmetic and geometric means. Its name comes from its links to musical ratios, which mattered to Greek number theory.
The F-measure, the family to which F1 belongs, was developed in the 1970s for evaluating information retrieval systems. It combines precision and recall with a harmonic mean, so that a system cannot score well by excelling at only one of them. Machine learning adopted it widely, and the F1 score is now a common summary for imbalanced classification problems.
Recap
- The harmonic mean is the reciprocal of the average of the reciprocals: .
- It is the right average for rates over equal amounts, such as speeds over equal distances, and it is pulled toward the smaller value.
- The F1 score is the harmonic mean of precision and recall, so a classifier needs both to score well.
- Read next: Geometric Mean is the average for values that multiply, and HyperLogLog uses the harmonic mean to estimate counts of distinct items.
Check yourself
QuestionRecall: how is the harmonic mean of positive numbers defined in words, and why is it not the same as the average of the reciprocals?Show answer
Take the reciprocal of each number, average those reciprocals, and then take the reciprocal of that average. The average of the reciprocals is one step earlier and is the reciprocal of the harmonic mean, so the two differ by a reciprocal. For the numbers 2 and 6, the average of the reciprocals is , while the harmonic mean is 3.
QuestionComputation: find the harmonic mean of 4 and 12.Show answer
The two-value formula gives . Check by reciprocals: and average to , and the reciprocal of is 6. The arithmetic mean is 8, and .
QuestionWhy: a runner goes up a hill at 6 km/h and down at 12 km/h over the same distance. Why is the average speed 8 km/h and not 9 km/h?Show answer
The distance is equal on both legs, so the harmonic mean is the right average. . The uphill leg takes longer, so the time spent at 6 km/h weighs more. The ordinary average of 9 km/h would assume equal time on each leg, which is not the case here.
QuestionApplied: a model has precision 0.95 and recall 0.05. Someone reports that its average of the two is 0.5 and calls it a good model. What is its F1 score, and what should you say about the report?Show answer
. The report is misleading. The ordinary average of 0.5 hides that the model finds only 5 percent of the real positives. The F1 score of about 0.095 shows the weakness, because the harmonic mean is pulled toward the smaller value.
Sources
- Wikipedia: Harmonic mean.
- Wikipedia: F-score.