
P1: JYS
c02 JWBK378-Fletcher May 1, 2009 19:51 Printer: Yet to come
2
The PPF Package
The source code accompanying this book implements a minimal library, ppf, for exploring
financial modelling in Python. The sections ahead outline the structure and ideas of the
package.
The following is a first example of a financial program expressed in Python – the ‘Hello
World’ of Quantitative Analysis programs, that is, the Black–Scholes formula for a European
option on a single asset:
from math import log, sqrt, exp
from ppf.math import N
def black
scholes(S, K, r, sig, T, CP, *arguments, **keywords):
"""The classic Black and Scholes formula.
>>> print black
scholes(S=42., K=40., r=0.1, sig= 0.2, T=0.5,
CP=CALL) 4.75942193531
>>> print black
scholes(S=42., K=40., r=0.1, sig= 0.2, T=0.5,
CP=PUT) 0.808598915338
"""
d1 = (log(S/K) + (r + 0.5*(sig*sig))*T)/(sig*sqrt(T))
d2 = d1 - sig*sqrt(T)
return CP*S*N(CP*d1) - CP*K*exp(-r*T)*N(CP*d2)
CALL, PUT = (1, -1)
def
test():
import doctest
doctest.testmod()
if
name == ’ main ’: test()
2.1 PPF TOPOLOGY
The ppf library is a Python package containing a family of sub-packages. The
black
scholes function listed above is housed in the ppf.core subpackage. The topol-
ogy of ppf is as follows:
ppf/
com/
core/
date
time/
5