In school, we are first taught the arithmetic mean, then later the geometric mean, and maybe one or two others like the harmonic/contraharmonic mean.

But most teachers do not teach the logic or derivation behind these means. They merely provide the formulas and perhaps provide some scenarios where each mean would be more useful than the others.

I’d like to illustrate the definition of a mean, in the most abstract sense, from a programmer’s perspective, an algorithmic point of view.

A Balancing Act

Suppose we have a list of N numbers, and a binary operation $f(a,b)$

If we apply that binary operation to one number from the list, we must apply its inverse operation to another number from the list.

We can continue applying $f$ to one element then $f^{-1}$ to another element as many times as we need to, until every number in the list is equal. That number that all elements are equal to…is the mean!

Means are just balancing acts!

** I am aware we haven’t specified the most optimized way to perform this algorithm to ensure it halts.

Example

let our list be $(2,4,6)$ and our $f(a,b)=a+b$

then we apply the operation and its inverse to two elements respectively:

$(f(2,2), 4 ,f^{-1}(6,2)) = (2+2, 4 ,6-2) = (4,4,4)$

So our mean is 4. We just performed the arithmetic mean!

What is it good for?

Well, given ANY invertible symmetric binary function, we can define a mean and derive its “shortcut” formula!

Here are the functions that define the means we usually come across:

arithmetic mean: $f(a,b) = a+b$

geometric mean: $f(a,b) = ab$

geometric mean (alternative definition): $f(a,b) = ln(a) + ln(b)$

harmonic mean: $f(a,b) = 1/(1/a + 1/b)$

Favoring Large or Small Numbers

Now, most of us learn that geometric mean tends to favor smaller numbers.

For example, the arithmetic mean of 1 and 100 is $50\frac{1}{2}$. But the geometric mean of 1 and 100 is 10.

Knowing what we do now, can we create a mean that favors smaller numbers even more?

I don’t know if these already exist in the formal mathematics universe, ie. books or published papers. So let me know if you find a previous name for them!

Franken-mean: $f(a,b) = a^b b^a$

Franken-mean v2: $f(a,b) = a^b + b^a$

You might ask if we can skew our mean toward smaller numbers even more than our above exponential mean. We sure can. Using something called Tetration, simply put – it is iterated exponentiation. There are even operations that encompass iterated tetration and so on…these kind of operations are called hyperoperations. So we can make a mean skewed toward lows or highs as much as we want, and it’s beautiful isn’t it? Before different means are taught, the theory of what defines a mean should really be taught.

But before I forget, you must be saying: “Where’s my shortcut formulas?!!”

Okay okay.

Deriving the Closed Formulas

Let’s derive the arithmetic mean first. Assume we have a list of 4 elements, ${x,y,z,t}$

Given the binary symmetric function that defines the arithmetic mean $f(a,b)=a+b$,

we let $g(a) = f(a,f(a,f(a,a))) = a + a + a + a = 4a$

Our formula for the mean of the 4 numbers in our list will be:

$g^{-1}(f(x,f(y,f(z,t))))$

We know $g(a) = 4a$, so $g^{-1}(a) = a/4$

That means our mean is $(x+y+z+t)/4$, which is what we expected from the arithmetic mean

Above was just a demonstration for a fixed size list of 4 elements. The concept is the same for any sized list. We can use induction to extrapolate what the formula will be for a list of n elements, $(x+y+z+t…….+w)/n$

Mathematica code for the generalized mean

GeneralizedMean[l_List, f_Function] := ( InverseFunction[ (Fold[f, ConstantArray[#, Length[l]]]) & ] [Fold[f, l]] );

Arithmetic Mean In:= GeneralizedMean[{x,y,z,t},(#+#2)&] Out= 1/4 (t+x+y+z)

Geometric Mean In:= GeneralizedMean[{x, y, z, t}, (#*#2) &] Out= (t x y z)^(1/4)

Harmonic Mean In:= GeneralizedMean[{x, y, z, t}, (1/(1/# + 1/#2)) &] Out= 4/(1/t + 1/x + 1/y + 1/z)

Franken Mean v1 In:= GeneralizedMean[{x, y}, (#^#2 * #2^#) &] Out= Log[x^y y^x]/(2 ProductLog[1/2 Log[x^y y^x]])

I had to use 2 variables for the above, because Mathematica’s symbolic inverses aren’t strong enough.

Franken Mean v2 does not even compute with symbolics or even numbers.

I will eventually work on this to hopefully get at least numerics to work nomatter what.

Alternative Implementation

This is an alternative implementation which seems to yield better results in some cases:

GeneralizedMean[l_List, f_Function] := ( Module[{x}, Solve[Fold[f, l] == Fold[f, ConstantArray[x, Length[l]]], x][[-1, 1, 2]] ]);

Create your own means!

Enjoy creating your own means! Number theoretic means on lists of primes and that sort of stuff should be very interesting to explore, cheers!

Continued in:

P.S.

Kolmogorov actually did something similar and came up with what’s called the Generalized f-mean

But he restricted his construct to only functions of the form $f(a,b) = g(a) + g(b)$, where g is the function that determines the mean and is to be specified.

Like this: Like Loading...