Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 115

Unit 4

Financial Analytics
Financial analytics is the creation of ad hoc analysis to answer specific
business questions and forecast possible future financial scenarios. The
goal of financial analytics is to shape business strategy through reliable,
factual insight rather than intuition.
• By offering detailed views of companies’ financial data, financial
analytics provides the tools for firms to gain deep knowledge of key
trends and take action to improve their performance.
The importance of financial analytics
• Financial analytics can help companies determine the risks they face,
how to enhance and extend the business processes that make them
run more effectively, and whether organizations’ investments are
focused on the right areas.
Top 3 predictive analytics models in finance
• In the finance context, these are the three most widely used predictive
models:
• Classification model:
• The classification model is among the most straightforward predictive
analytics models that produce a binary output. In the banking
context, classification models are often used to guide decisions based
on a broad assessment of the subject. For example, it can predict
whether the shares of a certain company will go up or down.
Outliers model:
• The outliers model is used to detect significant deviations in a dataset,
making it one of the most widely-used models for fraud detection. For
example, if a customer’s credit card is used to buy an overly expensive
watch in a city that he doesn’t live in, the outliers model will flag this
transaction as potentially fraudulent on the grounds of this being an
unusual behavior.
Time series model:
• The time series model tracks a certain variable throughout a specific
time period to predict how that variable will be affected at another
specific time frame. For example, in finance, the time series model is
often used to predict how a given financial asset (like a security’s price
or inflation ratio) will change over time.
Customer Profitability Analytics
• Customer Profitability Analysis (in short CPA) is a management accounting and a
credit underwriting method, allowing businesses and lenders to determine the
profitability of each customer or segments of customers, by attributing profits
and costs to each customer separately. CPA can be applied at the individual
customer level (more time consuming, but providing a better understanding of
business situation) or at the level of customer aggregates / groups (e.g.
Grouped by number of transactions, revenues, average transaction size, time
since starting business with the customer, distribution channels, etc.)
• CPA is a “retrospective” method, which means it analyses past events of
different customers, in order to calculate customer profitability for each
customer. Equally, research suggests that that credit score does not necessarily
impact the lenders’ profitability
Organizational Profitability Analytics
• PACE’s( Profitability Analytics Center of Excellence) Analytics (PA)
Framework is a process designed to produce high-quality internal
decision-support information that supports decision making throughout
the organization – from the C-suite to the shop floor and direct customer
contact points. It is built on a holistic view of revenue management,
managerial costing, and investment management within an organization.
• The PA Framework goes well beyond traditional financial accounting,
reporting, and analysis. It incorporates modern revenue management
techniques, modern managerial costing focused on internal decision
support, and new views of investments as both tangible and intangible.
Profitability Analytics Framework
• The Profitability Analytics Framework is composed of three primary
elements:​
• 1. Strategy Formulation – where an organization establishes its plan
for identifying and addressing its market(s) and for mobilizing its
resources to meet the demands created by that plan.
2. Strategy Validation – where causal models are developed that
directly enable the evaluation of strategy. These models employ the
principle of causality to quantify, in operational and monetary terms,
the revenue and cost impacts of an organization’s strategy, and then
track the execution and performance of that strategy
3. Strategy Execution – involves decision making that employs the
outputs of the causal models to provide an organization’s decision
makers with the accurate and relevant information they need to make
economically sound decisions as they execute and adapt tactics to meet
strategic goals.
Cash flow analytics
• How do you calculate whether a property will be successful or not?
One of the most import metrics real estate investors use to decide on
whether an investment is sound is cash flow.
• What is cash flow?
• Cash flow is a description of the way money flows through a rental
property, similar to the way water flows over a waterfall. — Roofstock
• Put simply cash flow is monthly income — monthly expenses. The
remainder amount is the number you profit on a monthly basis.
Why do we want POSITIVE cash flow?

For your investment to be beneficial it needs to generate a return for


you. Monthly cash flow does this by creating a passive income stream.
Here are the benefits of cash flow:
Cash flow creates more opportunity. Reinvesting profits from an
investment property into another investment is a great way to
exponentially grow your financial well-being.
• Cash flow creates safety. The extra monthly income you bring in can
help create a larger savings reserve to protect you against unexpected
life expenses (like medical bills, car maintenance, etc.).
How to calculate cash flow?
Let’s start with the equation:
• Cash flow = gross rental income — all expenses and cash reserves
Cash flow in python
1. Install Packages
• The first step is installing the necessary packages:
• !pip install plotly -q
• Plotly allows us interactive charts — see basic charts .
• 2. Import Packages
• Next, import the necessary packages.
• Import plotly.graph_objects as go
import pandas as pd
3. Data Load
• Let’s read in data for a sample property. The data is in a CSV format
and published publicly on Github.
• We can read in the CSV file using pandas. This will convert our CSV
file into a dataframe (table with rows and columns).
• df =
pd.read_csv(‘https://raw.githubusercontent.com/analyticsariel/public-
data/main/cash_flow_data.csv’)
• df.head()
• The dataset contains information on monthly income and expenses.
• Next, we assign each income and expense value to a variable. We
select the column in the dataframe and locate the first value (first row).
• #Assign variables to values in table
• property_address = df[‘property_address’].iloc[0]
• rent = df[‘rent’].iloc[0]
• additional_income = df[‘additional_income’].iloc[0]
• principal_and_interest = df[‘principal_and_interest’].iloc[0]
• taxes = df[‘taxes’].iloc[0]
Insurance = df[‘insurance’].iloc[0]
property_management = df[‘property_management’].iloc[0]
cap_ex = df[‘cap_ex’].iloc[0]
vacancy = df[‘vacancy’].iloc[0]
utilities = df[‘utilities’].iloc[0]
• additional_expenses = df[‘additional_expenses’].iloc[0]
We can create total income and expense variables. For example, total
income is rent + additional_income.
These new variables will allow us to view the flow of expenses and income.
# summary variables
total_income = rent + additional_income
fixed_expense = principal_and_interest + taxes + insurance
variable_expense = property_management + cap_ex + vacancy + utilities +
additional_expenses
• total_expense = fixed_expense + variable_expense
4.Transformation:
We want to create a Sankey diagram to visualize our income and
expenses, similar to the Plotly.
Sankey diagrams are a higher degree of complexity to chart relative to
other plotly express visualizations.
• Sankey diagrams visualize the contributions to a flow by defining
source to represent the source node, target for the target node, value
to set the flow volume, and label that shows the node name
We need to have three components to create our Sankey Diagram –
Node — starting point (i.e. Income)
Target — ending point, sub component of the node (i.e. Rent)
Value — value between flow (i.e. $2,600)
• Let’s start by creating a list of all of our labels. We will then assign
each label to an integer, which we will refer to in the flow.
Create list of labels for the chart
• label_list = [ “Income”, “Rent”, “Additional Income”,
• “Expense”, “Fixed”, “Variable”, “Principal & Interest”,
• “Taxes”, “Insurance”, “Property Management”,
“CapEx”, “Vacancy”, “Utilities”, “Additional Expenses”, ]
# append an index number to each element in the list label_dict = {}
for i in range(len(label_list)):
label_dict[label_list[i]] = i
• label_dict
Output
• Next, we create a nested list to define our node -> target -> value flow.
• Create sub list for each node -> target -> value
• sankey_list = [
• # income
• [“Income”, “Rent”, rent],
• [“Income”, “Additional Income”, additional_income],
• # expenses [fixed]
• [“Expense”, “Fixed”, fixed_expense],
• [“Fixed”, “Principal & Interest”, principal_and_interest],
• [“Fixed”, “Taxes”, taxes],
• [“Fixed”, “Insurance”, insurance],
# expenses [variable]
[“Expense”, “Variable”, variable_expense],
[“Variable”, “Property Management”, property_management],
[“Variable”, “CapEx”, cap_ex],
[“Variable”, “Vacancy”, vacancy],
[“Variable”, “Utilities”, utilities],
[“Variable”, “Additional Expenses”, additional_expenses],
•]
In order to plot our data, we need to pass three lists.
• For the source and target lists we need to pass the integer values assigned to
each label. The value list retrieves the last element in our nested list.
• #Create sublists from the sankey list
• source_list = [label_dict[x[0]] for x in sankey_list]
• target_list = [label_dict[x[1]] for x in sankey_list]
• value_list = [x[2] for x in sankey_list]
• print(‘Source list:’, source_list)
• print(‘Target list:’, target_list)
• print(‘Value list:’, value_list)
Output
• 5. Visualization
• We can use Plotly Indicator to visualize cash flow.
• Fig = go.Figure(go.Indicator(
• mode = “number+delta”,
• value = total_income – total_expense,
• number = {‘prefix’: “Cash Flow - $”},
• domain = {‘x’: [0, 1], ‘y’: [0, 1]})
• )
• fig.update_layout(height=300)
• fig.show()
• Next, we create our Sankey diagram by plotting our source, target, and value lists.
• Create figure
• fig = go.Figure(data=[go.Sankey(
• # nodes
• node = dict(
• pad = 15,
• thickness = 20,
• line = dict(color = “black”, width = 0.5),
• label = label_list, # cash flow parameters
• hovertemplate=‘%{label} has total value $%{value}’,
• ),
• # links
• link = dict(
• source = source_list, # start node
• target = target_list, # end node
• value = value_list # value of end node
• ))])
Fig.update_layout(
hovermode = ‘x’,
title=“Income & Expenses<br>Property Address: {}”.format(property_address),
# optional for black background
font=dict(size = 10, color = ‘white’),
plot_bgcolor=‘black’,
paper_bgcolor=‘black’
)
• fig.show()
Output
Value driven analytics
Business Value Transformation:
Business value from analytics can be envisioned across four key dimensions:
Value of data : Transforming and monetizing data from raw data to insights to
the decision and finally actions.
Business process transformation : From a business process centered to an
analytics centered process, analytics is embedded into everything.
Event driven action : From reactive action to customer, risk and operational
business imperatives to intelligence driven proactive actions.
• Customer expectation : Anytime, anywhere interaction that is personalized,
contextual, and omni-Channel.
• During every project, business analysis tools should be selected
individually for each situation depending on a number of factors,
including:
• Project characteristics
• Length of business analysis and its scope
• Tools used by the client (product owner, project manager, etc.)
• Any other factors the business analyst considers relevant
Shareholder value analytics

• Shareholder value is the value delivered by a company to investors


who own shares in the company. Shareholder value is created when a
company’s management team makes business decisions that enable
the company to increase its earnings, dividends, or share price.
How to measure your shareholder value
• Your shareholder value is directly correlated with how many shares of
a company you own. Here's how to compute your portion of
shareholder value:
• Determine the company's earnings per share.
• Add the company's stock price to its EPS to determine your
shareholder value on a per-share basis.
• Multiply the per-share shareholder value by the number of shares in
the company you own.
• If a company has EPS of 2₹ and a stock price of 40 ₹, then the
shareholder value on a per-share basis is 42₹. If you own 10 shares of
the company’s stock, then your individual shareholder value is 420 ₹.
Stock market analytics
• Stock is the small chunk of ownership in the company. The stock price
of the company reflects the net evaluation of the company and also
gives a little insight into its performance. These stocks are traded on
exchanges and their prices are constantly changing due to their
demand and supply in the market.
• If a stock is in high demand and low in supply i.e. More people want
to buy it and fewer people are willing to sell it then the price for the
stock will go up and similarly if the stock is in low demand and high on
supply which means people more people are ready to sell it but fewer
people are willing to buy it then its prices go down.
In python
• # import required libraries
• import pandas as pd
• import datetime
• Import numpy as np
• import matplotlib.pyplot as plt
• from pandas.plotting import scatter_matrix
• !pip install yfinance
• import yfinance as yf
• #%matplotlib inline
Data description
We have downloaded the daily stock prices data using the Yahoo
finance API functionality. It’s a five-year data capturing Open, High,
Low, Close, and Volume
Open: The price of the stock when the market opens in the morning
• Close: The price of the stock when the market closed in the evening
• High: Highest price the stock reached during that day
• Low: Lowest price the stock is traded on that day
• Volume: The total amount of stocks traded on that day.
• Here, we will take the Example of three companies TCS, Infosys, and
Wipro which are the industry leaders in providing IT services.
• Exploratory Analysis:
• Start = “2014-01-01”
• end = ‘2019-1-01’
• tcs = yf.download(‘TCS’,start,end)
• infy = yf.download(‘INFY’,start,end)
• wipro = yf.download(‘WIPRO.NS’,start,end)
Tcs[‘Open’].plot(label = ‘TCS’, figsize = (15,7))
infy[‘Open’].plot(label = “Infosys”)
wipro[‘Open’].plot(label = ‘Wipro’)
• plt.title(‘Stock Prices of TCS, Infosys and Wipro’)
• The above graph is the representation of open stock prices for these
three companies via line graph by leveraging matplotlib library in
python.
• The Graph clearly shows that the prices of Wipro is more when
comparing it to other two companies but we are not interested in the
absolute prices for these companies but wanted to understand how
these stock fluctuate with time.
Tcs[‘Volume’].plot(label = ‘TCS’, figsize = (15,7))
infy[‘Volume’].plot(label = “Infosys”)
wipro[‘Volume’].plot(label = ‘Wipro’)
plt.title(‘Volume of Stock traded’)
• plt.legend()
The Graph shows the volume traded by these companies which clearly shows that stocks of Infosys are traded more

