Here is a simple Python implementation of the arithmetic, geometric, and harmonic means of a (non-empty) list of numbers:
import functools
import math
import operator
def arithmetic_mean(arr):
return sum(arr) / len(arr)
def geometric_mean(arr):
return functools.reduce(operator.mul, arr, 1) ** (1 / len(arr))
def harmonic_mean(arr):
return len(arr) / sum(1 / x for x in arr)
Note that the harmonic mean is undefined if any value is zero, and the geometric mean may be complex if some values are negative. So for the sake of simplicity, assume that all numbers are positive.
If you put the three means into a list, calculate the same three means on that list, and do it repeatedly, it seems that the means will always converge to one number. For example, starting with a list of integers from 1 to 10, you get $4.396721093094297$.
>>> arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> while True:
... a = arithmetic_mean(arr)
... g = geometric_mean(arr)
... h = harmonic_mean(arr)
... print(f'a = {a}, g = {g}, h = {h}')
... arr = [a, g, h]
... if len(set(arr)) == 1:
... break
...
a = 5.5, g = 4.528728688116765, h = 3.414171521474055
a = 4.480966736530273, g = 4.39752289694313, h = 4.313272842437052
a = 4.397254158636819, g = 4.396721125488126, h = 4.396188059783766
a = 4.396721114636237, g = 4.396721093094297, h = 4.396721071552359
a = 4.396721093094297, g = 4.396721093094297, h = 4.396721093094297
I will call this value the iterated triple mean of arr.
I'm wondering: Is there a formula to obtain the iterated triple mean “directly”, without using an iterative or recursive definition?