Simple Returns using Python
Hi MLEnthusiasts! Today, we’ll be learning the concept of simple rate of return and how to calculate simple returns using python. And with this article Financial Analytics: Part 1 we are starting are Financial Analytics tutorial series. Through this series you will learn all aspects of Financial Analytics & will become pro to it.
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.
Calculating Simple Returns using Python
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()
Calculating Simple Rate of Return
#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 Simple Returns using Python
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.
To understand more concepts of Financial Analytics, please go through our next article Financial Analytics: Part 2
If learning from videos is your thing, than you should check our YouTube channel Ml for Analytics.
Stay Tuned! Keep Learning!
6 thoughts on “Simple Returns using Python: FA1”