Reference · Statistics

stats.geometric_mean

signature
stats.geometric_mean(list) -> float

What it does

The geometric mean of a list of positive numbers: the n-th root of their product, computed through logarithms as Python's statistics.geometric_mean does. It is the mean to use for growth factors and ratios: the geometric mean of yearly growth factors is the steady factor that gives the same total growth.

Examples

print(stats.geometric_mean([1, 4, 16]))print(stats.geometric_mean([54, 24, 36]))print(stats.geometric_mean([1.05, 1.1, 0.95]))
output
4.036.0000000000000141.031419164168326

Edge cases

print(stats.geometric_mean([7]), stats.geometric_mean([7]) | round(6))print(stats.geometric_mean([2, 8.0]))
output
6.999999999999999 7.04.0

Errors

No data, a zero, a negative number or something that is not a number are each a runtime error, not an error value.

this one fails
print(stats.geometric_mean([1, 0, 4]))
output
line 1: `stats.geometric_mean` requires positive numbers
this one fails
print(stats.geometric_mean([]))
output
line 1: `stats.geometric_mean` requires at least one data point

Chaining

let returns = [0.05, 0.10, -0.05]print(returns | map((r) => 1 + r) | stats.geometric_mean | round(4))
output
1.0314

See also

stats.mean, stats.harmonic_mean, math.prod

Thread
8Useful? Like it, or add to its thread below.

Thread 0 posts

On stats.geometric_mean(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo