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

Cumulative standard normal distribution


In Chapter 4, 13 Lines of Python to Price a Call Option, we used 13 lines of Python codes to price a call option since we have to write our own cumulative standard normal distribution. Fortunately, the cumulative standard normal distribution is included in the submodule of SciPy. The following example shows the value of the cumulative standard normal distribution at zero:

>>>from scipy.stats import norm
>>>norm.cdf(0)
0.5

Thus, we could simplify our call option model considerably using just five lines. The following code is a typical example of the benefits we can enjoy using various modules:

from scipy import log,exp,sqrt,stats
defbs_call(S,X,T,r,sigma):
    d1=(log(S/X)+(r+sigma*sigma/2.)*T)/(sigma*sqrt(T))
    d2 = d1-sigma*sqrt(T)
    return S*stats.norm.cdf(d1)-X*exp(-r*T)*stats.norm.cdf(d2)

Now, we could use the following function by inputting a set of values:

>>>price=bs_call(40,40,1,0.03,0.2)
>>>round(price...