Reader small image

You're reading from  Python for Finance

Product typeBook
Published inApr 2014
Reading LevelBeginner
Publisher
ISBN-139781783284375
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Yuxing Yan
Yuxing Yan
author image
Yuxing Yan

Yuxing Yan graduated from McGill University with a PhD in finance. Over the years, he has been teaching various finance courses at eight universities: McGill University and Wilfrid Laurier University (in Canada), Nanyang Technological University (in Singapore), Loyola University of Maryland, UMUC, Hofstra University, University at Buffalo, and Canisius College (in the US). His research and teaching areas include: market microstructure, open-source finance and financial data analytics. He has 22 publications including papers published in the Journal of Accounting and Finance, Journal of Banking and Finance, Journal of Empirical Finance, Real Estate Review, Pacific Basin Finance Journal, Applied Financial Economics, and Annals of Operations Research. He is good at several computer languages, such as SAS, R, Python, Matlab, and C. His four books are related to applying two pieces of open-source software to finance: Python for Finance (2014), Python for Finance (2nd ed., expected 2017), Python for Finance (Chinese version, expected 2017), and Financial Modeling Using R (2016). In addition, he is an expert on data, especially on financial databases. From 2003 to 2010, he worked at Wharton School as a consultant, helping researchers with their programs and data issues. In 2007, he published a book titled Financial Databases (with S.W. Zhu). This book is written in Chinese. Currently, he is writing a new book called Financial Modeling Using Excel — in an R-Assisted Learning Environment. The phrase "R-Assisted" distinguishes it from other similar books related to Excel and financial modeling. New features include using a huge amount of public data related to economics, finance, and accounting; an efficient way to retrieve data: 3 seconds for each time series; a free financial calculator, showing 50 financial formulas instantly, 300 websites, 100 YouTube videos, 80 references, paperless for homework, midterms, and final exams; easy to extend for instructors; and especially, no need to learn R.
Read more about Yuxing Yan

Right arrow

Chapter 7. Visual Finance via Matplotlib

Graphs and other visual representations have become more important in explaining many complex financial concepts, trading strategies, and formulae. In this chapter, we discuss the module matplotlib, which is used to create various types of graphs. In addition, the module will be used intensively in Chapter 9, The Black-Scholes-Merton Option Model, when we discuss the famous Black-Scholes-Merton option model and various trading strategies. The matplotlib module is designed to produce publication-quality figures and graphs. The matplotlib module depends on NumPy and SciPy which were discussed in Chapter 6, Introduction to NumPy and SciPy. There are several output formats, such as PDF, Postscript, SVG, and PNG.

In particular, we will cover the following:

  • Several ways to install matplotlib

  • Simple examples of using matplotlib

  • Net Present Value (NPV) profile, DuPont identity, stock returns, and histogram

  • Total risk, market risk (beta), and firm-specific risk...

Installing matplotlib via ActivePython


The first way to install the matplotlib module is via ActivePython. We install ActivePython first and then install matplotlib. In the process of installing matplotlib, NumPy would be installed as well since matplotlib depends on both NumPy and SciPy. The whole procedure has four steps as follows:

  1. Go to http://www.activestate.com/activepython/downloads.

  2. Choose an appropriate executable file to download.

  3. For Windows, navigate to All Programs | Accessories, and then click on Command Prompt. You will see the following window:

  4. After going to the appropriate directory, such as C:\Python27, type pypm install matplotlib as shown in the following screenshot:

The matplotlib module depends on both NumPy and SciPy. Since the NumPy module will be installed automatically when we install matplotlib, we need to install SciPy; see the following similar procedure:

To launch Python, navigate to All Programs | ActivateStateActive Python, and then click on IDLE (Python GUI)...

Alternative installation via Anaconda


In Chapter 6, Introduction to NumPy and SciPy, we discussed the dependency of a module. Because of such a dependency, it might be very difficult to install a module independently since it depends on many other modules. In this book, we use the so-called super-packages. If one of them is installed, most of our modules are installed simultaneously. We choose Anaconda. To install Anaconda, we have the following two steps:

  1. Go to http://continuum.io/downloads.

  2. Choose an appropriate package to download and install.

Understanding how to use matplotlib


