39,59 €
This book uses Python as its computational tool. Since Python is free, any school or
organization can download and use it.
This book is organized according to various finance subjects. In other words, the first edition focuses more on Python, while the second edition is truly trying to apply Python to finance.
The book starts by explaining topics exclusively related to Python. Then we deal with critical parts of Python, explaining concepts such as time value of money stock and bond evaluations, capital asset pricing model, multi-factor models, time series analysis, portfolio theory,
options and futures.
This book will help us to learn or review the basics of quantitative finance and apply Python to solve various problems, such as estimating IBM’s market risk,
running a Fama-French 3-factor, 5-factor, or Fama-French-Carhart 4 factor model, estimating the VaR of a 5-stock portfolio, estimating the optimal portfolio, and constructing the efficient frontier for a 20-stock portfolio with real-world stock, and with Monte Carlo Simulation. Later, we will also learn how to replicate the famous Black-Scholes-Merton option model and how to price exotic options such as the average price call option.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 559
Veröffentlichungsjahr: 2017
Copyright © 2017 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: April 2014
Second edition: June 2017
Production reference: 1270617
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78712-569-8
www.packtpub.com
Author
Yuxing Yan
Reviewers
Dr. Param Jeet
Nabih Ibrahim Bawazir, M.Sc.
Joran Beasley
Commissioning Editor
Amey Varangaonkar
Acquisition Editor
Tushar Gupta
Content Development Editor
Amrita Noronha
Technical Editor
Akash Patel
Copy Editor
Safis Editing
Project Coordinator
Shweta H Birwatkar
Proofreader
Safis Editing
Indexer
Mariammal Chettiyar
Graphics
Tania Dutta
Production Coordinator
Nilesh Mohite
Cover Work
Nilesh Mohite
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.
I would like to thank Ben Amoako-Adu, Brian Smith (who taught me the first two finance courses and offered unstinting support for many years after my graduation), George Athanassakos (one of his assignments "forced" me to learn C), and Jin-Chun Duan.
I would also like to thank Wei-Hung Mao, Jerome Detemple, Bill Sealey, Chris Jacobs, Mo Chaudhury, Summon Mazumdar (my former professors at McGill), and Lawrence Kryzanowski. (His wonderful teaching inspired me to concentrate on empirical finance and he edited my doctoral thesis word by word even though he was not my supervisor!). There is no doubt that my experience at Wharton has shaped my thinking and enhanced my skill sets. I thank Chris Schull and Michael Boldin for offering me the job; Mark Keintz, Dong Xu, Steven Crispi, and Dave Robinson, my former colleagues, who helped me greatly during my first two years at Wharton; and Eric Zhu, Paul Ratnaraj, Premal Vora, Shuguang Zhang, Michelle Duan, Nicholle Mcniece, Russ Ney, Robin Nussbaum-Gold, and Mireia Gine for all their help. In addition, I'd like to thank Shaobo Ji, Tong Yu, Shaoming Huang, Xing Zhang.
Dr. Param Jeet has a Ph.D. in mathematics from one of India's leading engineering institutes, IIT Madras. Dr. Param Jeet has a decade of experience in the data analytics industry. He started his career with Bank of America and since then worked with a few companies as a data scientist. He has also worked across domains such as capital market, education, telecommunication and healthcare. Dr. Param Jeet has expertise in Quantitative finance, Data analytics, machine learning, R, Python, Matlab, SQL, and big data technologies. He has also published a few research papers in reputed international journals, published and reviewed books, and has worked on Learning Quantitative Finance with R.
Nabih Ibrahim Bawazir, M.Sc. is a data scientist at an Indonesian financial technology start-up backed by Digital Alpha Group, Pte Ltd., Singapore. Most of his work is research on the development phase, from financial modeling to data-driven underwriting. Previously, he worked as actuary in CIGNA. He holds M.Sc in Financial Mathematics from Gadjah Mada University, Indonesia.
Joran Beasley received his degree in computer science from the University of Idaho. He works has been programming desktop applications in wxPython professionally for monitoring large scale sensor networks for use in agriculture for the last 7 years. He currently lives in Moscow Idaho, and works at Decagon Devices Inc. as a software engineer.
I would like to thank my wife Nicole, for putting up with my long hours hunched over a keyboard, and her constant support and help in raising our two wonderful children.
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.
Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/1787125696. If you'd like to join our team of regular reviewers, you can e-mail us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!
It is our firm belief that an ambitious student major in finance should learn at least one computer language. The basic reason is that we have entered a so-called big data era. In finance, we have a huge amount of data, and most of it is publically available free of charge. To use such rich sources of data efficiently, we need a tool. Among many potential candidates, Python is one of the best choices.
For the second edition, we have reorganized the structure of the book by adding more chapters related to finance. This is recognition and response to the feedbacks from numerous readers. For the second edition, the first two chapters are exclusively devoted to Python. After that, all remaining chapters are associated with finance. Again, Python in this book is used as a tool to help readers learn and understand financial theories better. To meet the demand of using all types of data by various quantitative programs, business analytics programs and financial engineering programs, we add Chapter 4, Sources of Data. Because of this restructuring, this edition is more suitable for a one-semester course such as Quantitative Finance, Financial Analysis using Python and Business Analytics. Two finance professors, Premal P. Vora, at Penn State University, Sheng Xiao, at Westminister College, have adopted the first edition as their textbook. Hopefully, more finance, accounting professors would find the second edition is more suitable for their students, especially for those students from a financial engineering program, business analytics and other quantitative areas.
There are various reasons that Python should be used. Firstly, Python is free in terms of license. Python is available for all major operating systems, such as Windows, Linux/Unix, OS/2, Mac, and Amiga, among others. Being free has many benefits. When students graduate, they could apply what they have learned wherever they go. This is true for the financial community as well. In contrast, this is not true for SAS and MATLAB. Secondly, Python is powerful, flexible, and easy to learn. It is capable of solving almost all our financial and economic estimations. Thirdly, we could apply Python to big data. Dasgupta (2013) argues that R and Python are two of the most popular open source programming languages for data analysis. Fourthly, there are many useful modules in Python. Each model is developed for a special purpose. In this book, we focus on NumPy, SciPy, Matplotlib, Statsmodels, and Pandas modules.
There is no doubt that the majority of programming books are written by professors from computer science. It seems odd that a finance professor writes a programming book. It is understandable that the focus would be quite different. If an instructor from computer science were writing this book, naturally the focus would be Python, whereas the true focus should be finance. This should be obvious from the title of the book Python for Finance. This book intends to change the fact that many programming books serving the finance community have too much for the language itself and too little for finance. Another unique feature of the book is that it uses a huge amount public data related to economics, finance and accounting, see Chapter 4, Sources of Data for more details.
Chapter 1, Python Basics, offers a short introduction, and explains how to install Python, how to launch and quit Python, variable assignment, vector, matrix and Tuple, calling embedded functions, write your own functions, input data from an input file, simple data manipulations, output our data and results, and generate a Python dataset with an extension of pickle.
Chapter 2, Introduction to Python Modules, discusses the meaning of a module, how to import a module, show all functions contained in an imported module, adopt a short name for an imported module, compare between import math and from math import, delete an imported module, import just a few functions from a module, introduction to NumPy, SciPy, matplotlib, statsmodels, pandas and Pandas_reader, find out all built-in modules and all available (preinstalled) modules, how to find a specific uninstalled module.
Chapter 3, Time Value of Money, introduces and discusses various basic concepts and formulae associated with finance, such as present value of one future cash flow, present value of (growing) perpetuity, present and future value of annuity, perpetuity vs. perpetuity due, annuity vs. annuity due, relevant functions contained in SciPy and numpy.lib.financial submodule, a free financial calculator, written in Python, definition of NPV (Net Present Value) and its related rule, definition of IRR (Internal Rate of Return) and its related rule, Python graphical presentation of time value of money, and NPV profile.
Chapter 4, Sources of Data, discusses how to retrieve data from various public sources, such as Yahoo!Finance, Google finance, FRED (Federal Reserve Bank's Economics Data Library), Prof. French's Data Library, BLS (Bureau of Labor Statistics) and Census Bureau. In addition, it would discuss various methods to input data, such as files with formats of csv, txt, pkl, Matlab, SAS or Excel.
Chapter 5, Bond and Stock Valuation, introduces interest rate and its related concepts, such as APR (Annual Percentage Rate), EAR (Effective Annual Rate), compounding frequency, how to convert one effective rate to another one, the term structure of interest rate, how to estimate the selling price of a regular bond, how to use the so-called discount dividend model to estimate the price of a stock and so on.
Chapter 6, Capital Asset Pricing Model, shows how to download data from Yahoo!Finance in order to run a linear regression for CAPM, rolling beta, several Python programs to estimate beta for multiple stocks, adjusted beta and portfolio beat estimation, two beta adjustment methods by Scholes and Williams (1977) Dimson (1979).
Chapter 7, Multifactor Models and Performance Measures, shows how to extend the single-factor model, described in Chapter 6, Capital Asset Pricing Model, to multifactor and complex models such as the Fama-French three-factor model, the Fama-French-Carhart four-factor model, and the Fama-French five-factor model, and performance measures such as the Sharpe ratio, Treynor ratios, Sortino ratio, and Jensen's alpha.
Chapter 8, Time-Series Analysis, shows how to design a good date variable, merge datasets by this date variable, normal distribution, normality tests, term structure of interest rate, 52-week high and low trading strategy, return estimation, convert daily returns to monthly or annual returns, T-test, F-test, Durbin-Watson test for autocorrelation, Fama-MacBeth regression, Roll (1984) spread, Amihud's (2002) illiquidity, Pastor and Stambaugh's (2003) liquidity measure, January effect, weekday effect, retrieving high-frequency data from Google Finance and from Prof. Hasbrouck's TORQ database (Trade, Order, Report and Quotation) and introduction to CRSP (Center for Research in Security Prices) database.
Chapter 9, Portfolio Theory, discusses mean and risk estimation of a 2-stock portfolio, N-stock portfolio, correlation vs. diversification effect, how to generate a return matrix, generating an optimal portfolio based on the Sharpe ratio, the Treynor ratio and the Sortinor ratio; how to construct an efficient frontier; Modigliani and Modigliani performance measure (M2 measure); and how to estimate portfolio returns using value-weighted and equal-weighed methodologies.
Chapter 10, Options and Futures, discusses payoff and profit/loss functions for calls and puts and their graphical representations, European versus American options; normal distribution; standard normal distribution; cumulative normal distribution; the famous Black-Scholes-Merton option model with/without dividend; various trading strategies and their visual presentations, such as covered call, straddle, butterfly, and calendar spread; Greeks; the put-call parity and its graphical representation; a graphical representation of a one-step and a two-step binomial tree model; how to use the binomial tree method to price both European and American options; and implied volatility, volatility smile, and skewness.
Chapter 11, Value at Risk, first reviews the density and cumulative functions of a normal distribution, then discusses the first method to estimate VaR based on the normality assumption, conversion from one day risk to n-day risk, one-day VaR to n-day VaR, normality tests, impact of skewness and kurtosis, modifying the VaR measure by including both skewness and kurtosis, the second method to estimate VaR based on historical returns, how to link two methods by using Monte Carlo simulation, back testing, and stress testing.
Chapter 12, Monte Carlo Simulation, discusses how to estimate the π value by using Monte Carlo simulation; simulating stock price movement with a lognormal distribution; constructing efficient portfolios and an efficient frontier; replicating the Black-Scholes-Merton option model by simulation; pricing several exotic options, such as lookback options with floating strikes; bootstrapping with/without replacements; long term expected return forecast and a related efficiency, quasi Monte Carlo simulation, and Sobol sequence.
Chapter 13, Credit Risk Analysis, discusses Moody's, Standard & Poor's, and Fitch's credit ratings, credit spread, 1-year and 5-year migration matrices, term structure of interest rate, Altman's Z-score to predict corporate bankruptcy, the KMV model to estimate total assets and its volatility, default probability and distance to default, and credit default swap.
Chapter 14, Exotic Options, first compares European and American options we learned about in Chapter 9, Portfolio Theory with Bermudan options, then discusses methods to price simple chooser options; shout, rainbow, and binary options; the average price option; barrier options such as the up-and-in option and the up-and-out option; and barrier options such as down-and-in and down-and-out options.
Chapter 15, Volatility, Implied Volatility, ARCH, and GARCH, focuses on two issues: volatility measures and ARCH/GARCH.
Based on the author's teaching experience at seven schools, McGill and Wilfrid Laurier University (in Canada), NTU (in Singapore), and Loyola University, Maryland, UMUC, Hofstra University, and Canisius College (in the United States), and his eight-year consulting experience at Wharton School, he knows that many finance students like small programs that solve one specific task. Most programming books offer just a few complete and complex programs. The number of programs is far too less than enough few. There are two side effects to such an approach. First, finance students are drowned in programming details, get intimidated, and eventually lose interest in learning a computer language. Second, they don't learn how to apply what they just learned, such as running a capital asset pricing model (CAPM) to estimate IBM's beta from 1990 to 2013. This book offers about 300 complete Python programs around many finance topics.
Another shortcoming of the majority of books for programming is that they use hypothetical data. In this book, we use real-world data for various financial topics. For example, instead of showing how to run CAPM to estimate the beta (market risk), I show you how to estimate IBM's, Apple's, or Walmart's betas. Rather than just presenting formulae that shows you how to estimate a portfolio's return and risk, the Python programs are given to download real-world data, form various portfolios, and then estimate their returns and risk, including Value at Risk (VaR). When I was a doctoral student, I learned the basic concept of volatility smiles. However, until writing this book, I had a chance to download real-world data to draw IBM's volatility smile.
Here, we use several concrete examples to show what a reader could achieve after going through this book carefully.
First, after reading the first two chapters, a reader/student should be able to use Python to calculate the present value, future value, present value of annuity, IRR (internal rate of return), and many other financial formulae. In other words, we could use Python as a free ordinary calculator to solve many finance problems. Second, after the first three chapters, a reader/student or a finance instructor could build a free financial calculator, that is, combine a few dozen small Python programs into a big Python program. This big program behaves just like any other module written by others. Third, readers learn how to write Python programs to download and process financial data from various public data sources, such as Yahoo! Finance, Google Finance, Federal Reserve Data Library, and Prof. French's Data Library.
Fourth, readers will understand basic concepts associated with modules, which are packages written by experts, other users, or us, for specific purposes. Fifth, after understanding the Matplotlib module, readers can produce various graphs. For instance, readers could use graphs to demonstrate payoff/profit outcomes based on various trading strategies by combining the underlying stocks and options. Sixth, readers will be able to download IBM's daily price, the S&P 500 index price, and data from Yahoo! Finance and estimate its market risk (beta) by applying CAPM. They will also be able to form a portfolio with different securities, such as risk-free assets, bonds, and stocks. Then, they can optimize their portfolios by applying Markowitz's mean-variance model. In addition, readers will know how to estimate the VaR of their portfolios.
Seventh, a reader should be able to price European and American options by applying both the Black-Scholes-Merton option model for European options only, and the Monte Carlo simulation for both European and American options. Last but not least, readers will learn several ways to measure volatility. In particular, they will learn how to use AutoRegressive Conditional Heteroskedasticity (ARCH) and Generalized AutoRegressive Conditional Heteroskedasticity (GARCH) models.
If you are a graduate student majoring in finance, especially studying computational finance, financial modeling, financial engineering, or business analytics, this book will benefit you greatly. Here are two examples: Prof. Premal P. Vora at Penn State University has used this book for his course titled Data Science in Finance, and Prof. Sheng Xiao at Westminister College has done so for his course titled Financial Analytics. If you are a professional, you could learn Python and use it in many financial projects. If you are an individual investor, you could benefit from reading this book as well.
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The sqrt(), square root, function is contained in the math module."
A block of code is set as follows:
Any command-line input or output is written as follows:
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "To write a Python program, we click File, then New File."
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <[email protected]>, and mention the book title via the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Python-for-Finance-Second-Edition. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the erratasubmissionform link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
You can contact us at <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.
In this chapter, we will discuss basic concepts and several widely used functions related to Python. This chapter plus the next one (Chapter 2, Introduction to Python Modules) are only the chapters exclusively based on Python techniques. Those two chapters serve as a review for readers who have some basic Python knowledge. There is no way that a beginner, with no prior Python knowledge, could master Python by reading just those two chapters. For a new learner who wants to learn Python in more detail, he/she could find many good books. From Chapter 3, Time Value of Money onward, we will use Python, which will help in explaining or demonstrating various finance concepts, running regression, and processing data related to economics, finance, and accounting. Because of this, we will offer more Python-related techniques and usages in each of the upcoming chapters.
In particular, in this chapter, we will discuss the following topics:
In this section, we will discuss how to install Python. More specifically, we will discuss two methods: installing Python via Anaconda and installing Python directly.
There are several reasons why the first method is preferred:
We could install Python in several ways. The consequence is that we will have different environments for writing a Python program and running a Python program.
The following is a simple two-step approach. First, we go to http://continuum.io/downloads and find an appropriate package; see the following screenshot:
For Python, different versions coexist. From the preceding screenshot, we see that there exist two versions, 3.5 and 2.7.
For this book, the version is not that critical. The old version had fewer problems while the new one usually has new improvements. Again, module dependency could be a big headache; see Chapter 2, Introduction to Python Modules for more detail. The version of Anaconda is 4.2.0. Since we will launch Python through Spyder, it might have different versions as well.
After Python is installed via Anaconda, we can navigate to Start (for a Windows version) |All Programs |Anaconda3(32-bit), as shown in the following screenshot:
After we click Spyder, the last entry in the preceding screenshot, we will see the following four panels:
The top-left panel (window) is our program editor, where we write our programs. The bottom-right panel is the IPython console, where we cantype our simple commands. IPython is the default one. To know more about IPython, just type a question mark; see the following screenshot:
Alternatively, we could launch Python console by clicking Consoles on the menu bar and then Open a Python console. After that, the following window will appear:
From the image with four panels, the top-right panel is our help window, where we can seek help. The middle one is called Variable Explorer, where the names of variables and their values are shown. Depending on personal preference, users will scale those panels or reorganize them.
For most users, knowing how to install Python via Anaconda is more than enough. Just for completeness, here the second way to install Python is presented.
The following steps are involved:
First, for Python language, an empty space or spaces is very important. For example, if we accidently have a space before typing pv=100, we will see the following error message:
The name of the error is called IndentationError. The reason is that, for Python, indentation is important. Later in the chapter, we will learn that a proper indentation will regulate/define how we write a function or why a group of codes belongs to a specific topic, function, or loop.
Assume that we deposit $100 in the bank today. What will be the value 3 years later if the bank offers us an annual deposit rate of 1.5%? The related codes is shown here:
In the preceding codes, ** means a power function. For example, 2**3 has a value of 8. To view the value of a variable, we simply type its name; see the previous example. The formula used is given here:
Here, FV is the future value, PV is the present value, R is the period deposit rate while n is the number of periods. In this case, R is the annual rate of 0.015 while n is 3. At the moment, readers should focus on simple Python concepts and operations.
In Chapter 3, Time Value of Money, this formula will be explained in detail. Since Python is case-sensitive, an error message will pop up if we type PV instead of pv; see the following code:
Unlike some languages, such as C and FORTRAN, for Python a new variable does not need to be defined before a value is assigned to it. To show all variables or function, we use the dir() function:
To find out all built-in functions, we type dir(__builtings__). The output is shown here:
Assume that we are interested in writing a Python function for equation (1).
After launching Spyder, click File, then New File. We write the following two lines, as shown in the left panel. The keyword def is for function,fv_f is the function name, and the three values of pv, r , and n in the pair of parentheses are input variables.
The colon (:) indicates the function hasn't finished yet. After we hit the Enter key, the next line will be automatically indented.
After we enter return pv*(1+r)**n and hit the Enter key twice, this simple program is completed. Obviously, for the second line, ** represents a power function.
Assume that we save it under c:/temp/temp.py:
To run or debug the program, click the arrow key under Run on the menu bar; see the preceding top-right image. The compiling result is shown by the bottom image right (the second image on top right). Now, we can use this function easily by calling it with three input values:
If some comments are added by explaining the meanings of input variables, the formula used, plus a few examples, it will be extremely helpful for other users or programmers. Check the following program with comments:
The comments or explanations are included in a pair of three double quotation marks (""" and """). The indentation within a comment is not consequential. When compiling, the underlying software will ignore all comments. The beauty of those comments is that we can use help(pv_f) to see them, as illustrated here:
In Chapter 2, Introduction to Python Modules, we will show how to upload a financial calculator written in Python, and in Chapter 3, Time Value of Money, we will explain how to generate such a financial calculator.
Let's generate a very simple input dataset first, as shown here. Its name and location is c:/temp/test.txt. The format of the dataset is text:
The code is shown here:
The print() function could be used to show the value of x:
For the second example, let's download the daily historical price for IBM from Yahoo!Finance first. To do so, we visit http://finance.yahoo.com:
Enter IBM to find its related web page. Then click Historical Data, then click Download:
Assume that we save the daily data as ibm.csv under c:/temp/. The first five lines are shown here:
The first line shows the variable names: date, open price, high price achieved during the trading day, low price achieved during the trading day, close price of the last transaction during the trading day, trading volume, and adjusted price for the trading day. The delimiter is a comma. There are several ways of loading the text file. Some methods are discussed here:
Alternatively, we could download the IBM daily price data directly from Yahoo!Finance; see the following code:
We could retrieve data from an Excel file by using the ExcelFile() function from thepandas module. First, we generate an Excel file with just a few observations; see the following screenshot:
Let's call this Excel file stockReturns.xlxs and assume that it is saved under c:/temp/. The Python code is given here:
To retrieve Python datasets with an extension of .pkl or .pickle, we can use the following code. First, we download the Python dataset called ffMonthly.pkl from the author's web page at http://www3.canisius.edu/~yany/python/ffMonthly.pkl.
Assume that the dataset is saved under c:/temp/. The function called read_pickle() included in the pandas module can be used to load the dataset with an extension of .pkl or .pickle:
The following is the simplest if function: when our interest rate is negative, print a warning message:
Conditions related to logical AND and OR are shown here:
For the multiple if...elif conditions, the following program illustrates its application by converting a number grade to a letter grade:
Note that it is a good idea for such multiple if...elif functions to end with an else condition since we know exactly what the result is if none of those conditions are met.
There are many different types of data, such as integer, real number, or string. The following table offers a list of those data types:
Data types
Description
Bool
Boolean (TRUE or FALSE) stored as a byte
Int
Platform integer (normally either int32 or int64)
int8
Byte (-128 to 127)
int16
Integer (-32768 to 32767)
int32
Integer (-2147483648 to 2147483647)
int64
Integer (9223372036854775808 to 9223372036854775807)
unit8
Unsigned integer (0 to 255)
unit16
Unsigned integer (0 to 65535)
unit32
Unsigned integer (0 to 4294967295)
unit64
Unsigned integer (0 to 18446744073709551615)
float
Short and for float6
float32
Single precision float: sign bit23 bits mantissa; 8 bits exponent
float64
52 bits mantissa
complex
Shorthand for complex128
complex64
Complex number; represented by two 32-bit floats (real and imaginary components)
complex128
Complex number; represented by two 64-bit floats (real and imaginary components)
Table 1.1 List of different data types
In the following examples, we assign a value to r, which is a scalar, and several values to pv, which is an array (vector).The type() function is used to show their types:
To choose the appropriate decision, we use the round()function; see the following example:
For data manipulation, let's look at some simple operations:
Some so-called dot functions are quite handy and useful:
Anything after the number sign of # will be a comment. Arrays are another important data type:
We could assign a string to a variable:
To find out all string-related functions, we use dir(''); see the following code:
For example, from the preceding list we see a function called split. After typinghelp(''.split), we will have related help information:
We could try the following example:
Matrix manipulation is important when we deal with various matrices:
The condition for equation (3) is that matrices A and B should have the same dimensions. For the product of two matrices, we have the following equation:
Here,A is an n by k matrix (n rows and k columns), while B is a k by m matrix. Remember that the second dimension of the first matrix should be the same as the first dimension of the second matrix. In this case, it is k. If we assume that the individual data items in C, A, and B are Ci,j (the ith row and the jth column), Ai,j, and Bi,j, we have the following relationship between them:
The dot() function from the NumPy module could be used to carry the preceding matrix multiplication:
We could manually calculate c(1,1): 1*1 + 2*3 + 3*4=19.
After retrieving data or downloading data from the internet, we need to process it. Such a skill to process various types of raw data is vital to finance students and to professionals working in the finance industry. Here we will see how to download price data and then estimate returns.
Assume that we have n values of x1, x2, … and xn. There exist two types of means: arithmetic mean and geometric mean; see their genetic definitions here:
Assume that there exist three values of 2,3, and 4. Their arithmetic and geometric means are calculated here:
For returns, the arithmetic mean's definition remains the same, while the geometric mean of returns is defined differently; see the following equations:
In Chapter 3, Time Value of Money, we will discuss both means again.
We could say that NumPy is a basic module while SciPy is a more advanced one. NumPy tries to retain all features supported by either of its predecessors, while most new features belong in SciPy rather than NumPy. On the other hand, NumPy and SciPy have many overlapping features in terms of functions for finance. For those two types of definitions, see the following example:
Our second example is related to processing theFama-French 3 factor time series. Since this example is more complex than the previous one, if a user feels it is difficult to understand, he/she could simply skip this example. First, a ZIP file called F-F_Research_Data_Factor_TXT.zip could be downloaded from Prof. French's Data Library. After unzipping and removing the first few lines and annual datasets, we will have a monthly Fama-French factor time series. The first few lines and last few lines are shown here:
Assume that the final file is called ffMonthly.txt under c:/temp/. The following program is used to retrieve and process the data: