1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| import numpy as np
from math import exp
import matplotlib.pyplot as plt
def Graph(S, N, r, M, sigma):
abscisse = np.linspace(0,N,N) ###########################
mat_graph = np.zeros(N) ###########################
NT = M/(N-1)
d = exp(-sigma*np.sqrt(NT))
u = 1/d
a=exp(r*NT)
p=(a-d)/(u-d)
for L in range(2,N+1):
A=np.zeros((L,L))
for i in range(L):
for j in range(L):
if i>j:
A[i,j]=np.nan
elif i<=j:
A[i,j]= S * (d**i) * (u**(j-i))
C=np.zeros((L,L))
D=np.zeros(((N,1)))
W=np.zeros((N,2))
for i in reversed(range(L)):
for j in reversed(range(L)):
if j==L-1:
C[i,j] = max(S - A[i,j], 0)
elif i > j:
C[i,j]=np.nan
elif i<= j:
C[i,j]=max(S - A[i,j], ( p*C[i,j+1] + (1-p)*C[i+1,j+1] ) *exp(-r*(M/(N-1))))
Z=C[0,0]
D[L-1,0]=Z
print(D)
for k in range(len(D)): ###########################
if D[k] != 0: ###########################
print(D[k]) ###########################
mat_graph[k]=D[k] ###########################
L =+ L+1
W[:,0]=range(1,N+1)
W[:,1]=D[:,0]
print(W)
print(mat_graph) ###########################
plt.plot(W[:,1])
plt.plot(abscisse,mat_graph) ###########################
plt.ylim(3.3, 5.3) ###########################
plt.title("Convergence of the price of the option")
plt.ylim(0,5)
plt.ylabel("Option Value")
plt.xlabel("No. of steps")
plt.show()
plt.close()
Graph(50,50,0.1,5/12,0.4) |
Partager