Data Science DS Concepts DS Languages Python

Adjusted Returns including Inflation: FA7

Adjusted Returns including Inflation

Hi All! In the part 6 of Financial Analytics series, we learnt how to fetch historical financial data from multiple sources using Python. In this tutorial, we will learn the ways of considering inflation in a return series to obtain adjusted returns including inflation and implementing them using Python. We will be fetching data from quandl and yfinance in this tutorial, so I recommend you to read part 6 of Financial Analytics series first. Also, if you’re new to this series and want to learn Financial Analytics using Python, go to part 1 of Financial Analytics series to have a good head-start!

Adjusted Returns including Inflation – Formula

We have R’ = (1 + R)/(1 + I) – 1

Here, R = simple return series, I = inflation rate series, R’ = Return series with inflation

Step 1: Downloading Customer Price Index from Quandl

In [1]:
# Importing libraries
import pandas as pd
import quandl as qd
import yfinance as yf
In [2]:
key = 'key_comes_here'
In [3]:
#Key authentication - key to be fetched from https://www.quandl.com/account/profile after signing up
qd.ApiConfig.api_key = key
In [4]:
#Fetching CPI - Customer Price Index for USA from quandl 
USA_CPI = qd.get(dataset='RATEINF/CPI_USA', 
                  start_date='2008-04-01',
                  end_date='2018-03-31')
In [5]:
#Checking head of the dataframe
USA_CPI.head()
Out[5]:
Value
Date
2008-04-30 214.823
2008-05-31 216.632
2008-06-30 218.815
2008-07-31 219.964
2008-08-31 219.086
In [6]:
# Renaming Value column to CPI
# Calling rename function on USA_CPI and passing dictionary to columns having keys as old column names and values as new column names
USA_CPI.rename(columns = {'Value': 'CPI'}, inplace = True)
USA_CPI.head()
Out[6]:
CPI
Date
2008-04-30 214.823
2008-05-31 216.632
2008-06-30 218.815
2008-07-31 219.964
2008-08-31 219.086

Step 2: Fetching stocks data from yfinance

In [7]:
# Downloading MSFT data from yfinance from 1st April 2008 to 31st March 2018
msftStockData = yf.download( 'MSFT',
                        start = '2008-04-01',
                        end = '2018-03-31',
                        progress = False)
In [8]:
# Checking what's in there the dataframe by loading first 5 rows
msftStockData.head()
Out[8]:
Open High Low Close Adj Close Volume
Date
2008-03-31 27.879999 28.590000 27.840000 28.379999 21.455233 46780600
2008-04-01 28.830000 29.540001 28.629999 29.500000 22.301943 65796200
2008-04-02 29.570000 29.580000 29.000000 29.160000 22.044899 49499400
2008-04-03 29.000000 29.320000 28.799999 29.000000 21.923944 38961400
2008-04-04 29.129999 29.260000 28.740000 29.160000 22.044899 43860800

Step 3: Making a date dataframe

In [9]:
dates = pd.DataFrame(index=pd.date_range(start='2008-04-01',
                                         end='2018-03-31'))
dates.head()
Out[9]:
2008-04-01
2008-04-02
2008-04-03
2008-04-04
2008-04-05

Step 4: Joining dates and stock data

In [10]:
# Joining by calling join function on dates dataframe. 
# In how parameter, we specify which join we want to perform 
# how = 'left' performs left join in which all rows of dates are fetched and matching rows from msftStockData is returned
# Unmatched values have NaN or Not a Number
# We are only interested in Adjusted close column, so we will include only that column from the right table
Date_Stock = dates.join(msftStockData[['Adj Close']], how = 'left')
Date_Stock.head(10)
Out[10]:
Adj Close
2008-04-01 22.301943
2008-04-02 22.044899
2008-04-03 21.923944
2008-04-04 22.044899
2008-04-05 NaN
2008-04-06 NaN
2008-04-07 22.044899
2008-04-08 21.734947
2008-04-09 21.840784
2008-04-10 22.007105
In [11]:
# In order to handling missing values, let's use missing value imputation
# Importing simpleImputer class from sklearn.impute library
from sklearn.impute import SimpleImputer

#Inititalizing the object of SimpleImputer class
data_Imputer = SimpleImputer()

