-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestStochasticSolv.py
58 lines (44 loc) · 1.22 KB
/
testStochasticSolv.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
#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt
class Model:
"""Stochastic model constants."""
mu = 3
sigma = 1
def dW(dt):
"""Random sample normal distribution."""
return np.random.normal(loc=0.0, scale=np.sqrt(dt))
def run_simulation():
""" Return the result of one full simulation."""
# One second and thousand grid points
T_INIT = 0
T_END = 1
N = 1000 # Compute 1000 grid points
DT = float(T_END - T_INIT) / N
TS = np.arange(T_INIT, T_END + DT, DT)
Y_INIT = 1
# Vectors to fill
ys = np.zeros(N + 1)
ys[0] = Y_INIT
for i in range(1, TS.size):
t = (i - 1) * DT
y = ys[i - 1]
dw = dW(DT)
del t
# Sum up terms as in the Milstein method
ys[i] = y + \
Model.mu * y * DT + \
Model.sigma * y * dw + \
(Model.sigma**2 / 2) * y * (dw**2 - DT)
return TS, ys
def plot_simulations(num_sims: int):
"""Plot several simulations in one image."""
for _ in range(num_sims):
plt.plot(*run_simulation())
plt.xlabel("time (s)")
plt.ylabel("y")
plt.grid()
plt.show()
if __name__ == "__main__":
NUM_SIMS = 2
plot_simulations(NUM_SIMS)