python - 奇怪的 SciPy ODE 集成错误

标签 python scipy ode

我正在实现一个非常简单的 Susceptible-Infected-Recovered 模型,该模型具有用于闲置项目的稳定人口 - 通常是一项非常微不足道的任务。但是我在使用 PysCeS 或 SciPy 时遇到了求解器错误,它们都使用 lsoda 作为它们的底层求解器。这只发生在参数的特定值上,我很困惑为什么。我使用的代码如下:

import numpy as np
from pylab import *
import scipy.integrate as spi

#Parameter Values
S0 = 99.
I0 = 1.
R0 = 0.
PopIn= (S0, I0, R0)
beta= 0.50     
gamma=1/10.  
mu = 1/25550.
t_end = 15000.
t_start = 1.
t_step = 1.
t_interval = np.arange(t_start, t_end, t_step)

#Solving the differential equation. Solves over t for initial conditions PopIn
def eq_system(PopIn,t):
    '''Defining SIR System of Equations'''
    #Creating an array of equations
    Eqs= np.zeros((3))
    Eqs[0]= -beta * (PopIn[0]*PopIn[1]/(PopIn[0]+PopIn[1]+PopIn[2])) - mu*PopIn[0] + mu*(PopIn[0]+PopIn[1]+PopIn[2])
    Eqs[1]= (beta * (PopIn[0]*PopIn[1]/(PopIn[0]+PopIn[1]+PopIn[2])) - gamma*PopIn[1] - mu*PopIn[1])
    Eqs[2]= gamma*PopIn[1] - mu*PopIn[2]
    return Eqs

SIR = spi.odeint(eq_system, PopIn, t_interval)

这会产生以下错误:

 lsoda--  at current t (=r1), mxstep (=i1) steps   
       taken on this call before reaching tout     
      In above message,  I1 =       500
      In above message,  R1 =  0.7818108252072E+04
Excess work done on this call (perhaps wrong Dfun type).
Run with full_output = 1 to get quantitative information.

通常当我遇到这样的问题时,我设置的方程系统有一些根本性的错误,但我都看不出有什么问题。奇怪的是,如果您将 mu 更改为类似 1/15550 的值,它也会起作用。以防系统出现问题,我按如下方式在 R 中实现了模型:

require(deSolve)

sir.model <- function (t, x, params) {
  S <- x[1]
  I <- x[2]
  R <- x[3]
  with (
    as.list(params),
{
    dS <- -beta*S*I/(S+I+R) - mu*S + mu*(S+I+R)
    dI <- beta*S*I/(S+I+R) - gamma*I - mu*I
    dR <- gamma*I - mu*R
  res <- c(dS,dI,dR)
  list(res)
}
  )
}

times <- seq(0,15000,by=1)
params <- c(
 beta <- 0.50,
 gamma <- 1/10,
 mu <- 1/25550
)

xstart <- c(S = 99, I = 1, R= 0)

out <- as.data.frame(lsoda(xstart,times,sir.model,params))

使用 lsoda,但似乎运行顺利。谁能看出 Python 代码中出了什么问题?

最佳答案

我认为对于您选择的参数,您遇到了 stiffness 的问题。 - 由于数值不稳定性,求解器的步长在求解曲线的斜率实际上很浅的区域变得非常小。由 scipy.integrate.odeint 包装的 Fortran 求解器 lsoda 尝试在适合“刚性”和“非刚性”系统的方法之间自适应切换,但在在这种情况下,它似乎无法切换到僵硬的方法。

非常粗略地,您可以大幅增加最大允许步数,求解器最终会到达那里:

SIR = spi.odeint(eq_system, PopIn, t_interval,mxstep=5000000)

更好的选择是使用面向对象的 ODE 求解器 scipy.integrate.ode,它允许您明确选择是使用刚性方法还是非刚性方法:

import numpy as np
from pylab import *
import scipy.integrate as spi

def run():
    #Parameter Values
    S0 = 99.
    I0 = 1.
    R0 = 0.
    PopIn= (S0, I0, R0)
    beta= 0.50     
    gamma=1/10.  
    mu = 1/25550.
    t_end = 15000.
    t_start = 1.
    t_step = 1.
    t_interval = np.arange(t_start, t_end, t_step)

    #Solving the differential equation. Solves over t for initial conditions PopIn
    def eq_system(t,PopIn):
        '''Defining SIR System of Equations'''
        #Creating an array of equations
        Eqs= np.zeros((3))
        Eqs[0]= -beta * (PopIn[0]*PopIn[1]/(PopIn[0]+PopIn[1]+PopIn[2])) - mu*PopIn[0] + mu*(PopIn[0]+PopIn[1]+PopIn[2])
        Eqs[1]= (beta * (PopIn[0]*PopIn[1]/(PopIn[0]+PopIn[1]+PopIn[2])) - gamma*PopIn[1] - mu*PopIn[1])
        Eqs[2]= gamma*PopIn[1] - mu*PopIn[2]
        return Eqs

    ode =  spi.ode(eq_system)

    # BDF method suited to stiff systems of ODEs
    ode.set_integrator('vode',nsteps=500,method='bdf')
    ode.set_initial_value(PopIn,t_start)

    ts = []
    ys = []

    while ode.successful() and ode.t < t_end:
        ode.integrate(ode.t + t_step)
        ts.append(ode.t)
        ys.append(ode.y)

    t = np.vstack(ts)
    s,i,r = np.vstack(ys).T

    fig,ax = subplots(1,1)
    ax.hold(True)
    ax.plot(t,s,label='Susceptible')
    ax.plot(t,i,label='Infected')
    ax.plot(t,r,label='Recovered')
    ax.set_xlim(t_start,t_end)
    ax.set_ylim(0,100)
    ax.set_xlabel('Time')
    ax.set_ylabel('Percent')
    ax.legend(loc=0,fancybox=True)

    return t,s,i,r,fig,ax

输出:

enter image description here

关于python - 奇怪的 SciPy ODE 集成错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16973036/

相关文章:

python - Python Scipy 中的麦克斯韦分布

python - 什么时候在 scipy 中使用 floc 和 fscale 参数?

python-3.x - 为什么我不能让这个 Runge-Kutta 求解器随着时间步长的减少而收敛?

python - 重新采样数据框以添加缺失的日期

Python Websocket 模块继续启动未被杀死的进程,导致内存问题

python - 类型错误 : can't use a string pattern on a bytes-like object

python - Python/SciPy 中低差异(例如 Sobol)准随机序列的建议?

python - Scipy、odeint 以及 odeint 输入函数的输入设置

matlab - 在matlab中将符号变量分配给数值变量

Python BeautifulSoup 网络爬虫 : Appending piece of data to list