#Fitting and transforming the data using the data_Imputer object and then wrapping it around pd.DataFrame function to convert it into a dataframe from numpy array.
imputedData = pd.DataFrame(data_Imputer.fit_transform(Date_Stock))
imputedData.head(10)
Out[11]:
0
0 22.301943
1 22.044899
2 21.923944
3 22.044899
4 34.965513
5 34.965513
6 22.044899
7 21.734947
8 21.840784
9 22.007105
In [12]:
# Replacing values from imputedData to Adj Close column of Date_Stock
Date_Stock['Adj Close'] = imputedData[0].values
Date_Stock.head(10)
Out[12]:
Adj Close
2008-04-01 22.301943
2008-04-02 22.044899
2008-04-03 21.923944
2008-04-04 22.044899
2008-04-05 34.965513
2008-04-06 34.965513
2008-04-07 22.044899
2008-04-08 21.734947
2008-04-09 21.840784
2008-04-10 22.007105
In [13]:
# Selecting end of month rows only to do Month on Month analysis
Date_Stock = Date_Stock.asfreq('M')
Date_Stock.head(10)
Out[13]:
Adj Close
2008-04-30 21.561066
2008-05-31 34.965513
2008-06-30 20.874075
2008-07-31 19.515852
2008-08-31 34.965513
2008-09-30 20.332642
2008-10-31 17.011156
2008-11-30 34.965513
2008-12-31 14.909866
2009-01-31 34.965513

Step 5: Joining Stock and CPI data

In [14]:
# Left joining stock and USA inflation (Customer Price Index) data
Stock_Inflation = Date_Stock.join(USA_CPI, how = 'left')
Stock_Inflation.head()
Out[14]:
Adj Close CPI
2008-04-30 21.561066 214.823
2008-05-31 34.965513 216.632
2008-06-30 20.874075 218.815
2008-07-31 19.515852 219.964
2008-08-31 34.965513 219.086

Step 6: Calculating simple returns and inflation rate

In [15]:
#Calculating simple returns by pct_change() function of the dataframe
Stock_Inflation['Simple Return'] = Stock_Inflation['Adj Close'].pct_change()
Stock_Inflation.head()
Out[15]:
Adj Close CPI Simple Return
2008-04-30 21.561066 214.823 NaN
2008-05-31 34.965513 216.632 0.621697
2008-06-30 20.874075 218.815 -0.403010
2008-07-31 19.515852 219.964 -0.065067
2008-08-31 34.965513 219.086 0.791647
In [16]:
#Calculating inflation rate by pct_change() function of the dataframe
Stock_Inflation['Inflation rate'] = Stock_Inflation['CPI'].pct_change()
Stock_Inflation.head()
Out[16]:
Adj Close CPI Simple Return Inflation rate
2008-04-30 21.561066 214.823 NaN NaN
2008-05-31 34.965513 216.632 0.621697 0.008421
2008-06-30 20.874075 218.815 -0.403010 0.010077
2008-07-31 19.515852 219.964 -0.065067 0.005251
2008-08-31 34.965513 219.086 0.791647 -0.003992

Step 7: Considering inflation rate in returns

In [17]:
# Applying the formula R' = (1 + Simple Return)/(1 + Inflation rate) - 1
Stock_Inflation['Adj Return'] = ((1 + Stock_Inflation['Simple Return'])/(1 + Stock_Inflation['Inflation rate'])) - 1
Stock_Inflation.head()
Out[17]:
>

Step 8: Seeing the results on line charts

In [18]:
# importing matplotlib library
import matplotlib.pyplot as plt
In [19]:
# Seeing simple return on line chart
Stock_Inflation['Simple Return'].plot(figsize = (14,10))
Out[19]:

 

 

 

simple return

 

 

 

 

 

In [20]:

 

# Seeing adjusted return on line chart
Stock_Inflation['Adj Return'].plot(figsize = (14,10))
Out[20]:

 

 

 

Adjusted return

 

 

 

 

 

In [21]:

 

# Seeing inflation rate on line chart
Stock_Inflation['Inflation rate'].plot(figsize = (14,10))
Out[21]:

Inflation rate

So guys, in this tutorial, we learnt how to obtain adjusted return – considering inflation rate in simple returns.
Stay tuned for next tutorial on this Financial Analytics series. Don’t forget to subscribe to our YouTube channel.

 

One thought on “Adjusted Returns including Inflation: FA7

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top
%d bloggers like this: