Section 11.39

Geometric mean

Definition

The geometric mean of nn positive numbers is the nnth root of their product. It is the average for quantities that combine by multiplication, such as ratios and growth factors: replacing every number by the geometric mean leaves the product unchanged, just as replacing every number by the arithmetic mean §11.12 (the ordinary average) leaves the sum unchanged. Equivalently, it is the result of averaging the logarithms of the numbers and converting back. For ratios it treats a doubling and a halving as equal and opposite changes.

The idea

A new compiler makes benchmark A run in half the time and benchmark B run in twice the time. Measured as speedup §18.1.13, old time divided by new time, the results are 2 and 0.5. Their arithmetic mean is (2+0.5)/2=1.25(2 + 0.5)/2 = 1.25, a 25% average speedup. Now describe the same data the other way round, as the old compiler’s speedup over the new one: 0.5 and 2, whose arithmetic mean is again 1.25. Each compiler appears 25% faster than the other, which is impossible. The geometric mean of 2 and 0.5 is 2×0.5=1=1\sqrt{2 \times 0.5} = \sqrt{1} = 1 in both directions: no change on balance.

The reason is that ratios live on a multiplicative scale. Doubling and halving are opposite steps of the same size: in powers of two, 2 is 212^{1} and 0.5 is 2−12^{-1}. The arithmetic mean adds the ratios themselves, and on that scale the step from 1 up to 2 is +1+1 while the step from 1 down to 0.5 is only −0.5-0.5, so increases outweigh decreases. The geometric mean averages the exponents, +1+1 and −1-1, to 0, and converts back: 20=12^{0} = 1.

The geometric mean answers one question: what single factor, applied to every benchmark alike, gives the same combined effect. How much faster a fixed mix of programs runs as a whole is a different question, and it needs the actual times.

How it works

  1. Fix one direction for every ratio, for example old time divided by new time, so that values above 1 mean the new system is faster.

  2. Check that every ratio is positive; logarithms, and with them the geometric mean, exist only for positive numbers.

  3. Multiply the nn ratios and take the nnth root. With many ratios, average their logarithms instead and raise the base to that average (with base-2 logarithms, compute 2 to the power of the average); this avoids a product too large or too small for the computer’s number format.

  4. Report the individual ratios alongside the summary, and state the set of benchmarks and any weights.

  5. For uncertainty, compute a confidence interval §11.18 for the mean of the logarithms and convert both ends back; the resulting interval is not symmetric around the geometric mean.

Three properties make the geometric mean the natural summary of normalized results, that is, results expressed as ratios to a reference. Reversing the direction of every ratio turns the geometric mean into its reciprocal. The geometric mean of the ratios ai/bia_i / b_i equals the geometric mean of the aia_i divided by the geometric mean of the bib_i; so when two systems are each compared with a reference machine, their ranking does not depend on which reference machine was chosen. And the geometric mean of the speedups is the single speedup that, applied to every benchmark, gives the same product of speedups. The Standard Performance Evaluation Corporation (SPEC) summarizes its SPEC CPU benchmark results this way: each benchmark’s ratio of a reference machine’s time to the measured time, combined by the geometric mean.

Worked example

Four benchmarks take 10, 10, 40 and 20 seconds on the old system and 5, 20, 10 and 20 seconds on the new one. The speedups, old time divided by new time, are 10/5=210/5 = 2, 10/20=0.510/20 = 0.5, 40/10=440/10 = 4 and 20/20=120/20 = 1.

Choice of reference machine. System X takes 2 and 10 seconds on two benchmarks; system Y takes 4 and 5 seconds. Normalizing divides each time by the reference system’s time on the same benchmark, so lower is faster. Normalized to X, Y’s times are 4/2=24/2 = 2 and 5/10=0.55/10 = 0.5, with arithmetic mean 1.25 against X’s 1, so X looks faster. Normalized to Y, X’s times are 2/4=0.52/4 = 0.5 and 10/5=210/5 = 2, with arithmetic mean 1.25 against Y’s 1, so Y looks faster. The geometric mean of the other system’s normalized times is 2×0.5=1\sqrt{2 \times 0.5} = 1 with either reference: a tie, whichever reference is chosen. The total time, 2+10=122 + 10 = 12 seconds for X and 4+5=94 + 5 = 9 seconds for Y, favors Y and answers a different question.

Stated precisely

