-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.py
116 lines (89 loc) · 3.58 KB
/
main.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 18 15:19:37 2017
@author: Quintus
"""
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from ExplicitEu import ExplicitEu
from ImplicitEu import ImplicitEu
from CNEu import CNEu
from ImplicitAm import ImplicitAmBer
from ImplicitAm import ImplicitAmBre
from BSClosedForm import BSClosedForm
if __name__ == "__main__":
# set up parameters
S0 = 50
K = 50
r = 0.1
T = 5/12
sigma = 0.4
Smax = 100
M = 100 # S
N = 1000 # t
is_call = False
# pricing test
option = ExplicitEu(S0, K, r, T, sigma, Smax, M, N, is_call)
print(option.price())
option = ExplicitEu(S0, K, r, T, sigma, Smax, M, 100, is_call)
print(option.price())
option = ImplicitEu(S0, K, r, T, sigma, Smax, M, 1000, is_call)
print(option.price())
option = ImplicitEu(S0, K, r, T, sigma, Smax, M, 100, is_call)
print(option.price())
option = CNEu(S0, K, r, T, sigma, Smax, M, 1000, is_call)
print(option.price())
option = CNEu(S0, K, r, T, sigma, Smax, M, 100, is_call)
print(option.price())
option = ImplicitAmBer(S0, K, r, T, sigma, Smax, M, 1000, is_call)
print(option.price())
option = ImplicitAmBre(S0, K, r, T, sigma, Smax, M, 1000, is_call)
print(option.price())
# convergence error
def convergeError(method, S0, K, r, T, sigma, Smax, m, n, is_call):
p = np.array([])
for M, N in zip(m, n):
option = method(S0, K, r, T, sigma, Smax, M, N, is_call)
p = np.append(p, option.price())
option = BSClosedForm(S0, K, r, T, sigma, is_call)
error = np.abs(p-option.price())
return error, p, option.price()
m = np.array([50, 100, 200, 400, 800])
n = np.array([100, 200, 400, 800, 1600])
errorImp, impPrice, anaPrice = convergeError(ImplicitEu, S0, K, r, T, sigma, Smax, m, n, is_call)
errorCN, CNPrice, anaPrice = convergeError(CNEu, S0, K, r, T, sigma, Smax, m, n, is_call)
#==============================================================================
# plotting
#==============================================================================
# price surface
def priceSurface(method, S0, K, r, T, sigma, Smax, M, N, is_call):
fig = plt.figure(figsize=(6,5))
ax = Axes3D(fig)
t, S = np.meshgrid(np.linspace(0, 1, N+1), np.linspace(0, Smax, M+1))
option = method(S0, K, r, T, sigma, Smax, M, N, is_call)
option.price()
ax.plot_surface(t, S, option.grid, cmap='coolwarm', linewidth=0, antialiased=False)
ax.set_xlabel('time')
ax.set_ylabel('stock price')
ax.set_zlabel('option price')
plt.show()
N = 1000
priceSurface(ExplicitEu, S0, K, r, T, sigma, Smax, M, N, is_call)
N = 100
priceSurface(ExplicitEu, S0, K, r, T, sigma, Smax, M, N, is_call)
# convergence
fig = plt.figure(figsize=(6,5))
ax = fig.gca()
ax.plot(np.log(n), np.log(errorImp), '*-', c='royalblue')
ax.plot(np.log(n), np.log(errorCN), '*-', c='darkorange')
ticks = ax.set_xticks(np.log(n))
labels = ax.set_xticklabels(['log(100)', 'log(200)',
'log(400)', 'log(800)',
'log(1600)'], rotation=10, fontsize='small')
ax.set_xlabel('log(N)')
ax.set_ylabel('log(Error)')
ax.set_title('Difference between numerical solution \n and analytical solution')
plt.legend(['Implicit Method', 'CN method'])
plt.show()