Skip to content

Instantly share code, notes, and snippets.

@TimTaylor
Created March 5, 2021 13:44
Show Gist options
  • Save TimTaylor/0fa640d1b87b26457f7d9df27c5cef01 to your computer and use it in GitHub Desktop.
Save TimTaylor/0fa640d1b87b26457f7d9df27c5cef01 to your computer and use it in GitHub Desktop.
How to test - SIR script example refactored
# parameters --------------------------------------------------------------
N <- 1000 # network size
avk <- 7 # desired average degree
gamma <- 0.25 # recovery rate
tau <- 0.27 # per-edge infection rate
max_time <- 50 # maximum simulation length
i0 <- 10 # initial number of infected individuals
# -------------------------------------------------------------------------
# build erdos-renyi random-graph
network <- erdos_matrix(N, avk)
# initialise infected
initially_infected <- init_infected(N, i0)
# initialise status
status <- init_status(N, initially_infected)
# initialise_rates
rates <- init_rates(network, gamma, tau, status)
# initial conditions
times <- rep(NA_real_, 2 * N) # preallocate for output (R thing)
INF <- rep(NA_integer_, 2 * N)
SUS <- rep(NA_integer_, 2 * N)
t <- 0 # time
i <- 1 # index
INF[i] <- i0
SUS[i] <- N - i0
times[i] <- 0
while((t < max_time) && (INF[i] > 0)) {
# which node is the vent happening to
event <- which_event(rate)
# update values (note order as can effect function requirements)
t <- t + step_update(rate)
status[event] <- status_update(status(event))
rate <- rate_update(event, rate, status, graph)
# update times, INF and SUS
if (status[event] == 1) {
# S -> I
INF[i + 1] <- INF[i] + 1
SUS[i + 1] <- SUS[i] - 1
} else {
# I -> R
INF[i + 1] <- INF[i] - 1
SUS[i + 1] <- SUS[i]
}
times[i + 1] <- t
# increment index
i <- i + 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment