-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcos_method_results.py
87 lines (63 loc) · 2.93 KB
/
cos_method_results.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
from cos_method import *
class CosResults(CosMethod):
def price_option_Cos():
S0_list = [100, 110, 120]
for S0 in S0_list:
cos_object = CosMethod(
r=0.04,
sigma=0.30,
S0=S0,
K=110,
T=1,
L=11,
n_iterations=64)
cos_object.characteristic_function()
cos_object.compute_Fn()
cos_object.compute_Gn()
price_est = cos_object.compute_price_approximation()
price_analytical = cos_object.analytical_solution()
print(f"(COS-estimate,BS) = {price_est,price_analytical} for params (r = {cos_object.r}, vol = {cos_object.sigma}, S0 = {cos_object.S0},K = {cos_object.K},T = {cos_object.T})")
def convergence_to_bs():
S0_list = [100, 110, 120]
price_ests_list = [] # A list of lists
price_analytical_list = []
n_terms = np.arange(1, 65, 1)
# Prepare subplots arranged horizontally
fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(12, 7), dpi=300)
fig.tight_layout(pad=4.0)
for idx, S0 in enumerate(S0_list):
price_ests = []
for n in n_terms:
cos_object = CosMethod(
r=0.04,
sigma=0.30,
S0=S0,
K=110,
T=1,
L=11,
n_iterations=n,
)
cos_object.characteristic_function()
cos_object.compute_Fn()
cos_object.compute_Gn()
price_est = cos_object.compute_price_approximation()
price_ests.append(price_est)
price_ests_list.append(price_ests)
price_analytical = cos_object.analytical_solution() # Obtain BS solution
price_analytical_list.append(price_analytical)
# Plotting on separate subplots arranged horizontally
axes[0, idx].plot(n_terms, price_ests, color="blue", label=f"COS-Method S0 = {S0}")
axes[0, idx].axhline(price_analytical, linestyle="--", color="green", label=f"BS Solution S0 = {S0}")
axes[0,idx].set_xlabel("Number of terms")
axes[0,idx].set_ylabel("Price")
axes[0, idx].legend(fontsize = 10)
# Calculating and plotting the error on the corresponding subplot below
error = np.abs(np.array(price_ests) - price_analytical)
axes[1, idx].plot(n_terms, error, color="red", label=f"Error for S0 = {S0}")
axes[1, idx].set_xlabel("Number of terms")
axes[1,idx].set_ylabel("Error")
axes[1, idx].legend(fontsize = 10)
plt.savefig("Cos-Convergence", dpi = 300)
plt.show()
if __name__ == "__main__":
CosResults.convergence_to_bs()