The best way to understand the usage of the matplotlib module is through examples. The following example could be the simplest one since it has just three lines of Python code. The objective is to link several points. By default, the matplotlib module assumes that the x axis starts at zero and moves by one on every element of the array. The following command lines illustrate this situation:

>>>from matplotlib.pyplot import *
>>>plot([1,2,3,10])
>>>show()

After we press the Enter key after typing the last command of show(), the following graph will appear:

At the bottom of the graph, we can find a set of icons, and based on them, we could adjust our image and other functions, such as saving our image. After closing the preceding figure, we could return to Python prompt. On the other hand, if we issue show() the second time, nothing will happen. To repeat the preceding graph, we have to issue both plot([1,2,3,10]) and show().

We...

Understanding simple and compounded interest rates


Many students and practitioners are confused with the difference between simple interest and compound interest. Simple interest does not consider interest on interest while compound interest does. It is a good idea to represent them with a graph. For instance, we borrow $1,000 today for 10 years with an annual interest of 8 percent per year. What are the future values if 8 percent is the simple interest and compounded interest rate? The formula for payment of a simple interest rate is as follows:

The future value for compounded interest is as follows:

Here, PV is the load we borrow today, that is, present value, R is the period rate, and n is the number of periods. Thus, those two future values will be $1,800 and $2,158.93. The following program offers a graphic representation of a principal, simple interest payment, and the future values:

import numpy as np
from matplotlib.pyplot import *
from pylab import *
pv=1000
r=0.08
n=10
t=linspace...

Adding texts to our graph


In the following example, we simply insert a text. Remember that the x and y scale is from 0 to 1:

>>>from pylab import *
>>>x = [0,1,2]
>>>y = [2,4,6]
>>>plot(x,y)
>>>figtext(0.2, 0.7, 'North & West')
>>>figtext(0.7, 0.2, 'East & South')
>>>show()

The corresponding output graph is given as follows:

Let's make it more complex. From the National Bureau of Economic Research web page at http://www.nber.org/cycles.html, we can find the following table showing the business cycle in the past two decades:

Working with DuPont identity


In finance, we could find useful information from a firm's financial statements such as annual income statement, balance sheet, and cash flow statement. Ratio analysis is one of the commonly used tools to compare the performance among different firms and for the same firm over the years. DuPont identity is one of them. DuPont identity divides Return on Equity (ROE) into three ratios: Gross Profit Margin, Assets Turnover, and Equity Multiplier:

The following code will show those three ratios with different colors. Here we have the following information about some firms:

Turning Point Date

Peak or Trough

Announcement Date

June 1, 2009

Trough

September 20, 2010

December 1, 2007

Peak

December 1, 2008

November 1, 2001

Trough

July 17, 2003

March 1, 2001

Peak

November 26, 2001

March 1, 1991

Trough

December 22, 1992

July 1, 1990

Peak

April 25, 1991

November 1, 1982

Trough

July 8, 1983

July 1, 1981

Peak

June 1, 1982

July 1, 1980

Trough

July 8,...

Ticker

Fiscal Year Ending Date

ROE

Gross Profit Margin

Assets Turnover

Equity Multiplier

IBM

December 31, 2012

0.8804

0.1589

0.8766

6.3209

DELL

February 1, 2013

0.2221

0.0417

1.1977

4.4513

WMT

January 31, 2013

0.2227

0.0362

2.3099

2.6604

The Python code is as follows:

import numpy as np
import matplotlib.pyplot as plt
ind = np.arange(3)
plt.title("DuPont Identity")
plt.xlabel...

Understanding the Net Present Value (NPV) profile


Gradually, we use graphs and other visual representations to explain many complex financial concepts, formulae, and trading strategies. An NPV profile is the relationship between a project's NPV and its discount rate (cost of capital). For a normal project, where cash outflows first then cash inflows, its NPV will be a decreasing function of the discount rate. The reason is that when the discount rate increases, the present value of the future cash flows (most time benefits) will decrease more than the current or the latest cash flows (most time costs). The NPV is defined by the following formula:

The following program demonstrates a negative correlation between NPV and the discount rate:

>>>import scipy as sp
>>>from matplotlib.pyplot import *
>>>cashflows=[-100,50,60,70]
>>>rate=[]
>>>npv=[]
>>>x=(0,0.7)
>>>y=(0,0)
>>>for i in range(1,70):
    rate.append(0.01*i)
  ...

