-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectorial_diffusion_process.py
92 lines (77 loc) · 2.76 KB
/
vectorial_diffusion_process.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
import matplotlib.pyplot as plt
import numpy as np
from src.stochastic_processes import CorrelatedBrownianMotions
INIT_WS = np.array([[10], [10]], dtype="f")
START_TIME = 0
END_TIME = 10
TIMESTEP = 0.01
def run_simulation(rho, sigma1, sigma2):
global INIT_WS, START_TIME, END_TIME, TIMESTEP
Ws, ts = np.array(INIT_WS, copy=True), np.array(START_TIME, copy=True)
dWs_t = CorrelatedBrownianMotions(
rho=rho,
sigma1=sigma1,
sigma2=sigma2,
xs=np.copy(Ws),
t=START_TIME,
dt=TIMESTEP
)
for dWs, t in dWs_t:
Ws = np.concatenate([Ws, dWs], axis=1)
ts = np.append(ts, t)
if t > END_TIME:
break
else:
continue
return Ws, ts
r = lambda x: round(x, 2)
CORRELATION1, CORRELATION2, CORRELATION3 = 0.85, 0.00, -0.85
VOLATILITY1, VOLATILITY2 = 0.20, 0.25
Ws1, ts1 = run_simulation(CORRELATION1, VOLATILITY1, VOLATILITY2)
Ws2, ts2 = run_simulation(CORRELATION2, VOLATILITY1, VOLATILITY2)
Ws3, ts3 = run_simulation(CORRELATION3, VOLATILITY1, VOLATILITY2)
_, (ax1, ax2, ax3) = plt.subplots(3,1)
ax1.set_title(f"Correlation = {r(CORRELATION1)}")
ax1.plot(ts1, Ws1[0, :], "blue", label=f"sigma={r(VOLATILITY1)}")
ax1.plot(ts1, Ws1[1, :], "orange", label=f"sigma={r(VOLATILITY2)}")
ax1.grid()
ax1.set_xlabel("time")
ax1.legend()
ax2.set_title(f"Correlation = {r(CORRELATION2)}")
ax2.plot(ts2, Ws2[0, :], "blue", label=f"sigma={r(VOLATILITY1)}")
ax2.plot(ts2, Ws2[1, :], "orange", label=f"sigma={r(VOLATILITY2)}")
ax2.grid()
ax2.legend()
ax2.set_xlabel("time")
ax3.set_title(f"Correlation = {r(CORRELATION3)}")
ax3.plot(ts3, Ws3[0, :], "blue", label=f"sigma={r(VOLATILITY1)}")
ax3.plot(ts3, Ws3[1, :], "orange", label=f"sigma={r(VOLATILITY2)}")
ax3.grid()
ax3.legend()
ax3.set_xlabel("time")
plt.tight_layout()
VOLATILITY1, VOLATILITY2 = 0.25, 0.50
Ws1, ts1 = run_simulation(CORRELATION1, VOLATILITY1, VOLATILITY2)
Ws2, ts2 = run_simulation(CORRELATION2, VOLATILITY1, VOLATILITY2)
Ws3, ts3 = run_simulation(CORRELATION3, VOLATILITY1, VOLATILITY2)
_, (ax1, ax2, ax3) = plt.subplots(3,1)
ax1.set_title(f"Correlation = {r(CORRELATION1)}")
ax1.plot(ts1, Ws1[0, :], "blue", label=f"sigma={r(VOLATILITY1)}")
ax1.plot(ts1, Ws1[1, :], "orange", label=f"sigma={r(VOLATILITY2)}")
ax1.grid()
ax1.set_xlabel("time")
ax1.legend()
ax2.set_title(f"Correlation = {r(CORRELATION2)}")
ax2.plot(ts2, Ws2[0, :], "blue", label=f"sigma={r(VOLATILITY1)}")
ax2.plot(ts2, Ws2[1, :], "orange", label=f"sigma={r(VOLATILITY2)}")
ax2.grid()
ax2.legend()
ax2.set_xlabel("time")
ax3.set_title(f"Correlation = {r(CORRELATION3)}")
ax3.plot(ts3, Ws3[0, :], "blue", label=f"sigma={r(VOLATILITY1)}")
ax3.plot(ts3, Ws3[1, :], "orange", label=f"sigma={r(VOLATILITY2)}")
ax3.grid()
ax3.legend()
ax3.set_xlabel("time")
plt.tight_layout()
plt.show()