Review the material presented in Lab 6, and examine the Sequential-Monte-Carlo method of Beaumont, 2010.
In the previous exercise, we performed ABC according to the rejection algorithm:
simulate_prior <- function(n){
data.frame(beta0 = rnorm(n, sd = 5),
beta1 = rnorm(n, sd = 5),
rho = rbeta(n, 1, 5),
pi_EI = rbeta(n, 115,885),
pi_IR = rbeta(n, 200,800),
S01 = rpois(n,50000),
S02 = rpois(n,50000),
S03 = rpois(n,10000),
E01 = rpois(n,1),
E02 = rpois(n,1),
E03 = rpois(n,1),
I01 = rpois(n,1),
I02 = rpois(n,1),
I03 = rpois(n,1))
}
getX <- function(ntpts, nloc){
# Intercept and intervention term
X0 <- cbind(1, cumsum(1:ntpts > round(ntpts/2)))
X0[,2] <- X0[,2]/max(X0[,2])
X0[rep(1:ntpts, nloc),]
}
getD <- function(nloc){
# constant overall contact:
D <- 1-diag(nloc)
D/(nloc-1)
}
# Num time points = 150
simulate_data <- function(single_par, Nt = 150, N = c(51000, 51000, 11000)){
# Set up
X <- getX(Nt, length(N))
D <- getD(length(N))
S = E = I = R = Estar = Istar = Rstar = matrix(NA, nrow = 150 + 1, ncol = 3)
S[1,] = with(single_par, c(S01, S02, S03))
E[1,] = with(single_par, c(E01, E02, E03))
I[1,] = with(single_par, c(I01, I02, I03))
R[1,] = N - S[1,] - E[1,] - I[1,]
if (any(R[1,] < 0)){
# Bad starting value, return bad result
S[] <- Inf
E[] <- Inf
I[] <- Inf
R[] <- Inf
Estar[] <- Inf
Istar[] <- Inf
Rstar[] <- Inf
return(list(S=S[1:Nt,],
E=E[1:Nt,],
I=I[1:Nt,],
R=R[1:Nt,],
Estar=Estar[1:Nt,],
Istar=Istar[1:Nt,],
Rstar=Rstar[1:Nt,],
param = single_par))
}
beta = with(single_par, c(beta0, beta1))
rho = single_par$rho
theta_components = exp(matrix(X %*% beta, ncol = length(N)))
for (i in 1:Nt){
# Draw transition counts
theta <- I[i,]*theta_components[i,]/N
pi_SE = 1-exp(-theta - t(rho*(D %*% theta)))
Estar[i,] = rbinom(n = rep(1, ncol(S)), size = S[i,], prob = pi_SE)
Istar[i,] = rbinom(n = rep(1, ncol(S)), size = E[i,], prob = single_par$pi_EI)
Rstar[i,] = rbinom(n = rep(1, ncol(S)), size = I[i,], prob = single_par$pi_IR)
# Update compartments
S[i+1,] = S[i,] - Estar[i,]
E[i+1,] = E[i,] - Istar[i,] + Estar[i,]
I[i+1,] = I[i,] - Rstar[i,] + Istar[i,]
R[i+1,] = R[i,] + Rstar[i,]
}
list(S=S[1:Nt,],
E=E[1:Nt,],
I=I[1:Nt,],
R=R[1:Nt,],
Estar=Estar[1:Nt,],
Istar=Istar[1:Nt,],
Rstar=Rstar[1:Nt,],
param = single_par)
}
system.time({
pars <- simulate_prior(1000)
sim.list <- lapply(1:nrow(pars), function(i){if (i %% 100 ==0){print(i)};simulate_data(pars[i,])$Istar})
})
## [1] 100
## [1] 200
## [1] 300
## [1] 400
## [1] 500
## [1] 600
## [1] 700
## [1] 800
## [1] 900
## [1] 1000
## user system elapsed
## 6.70 0.02 6.74
set.seed(123123)
true_data <- simulate_data(single_par = data.frame(beta0=-1, beta1=-2,
rho=0.1,
pi_EI=0.14,pi_IR=0.2,
S01=50000,S02=50000,S03=10000,
E01=0,E02=0,E03=5,
I01=0,I02=0,I03=0))
true_Istar <- true_data$Istar
e <- sqrt(sapply(sim.list, function(simI){sum((simI-true_Istar)^2)}))
keepers <- pars[e <300,]
keepers$e <- e[e<300]
hist(keepers$beta0, main = "Beta 0")
If we want to generate more samples, we know that samples generated close to those already accepted are likely to perform better than those simulated directly from the prior, but our ABC algorithm relies on samples generated from the prior to provide valid posterior inference.
The SMC algorithm in the Beaumont paper provides a mechanism to resample the already accepted particles
Implement the Beaumont SMC-ABC algorithm for these data set and summarize your results. Please submit both your Rmd file and the compiled HTML file on ICON.