-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_gbm.py
More file actions
83 lines (65 loc) · 2.18 KB
/
plot_gbm.py
File metadata and controls
83 lines (65 loc) · 2.18 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""
Figure 8 from the thesis:
Fine and coarse Euler approximations of a geometric Brownian motion,
including comparison with Milstein method and running maximum.
"""
import numpy as np
import matplotlib.pyplot as plt
from core import sim_brownian_motion, euler_gbm, milstein_gbm, exact_gbm
np.random.seed(42)
s0 = 100.0
mu = 0.05
sigma = 0.2
T = 1.0
l = 10
bm = sim_brownian_motion(T, l, 1)
t_fine = np.linspace(0, T, 2**l + 1)
# Euler approximation
gbm_euler = euler_gbm(s0, mu, sigma, T, l, bm)
# Milstein approximation
gbm_milstein = milstein_gbm(s0, mu, sigma, T, l, bm)
# Running maximum of Euler path
max_path = np.maximum.accumulate(gbm_euler[:, 0])
fig, ax = plt.subplots(figsize=(8, 5))
ax.plot(t_fine, gbm_euler[:, 0], label='Euler', linewidth=0.7)
ax.plot(t_fine, gbm_milstein[:, 0], label='Milstein', linewidth=0.7)
ax.plot(t_fine, max_path, label='Running max', linewidth=0.7, linestyle='--')
# Coarse Euler (l=3)
lc = 3
step = 2**(l - lc)
t_coarse = t_fine[::step]
bm_coarse = bm[::step, :]
gbm_coarse = euler_gbm(s0, mu, sigma, T, lc, bm_coarse)
ax.plot(t_coarse, gbm_coarse[:, 0], label=f'Euler coarse (l={lc})', linewidth=1.5)
ax.set_xlabel('t')
ax.set_ylabel(r'$\hat{S}^{(2^l)}(t)$')
ax.legend()
ax.set_title('Geometric Brownian motion: Euler vs Milstein')
fig.tight_layout()
fig.savefig('plot_gbm_euler_milstein.png', dpi=150)
print("Saved: plot_gbm_euler_milstein.png")
# --- Fine and coarse GBM on [T0, T1] = [1, 2] (Figure 8 in thesis) ---
T0 = 1.0
T1 = 2.0
l = 10
bm2 = sim_brownian_motion(T1 - T0, l, 1)
t2 = np.linspace(T0, T1, 2**l + 1)
gbm_exact = exact_gbm(s0, mu, sigma, t2 - T0, bm2[:, 0])
fig2, ax2 = plt.subplots(figsize=(8, 5))
# Fine (l=10)
ax2.plot(t2, gbm_exact, label=f'l = {l}', linewidth=0.5)
# Coarse approximations
for lc in [4, 3]:
step = 2**(l - lc)
t_c = t2[::step]
bm_c = bm2[::step, 0]
gbm_c = exact_gbm(s0, mu, sigma, t_c - T0, bm_c)
ax2.plot(t_c, gbm_c, label=f'l = {lc}', linewidth=1.5)
ax2.set_xlabel('t')
ax2.set_ylabel(r'$\hat{S}^{(2^l)}(t)$')
ax2.legend()
ax2.set_title('Fine and coarse Euler approximations of a GBM')
fig2.tight_layout()
fig2.savefig('plot_gbm_fine_coarse.png', dpi=150)
print("Saved: plot_gbm_fine_coarse.png")
plt.show()