Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logarithmic returns in pandas dataframe

Tags:

python

pandas

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_change() 

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

ndf['retlog']=np.log(ndf['TypicalPrice'].astype('float64')/ndf['TypicalPrice'].astype('float64').shift(1)) #np is for numpy 
like image 589
AmanArora Avatar asked Jul 08 '15 08:07

AmanArora


People also ask

What is the log return in Python?

Log returns are simply the natural log of 1 plus the arithmetic return. So how about this? df['pct_change'] = df.price.pct_change() df['log_return'] = np.log(1 + df.pct_change)

How do you log a DataFrame in Python?

After the dataframe is created, we can apply numpy. log2() function to the columns. In this case, we will be finding the logarithm values of the column salary. The computed values are stored in the new column “logarithm_base2”.

What is logarithmic return?

Logarithmic return is also called the continuously compounded return. This means that the frequency of compounding does not matter, making returns of different assets easier to compare.

How to find natural log and logarithmic value of a column in pandas?

Log and natural logarithmic value of a column in pandas python is carried out using log2 (), log10 () and log ()function of numpy. Let’s see how to

How do I get the log of a Dataframe column in Python?

df1 = pd.DataFrame (df1,columns=['Name','University_Rank']) Natural log of the column (University_Rank) is computed using log () function and stored in a new column namely “log_value” as shown below.

How do I calculate the returns for stock prices in Python pandas?

Python pandas has a pct_change function which I use to calculate the returns for stock prices in a dataframe: I am using the following code to get logarithmic returns, but it gives the exact same values as the pct.change () function: Here is one way to calculate log return using .shift ().

Where are the natural logarithm values stored in Python?

In this case, we will be finding the natural logarithm values of the column salary. The computed values are stored in the new column “natural_log”. Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.


1 Answers

Here is one way to calculate log return using .shift(). And the result is similar to but not the same as the gross return calculated by pct_change(). Can you upload a copy of your sample data (dropbox share link) to reproduce the inconsistency you saw?

import pandas as pd import numpy as 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] 
like image 181
Jianxun Li Avatar answered Sep 18 '22 19:09

Jianxun Li