compared to other IT stocks .


#Market Capitalisation
tcs[‘MarktCap’] = tcs[‘Open’] * tcs[‘Volume’]
infy[‘MarktCap’] = infy[‘Open’] * infy[‘Volume’]
wipro[‘MarktCap’] = wipro[‘Open’] * wipro[‘Volume’]
tcs[‘MarktCap’].plot(label = ‘TCS’, figsize = (15,7))
infy[‘MarktCap’].plot(label = ‘Infosys’)
wipro[‘MarktCap’].plot(label = ‘Wipro’)
plt.title(‘Market Cap’)
• plt.legend()
Only volume or stock prices do not provide a comparison between companies. In this case, we have plotted a
graph for Volume * Share price to better compare the companies. As we can clearly see from the graph that
Wipro seems to be traded on a higher side.
Moving Averages
• As we know the stock prices are highly volatile and prices change
quickly with time. To observe any trend or pattern we can take the
help of a 50-day 200-day average.
• tcs[‘MA50’] = tcs[‘Open’].rolling(50).mean()
• tcs[‘MA200’] = tcs[‘Open’].rolling(200).mean()
• tcs[‘Open’].plot(figsize = (15,7))
• tcs[‘MA50’].plot()
• tcs[‘MA200’].plot()
Scattered plot matrix
data = pd.concat([tcs[‘Open’],infy[‘Open’],wipro[‘Open’]],axis = 1)
data.columns = [‘TCSOpen’,’InfosysOpen’,’WiproOpen’]
• scatter_matrix(data, figsize = (8,8), hist_kwds= {‘bins’:250})
The graph is the combination of
histograms for each company and a
subsequent scattered plot taking two
companies’ stocks at a time. From the
graph, we can clearly figure out that
Wipro stocks are loosely showing a
linear correlation with Infosys.
Financial control Systems
• Financial controls are the procedures, policies, and means by which an
organization monitors and controls the direction, allocation, and usage
of its financial resources. Financial controls are at the very core of
resource management and operational efficiency in any organization.
• Required Processes:
• The implementation of effective financial control policies should be
done after a thorough analysis of the existing policies and future
outlook of a company. In addition, it is important to ensure the
following four processes are completed before implementing financial
control in a business:
• 1. Detecting overlaps and anomalies
• Financial budgets, financial reports, profit & loss statements,
balance sheets, etc., present the overall performance and/or
operational picture of a business. Hence, while formulating
financial control policies, it is very important to detect any
overlaps and/or anomalies arising out of the data available.
It helps in detecting any existing loopholes in the current
management framework and eliminating them.
2. Timely updating:
Financial control is the essence of resource management and, hence,
the overall operational efficiency and profitability of a business.
Timely updates of all available data are very important. In addition,
updating all management practices and policies concerning the
existing financial control methods is also equally important.
3. Analyzing all possible operational scenarios:
Before implementing a fixed financial control strategy in an
organization, it is important to thoroughly evaluate all possible
operational scenarios. Viewing the policies from the perspectives of
different operational scenarios – such as profitability, expenditures,
safety, and scale of production or volume – can provide the necessary
information. Also, it helps establish an effective financial control
policy that covers all operational aspects of the organization.
4. Forecasting and making projections:
While implementing a financial control policy, forecasting and making
projections are very important steps. They provide an insight into the
future goals and objectives of the business. In addition, they can help
establish a financial control policy in accordance with the business
objectives and act as a catalyst in achieving such goals.
Types of Financial Controls
Types Of Financial Controls
• There are mainly three types of finance controls based on their
purpose and target areas:
• 1. Immediate (Directional) Financial Control
• It involves taking quick actions in response to discrepancies in
financial reports, which if ignored can result in significant losses or
undermine a company’s goals and operations.
• 2. Selective Financial Control
• It concentrates on particular aspects of a company, such as
management and production. It evaluates how a process operates,
how it adheres to guidelines, and whether it contains flaws or margins
of error. Then it employs all available metrics or makes amendments
to improve performance by maximizing resource utilization.
3. Postdate Financial Control
• It usually takes place after operations have occurred and identifies
flaws in current policies and regulations. A corporation evaluates its
existing strategy and performance compared to its anticipated
objectives and then makes necessary changes or improvements
based on the existing outcomes.
• Aside from these, the balance sheet, cash flow statement, and income
(profit and loss) statement assist organizations in evaluating their
operations concerning their objectives:
• 1. Balance Sheet
• It provides information about the financial success and position of the
company against expectations at a particular point in time. It has two
components – assets and liabilities.
• 2. Cash Flow Statement
• It shows the total cash available for a business, i.e., cash received minus
cash expended every month of a fiscal year. Negative cash flow
necessitates a re-evaluation of corporate strategies
3. Income (Profit and Loss) Statement
• It depicts the relationship between business income and expenses
over time. It consists of revenue, sales cost, gross profit, operating
costs, and net income.
Financial Engineering
• Financial engineering is an interdisciplinary branch of the investment industry
that makes use of applied mathematics, statistics, computer science, financial
theory, and economics to conduct quantitative analysis on the financial
markets.
• The field focuses on developing models and techniques to develop and test
investment strategies, to envision and create new financial products, to
manage risk, and to produce scenarios and forecasts for both short- and long-
term perspectives on the markets.
• Financial engineers are employed by investment banks, hedge funds, asset
managers, commercial banks, insurance agencies, and consultancies to the
financial industry. They also work in corporate treasuries, regulatory agencies,
and in international quasi-governmental organizations like The World Bank.
What is a Financial Engineer?
• Financial engineers (also known as “quantitative analysts” or
“quants”) are practitioners in the financial industry who are
responsible for developing, testing, and improving on models, tools,
and techniques that are prevalent in quantitative finance.
Financial Engineering in Quantitative
Finance
Financial engineering is used to address a wide range of challenges and opportunities in the
financial world. Applications for its methods can be found across such diverse areas as:
Securities analysis
Derivatives and option pricing
Structured products
Trading and arbitrage, including algorithmic trading
Portfolio management
Risk management, including credit risk management
Behavioral finance
• Machine learning as applied to investment strategy and portfolio management
Types of Financial Engineering
• Let us understand the different types of financial engineering prospects:
• 1. Foreign Exchange Market Trading
• The engineers utilize the foreign exchange market to soar the corporate
profit and exploit the international marketplace. With currency exchange
rates as its major trade, the firm typically sustains the fund by holding
diverse types of currencies from different countries.
• The financial engineer can forecast exchange rates between the
currencies to predict the increase or decline of the currency’s value.
Accordingly, associated brokers may trade currencies to optimize profit
and reduce losses.
• 2. Derivatives Trading
• Financial agreements don’t have apparent fiscal worth and may be
associated with interest rates, indexes, or assets. Moreover, the value
of derivatives relies upon the gradual performance of the underlying
entity. This aids professionals with financial engineering jobs to have
more chances for gains.
Financial Risk Assessment using Python
Simple Moving Average:
The strategy being used will not tell you exactly which stock to pick or
what price a stock will be at in the future. It is simply a way of
comparing companies that are similar.
• We will be using a SMA (Simple Moving Average) to determine how
volatile a stock is. A Simple moving average can be calculated by
taking any number of consecutive closing prices and dividing them by
the period you chose.
• Formula for SMA

