Book Image

Python for Finance

By : Yuxing Yan
Book Image

Python for Finance

By: Yuxing Yan

Overview of this book

Table of Contents (20 chapters)
Python for Finance
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Introduction to Python


Our society entered the information era many years ago. Actually, we are drowning in a sea of information, such as too many e-mails to read or too many web pages we could possibly explore. With the Internet, we could find a huge amount of information about almost everything such as important events or how to learn Python. We could find information for a specific firm by searching online. For instance, if we want to collect financial information associated with International Business Machines (IBM), we could use Yahoo! Finance, Google Finance, Securities and Exchange Commission (SEC) filings, and the company's web pages. Since we are confronted with a lot of publicly available information, investors, professionals, and researchers need a tool to process such a huge amount of information. In addition, our society would move towards a more open and transparent society. In finance, a new concept of open source finance has merged recently. Dane and Masters (2009) suggest three components for open source finance: open software, open data, and open codes. For the first component of open software, Python is one of the best choices. An equally popular open source software is R. In the next section, we summarize the advantages of learning and applying Python to finance.

Firstly, Python is free in terms of license. Being free has many benefits. Let's perform a simple experiment here. Let's assume readers know nothing about Python and they have no knowledge about option theory. How long do you think it would take them to run a Python program to price a Black-Scholes call option? Less than 2 hours? Here is what they could do; they could download and install Python after reading the Installing Python section of this chapter, and it would take less than 10 minutes. Spend another 10 minutes to launch and quit Python and also try a few examples. Then, read the first page of Chapter 4, 13 Lines of Python to Price a Call Option, which contains the code for the famous Black-Scholes call option model. In total, the program has 13 lines. The reader could spend the next 40 minutes typing, correcting typos, and retyping those 13 lines. With less than 2 hours, they should be able to run the program to price a call option. The cost of adopting a new computer language includes many aspects such as annual license cost, maintenance costs, available packages, and support.

Another example is related to an SEC proposal. In 2010, the SEC proposed that all financial institutions are to accompany their new Asset-Backed Security (ABS) with a computer program showing the contractual cash flows of the securities (www.sec.gov/rules/proposed/2010/33-9117.pdf). The proposed computer language is Python. Obviously, any investor can access Python because it is free.

For bond analytics or credit risks, Roger Ehrenberg (2007) argues for an open source approach. Whether or not ratings should be required for institutional investors to buy certain securities is not the issue; the essential point is getting better transparency and analysis of instruments constituting the investable universe. Just imagine what the impact would be if many financial institutions adopt the open source initiative by storing their own debt ratings models into the public domain and allowing others to contribute to its development! To contribute to such an open source approach, Python (or R, free software as well) would be ideal in terms of computational tools.

Secondly, Python is powerful, flexible, and easy to learn. It is capable of solving almost all our financial and economic estimations. Python is available for all major operating systems such as Windows, Linux/Unix, OS/2, Mac, and Amiga, among others.

Thirdly, Python is suitable for Big Data. Dasgupta (2013) argues that R and Python are two of the most popular open source programming languages for data analysis. Compared with R, Python is generally a better overall language, especially when you consider its blend of functional programming with object orientation. Combined with Scipy/Numpy, Matplotlib, and Statsmodel, it provides a powerful tool. In this book, we will discuss a module called Pandas when we deal with financial data.

Fourthly, there are many useful modules for Python, such as toolboxes in MATLAB and packages in R. Each model is developed for a special purpose. Later in the book, we will touch base with about a dozen modules. However, we will pay special attention to five of the most useful modules in finance: NumPy, SciPy, Matplotlib, Statsmodels, and Pandas. The first two modules are associated with mathematical estimations, formulae, matrices and their manipulation, data formats, and data manipulations. Matplotlib is for visual presentations such as graphs. In Chapter 8, Introduction to the Black-Scholes Option Model, we use this module intensively to explain visually different payoff functions and profit/loss functions for various trading strategies. The Statsmodels module deals with econometrics such as T-test, F-test, and GARCH models. Again, the Pandas module is used for financial data analysis.

We should mention some disadvantages of Python as well. The most important shortcoming is the lack of support because it is free. Some experts argue that the Python community needs to grow and should include more statisticians and mathematicians.