Let r1,…,rnr_1, \dots, r_n be positive numbers. Their geometric mean is G(r)=(∏i=1nri)1/n=exp(1n∑i=1nlnri),G(r) = \left(\prod_{i=1}^{n} r_i\right)^{1/n} = \exp\left(\frac{1}{n}\sum_{i=1}^{n} \ln r_i\right), where ∏\prod denotes the product of the terms, ln\ln is the natural logarithm and exp(y)=ey\exp(y) = e^{y} is its inverse. Any base of logarithm gives the same result if the same base is used to convert back. With weights wi≥0w_i \ge 0 that sum to 1, the weighted geometric mean is ∏i=1nriwi\prod_{i=1}^{n} r_i^{\,w_i}. For positive numbers aia_i and bib_i:

  1. Reciprocal. G(1/r)=1/G(r)G(1/r) = 1/G(r), because ∏i(1/ri)=1/∏iri\prod_i (1/r_i) = 1/\prod_i r_i.

  2. Ratios. G(a/b)=G(a)/G(b)G(a/b) = G(a)/G(b), because ∏i(ai/bi)=∏iai/∏ibi\prod_i (a_i/b_i) = \prod_i a_i / \prod_i b_i.

  3. Bounds. miniri≤G(r)≤maxiri\min_i r_i \le G(r) \le \max_i r_i. The inequality of arithmetic and geometric means states G(r)≤A(r)G(r) \le A(r), where A(r)=1n∑iriA(r) = \frac{1}{n}\sum_i r_i, with equality only when all rir_i are equal. Also H(r)≤G(r)H(r) \le G(r), where H(r)=n/∑i(1/ri)H(r) = n / \sum_i (1/r_i) is the harmonic mean. For 2 and 0.5: A=1.25A = 1.25, G=1G = 1 and H=2/(0.5+2)=0.8H = 2/(0.5 + 2) = 0.8.

  4. Statistical meaning. If the logarithms of the ratios follow a normal distribution, the symmetric bell-shaped distribution of classical statistics, the geometric mean of a sample estimates the median §11.13 ratio.

These properties need four conditions: every value is positive; every ratio is taken in the same direction; every ratio compares the same quantity, such as running time; and the benchmarks are equally important, or their weights are stated.

Limits and common mistakes

Only positive values are allowed. A failed benchmark has no ratio, and a ratio of 0 makes the product 0. A percent change such as −30%-30\% must first become a ratio, 1−0.30=0.701 - 0.30 = 0.70; averaging percent changes directly is a common mistake.

The geometric mean is not total time. For a fixed workload in which each benchmark runs a known number of times, the overall speedup is total old time divided by total new time, the ratio of aggregates §11.40. The geometric mean can disagree with it (1.414 against 1.455 above) and can even point the other way: speedups of 2 on a 1-second benchmark and 100/110≈0.909100/110 \approx 0.909 on a 100-second benchmark have a geometric mean of 2×0.909≈1.35\sqrt{2 \times 0.909} \approx 1.35, yet the total time grows from 1+100=1011 + 100 = 101 to 0.5+110=110.50.5 + 110 = 110.5 seconds.

A summary can hide a regression. Speedups of 10, 10 and 0.01 multiply to 1, so their geometric mean is 1, “no change”, while one benchmark became 100 times slower. Show the individual ratios.

The choice of benchmarks is a choice of weights: adding three near-copies of one benchmark gives its behavior three votes.

Raw times and rates need other means. For rates measured on equal amounts of work, such as operations per second, total work divided by total time is the harmonic mean of the rates; for programs run equally often, the total time is the sum of their times.

Questions answered

Why is it often preferable to an arithmetic mean for speedup ratios?

The geometric mean, the nnth root of the product of nn numbers, is often preferable for speedup ratios because ratios combine by multiplication, and the geometric mean treats a doubling and a halving as opposite changes of equal size. As a result it gives consistent answers whichever way the ratios are written: the geometric mean of the reversed ratios (new time over old time instead of old over new) is exactly the reciprocal. And when results are normalized to a reference machine, the ranking of two systems does not depend on which reference was chosen. The arithmetic mean has neither property. For example, speedups of 2 and 0.5 have arithmetic mean 1.25, and the reversed ratios 0.5 and 2 also have arithmetic mean 1.25, so each system appears 25% faster than the other; the geometric mean is 2×0.5=1\sqrt{2 \times 0.5} = 1 in both directions.

The geometric mean answers the question “what is the typical multiplicative change per benchmark, with every benchmark counted equally”. If the question is how much faster a fixed workload runs as a whole, divide the total old time by the total new time instead. In both cases show the individual ratios, because a single large regression can disappear inside a summary.

Related entries: Effect size (§11.24), Ratio of aggregates versus aggregate of ratios (§11.40), Sample mean (§11.12).