Uncategorized

# Financial Analytics: Part 1

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

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

## Simple Rate of Return¶

According to investopedia, Simple Rate of Return is defined as the net gain or loss on an investment over a specified time period, expressed as a percentage of the investment’s initial cost. Gains on investments are defined as income received plus any capital gains realized on the sale of the investment.

The formula comes out to be

It is often defined as basic growth rate or Return on Investment.

Let’s start our analysis by importing the required packages for this analysis.

In [22]:
```#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 dr

#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 [23]:
```#Importing the data
PG = dr.DataReader('PG', data_source='yahoo', start='1995-1-1')
```
In [24]:
```#Displaying the head of the dataframe PG. head() displays first 5 rows by default.
```
Out[24]:
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 [25]:
```#Displaying the tail of the dataframe PG. tail() displays last 5 rows by default.
PG.tail()
```
Out[25]:
High Low Open Close Volume Adj Close
Date
2019-09-17 121.989998 119.440002 119.629997 121.160004 6377100.0 121.160004
2019-09-18 121.959999 120.769997 121.269997 121.410004 5684200.0 121.410004
2019-09-19 122.230003 121.239998 121.389999 121.900002 4525800.0 121.900002
2019-09-20 122.599998 121.750000 122.129997 122.239998 15362000.0 122.239998
2019-09-23 123.769997 121.970001 122.199997 123.220001 6060400.0 123.220001

## Calculating Simple Rate of Return¶

In [26]:
```#pandas.Dataframe.shift(# lags)
#Using shift(1), we can get the row just above the present row. Here, # lags is 1.

print(PG['Simple Return'])
```
```Date
1995-01-03         NaN
1995-01-04   -0.008016
1995-01-05   -0.014142
1995-01-06    0.002050
1995-01-09   -0.004090
1995-01-10    0.012321
1995-01-11   -0.002029
1995-01-12    0.010163
1995-01-13    0.029230
1995-01-16    0.008001
1995-01-17    0.003968
1995-01-18   -0.021739
1995-01-19   -0.004041
1995-01-20   -0.004057
1995-01-23    0.010183
1995-01-24   -0.002016
1995-01-25    0.014141
1995-01-26    0.003985
1995-01-27    0.027778
1995-01-30    0.015443
1995-01-31   -0.009505
1995-02-01   -0.019194
1995-02-02    0.007828
1995-02-03    0.009708
1995-02-06    0.023077
1995-02-07   -0.001879
1995-02-08   -0.011299
1995-02-09   -0.001904
1995-02-10   -0.001909
1995-02-13    0.007649
...
2019-08-12   -0.006422
2019-08-13    0.010515
2019-08-14   -0.012452
2019-08-15    0.013818
2019-08-16    0.015248
2019-08-19    0.008894
2019-08-20   -0.011144
2019-08-21    0.002523
2019-08-22    0.001846
2019-08-23   -0.017585
2019-08-26    0.017047
2019-08-27    0.010308
2019-08-28    0.007051
2019-08-29   -0.001812
2019-08-30   -0.007840
2019-09-03    0.009399
2019-09-04    0.015244
2019-09-05   -0.003652
2019-09-06    0.000896
2019-09-09   -0.005697
2019-09-10   -0.018744
2019-09-11    0.011595
2019-09-12    0.012369
2019-09-13   -0.005294
2019-09-16   -0.019407
2019-09-17    0.011775
2019-09-18    0.002063
2019-09-19    0.004036
2019-09-20    0.002789
2019-09-23    0.008017
Name: Simple Return, Length: 6225, dtype: float64
```

Plotting the Simple Return series using matplotlib.pyplot

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

## Calculating Daily Average Returns¶

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

In [28]:
```daily_avg_returns = PG['Simple Return'].mean()
daily_avg_returns
```
Out[28]:
`0.0005731805529415197`

## Calculating Annual Average Returns¶

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

In [29]:
```annual_avg_returns = PG['Simple Return'].mean() * 250
annual_avg_returns
```
Out[29]:
`0.14329513823537993`

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

In [30]:
```print(str(round(annual_avg_returns*100, 2))+' %')
```
```14.33 %
```

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