Arithmetic mean or geometric mean ?
A quick sympy/numpy toy exercice.
4 min readDec 21, 2023
In this short post I want to show few things about arithmetic mean and geometric mean. We’ll use 2 variables that span the [0–1] segments. See this post as a toy snippet for sympy, numpy and matplotlib.
Analytical solution with sympy
Let’s first use sympy to check in which case one is greater that the other
import sympy as sp
x, y = sp.symbols("x, y")
geom = sp.sqrt(x * y)
arit = (x + y)/2
display(geom) # jupyter display function
display(arit)
Now let’s create a new equation as the difference between them:
poly = arit - geom
display(poly>=0)
Now lets plot the xy plane and highlight a blue zone where the artihmetic mean is greater than the geometrical mean.
sp.plot_implicit(poly>=0, x_var=(x, -0.1, 1.1), y_var=(y, -0.1, 1.1))
So sympy tells use that the geometric mean is always smaller than the aritmetic mean.