Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
rolling geometric mean - I think I have an approach

Does anyone know if there is python function to compute a rolling geometric mean? Or a way to modify the rolling method to include a geometric mean option?
thanks
I no longer need help on this one although solutions are welcome. I found out that scipy.stats module has a geomean module, I can figure it out from here.

4 responses

Hello,

Hope it'll help !

import numpy as np  
import pandas as pd  
from scipy.stats.mstats import gmean

x = np.random.uniform(0,1,260)  
geo_mean = pd.Series(x).rolling(window=30).apply(gmean)  

Thank you so much Mathieu!

Another easy way to work with geomean is just take the mean of the logarithms.

Thanks Tony. This advice may come in handy as I will probably have to create a loop and I think this method will help.