The period of the SMA can be any natural number really (N > 0), but the most
useful SMA values fall between 20–200 depending on the timeframe that is being
used. A lower SMA value will respond faster to changes in the underlying price.
It is important to note that because our number of daily closing prices is finite
there will be instances where the SMA cannot be calculated (i.e.; there are not
enough future underlying prices to calculate the appropriate SMA). We will have
to take this into account when writing the code.
Residual:
In order to turn the SMA values calculated into a data that is
interpretable, we will be calculating the difference between the
underlying and the SMA value at each point where both are defined.
• Then we can sum the differences and divide the sum by the number
of values we found. This will give us a raw value that could potentially
be compared to those of other similar companies.
The Code
In the Highest of levels, we need to:
Decide who we will be analyzing and when we want to analyze them.
Establish a data source to retrieve adjusted closing prices for each day
within the chosen period.
Plot the Underlying vs. The SMA for visualization.
• Calculate Average of Residuals as explained previously.
In python
• Let’s begin by importing the necessary packages:
• datetime will allow our program to read our specified time period.
• Pandas_datareader.data as web helps us extract data from the Web.
• All three variations of Matplotlib will allow us to actually graph and
visualize the data that we are interpreting.
• Functools will let us reduce our array of residual differences to one
total Sum.
Import datetime
import pandas_datareader.data as web
import matplotlib.pyplot as plt
from matplotlib import style
import functools
• import matplotlib as mpl
Next, we can initialize and set the time periods we want to analyze. I
chose a two year period for a more simple data visualization:
# Initialize and set the time periods that we want to analyze
start = datetime.datetime(2017, 1, 1)
• end = datetime.datetime(2019, 11, 2)
Next, we should pick the companies that we want to compare. I chose
Apple, Microsoft, and Netflix. These companies have relatively similar
underlying prices at the publication date of this article:
# make a list of companies that should be tested
# Apple: AAPL, Microsoft: MSFT, Netflix: NFLX
• stock_tickers = [‘AAPL’, ‘MSFT’, ‘NFLX’]
• Now we will enter our main loop that plots the stock charts along with the Simple Moving Averages. I
chose a 20 day period for my SMA soely for data visualization:
• Iterate over each company in the list and produce:
• # 1. Graph of Moving average vs stock price
• # 2. Average of the residual for each day moving average can be calculated
• for i in range(len(stock_tickers)):
• # Read our data from Yahoo Finance
• df = web.DataReader(stock_tickers[i], ‘yahoo’, start, end)
• df.tail()
• # Create a list of the closing prices for each day
• close_price = df[‘Adj Close’]
• # Calculate the SMA for each ticker with a 20 day period
• moving_avg = close_price.rolling(window=20).mean()
Now we must Plot the values that we have calculated (This is still contained within the original ‘for’
loop). I used plt.show() to physically show my graphs. If you are using a Collab Notebook like
Jupyter, you can use %matplotlib inline to display graphs.
# Adjusting the size of matplotlib
mpl.rc(‘figure’, figsize=(8, 7))
# Adjusting the style of matplotlib
style.use(‘ggplot’)
# Plot the Moving Average vs. Closing Price
close_price.plot(label=stock_tickers[i])
moving_avg.plot(label=‘moving_avg’)
plt.legend()
• plt.show()
• From Matplotlib we receive these graphs. The blue line represents the
Simple Moving Average with a 20 day period:
• Now to make use of this SMA data, we will calculate the average of
the residuals (This is still contained within the original for loop). It is
important to note that because the number of days we can collect
closing prices for is finite and bounded, some values will result in
‘NaN’ which means Not a Number. These values are eliminated from
the parsed list through a mini function called lambda. This is
comparable to the Lambda Function notation in Java or Arrow
Function in JavaScript.
Finally, Sum the the Residuals of Moving Average ~ Closing Price
differences = []
# Iterate over each list and calculate differences
for j in range(len(close_price)):
x = abs(moving_avg[j] – close_price[j])
differences.append(x)
# Eliminate the values that are of type ‘nan’
• cleanedList = [x for x in differences if str(x) != ‘nan’]
Use the Reduce method and a short form function ‘lambda’ combine all
elements of the list
combined_value = functools.reduce(lambda a, b: a + b, cleanedList)
# Calculate the average of the residuals
average = combined_value / len(cleanedList)
• print(“The residual average of “ + stock_tickers[i] + “ is: “ +
str(average))
The final output yields:
The residual average of AAPL is: 5.875529758261265
The residual average of MSFT is: 2.0096710682601384
• The residual average of NFLX is: 12.446982918608967
Financial Modeling in python
• Financial Modeling in Python refers to the method used to build a
financial model using a high-level python programming language with
a rich collection of built-in data types.
• This language can be used for modification and analysis of excel
spreadsheets and automation of certain tasks that exhibit repetition.
• Since financial models use spreadsheets extensively, Python has
become one of the most popular programming languages in finance.
PPF Package for Python
PPF Package for Python
• The PPF package or library refers to the Python package that comprises a family of
sub-packages. In other words, it is a mixture of various supporting extension
modules that facilitate the implementation of Python programming. Please find
below the summary of the various PPF sub-packages:
• Com: It is used for trade, market, and pricing functionality.
• Core: It is used in the representation of types and functions of financial quantities.
• Date_time: It is used in the manipulation and calculation of date and time.
• Market: It represents the types and functions of standard curves and surfaces in
financial programming (e.g., volatility surfaces, discount factor curves, etc.).
• Math: It is used for general mathematical algorithms.
Model: It is used for coding various numerical pricing models.
Pricer: It is for types and functions used for valuing financial structures
Text: It is used for the test suite.
• Utility: It is used for tasks that are general in nature (e.g., algorithms
for searching and sorting).
Mathematical Tools for Python
• Some of the major mathematical tools available in Python are as
follows:
• N(.): It is a function in the ppf.math.special functions module that helps
approximate the standard normal cumulative distribution function, which is
used in the Black–Scholes option pricing model.
• Interpolation: It is the process that is used to estimate the values of a
function y(x) for arguments between several known data points (x0, y0), (x1,
y1) . . . , (xn, yn). The ppf. Utility. The bound module is used in its
implementation. Some of the variants of interpolation are:
• Linear interpolation
• Loglinear interpolation
• Linear on zero interpolation
• Cubic spline interpolation
Root Finding: It is used to find the root with or without derivative
information using the ppf. Math. Root finding module. Some of the
variants of root finding are:
Bisection method
Newton-Raphson method
• Linear Algebra: The linear algebra functions are mostly covered in the
NumPy package. It is implemented using the ppf. Math. Linear-algebra
module. Some of the variants of linear algebra are:
Matrix Multiplication
Matrix Inversion
Matrix Pseudo-Inverse
Solving Linear Systems
• Solving Tridiagonal Systems
• Generalized Linear Least Squares: It fits a set of data points to a linear
combination of some basic functions. The algorithms for this function
are implemented using the ppf: math and generalized least squares
module.
Quadratic and Cubic Roots: These functions are used to find the real
roots of a quadratic or cubic equation. The ppf.math.quadratic roots
module is used to find the real roots of a quadratic equation, while the
ppf. Math. The cubic roots module is used for the cubic roots algorithm.
Integration: This tool is used to calculate the expected value of a function
with random variables. It is primarily used in the calculation of financial
payoffs. Some of the variants of integration are:
Piecewise Constant Polynomial Fitting
Piecewise Polynomial Integration
• Semi-analytic Conditional Expectations
Portofolio Analysis using Python
Installing the required libraries
Open the terminal and activate the conda environment to install the
following packages.
Pip install matplotlib
pip install seaborn
• pip install nsepy
Importing the libraries

