-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Acceptance Ratio being 0 #4
Comments
usually this happens when your initial values is out of the support range for the respective distribution. the sampler may not be able to figure out how to jump into the support range since it's just a random walk jumper... I thought I had added some messages about initializing an mcmc.object whith values outside the support but possibly not. I'll have to check the code. Alternatively the issue could be that the initial value is in the support range, but the variance of the jumper is so bit that all of the random walk jumps go outside the support range. I haven't looked at your code, but you may be able to narrow down the issue just from knowing which nodes have narrow support ranges... I'm glad you are using the package. I really need to release an update as there have been numerous changes to the backend (the c++ package). If you can't get the R bit to work you can always code up your model directly in c++ (and then print more diagnostic messages as the chain progresses). Let me know if you want to go that route. -Whit |
Hi, Whit, Thanks for your reply. I will try out all possibilities and get back to you. This is still my first try. My skills in c++ are at the very basic level and I will try that as the last step. Also, I'm not sure if you noticed but in the model function there is a Another thing in this particular model is that the two parameters are actually highly correlated. One of the reasons that I can guess is that random walk steps are too independent to jump to acceptable values for both parameters. Maybe adding an covariance structure in the priors would help but I am not sure if this can be done in rcppbugs. |
Update: just did a run with simulated dataset instead of the real ones. I also restricted in the simulation that the 2 parameters in the original model must be equal. Then the sampling actually updates well and the acceptance ratio was 0.17 for 100 samples. I am going to check some other MCMC algorithms to make a simple comparison. |
Now I did a simulation study without calling other computer program. But the acceptance ratio is still 0. The functions needed for this example is in a package repository at Github. and the results are: install_github("zhenglei-gao/SedimentWater",args="--no-multiarch")
library("SedimentWater")
n <- 4
yini <- c(1,rep(0,n))
SWModel <- gen.TOXSWA(n=n)
library(ggplot2)
library(reshape)
theme_set(theme_bw())
##cat(SWModel)
names(yini) <- c("Pw",paste0("Ps",0:(n-1)))
Zs <- 2
Zsed <- rep(Zs/n,n)
names(Zsed) <- paste0("Zs",0:(n-1))
parms <- c(kpwa = log(2)/50, kpse = log(2)/50, Kd = 50,rhob=2.05,theta=0.5,Zw=5,Dp=0.05,delta=1,Zsed)
cToxswaA <- compile.ode(SWModel, language = "C",
parms = parms,y=yini)
code(cToxswaA)
sigmaS <- c(sigmaW=0.1,sigmaS=0.01)
simData <- simSW(func = cToxswaA, yini = yini, parms = parms, times = seq(0,100,by=10),sigmaS =sigmaS,plotting=TRUE)
nparms <- length(parms)
res <- nls.lm(par=parms[1:3],lower=rep(0,3),fn=calcResid,data=simData,cModel=cToxswaA,fixParms=parms[4:nparms],fixInis=yini,transParms = NULL,sepn=3)
summary(res)
parms[1:3]
## Plot fitted
fitparms <- parms
fitparms[1:3] <- res$par
dta <- ode (func = cToxswaA, y = yini, parms = fitparms,times = seq(0,100,by=10))
plotSW(dta,origDta=simData)
y <- simData$Concentration
e <- calcResid(par=res$par,data=simData,cModel=cToxswaA,fixParms=parms[4:nparms],fixInis=yini,transParms = NULL,sepn=3)
obsDat <- cast(simData,Time~Media,value="Concentration")
library(rcppbugs)
sepWatSed <- FALSE
if(sepWatSed) tau.y <- mcmc.gamma(
c(sd(obsDat$Water),sd(obsDat$Sediment,na.rm=TRUE)),
alpha=0.1,beta=0.1) else {
tau.y <- mcmc.gamma(sd(e),alpha=0.1,beta=0.1)
}
sepn <-3
logpar <- mcmc.normal(rnorm(sepn),mu=log(res$par),tau=1)
logpar <- mcmc.normal(rnorm(sepn),mu=log(c(100,100, 5
)),tau=1)
y.hat <- deterministic(getY,logpar,data=simData,cModel=cToxswaA, fixParms=parms[4:nparms],fixInis=yini,transParms = NULL,sepn=3)
y.lik <- mcmc.normal(y,mu=y.hat,tau=tau.y,observed=TRUE)
m <- create.model(logpar, tau.y, y.hat, y.lik)
#runtime <- system.time(ans <- run.model(m, iterations=1e5L, burn=1e4L, adapt=2e3L, thin=10L))
cat("running model...\n")
runtime <- system.time(ans <- run.model(m, iterations=1e5L, burn=0, adapt=0, thin=10L))
print(apply(ans[["logpar"]],2,mean))
cat("acceptance.ratio:",get.ar(ans),"\n")
print(runtime) The model defined in
|
I am not sure where it went wrong. Optimization produced sensible results but are quite far away from the true values. Is it possible that it is because there are multiple local minima? |
If your acceptance ratio is at zero, then the chain simply cannot progress at all. I thought that I had checks in place so that one gets a 'stop' error thrown if you construct the initial values outside the support range of the statistic, but perhaps there is a bug. you have your model posted at that github link? I'll try to have a look. |
Yes, the model and the result should be reproducible from the above code. The R folder in that github link contains the functions used. The initial values are from the least square estimates. I will also try to use other prior variances to see if things change. Maybe it is a stupid mistake somewhere. |
have you tried STAN for this model? |
No, I haven't yet. Last time I checked there was no suport for ODE integrator yet. But I see now they have a working code brunch. However I need time to figure out how to define and use the ODE integrator in STAN. |
Hi Whit, I have ran the model in STAN and the chains freeze very often. I asked in stan user mailing list here where I got an answer related to the stiffness of the ODEs. I am not sure whether this caused the freezing of the sampling or the acceptance ratio being 0. The stan files used to simulate data and define the model were attached in the above link. The R code were also included. |
I think the main advantages of rcppbugs compared to JAGS and STAN is that user can have customized function in R in
rcppbugs
. Users can't define new functions as part of the modelling language in BUGS and JAGS. Although it could be done using low-level prgramming for all 3.I would like to implement MCMC methods for parameter estimation and inference for a complex computer model (which takes parameters and generates simulated results), in this case TOXSWA.
In the script below, I defined a function
toxswa
which write the parameter values into input files, then run command line version of toxswa and extract the output. It seems working fine but the acceptance ratio from the ouput chain (300 runs) is 0. I am not sure where it went wrong.The text was updated successfully, but these errors were encountered: