Uncategorized

# Financial Analytics – Part 2

Hi MLEnthusiasts! Today, we’ll be learning the concept of logarithmic rate of return and how to calculate it using python.

So, let’s get started by understanding the concept of Logarithmic Rate of Return.

As time progresses, the investments that we take part in increase and earn a compounded return. For bonds, the compound interest is fixed but for stocks, it varies. To calculate this continuously compounded interest, natural logarithm function is used by investors.

For bonds, the formula of log return is given by ln(1 + stated rate of interest)

For stocks, this becomes

The output of this formula results in the continuously compounded rate of return for the given time period.

Let’s begin our analysis by importing the required packages.

In [1]:
```#Numpy is the package for scientific computing with python. Array computations are made very easy by using this package.
import numpy as np

#Functions from pandas_datareader.data and pandas_datareader.wb extract data from various Internet sources into a pandas DataFrame.
from pandas_datareader import data as wb

#Using pyplot, we can get interactive plots and generate programmatic plots
import matplotlib.pyplot as plt
```

Now, let’s extract the historical prices for Procter & Gamble company from yahoo finance using DataReader.

In [2]:
```#Importing the data
```
In [3]:
```#Displaying the head of the dataframe PG. head() displays first 5 rows by default.
```
Out[3]:
High Low Open Close Volume Adj Close
Date
1995-01-03 15.62500 15.43750 15.46875 15.59375 3318400.0 6.441071
1995-01-04 15.65625 15.31250 15.53125 15.46875 2218800.0 6.389442
1995-01-05 15.43750 15.21875 15.37500 15.25000 2319600.0 6.299082
1995-01-06 15.40625 15.15625 15.15625 15.28125 3438000.0 6.311993
1995-01-09 15.40625 15.18750 15.34375 15.21875 1795200.0 6.286175
In [4]:
```#pandas.Dataframe.shift(# lags)
#Using shift(1), we can get the row just above the present row. Here, # lags is 1.
#log() is a function given in numpy package in python. It calculates the natural log of the value given inside it.

#print() function prints the value inside it on the console.
print(PG['Log Return'])
```
```Date
1995-01-03         NaN
1995-01-04   -0.008048
1995-01-05   -0.014243
1995-01-06    0.002047
1995-01-09   -0.004099
1995-01-10    0.012246
1995-01-11   -0.002031
1995-01-12    0.010112
1995-01-13    0.028811
1995-01-16    0.007969
1995-01-17    0.003960
1995-01-18   -0.021979
1995-01-19   -0.004049
1995-01-20   -0.004065
1995-01-23    0.010132
1995-01-24   -0.002018
1995-01-25    0.014042
1995-01-26    0.003977
1995-01-27    0.027399
1995-01-30    0.015325
1995-01-31   -0.009551
1995-02-01   -0.019381
1995-02-02    0.007798
1995-02-03    0.009661
1995-02-06    0.022815
1995-02-07   -0.001881
1995-02-08   -0.011364
1995-02-09   -0.001906
1995-02-10   -0.001910
1995-02-13    0.007619
...
2019-08-13    0.010460
2019-08-14   -0.012530
2019-08-15    0.013724
2019-08-16    0.015133
2019-08-19    0.008855
2019-08-20   -0.011207
2019-08-21    0.002520
2019-08-22    0.001844
2019-08-23   -0.017741
2019-08-26    0.016904
2019-08-27    0.010256
2019-08-28    0.007026
2019-08-29   -0.001814
2019-08-30   -0.007870
2019-09-03    0.009355
2019-09-04    0.015129
2019-09-05   -0.003659
2019-09-06    0.000896
2019-09-09   -0.005713
2019-09-10   -0.018922
2019-09-11    0.011528
2019-09-12    0.012293
2019-09-13   -0.005308
2019-09-16   -0.019598
2019-09-17    0.011706
2019-09-18    0.002061
2019-09-19    0.004028
2019-09-20    0.002785
2019-09-23    0.007985
2019-09-24    0.003160
Name: Log Return, Length: 6226, dtype: float64
```

Plotting the Simple Return series using matplotlib.pyplot

In [5]:
```PG['Log Return'].plot(figsize=(8,5))
plt.show()
```

## Calculating Daily Average Returns¶

Daily Average Returns are given by computing the mean of the log rate of return series.

In [6]:
```daily_log_returns = PG['Log Return'].mean()
daily_log_returns
```
Out[6]:
`0.0004746082918880089`

## Calculating Annual Average Returns¶

Annual Average Returns are given by computing the mean of the log rate of return series and then multiplying the value by 250 since 250 days exist in a business day system.

In [7]:
```annual_log_returns = PG['Log Return'].mean() * 250
annual_log_returns
```
Out[7]:
`0.11865207297200223`

Annual average returns percent for PG is given by the following code.

In [9]:
```print(str(round(annual_log_returns*100, 2)) +' %')
```
```11.87 %
```

So, guys, this was the second tutorial on how to apply python in the field of finance and trading. Stay tuned for more tutorials on this.