Error in node obs[1,1:2] Failure to calculate log density

  • Sheeraz Ahmad
    Sheeraz Ahmad

    I recently started using JAGS for some simulations. I tried using MATJAGS on Mac Mavericks which worked fine for some cases, but not always. So, I reverted to just JAGS. However, when I try to run my model I get the aforementioned error.

    I came across a couple of posts here where Martyn suggested that one might be "generating non-finite values somewhere in your model." However, I don't think my code has that issue; I use power function (x^y) but the base is positive coming from a gamma distribution. Can someone please help me with this? I am shooting for a paper deadline next week (06/06/14), and this is the bottleneck now. The code, data, parameters and script are attached. Thanks!

    # BUGS Model (CE_Model1.bug):
    model {
       # Prior on market trade-off
       gamma ~ dbeta(a,b)
       # gamma ~ dunif(a,b)
       for (i in 1:N){
        # Priors and CPTs
        v[i] ~ dgamma(k,k/mu)
        theta[i] ~ dnorm(0,tautheta)
        f[i,1] <- pow(v[i], (1+theta[i])/(2*gamma))
        f[i,2] <- pow(v[i], (1-theta[i])/(2*(1-gamma)))
        obs[i,1:2] ~ dmnorm(f[i,1:2], tau0mat[,])
    # Data (CE_data1.R):
    “a” <- 2
    “b” <- 2
    “k” <- 20
    “mu” <- 100
    “tautheta” <- 0.0025
    “tau0mat” <- structure(c(0.25,0,0,0.25), .Dim = c(2,2))
    “N” <- 2
    “obs” <- structure(c(40,60,60,40), .Dim = c(2,2))
    # Parameters (CE_init1.R):
    “v” <- c(100,100)
    “gamma” <- 0.5
    “theta” <- c(0,0)
    # Script (CE_test1.cmd):
    model in CE_model1.bug
    data in CE_data1.R
    compile, nchains(2)
    parameters in CE_init1.R
    update 2000 
    monitor set gamma, thin(2)
    monitor set v, thin(2)
    monitor deviance
    update 10000 
    coda *
  • Martyn Plummer
    Martyn Plummer

    You are getting a numerical overflow in the pow function. By running the example through a debugger I found that the error occurs when JAGS tries to calculate pow(96, 369), which returns inf. To stop the exponent from getting too large, you need to bound gamma away from the limits [0,1]. You can do this with

       gamma ~ dbeta(a,b) T(0.01, 0.99)


       gamma ~ dunif(0.01, 0.99)

    The model now runs, but the monitored parameters are all highly correlated

    > x <- read.openbugs()
    > crosscorr(x)
               gamma       v[1]       v[2]
    gamma  1.0000000 -0.8966695  0.9066792
    v[1]  -0.8966695  1.0000000 -0.8017520
    v[2]   0.9066792 -0.8017520  1.0000000

    and the mixing is poor (See plot(x)) so a very very long thining interval is required.

    Note that you need to load the "dic" module to monitor the deviance.

  • Sheeraz Ahmad
    Sheeraz Ahmad

    Thanks a lot Martyn! I tried your suggestion of truncating the gamma parameter, and that did resolve the issue. I would be mindful about the correlation between the parameters. What is the debugger that you used? Hopefully, in the future I can use it to try and find out the problem myself. Really appreciate the help.

  • Martyn Plummer
    Martyn Plummer

    This is for experts only, but can run the command line version of jags through the gdb debugger with jags -d gdb. You should compile jags without any optimization, otherwise you will find it difficult to step through functions (e.g. some variables are optimized out, STL template code becomes incomprehensible, etc.).

  • Sheeraz Ahmad
    Sheeraz Ahmad

    Thanks again! I will give the debugger a shot next time I run into a problem, and hopefully I would be able to make it work, although I don't have much expertise in JAGS yet.