• import numpy as np
• import pandas as pd
• import matplotlib.pyplot as plt
• import seaborn as sb
• from datetime import date
• from nsepy import get_history as gh
• plt.style.use('fivethirtyeight') #setting matplotlib style
Defining Parameters
• stocksymbols = ['TATAMOTORS','DABUR',
'ICICIBANK','WIPRO','BPCL','IRCTC','INFY','RELIANCE’]
• srartdate = date(2019,10,14)
• end_date = date.today()
• print(end_date)
• print(f"You have {len(stocksymbols)} assets in your porfolio" )
• Here, we’ve created a list of stocks for which we want to fetch data and
analyze that data. We’ve defined the starting date, i.e., the date from
which we want to fetch the data, and the end date as well, i.e., today.
Fetching Data
• Now, we’ll be iterating over the list of stocks to fetch data one by one for every single stock and
combine it towards the end to have it in one data frame.
• df = pd.DataFrame()
• for i in range(len(stocksymbols)):
• data = gh(symbol=stocksymbols[i],start=startdate, end=(end_date))[[‘Symbol’,’Close’]]
• data.rename(columns={‘Close’:data[‘Symbol’][0]},inplace=True)
• data.drop([‘Symbol’], axis=1,inplace=True)
• if i == 0:
• df = data
• if i != 0:
• df = df.join(data)
• df
• We’ve fetched the data for two columns only, the Symbol and Close
Price. While fetching the data, we renamed the Close Price Column
with the Symbol/Ticker and then dropped the Symbol Column.
• #Output
Now, with this dataset,
we'll do a great deal of
Portfolio Analysis.
Analysis
• Plotting the Close Price history.
• Fig, ax = plt.subplots(figsize=(15,8))
• for i in df.columns.values :
• ax.plot(df[i], label = i)
• ax.set_title(“Portfolio Close Price History”)
• ax.set_xlabel(‘Date’, fontsize=18)
• ax.set_ylabel(‘Close Price INR (₨)’ , fontsize=18)
• ax.legend(df.columns.values , loc = ‘upper left’)
• plt.show(fig)
• Output
Correlation Matrix
• A Coefficient of correlation is a statistical measure of the relationship
between two variables. It varies from -1 to 1, with 1 or -1 indicating
perfect correlation. A correlation value close to 0 indicates no
association between the variables. A correlation matrix is a table
showing correlation coefficients between variables. Each cell in the
table shows the correlation between two variables.
• The correlation matrix will tell us the strength of the relationship
between the stocks in our portfolio, which essentially can be used for
effective diversification.
• Code to determine correlation matrix:
• Correlation_matrix = df.corr(method=‘pearson’)
• correlation_matrix
• Output:
• Plotting the Correlation Matrix:
• fig1 = plt.figure()
• sb.heatmap(correlation_matrix,xticklabels=correlation_matrix.column
s, yticklabels=correlation_matrix.columns, cmap=‘YlGnBu’,
annot=True, linewidth=0.5)
• print(‘Correlation between Stocks in your portfolio’)
• plt.show(fig1)
With this matrix, we can see
that Wipro and Infosys are heavily
correlated, which is very logical as both
companies belong to the same industry. It
can also be seen that BPCL and IRCTC are
negatively correlated. Hence, it is wise to
have them in our portfolio for efficient
diversification, which ensures that if, for
some reason, the BPCL goes in one
particular direction, let's say down. There's
less chance of IRCTC also moving in the
same direction
Risk & Return
Daily Simple Returns:
• To ascertain daily simple return, we’ll write this code:
• daily_simple_return = df.pct_change(1)
• daily_simple_return.dropna(inplace=True)
• daily_simple_return
Output

Daily Simple Returns is


essentially the percentage
change in the Prices being
calculated daily.
Average daily Returns
print(‘Average Daily returns(%) of stocks in your portfolio’)
Avg_daily = daily_simple_return.mean()
• print(Avg_daily*100)
Risk
• Plotting Risk using Daily Returns:
• daily_simple_return.plot(kind = "box",figsize = (20,10), title = "Risk
Box Plot")
• The largest spread in the above box plot is for the TATAMOTORS,
which makes sense as TATAMOTORS had the highest Average Daily
Returns. The smallest spread in the above box plot is for DABUR. It
should be noted that although IRCTC does not have the largest
spread, it does have more positive outliers, which translates into a
higher average daily return.
• Before moving forward, it was necessary to calculate each portfolios’
Standard Deviation using the “.std()” function, along with the
annualized Standard Deviation:
• print('Annualized Standard Deviation (Volatality(%), 252 trading days)
of individual stocks in your portfolio on the basis of daily simple
returns.’)
• print(daily_simple_return.std() * np.sqrt(252) * 100)
• Output
• Return Per Unit Of Risk:
• Avg_daily / (daily_simple_return.std() * np.sqrt(252)) *100
• Output:
• The higher this ratio, the better it is. Hence, IRCTC has the best Return
to Risk ratio, BPCL having the lowest. After adjusting for a risk-free
rate, this ratio is also called Sharpe Ratio, a measure of risk-adjusted
return. It describes how much excess return you receive for the
volatility of holding a riskier asset.
• Cumulative Returns:
• Daily_cummulative_simple_return
=(daily_simple_return+1).cumprod()
• daily_cummulative_simple_return
• Output
Visualize the daily cummulative simple return
print(‘Cummulative Returns’)
fig, ax = plt.subplots(figsize=(18,8))
for i in daily_cummulative_simple_return.columns.values :
ax.plot(daily_cummulative_simple_return[i], lw =2 ,label = i)
ax.legend( loc = ‘upper left’ , fontsize =10)
ax.set_title(‘Daily Cummulative Simple returns/growth of investment’)
ax.set_xlabel(‘Date’)
ax.set_ylabel(‘Growth of ₨ 1 investment’)
• plt.show(fig)
• Output
• Based on the above graph, during this 2-year stretch from 2019-2021,
IRCTC performed the best and led to the most cumulative returns: it
started to separate itself in early 2020. This is followed by
TATAMOTORS, and in third, WIPRO. BPCL had the worst cumulative
returns over the 2-year period; in fact, it is the only one to finish in
the red.

You might also like