python - Logarithmic returns in pandas dataframe -


python pandas has pct_change function use calculate returns stock prices in dataframe:

ndf['return']= ndf['typicalprice'].pct_change() 

i using following code logarithmic returns, gives exact same values pct.change() function:

ndf['retlog']=np.log(ndf['typicalprice'].astype('float64')/ndf['typicalprice'].astype('float64').shift(1)) #np numpy 

here 1 way calculate log return using .shift(). , result similar not same gross return calculated pct_change(). can upload copy of sample data (dropbox share link) reproduce inconsistency saw?

import pandas pd import numpy np  np.random.seed(0) df = pd.dataframe(100 + np.random.randn(100).cumsum(), columns=['price']) df['pct_change'] = df.price.pct_change() df['log_ret'] = np.log(df.price) - np.log(df.price.shift(1))  out[56]:         price  pct_change  log_ret 0   101.7641         nan      nan 1   102.1642      0.0039   0.0039 2   103.1429      0.0096   0.0095 3   105.3838      0.0217   0.0215 4   107.2514      0.0177   0.0176 5   106.2741     -0.0091  -0.0092 6   107.2242      0.0089   0.0089 7   107.0729     -0.0014  -0.0014 ..       ...         ...      ... 92  101.6160      0.0021   0.0021 93  102.5926      0.0096   0.0096 94  102.9490      0.0035   0.0035 95  103.6555      0.0069   0.0068 96  103.6660      0.0001   0.0001 97  105.4519      0.0172   0.0171 98  105.5788      0.0012   0.0012 99  105.9808      0.0038   0.0038  [100 rows x 3 columns] 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -