Skip to content

Instantly share code, notes, and snippets.

@sdwfrost
Created November 29, 2017 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdwfrost/94d4a61b3aef277fc3814fee8dea06a5 to your computer and use it in GitHub Desktop.
Save sdwfrost/94d4a61b3aef277fc3814fee8dea06a5 to your computer and use it in GitHub Desktop.
Discrete SIR model using the DiffEq integrator interface
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@sdwfrost
Copy link
Author

@ChrisRackauckas any idea what's going wrong here?

@ChrisRackauckas
Copy link

using OrdinaryDiffEq
using Distributions

function sir(t,u,du,parms)
    (S,I,R,Y)=u
    (β,γ,α,ι,N,δt)=parms
    λ=β*((convert(Float64,I)+ι)^α)/N
    ifrac=1.0-exp(-λ*δt)
    rfrac=1.0-exp(-γ*δt)
    infection=rand(Binomial(S,ifrac))
    recovery=rand(Binomial(I,rfrac))
    du[1]=S-infection
    du[2]=I+infection-recovery
    du[3]=R+recovery
    du[4]=Y+infection
end

γ=7.0/13.0
α=1.0
ι=0.5
N=403.0
M=10
R₀=5.0
δt=1.0/convert(Float64,M)
β=R₀*(1.0-exp(-γ*δt))/δt;

u0=[60,1,342,0]
tspan=(0,540.0)
parms=(β,γ,α,ι,N,δt)

f = (t,u,du)->sir(t,u,du,parms)
prob=DiscreteProblem(f,u0,tspan)
@time solve(prob,FunctionMap(scale_by_time=false);dt=δt)

The issue was tspan=(0,540) meant time was integers when you should've been using Float64 (or Rationals to be exact). I think this is the kind of "quick" problem where you'll measure our keyword argument + startup time though,

0.001946 seconds (10.97 k allocations: 1.105 MiB)

is definitely too much and mostly due to kwarg + splat penalty for the big type. Hopefully that's fixed in 1.0 though!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment