Logarithmic returns in pandas dataframe

后端 未结 5 1205
悲&欢浪女
悲&欢浪女 2020-12-07 15:45

Python pandas has a pct_change function which I use to calculate the returns for stock prices in a dataframe:

ndf[\'Return\']= ndf[\'TypicalPrice\'].pct_chan         


        
5条回答
  •  -上瘾入骨i
    2020-12-07 16:17

    The results might seem similar, but that is just because of the Taylor expansion for the logarithm. Since log(1 + x) ~ x, the results can be similar.

    However,

    I am using the following code to get logarithmic returns, but it gives the exact same values as the pct.change() function.

    is not quite correct.

    import pandas as pd
    
    df = pd.DataFrame({'p': range(10)})
    
    df['pct_change'] = df.pct_change()
    df['log_stuff'] = \
        np.log(df['p'].astype('float64')/df['p'].astype('float64').shift(1))
    df[['pct_change', 'log_stuff']].plot();
    

    enter image description here

提交回复
热议问题