-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfinite_difference_methods.py
242 lines (178 loc) · 7.56 KB
/
finite_difference_methods.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 4 00:07:27 2020
@author: Justin Yu, M.S. Financial Engineering, Stevens Institute of Technology
Finite difference methods for pricing European call and put options
"""
import numpy as np
def explicit_fin_diff(S,K,T,sigma,r,q,N,Nj,CallPut):
'''
Explicit finite difference method for pricing European call and put options
Args:
S - intial price of underlying asset
K - strike price
T - time to maturity
sigma - volatility
r - risk-free rate
q - dividend rate
N - number of spacing points along the time partition (horizontal)
Nj - number of partition points from 0 to the upper/lower boundary (vertical)
CallPut - 'Call' or 'Put'
Returns the option price estimated by the finite difference grid
On the choice of N and Nj:
The following condition must be met to guarantee convergence of the explicit finite difference method:
dx >= sigma*sqrt(3*dt)
In fact, the best choice for dx is:
dx = sigma*sqrt(3*dt)
Therefore, finding the number N of time intervals is quite simple. For a given error epsilon, we set:
(sigma^2)*3*dt + dt = epsilon or dt = epsilon/(1 + 3*sigma^2)
Since dt = T/N, we can easily solve for N.
'''
dt = T/N; dx = sigma*np.sqrt(3*dt)
nu = r - q - 0.5*sigma**2
pu = 0.5*dt*((sigma/dx)**2 + nu/dx); pm = 1.0 - dt*(sigma/dx)**2 - r*dt; pd = 0.5*dt*((sigma/dx)**2 - nu/dx)
grid = np.zeros((N+1,2*Nj+1))
# Asset prices at maturity:
St = [S*np.exp(-Nj*dx)]
for j in range(1, 2*Nj+1):
St.append(St[j-1]*np.exp(dx))
# Option value at maturity:
for j in range(2*Nj+1):
if CallPut == 'Call':
grid[N,j] = max(0, St[j] - K)
elif CallPut == 'Put':
grid[N,j] = max(0, K - St[j])
# Backwards computing through grid:
for i in range(N-1, -1, -1):
for j in range(1, 2*Nj):
grid[i,j] =pu*grid[i+1,j+1] + pm*grid[i+1,j] + pd*grid[i+1,j-1]
# Boundary conditions
grid[i,0] = grid[i,1]
grid[i,2*Nj] = grid[i,2*Nj-1] + (St[2*Nj]-St[2*Nj-1])
return grid[0,Nj]
def implicit_fin_diff(S,K,T,sigma,r,q,N,Nj,CallPut):
'''
Implicit finite difference method for pricing European call and put options
Args:
S - intial price of underlying asset
K - strike price
T - time to maturity
sigma - volatility
r - risk-free rate
q - dividend rate
N - number of time intervals (horizontal)
Nj - number of partition points from 0 to the upper/lower boundary (vertical)
CallPut - 'Call' or 'Put'
Returns the option price estimated by the finite difference grid
On the choice of N and Nj:
Similar to the case of the explicit method, we again choose dt and dx such that:
(dx)^2 + dt = epsilon
We find that setting each term to 0.5*epsilon and solving for N and Nj provides pretty good results.
'''
dt = T/N; #dx = sigma*np.sqrt(3*dt)
dx = 1.0/(2*Nj+1)
nu = r - q - 0.5*sigma**2
pu = -0.5*dt*((sigma/dx)**2 + nu/dx); pm = 1.0 + dt*(sigma/dx)**2 + r*dt; pd = -0.5*dt*((sigma/dx)**2 - nu/dx)
grid = np.zeros(2*Nj+1)
# Asset prices at maturity:
St = [S*np.exp(-Nj*dx)]
for j in range(1, 2*Nj+1):
St.append(St[j-1]*np.exp(dx))
# Option value at maturity:
for j in range(2*Nj+1):
if CallPut == 'Call':
grid[j] = max(0, St[j] - K)
elif CallPut == 'Put':
grid[j] = max(0, K - St[j])
# Boundary Conditions:
if CallPut == 'Call':
lambdaU = St[2*Nj] - St[2*Nj-1]; lambdaL = 0.0;
elif CallPut == 'Put':
lambdaU = 0.0; lambdaL = -1.0*(St[1] - St[0])
# Backwards computing through grid
def tridiagonal(C,pU,pM,pD,lambda_L,lambda_U,nj):
'''
Helper function for solving the tridiagonal matrix system specified by the
implicit finite difference method
'''
C1 = np.zeros(2*nj+1)
pmp = [pM+pD]
pp = [C[1]+pD*lambda_L]
for j in range(2,2*nj):
pmp.append(pM - pU*pD/pmp[j-2])
pp.append(C[j] - pp[j-2]*pD/pmp[j-2])
C1[2*nj] = (pp[len(pp)-1] + pmp[len(pmp)-1]*lambda_U)/(pU + pmp[len(pmp)-1])
C1[2*nj-1] = C1[2*nj] - lambda_U
for j in range(2*nj-2, -1, -1):
C1[j] = (pp[j-1] - pU*C1[j+1])/pmp[j-1]
C1[0] = C1[1] - lambda_L
return C1
for i in range(N):
grid = tridiagonal(grid,pu,pm,pd,lambdaL,lambdaU,Nj)
return grid[Nj]
def crank_nicolson(S,K,T,sigma,r,q,N,Nj,CallPut):
'''
Crank-Nicolson finite difference method for pricing European calls and puts
Args:
S - intial price of underlying asset
K - strike price
T - time to maturity
sigma - volatility
r - risk-free rate
q - dividend rate
N - number of spacing points along the time partition (horizontal)
Nj - number of partition points from 0 to the upper/lower boundary (vertical)
CallPut - 'Call' or 'Put'
Returns the option price estimated by the finite difference grid
On the choice of N and Nj:
dt and dx should be chosen specifically such that the following condition holds:
(dx)^2 + (0.5*dt)^2 = epsilon
Again, we find that setting each term to 0.5*epsilon and solving for N and Nj gives pretty good results
'''
dt = T/N;
dx= 1.0/(2*Nj+1)
nu = r - q - 0.5*sigma**2
pu = -0.25*dt*((sigma/dx)**2 + nu/dx)
pm = 1.0 + 0.5*dt*((sigma/dx)**2) + 0.5*r*dt
pd = -0.25*dt*((sigma/dx)**2 - nu/dx)
grid = np.zeros(2*Nj+1)
# Asset prices at maturity:
St = [S*np.exp(-Nj*dx)]
for j in range(1, 2*Nj+1):
St.append(St[j-1]*np.exp(dx))
# Option value at maturity:
for j in range(2*Nj+1):
if CallPut == 'Call':
grid[j] = max(0, St[j] - K)
elif CallPut == 'Put':
grid[j] = max(0, K - St[j])
# Boundary Conditions:
if CallPut == 'Call':
lambdaU = St[2*Nj] - St[2*Nj-1]
lambdaL = 0.0
elif CallPut == 'Put':
lambdaU = 0.0
lambdaL = -1.0*(St[1] - St[0])
# Backwards computing through grid:
def tridiagonal(C,pU,pM,pD,lambda_L,lambda_U,nj):
'''
Helper function for solving the tridiagonal matrix system specified by the
Crank-Nicolson finite difference method
'''
C1 = np.zeros(2*nj+1)
pmp = [pM+pD]
pp = [-pU*C[2]-(pM-2)*C[1]-pD*C[0]+pD*lambda_L]
for j in range(2,2*nj):
pmp.append(pM - pU*pD/pmp[j-2])
pp.append(-pU*C[j+1] - (pM-2)*C[j] - pD*C[j-1] - pp[j-2]*pD/pmp[j-2])
# Boundary conditions:
C1[2*nj] = (pp[len(pp)-1] + pmp[len(pmp)-1]*lambda_U)/(pU + pmp[len(pmp)-1])
C1[2*nj-1] = C1[2*nj] - lambda_U
# Back substitution
for j in range(2*nj-2, 0, -1):
C1[j] = (pp[j-1] - pU*C1[j+1])/pmp[j-1]
C1[0] = C[0]
return C1
for i in range(N):
grid = tridiagonal(grid,pu,pm,pd,lambdaL,lambdaU,Nj)
return grid[Nj]