Harmonic Mean
The harmonic mean of \(n\) positive numbers is the reciprocal of the arithmetic mean of their reciprocals:
\[ H = \frac{n}{\frac{1}{x_1} + \frac{1}{x_2} + \dots + \frac{1}{x_n}} \]
For two numbers it simplifies to \(\frac{2ab}{a+b}\), and it always sits at the bottom of the standard ordering: \(HM \leq GM \leq AM\), with equality only when the numbers are identical.
It is described as “giving more weight to smaller values.” That is right, and the weight is heavier than the phrase suggests.
How much the small value dominates
import numpy as np
def means(a, b):
return (a + b) / 2, np.sqrt(a * b), 2 * a * b / (a + b)
print(f"{'precision':>10} {'recall':>8} {'arithmetic':>11} {'geometric':>10} {'harmonic (F1)':>14}")
for p, r in ((0.90, 0.90), (0.95, 0.85), (0.99, 0.70), (0.99, 0.20), (0.99, 0.02)):
a, g, h = means(p, r)
print(f"{p:>10.2f} {r:>8.2f} {a:>11.4f} {g:>10.4f} {h:>14.4f}")
distance = 60.0
print(f"\naverage speed over {distance:.0f} km at 60 km/h then {distance:.0f} km at 120 km/h")
total_time = distance / 60 + distance / 120
print(f" total distance {2 * distance:.0f} km in {total_time:.1f} h, so the true average is {2 * distance / total_time:.1f} km/h")
print(f" arithmetic mean of the two speeds {means(60, 120)[0]:.1f} km/h harmonic mean {means(60, 120)[2]:.1f} km/h")
print(f"\nthe same two speeds, but 60 km at 60 km/h then 600 km at 120 km/h")
t2 = 60 / 60 + 600 / 120
print(f" true average {660 / t2:.1f} km/h - the harmonic mean of the speeds is still {means(60, 120)[2]:.1f}")
# precision recall arithmetic geometric harmonic (F1)
# 0.90 0.90 0.9000 0.9000 0.9000
# 0.95 0.85 0.9000 0.8986 0.8972
# 0.99 0.70 0.8450 0.8325 0.8201
# 0.99 0.20 0.5950 0.4450 0.3328
# 0.99 0.02 0.5050 0.1407 0.0392
#
# average speed over 60 km at 60 km/h then 60 km at 120 km/h
# total distance 120 km in 1.5 h, so the true average is 80.0 km/h
# arithmetic mean of the two speeds 90.0 km/h harmonic mean 80.0 km/h
#
# the same two speeds, but 60 km at 60 km/h then 600 km at 120 km/h
# true average 110.0 km/h - the harmonic mean of the speeds is still 80.0
At 0.90 and 0.90 the three means agree. At 0.95 and 0.85 they differ in the third decimal. The divergence only becomes interesting when the two numbers do.
Precision 0.99 with recall 0.20 gives an arithmetic mean of 0.5950 and a harmonic mean of 0.3328. Precision 0.99 with recall 0.02 gives 0.5050 and 0.0392. In the last row the 0.99 has essentially stopped contributing: the harmonic mean of \(a\) and a very small \(b\) approaches \(2b\), regardless of how large \(a\) is.
This is exactly the property wanted from F1. A model that flags three emails, all of them spam, has perfect precision and useless recall, and an arithmetic mean would report it as half-decent. The harmonic mean reports it as nearly worthless, which is the honest summary — a classifier has to be adequate at both to be adequate at all.
\[ F_1 = \frac{2}{\frac{1}{\text{precision}} + \frac{1}{\text{recall}}} \]
It also means F1 embeds an assumption: that precision and recall matter equally. Where they do not — a screening test where a miss is far worse than a false alarm — \(F_\beta\) generalizes it, with \(\beta\) setting how many times more recall is worth than precision. Reaching for F1 without asking whether the costs are symmetric is the same omission as leaving the decision threshold at 0.5.
The speed example, with its condition
Driving 60 km at 60 km/h and then 60 km at 120 km/h, the average speed is not 90 km/h. Total distance 120 km, total time 1.5 hours, so it is 80.0 km/h — and the harmonic mean of the two speeds is 80.0. This is the standard illustration of why rates should be averaged harmonically.
The condition usually goes unstated. The harmonic mean gives the right answer because the two distances are equal. Drive 60 km at 60 km/h and then 600 km at 120 km/h and the true average is 110.0 km/h, while the harmonic mean of the speeds is still 80.0.
Unequal segments need a weighted harmonic mean, with each rate weighted by the distance it covered. The general rule underneath is that averaging rates correctly means summing the numerators and the denominators separately and dividing once — total distance over total time — which is the same operation micro averaging performs on true and false positives.
Where to use which mean
- Arithmetic for quantities that add — costs, counts, durations.
- Geometric for quantities that multiply — growth rates, index ratios, compound returns.
- Harmonic for rates defined per unit of something, where the units are what you hold fixed — speed per distance, price-earnings ratios per unit of earnings, precision and recall per unit of truth.
The reciprocal formula is not directly defined when an input is zero. With the other inputs positive and fixed, its limit as one input approaches zero from above is zero, so the usual zero extension agrees with that limit. For F1, use the count-based denominator to distinguish a zero score from an entirely absent class requiring a convention.
Macro averaging also requires an explicit policy for absent classes.
References
Discover more from Insightful Data Lab
Subscribe to get the latest posts sent to your email.