Graphical representation of the portfolio diversification effect


In finance, we could remove firm-specific risk by combining different stocks in our portfolio. First, let us look at a hypothetical case by assuming that we have 5 years' annual returns of two stocks as follows:

Year

Stock A

Stock B

2009

0.102

0.1062

2010

-0.02

0.23

2011

0.213

0.045

2012

0.12

0.234

2013

0.13

0.113

We form an equal-weighted portfolio using those two stocks. Using the mean() and std() functions contained in NumPy, we can estimate their means, standard deviations, and correlation coefficients as follows:

>>>import numpy as np
>>>A=[0.102,-0.02, 0.213,0.12,0.13]
>>>B=[0.1062,0.23, 0.045,0.234,0.113]
>>>port_EW=(np.array(ret_A)+np.array(ret_B))/2.
>>>round(np.mean(A),3),round(np.mean(B),3),round(np.mean(port_EW),3)
(0.109, 0.146, 0.127)
>>>round(np.std(A),3),round(np.std(B),3),round(np.std(port_EW),3)
(0.075, 0.074, 0.027)

In the preceding code...

Retrieving historical price data from Yahoo! Finance


The function called quotes_historical_yahoo() in the matplotlib module could be used to download historical price data from Yahoo! Finance. For example, we want to download daily price data for IBM over the period from January 1, 2012 to December 31, 2012, we have the following four-line Python code:

>>>from matplotlib.finance import quotes_historical_yahoo
>>>date1=(2012, 1, 1)
>>>date2=(2012, 12,31)
>>>price=quotes_historical_yahoo('IBM', date1, date2)

To download IBM's historical price data up to today, we could use the datetime.date.today() function as follows:

>>>import datetime
>>>import matplotlib.finance as finance
>>>import matplotlib.mlab as mlab
>>>ticker = 'IBM'
>>>begdate = datetime.date(2013,1,1)
>>>enddate = datetime.date.today()
>>>price = finance.fetch_historical_yahoo(ticker, begdate, enddate)
>>>r = mlab.csv2rec...

Understanding the time value of money


In finance, we know that $100 received today is more valuable than $100 received one year later. If we use size to represent the difference, we could have the following Python program to represent the same concept:

from matplotlib.pyplot import *
fig1 = figure(facecolor='white')
ax1 = axes(frameon=False)
ax1.set_frame_on(False)
ax1.get_xaxis().tick_bottom() 
ax1.axes.get_yaxis().set_visible(False) 
x=range(0,11,2)
x1=range(len(x),0,-1)
y = [0]*len(x);
annotate("Today's value of $100 received today",xy=(0,0),xytext=(2,0.001),arrowprops=dict(facecolor='black',shrink=0.02))
annotate("Today's value of $100 received in 2 years",xy=(2,0.00005),xytext=(3.5,0.0008),arrowprops=dict(facecolor='black',shrink=0.02))
annotate("received in 6 years",xy=(4,0.00005),xytext=(5.3,0.0006),arrowprops=dict(facecolor='black',shrink=0.02))
annotate("received in 10 years",xy=(10,-0.00005),xytext=(4,-0.0006),arrowprops=dict(facecolor='black',shrink=0.02))
s = [50*2.5**n for n...

Candlesticks representation of IBM's daily price


We could use candlesticks to represent the daily opening, high, low, and closing prices. The vertical line represents high and low prices, while a rectangular bar represents open-close span. When the close price is higher than the opening price, we have a black bar. Otherwise, we would have a red bar. The following program will show exactly this:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.dates import DateFormatter, WeekdayLocator, HourLocator, DayLocator, MONDAY
from matplotlib.finance import quotes_historical_yahoo, candlestick,\
     plot_day_summary, candlestick2
date1 = ( 2013, 10, 20)
date2 = ( 2013, 11, 10 )
ticker='IBM'
mondays = WeekdayLocator(MONDAY)        # major ticks on the mondays
alldays    = DayLocator()               # minor ticks on the days
weekFormatter = DateFormatter('%b %d')  # e.g., Jan 12
dayFormatter = DateFormatter('%d')      # e.g., 12
quotes = quotes_historical_yahoo(ticker, date1, date2...

IBM's intra-day graphical representations


We could demonstrate the price movement of a stock for a given period, for example, from January 2009 to today. First, let's look at the intra-day price pattern. The following program will be explained in the next chapter:

import pandas as pd, numpy as np, datetime 
ticker='AAPL'
path='http://www.google.com/finance/getprices?q=ttt&i=60&p=1d&f=d,o,h,l,c,v'
p=np.array(pd.read_csv(path.replace('ttt',ticker),skiprows=7,header=None))
date=[]
for i in arange(0,len(p)):
    if p[i][0][0]=='a':
        t= datetime.datetime.fromtimestamp(int(p[i][0].replace('a','')))
        date.append(t)
    else:
        date.append(t+datetime.timedelta(minutes =int(p[i][0])))
final=pd.DataFrame(p,index=date)
final.columns=['a','Open','High','Low','Close','Vol']
del final['a']
x=final.index
y=final.Close
title('Intraday price pattern for ttt'.replace('ttt',ticker))
xlabel('Price of stock')
ylabel('Intro-day price pattern')
plot(x,y)
show()

The graph is shown...

Presenting both closing price and trading volume


Sometimes, we like to view both price movement and trading volumes simultaneously. The following program accomplishes this:

>>>from pylab import plotfile, show
>>>import matplotlib.finance as finance
>>>ticker = 'IBM'
>>>begdate = datetime.date(2013,1,1)
>>>enddate = datetime.date.today()
>>>x= finance.fetch_historical_yahoo(ticker, begdate, enddate)
>>>plotfile(x, (0,6,5))
>>>show()

The output graph is shown as follows:

Adding mathematical formulae to our graph

In finance, we use many mathematical formulae. Occasionally, we need to add a mathematical formula to our figure. A set of formulae for a call option by using the matplotlib module is shown in the following program:

import numpy as np
import matplotlib.mathtext as mathtext
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rc('image', origin='upper')
parser = mathtext.MathTextParser("Bitmap")
r'$\left[...

Performance comparisons among stocks


In the following program, we compare the performance of several stocks in terms of their returns in 2013:

import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.finance import quotes_historical_yahoo
stocks = ('IBM', 'DELL', 'WMT', 'C', 'AAPL')
begdate=(2013,1,1)
enddate=(2013,11,30)
def ret_annual(ticker):
    x = quotes_historical_yahoo(ticker, begdate, enddate,asobject=True, adjusted=True)
    logret = log(x.aclose[1:]/x.aclose[:-1])
    return(exp(sum(logret))-1)
performance = []
for ticker in stocks:
    performance.append(ret_annual(ticker))
y_pos = np.arange(len(stocks))
plt.barh(y_pos, performance, left=0, alpha=0.3)
plt.yticks(y_pos, stocks)
plt.xlabel('Annual returns ')
plt.title('Performance comparisons (annual return)')
plt.show()

The related bar chart is shown in the following figure:

Comparing return versus volatility for several stocks


The following program shows the locations of five stocks on the return versus volatility graph:

import numpy as np
import matplotlib.pyplot as plt; plt.rcdefaults()
from matplotlib.finance import quotes_historical_yahoo
stocks = ('IBM', 'GE', 'WMT', 'C', 'AAPL')
begdate=(2013,1,1)
enddate=(2013,11,30)
def ret_vol(ticker):
    x = quotes_historical_yahoo(ticker,begdate,enddate,asobject=True,adjusted=True)
    logret = log(x.aclose[1:]/x.aclose[:-1])
    return(exp(sum(logret))-1,std(logret))    
ret=[];vol=[]
for ticker in stocks:
    r,v=ret_vol(ticker)
    ret.append(r)
    vol.append(v*sqrt(252))
labels = ['{0}'.format(i) for i in stocks]
xlabel('Volatility (annualized)')
ylabel('Annual return')
title('Return vs. volatility')
plt.subplots_adjust(bottom = 0.1)
color=np.array([ 0.18,  0.96,  0.75,  0.3,  0.9])
plt.scatter(vol, ret, marker = 'o',  c=color,s = 1000,cmap=plt.get_cmap('Spectral'))
for label, x, y in zip(labels, vol, ret)...

Finding manuals, examples, and videos


The web page offering examples is http://matplotlib.org/examples/index.html. We believe it is a good idea to study the examples given on the web page before writing our own applications. The web pages related to matplotlib are as follows:

The web page of 5,000 examples is as follows:

How do we install ActivePython and matplotlib?

Visual financial statements can be found at the following location:

Installing the matplotlib module independently


There are two steps to install the module:

  1. Go to http://matplotlib.org/downloads.html.

  2. Choose an appropriate package and download it, such as matplotlib-1.2.1.win-amd64-py3.2.exe.

Summary


In this chapter, we showed how to use the matplotlib module to vividly explain many financial concepts by using graph, pictures, color, and size. For example, in a two-dimensional graph, we showed a few stocks' returns and volatility, the NPV profile, multiple IRRs, and the portfolio diversification effect.

In Chapter 8, Statistical Analysis of Time Series, first we demonstrate how to retrieve historical time series data from several public data sources, such as Yahoo! Finance, Google Finance, Federal Reserve Data Library, and Prof. French's Data Library. Then, we discussed various statistical tests, such as T-test, F-test, and normality test. In addition, we presented Python programs to run capital asset pricing model (CAPM), run a Fama-French three-factor model, estimate the Roll (1984) spread, estimate Value at Risk (VaR) for individual stocks, and also estimate the Amihud (2002) illiquidity measure, and the Pastor and Stambaugh (2003) liquidity measure for portfolios. For the...

Exercises


1. What is the potential usage of matplotlib?

2. How do we install matplotlib?

3. Does the matplotlib module depend on NumPy? Does it depend on SciPy?

4. Write a Python function to generate an NPV profile with a set of input cash flows.

5. Write a Python function to download daily price time series from Yahoo! Finance.

6. We have six-year return vectors for two stocks and intend to construct a simple equal-weighted portfolio. Interpret the following Python codes and explain the result for the portfolio:

>>>A=[0.09,0.02, -0.13,0.20,-0.09,-0.03]
>>>B=[0.10,-0.3, -0.02,0.14,-0.13,0.23]
>>>C=[0.08,-0.16, 0.033,-0.24,0.053,-0.39]
>>>port_EW=(A+B)/3.

7. What is the standard deviation in terms of stock daily returns for the stocks of IBM, DELL, WMT, and C and GE in 2011?

8. How do we estimate a moving beta for a set of given tickers?

9. How do we generate a histogram in terms of daily returns for IBM? You can use five-year daily data from Yahoo! Finance.

10...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Python for Finance
Published in: Apr 2014Publisher: ISBN-13: 9781783284375
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Yuxing Yan

Yuxing Yan graduated from McGill University with a PhD in finance. Over the years, he has been teaching various finance courses at eight universities: McGill University and Wilfrid Laurier University (in Canada), Nanyang Technological University (in Singapore), Loyola University of Maryland, UMUC, Hofstra University, University at Buffalo, and Canisius College (in the US). His research and teaching areas include: market microstructure, open-source finance and financial data analytics. He has 22 publications including papers published in the Journal of Accounting and Finance, Journal of Banking and Finance, Journal of Empirical Finance, Real Estate Review, Pacific Basin Finance Journal, Applied Financial Economics, and Annals of Operations Research. He is good at several computer languages, such as SAS, R, Python, Matlab, and C. His four books are related to applying two pieces of open-source software to finance: Python for Finance (2014), Python for Finance (2nd ed., expected 2017), Python for Finance (Chinese version, expected 2017), and Financial Modeling Using R (2016). In addition, he is an expert on data, especially on financial databases. From 2003 to 2010, he worked at Wharton School as a consultant, helping researchers with their programs and data issues. In 2007, he published a book titled Financial Databases (with S.W. Zhu). This book is written in Chinese. Currently, he is writing a new book called Financial Modeling Using Excel — in an R-Assisted Learning Environment. The phrase "R-Assisted" distinguishes it from other similar books related to Excel and financial modeling. New features include using a huge amount of public data related to economics, finance, and accounting; an efficient way to retrieve data: 3 seconds for each time series; a free financial calculator, showing 50 financial formulas instantly, 300 websites, 100 YouTube videos, 80 references, paperless for homework, midterms, and final exams; easy to extend for instructors; and especially, no need to learn R.
Read more about Yuxing Yan