Thursday, April 23, 2020

SIR with Sage

Very simple implementation of the SIR epidemic model using Sage
mathematical framework.
# Infected
IInit = 1
# Susceptible
SInit = N - IInit
# Resistant
RInit = 0
# Transmission rate
betaInit = 0.5
# Transmission rate attenuation
delta = 0.01
# Recovery rate
gamma = 0.1
# Population size
N = 10000
# End time
tMax = 100
# Number of points
points = 1000

# Standard SIR model
def ODE_RHS(t, Y):
  (S, I, R, beta) = Y    
  dS = - beta * S * I / N
  dI = beta * S * I / N - gamma * I
  dR = gamma * I
  dbeta = - beta * delta
  return (dS, dI, dR, dbeta)

# Set up numerical solution of ODE
solver = ode_solver(function = ODE_RHS,
                    y_0 = (SInit, IInit, RInit, betaInit),
                    t_span = (0, tMax),
                    algorithm = 'rk8pd')

# Numerically solve
solver.ode_solve(num_points = points)

# Plot solution
show(plot(solver.interpolate_solution(i = 0), 0, tMax, legend_label = 'S(t)', color = 'green')
   + plot(solver.interpolate_solution(i = 1), 0, tMax, legend_label = 'I(t)', color = 'red')
   + plot(solver.interpolate_solution(i = 2), 0, tMax, legend_label = 'R(t)', color = 'blue')) 

Result:

No comments:

Post a Comment