如何在python中实现随机ODEs(sde)系统?

2024-03-28 12:13:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个ODEs系统,我试图在其中加入一个“错误”项,这样它就变成了一个随机的ODEs系统。在

为了解决python中的ODEs系统,我通常使用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来求解随机常微分方程组? 例如,如果我想在方程的出生率(P)中加入一个误差项/随机游走?在

我的想法是在系统中使用一个额外的方程式来定义一个随机行走(随机抽样死亡率(使用随机。正态变量())为了解决这个问题:

^{pr2}$

这是解决sde系统的正确方法吗?或者我必须对随机ode使用不同的求解器吗?在


Tags: theimport系统pltinitialpopulationpercentday
1条回答
网友
1楼 · 发布于 2024-03-28 12:13:59

在这种帮助下,ODEs系统被改写成SDEs系统,其中出生率是一个随机过程。在

建议使用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()

相关问题 更多 >