Repository navigation
Expand file tree
/
Copy pathconvergence_plot.py
More file actions
65 lines (44 loc) · 1.81 KB
/
Copy pathconvergence_plot.py
File metadata and controls
65 lines (44 loc) · 1.81 KB
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
import matplotlib.pyplot as plt
from matplotlib.widgets import TextBox, Button
from pricing.binomial_method import binomial_method
from pricing.black_scholes import black_scholes
"""
Computes and plots the absolute pricing error between the binomial method and Black-Scholes as a function of the number of steps (N)
"""
t_0 = 0
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.4)
ax_S = plt.axes([0.3, 0.25, 0.3, 0.05])
box_S = TextBox(ax_S, "Share Price: ", initial="100")
ax_E = plt.axes([0.3, 0.2, 0.3, 0.05])
box_E = TextBox(ax_E, "Strike Price: ", initial="105")
ax_r = plt.axes([0.3, 0.15, 0.3, 0.05])
box_r = TextBox(ax_r, "Interest Rate:", initial = "0.1")
ax_sigma = plt.axes([0.3, 0.1, 0.3, 0.05])
box_sigma = TextBox(ax_sigma, "Volatility:", initial = "0.1")
ax_T = plt.axes([0.3, 0.05, 0.3, 0.05])
box_T = TextBox(ax_T, "Expiry:", initial = "3")
def submit(event):
S = float(box_S.text)
E = float(box_E.text)
r = float(box_r.text)
sigma = float(box_sigma.text)
T = float(box_T.text)
_, _, bs_price, _ = black_scholes(S, E, r, sigma, T, t_0)
print(f"The fair price for a European Vanilla Call on this asset is" ,bs_price)
N_values = [3, 10, 25, 50, 100, 250, 500, 1000, 2000]
binomial_prices = [binomial_method(S, E, r, sigma, T, t_0, N) for N in N_values]
errors = [abs(bp - bs_price) for bp in binomial_prices]
ax.clear()
ax.plot(N_values, errors, marker='o')
ax.set_xscale('log')
ax.set_yscale('log')
ax.set_xlabel('Number of steps, N')
ax.set_ylabel('|Binomial - Black-Scholes|')
ax.set_title('Convergence of Binomial Tree to Black-Scholes')
ax.grid(alpha=0.3, which = 'both')
fig.canvas.draw_idle()
ax_button = plt.axes([0.75, 0.1, 0.2, 0.06])
run_button = Button(ax_button, "Calculate")
run_button.on_clicked(submit)
plt.show()