10.12.2019 Views

Python for Finance

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 14

x=40. # exercise price

T=6./12 # maturity date ii years

tao=1/12 # when to choose

r=0.05 # risk-free rate

sigma=0.2 # volatility

n=1000 # number of steps

#

price=binomialCallAmerican(s,x,T,r,sigma,n)

print("American call =", price)

('American call =', 2.7549263174936502)

The price of this American call is $2.75. The key for modifying the previous program

to satisfy only a few exercise prices is the following two lines:

v2=max(v[i][j]-x,0)

v[i][j]=max(v1,v2)

# early exercise

Here is the Python program for a Bermudan call option. The key different is the

variable called T2, which contains the dates when the Bermudan option could

be exercised:

def callBermudan(s,x,T,r,sigma,T2,n=100):

from math import exp,sqrt

import numpy as np

n2=len(T2)

deltaT = T /n

u = exp(sigma * sqrt(deltaT))

d = 1.0 / u

a = exp(r * deltaT)

p = (a - d) / (u - d)

v =[[0.0 for j in np.arange(i + 1)] for i in np.arange(n + 1)]

for j in np.arange(n+1):

v[n][j] = max(s * u**j * d**(n - j) - x, 0.0)

for i in np.arange(n-1, -1, -1):

for j in np.arange(i + 1):

v1=exp(-r*deltaT)*(p*v[i+1][j+1]+(1.0-p)*v[i+1][j])

for k in np.arange(n2):

if abs(j*deltaT-T2[k])<0.01:

v2=max(v[i][j]-x,0) # potential early exercise

else:

v2=0

v[i][j]=max(v1,v2)

return v[0][0]

#

s=40.

# stock price today

[ 493 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!