| 1 |
""" |
| 2 |
Demonstrate the correctness of the "derivative-free Milstein method" for |
| 3 |
multiplicative noise. |
| 4 |
""" |
| 5 |
from brian2 import * |
| 6 |
# We only get exactly the same random numbers for the exact solution and the |
| 7 |
# simulation if we use the numpy code generation target |
| 8 |
prefs.codegen.target = 'numpy' |
| 9 |
|
| 10 |
# setting a random seed makes all variants use exactly the same Wiener process |
| 11 |
seed = 12347 |
| 12 |
|
| 13 |
X0 = 1 |
| 14 |
mu = 0.5/second # drift |
| 15 |
sigma = 0.1/second #diffusion |
| 16 |
|
| 17 |
runtime = 1*second |
| 18 |
|
| 19 |
|
| 20 |
def simulate(method, dt): |
| 21 |
""" |
| 22 |
simulate geometrical Brownian with the given method |
| 23 |
""" |
| 24 |
np.random.seed(seed) |
| 25 |
G = NeuronGroup(1, 'dX/dt = (mu - 0.5*second*sigma**2)*X + X*sigma*xi*second**.5: 1', |
| 26 |
dt=dt, method=method) |
| 27 |
G.X = X0 |
| 28 |
mon = StateMonitor(G, 'X', record=True) |
| 29 |
net = Network(G, mon) |
| 30 |
net.run(runtime) |
| 31 |
return mon.t_[:], mon.X.flatten() |
| 32 |
|
| 33 |
|
| 34 |
def exact_solution(t, dt): |
| 35 |
""" |
| 36 |
Return the exact solution for geometrical Brownian motion at the given |
| 37 |
time points |
| 38 |
""" |
| 39 |
# Remove units for simplicity |
| 40 |
my_mu = float(mu) |
| 41 |
my_sigma = float(sigma) |
| 42 |
dt = float(dt) |
| 43 |
t = asarray(t) |
| 44 |
|
| 45 |
np.random.seed(seed) |
| 46 |
# We are calculating the values at the *start* of a time step, as when using |
| 47 |
# a StateMonitor. Therefore the Brownian motion starts with zero |
| 48 |
brownian = np.hstack([0, cumsum(sqrt(dt) * np.random.randn(len(t)-1))]) |
| 49 |
|
| 50 |
return (X0 * exp((my_mu - 0.5*my_sigma**2)*(t+dt) + my_sigma*brownian)) |
| 51 |
|
| 52 |
figure(1, figsize=(16, 7)) |
| 53 |
figure(2, figsize=(16, 7)) |
| 54 |
|
| 55 |
methods = ['milstein', 'heun'] |
| 56 |
dts = [1*ms, 0.5*ms, 0.2*ms, 0.1*ms, 0.05*ms, 0.025*ms, 0.01*ms, 0.005*ms] |
| 57 |
|
| 58 |
rows = int(sqrt(len(dts))) |
| 59 |
cols = int(ceil(1.0 * len(dts) / rows)) |
| 60 |
errors = dict([(method, zeros(len(dts))) for method in methods]) |
| 61 |
for dt_idx, dt in enumerate(dts): |
| 62 |
print('dt: %s' % dt) |
| 63 |
trajectories = {} |
| 64 |
# Test the numerical methods |
| 65 |
for method in methods: |
| 66 |
t, trajectories[method] = simulate(method, dt) |
| 67 |
# Calculate the exact solution |
| 68 |
exact = exact_solution(t, dt) |
| 69 |
|
| 70 |
for method in methods: |
| 71 |
# plot the trajectories |
| 72 |
figure(1) |
| 73 |
subplot(rows, cols, dt_idx+1) |
| 74 |
plot(t, trajectories[method], label=method, alpha=0.75) |
| 75 |
|
| 76 |
# determine the mean absolute error |
| 77 |
errors[method][dt_idx] = mean(abs(trajectories[method] - exact)) |
| 78 |
# plot the difference to the real trajectory |
| 79 |
figure(2) |
| 80 |
subplot(rows, cols, dt_idx+1) |
| 81 |
plot(t, trajectories[method] - exact, label=method, alpha=0.75) |
| 82 |
|
| 83 |
figure(1) |
| 84 |
plot(t, exact, color='gray', lw=2, label='exact', alpha=0.75) |
| 85 |
title('dt = %s' % str(dt)) |
| 86 |
xticks([]) |
| 87 |
|
| 88 |
figure(1) |
| 89 |
legend(frameon=False, loc='best') |
| 90 |
tight_layout() |
| 91 |
|
| 92 |
figure(2) |
| 93 |
legend(frameon=False, loc='best') |
| 94 |
tight_layout() |
| 95 |
|
| 96 |
figure(3) |
| 97 |
for method in methods: |
| 98 |
plot(array(dts) / ms, errors[method], 'o', label=method) |
| 99 |
legend(frameon=False, loc='best') |
| 100 |
xscale('log') |
| 101 |
yscale('log') |
| 102 |
xlabel('dt (ms)') |
| 103 |
ylabel('Mean absolute error') |
| 104 |
tight_layout() |
| 105 |
|
| 106 |
show() |