Wednesday, September 5, 2012

Parallel Computing

Someone said a while ago that MCMC is an embarrassingly parallel (computing) problem. I wanted to try some R implementations of parallel computing for BUGS a few months ago when I was running some models with large data sets.  I tried the function jags.parallel() in the R2jags package. It works for some models but fails for somewhat more complicated ones. This week, I learned a new package dclone and tried a toy problem.  I have used this setup on several models and all worked out well.


packages(snow)
packages(rjags)
packages(dclone)

bugs.in <- function(chains=3){
  x <- 1:100
  y <- 3 + 5 * x + rnorm(100,0, 0.25)
  bugs.ini <- list()
  bugs.data <- list(n=100,y=y,x=x)
  for (i in 1:chains)
    bugs.ini [[i]] <- list(
                           beta=rnorm(2), sigma=runif(1))
  bugs.para <- c("beta", "sigma")
  return(list(data=bugs.data, inits=bugs.ini,
              para=bugs.para, n.chains=chains, 
              model=function() {
                for (i in 1:n){
                  y[i] ~ dnorm(mu[i], tau)
                  mu[i] <- beta[1] + beta[2]* x[i]
                }
                for (j in 1:2){
                  beta[j] ~ dnorm(0,0.0001)
                }
                tau <- pow(sigma, -2)
                sigma ~ dunif(0,10)
              }))
}

input.to.bugs <- bugs.in()
n.chains <- input.to.bugs$n.chains
n.adapt <- 10000
n.update <- 10000
n.iter <- 10000
n.keep <- 1000
thin <- max(c(1,floor((n.iter)*n.chains/n.keep)))
date()
cl <- makeCluster(3, type = "SOCK")
m2 <- jags.parfit(cl, data = input.to.bugs$data, 
                  params = input.to.bugs$para, 
                  model = input.to.bugs$model,
                  inits = input.to.bugs$inits,
                  n.adapt = n.adapt, n.update = n.update,
                  n.iter = n.iter, thin = thin, n.chains = n.chains)
date()
stopCluster(cl)

Log or not log

LOGorNOTLOG.html Log or not log, that is the question May 19, 2018 In 2014 I taught a special topics class on statistical i...