如何在 python 中实现随机常微分方程 (SDE) 系统?

How to implement a system of stochastic ODEs (SDEs) in python?

我有一个 ODE 系统,我试图在其中包含一个 'error' 项,以便它成为一个随机 ODE 系统。

为了求解 python 中的 ODE 系统,我通常使用 scipy 的 odeint

源自 Scipy Cookbook 的示例,涉及著名的僵尸启示录:

# zombie apocalypse modeling
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
plt.rcParams['figure.figsize'] = 10, 8

P = 0      # birth rate
d = 0.0001  # natural death percent (per day)
B = 0.0095  # transmission percent  (per day)
G = 0.0001  # resurect percent (per day)
A = 0.0001  # destroy percent  (per day)

# solve the system dy/dt = f(y, t)
def f(y, t):
     Si = y[0]
     Zi = y[1]
     Ri = y[2]
     # the model equations (see Munz et al. 2009)
     f0 = P - B*Si*Zi - d*Si
     f1 = B*Si*Zi + G*Ri - A*Si*Zi
     f2 = d*Si + A*Si*Zi - G*Ri
     return [f0, f1, f2]

# initial conditions
S0 = 500.              # initial population
Z0 = 0                 # initial zombie population
R0 = 0                 # initial death population
y0 = [S0, Z0, R0]     # initial condition vector
t  = np.linspace(0, 5., 1000)         # time grid

# solve the DEs
soln = odeint(f, y0, t)
S = soln[:, 0]
Z = soln[:, 1]
R = soln[:, 2]

# plot results
plt.figure()
plt.plot(t, S, label='Living')
plt.plot(t, Z, label='Zombies')
plt.xlabel('Days from outbreak')
plt.ylabel('Population')
plt.title('Zombie Apocalypse - No Init. Dead Pop.; No New Births.')
plt.legend(loc=0)
plt.show()

是否可以使用 odeint 求解随机 ODE 系统? 例如,如果我想在方程式的出生率 (P) 中加入错误 term/random?

我的想法是在系统中使用一个额外的方程式来定义随机游走(随机抽样死亡率(使用 random.normalvariate())并像这样求解系统:

 f0 = P - B*Si*Zi - f3*Si
 f1 = B*Si*Zi + G*Ri - A*Si*Zi
 f2 = f3*Si + A*Si*Zi - G*Ri
 f3 = random.normalvariate(mu, sigma)
 return [f0, f1, f2]

这是求解 SDE 系统的正确方法吗?还是我必须对随机 ODE 使用不同的求解器?

在帮助下,ODE 系统被重写为 SDE 系统,其中出生率是一个随机过程。

使用 SDEint 包是一个很好的建议。

# Zombie apocalypse SDE model

import matplotlib.pyplot as plt
import numpy as np
import sdeint

P, d, B, G, A = 0.0001, 0.0001, 0.0095, 0.0001, 0.0001

tspan = np.linspace(0, 5., 1000)
y0 = np.array([500., 0., 0., P])


def f(y, t):
    Si = y[0]
    Zi = y[1]
    Ri = y[2]

    f0 = y[3] - B * Si * Zi - d * Si
    f1 = B * Si * Zi + G * Ri - A * Si * Zi
    f2 = d * Si + A * Si * Zi - G * Ri
    f3 = 0
    return np.array([f0, f1, f2, f3])


def GG(y, t):
    return np.diag([0, 0, 0, 100])

result = sdeint.itoint(f, GG, y0, tspan)

plt.plot(result)
plt.show()