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.
#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.
#Importing the data
PG = dr.DataReader('PG', data_source='yahoo', start='1995-1-1')
#Displaying the head of the dataframe PG. head() displays first 5 rows by default.
PG.head()
#Displaying the tail of the dataframe PG. tail() displays last 5 rows by default.
PG.tail()
#pandas.Dataframe.shift(# lags)
#Using shift(1), we can get the row just above the present row. Here, # lags is 1.
PG['Simple Return'] = (PG['Adj Close']/PG['Adj Close'].shift(1))-1
print(PG['Simple Return'])
Plotting the Simple Return series using matplotlib.pyplot
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.
daily_avg_returns = PG['Simple Return'].mean()
daily_avg_returns
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.
annual_avg_returns = PG['Simple Return'].mean() * 250
annual_avg_returns
Annual average returns percent for PG is given by the following code.
print(str(round(annual_avg_returns*100, 2))+' %')
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.