An inventory index grows 25% one month and shrinks 20% the next. The arithmetic average of those two rates is positive, but an index that started at 100 rose to 125 and then returned to 100. To summarize sequential changes with one constant rate, average their multiplicative factors, not the signed percentages.
Prerequisites and example
Use Python 3.12 and save this as growth.py. Pass rates as decimal fractions, such as 0.21 for 21%, and supply one rate per equal-length period. This example accepts finite rates above −100%, which guarantees positive growth factors.
from math import isclose, isfinite
from statistics import geometric_mean
def per_period_growth(rates):
factors = []
for rate in rates:
if not isfinite(rate) or rate <= -1:
raise ValueError("need finite rates above -100%")
factors.append(1 + rate)
if not factors:
raise ValueError("need at least one period")
return geometric_mean(factors) - 1
assert isclose(per_period_growth([0.25, -0.20]), 0, abs_tol=1e-15)
assert isclose(per_period_growth([0.21, 0]), 0.10)
for bad in [[], [-1], [float("nan")]]:
try:
per_period_growth(bad)
except ValueError:
continue
raise AssertionError(f"accepted {bad}")
print(f"Equivalent growth: {per_period_growth([0.21, 0]):.2%}")Run python3 growth.py. Expected output:
Equivalent growth: 10.00%Check the result by compounding it back
The second fixture has factors 1.21 and 1.00. Their product is 1.21. A constant factor of 1.10 applied twice also produces 1.21, so the equivalent rate is 10%. Subtract one after taking the mean to turn the factor back into a rate. Formatting with .2% multiplies by 100 for display and adds a percent sign. Do not multiply by 100 again before formatting.
A missing month is not a month of zero growth. Include an explicit zero only when the underlying index did not change. If periods have different lengths, an unweighted mean here has no consistent time unit. To derive a rate per time unit from observed factors, use the total product over the total elapsed time expressed in that unit, assuming those factors cover the whole interval without overlap. Do not use this shortcut for a series with outside additions or removals unless your rates already account for them.
A −100% period makes the factor zero. This helper rejects it rather than silently extending the positive-factor contract. If zero is meaningful for your metric, define how later growth from a zero base is handled before changing the implementation. For results near zero, compare with an absolute tolerance as the first assertion does. Keep full precision while calculating and round only at display time. If the resulting percentage is unexpectedly large, check first that inputs were supplied as fractions rather than percentage points.
Reference: Python 3.12 statistics.geometric_mean documentation. More data-science tutorials.