PacktLib: Python for Finance

Python for Finance

Credits

About the Author

Acknowledgments

About the Reviewers

www.PacktPub.com

Preface

Introduction and Installation of Python

Introduction to Python

Installing Python

Different versions of Python

Ways to launch Python

Quitting Python

Error messages

Python language is case sensitive

Initializing the variable

Finding the help window

Finding manuals and tutorials

Finding the version of Python

Summary

Exercises

Using Python as an Ordinary Calculator

Assigning values to variables

Error messages

Choosing meaningful names

Using dir() to find variables and functions

Basic math operations – addition, subtraction, multiplication, and division

The power function, floor, and remainder

Choosing appropriate precision

Finding out more information about a specific built-in function

Importing the math module

A few frequently used functions

The tuple data type

Summary

Exercises

Using Python as a Financial Calculator

Writing a Python function without saving it

Default input values for a function

Indentation is critical in Python

Checking the existence of our functions

Defining functions from our Python editor

Activating our function using the import function

Debugging a program from a Python editor

Two ways to call our pv_f() function

Generating our own module

Types of comments

The if() function

Annuity estimation

Converting the interest rates

Continuously compounded interest rate

A data type – list

Net present value and the NPV rule

Defining the payback period and the payback period rule

Defining IRR and the IRR rule

Showing certain files in a specific subdirectory

Using Python as a financial calculator

Adding our project directory to the path

Summary

Exercises

13 Lines of Python to Price a Call Option

Writing a program – the empty shell method

Writing a program – the comment-all-out method

Using and debugging other programs

Summary

Exercises

Introduction to Modules

What is a module?

Importing a module

Module dependency

Summary

Exercises

Introduction to NumPy and SciPy

Installation of NumPy and SciPy

Launching Python from Anaconda

Showing all functions in NumPy and SciPy

More information about a specific function

Understanding the list data type

Working with arrays of ones, zeros, and the identity matrix

Performing array manipulations

Performing array operations with +, -, *, /

The x.sum() dot function

Looping through an array

Using the help function related to modules

A list of subpackages for SciPy

Cumulative standard normal distribution

Logic relationships related to an array

Statistic submodule (stats) from SciPy

Interpolation in SciPy

Solving linear equations using SciPy

Generating random numbers with a seed

Finding a function from an imported module

Understanding optimization

Linear regression and Capital Assets Pricing Model (CAPM)

Retrieving data from an external text file

Installing NumPy independently

Understanding the data types

Summary

Exercises

Visual Finance via Matplotlib

Installing matplotlib via ActivePython

Alternative installation via Anaconda

Understanding how to use matplotlib

Understanding simple and compounded interest rates

Adding texts to our graph

Working with DuPont identity

Understanding the Net Present Value (NPV) profile

Graphical representation of the portfolio diversification effect

Retrieving historical price data from Yahoo! Finance

Understanding the time value of money

Candlesticks representation of IBM's daily price

IBM's intra-day graphical representations

Presenting both closing price and trading volume

Performance comparisons among stocks

Comparing return versus volatility for several stocks

Finding manuals, examples, and videos

Installing the matplotlib module independently

Summary

Exercises

Statistical Analysis of Time Series

Installing Pandas and statsmodels

Using Pandas and statsmodels

Open data sources

Retrieving data to our programs

Several important functionalities

Return estimation

Merging datasets by date

T-test and F-test

Many useful applications

Constructing an efficient frontier

Understanding the interpolation technique

Outputting data to external files

Python for high-frequency data

More on using Spyder

A useful dataset

Summary

Exercise

The Black-Scholes-Merton Option Model

Payoff and profit/loss functions for the call and put options

European versus American options

Cash flows, types of options, a right, and an obligation

Normal distribution, standard normal distribution, and cumulative standard normal distribution

The Black-Scholes-Merton option model on non-dividend paying stocks

The p4f module for options

European options with known dividends

Various trading strategies

Relationship between input values and option values

Greek letters for options

The put-call parity and its graphical representation

Binomial tree (the CRR method) and its graphical representation

Hedging strategies

Summary

Exercises

Python Loops and Implied Volatility

Definition of an implied volatility

Understanding a for loop

Estimation of IRR via a for loop

Understanding a while loop

Using keyboard commands to stop an infinitive loop

Estimating implied volatility by using an American call

Measuring efficiency by time spent in finishing a program

The mechanism of a binary search

Sequential versus random access

Looping through an array/DataFrame

Retrieving option data from CBOE

Retrieving option data from Yahoo! Finance

The put-call ratio

Summary

Exercises

Monte Carlo Simulation and Options

Generating random numbers from a standard normal distribution

Generating random numbers from a uniform distribution

Using simulation to estimate the pi value

Generating random numbers from a Poisson distribution

Bootstrapping with/without replacements

Distribution of annual returns

Simulation of stock price movements

Finding an efficient portfolio and frontier

Geometric versus arithmetic mean

Long-term return forecasting

Pricing a call using simulation

Exotic options

Barrier in-and-out parity

Pricing lookback options with floating strikes

Using the Sobol sequence to improve the efficiency

Summary

Exercises

Volatility Measures and GARCH

Conventional volatility measure – standard deviation

Tests of normality

Lower partial standard deviation

Test of equivalency of volatility over two periods

Test of heteroskedasticity, Breusch, and Pagan (1979)

Retrieving option data from Yahoo! Finance

Volatility smile and skewness

The ARCH model

The GARCH (Generalized ARCH) model

Summary

Exercises

Index