Home > Blockchain >  Calculation of the geometric mean from closing prices | Python | Refintiv
Calculation of the geometric mean from closing prices | Python | Refintiv

Time:06-04

I am trying to calculate the geometric mean from the closing prices of an index. I get the data also Refinitiv as follows:

msci_usa_growth = ek.get_timeseries(['.dMIUS0000GGUS'], start_date='2005-01-01', end_date='2022-05-01',interval="yearly")

And so I try to calculate the gmean:

stats.gmean(msci_usa_growth.loc[:, "CLOSE"])

The courses are in the column CLOSE. I will try to attach an image of the table.

However, I always get the following error message:

'float' object has no attribute 'log' or TypeError: loop of ufunc does not support argument of type float which has no callable log method

enter image description here

CodePudding user response:

I'm not getting an error on a test dataset. Does the following work for you? Maybe there is something unique about your data? Maybe you're importing gmean differently?

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


df = pd.DataFrame({
    'open':[24, 19, 58, 32, 93, 63, 91, 28, 41,  6],
    'close':[2339.42, 1198.09, 2525.13, 514.43, 172.33, 2381.69, 2008.74, 1561.23, 2693.69, 2237.18]
})

print(gmean(df.loc[:,'close']))
#1396.655496870409

CodePudding user response:

thanks a lot that works for me. It is probably related to how I pull the data from refinitv. My guess is that it is because of the date column.

Other question/problem: With the DataFrame I created above, how do I handle negative numbers? If I calculate with negative numbers, then I get as result the value "nan".

Many thanks. BR David

  • Related