Can I use my defined likelihood for runMCMC?

116 views
Skip to first unread message

Oyama

unread,
Sep 21, 2023, 5:46:28 AM9/21/23
to nimble-users
Hi all,

I am a new user for nimble, so I don't understand any system well. 

In my model, I calculate likelihood from my own equation that contain estimated value from MCMC. For example, in the model below, calculated likelihood is "Likelihoods", and estimated values from MCMC are "mu" and "std".

Can I use this likelihood for Metropolis-Hastings(synonymous with mcmc) ? 

It is very difficult to communicate in writing and may not be well explained.
Please ask me any questions if my explanation doesn't make sense.

Thank you for your attention, and any help would be much appreciated!

Oyama


This model below is a very simple example to calculate like-likelihood value.
#### Example  Model  ####
likeliCode <- nimbleCode({  
  # prior
  mu ~ dexp(1.0)
  std ~ dgamma(0.1,1.0)

  for (i in 1:N){
    likelihood[i] <- x[i] - dnorm(mu,std)
  }
  Likelihoods <-sum(likelihood[1:N])
})

num = 10
Consts <- list(N = num)
Data <- list(x = c(5, 1, 5, 14, 3, 19, 1, 1, 4, 22))
Inits <- list(mu= 1,
              std = 1)
likeli <- nimbleModel(code = likeliCode, name = "likeli", constants = Consts,
                    data = Data, inits = Inits)
likeConf <- configureMCMC(likeli, print = TRUE)
likeConf$addMonitors(c("Likelihoods", "mu", "std"))
likeMCMC <- buildMCMC(likeConf)
clikeli <- compileNimble(likeli)                       
ClikeMCMC <- compileNimble(likeMCMC, project = clikeli, resetFunctions = TRUE)   #only compiled project is available??

niter <- 1000
set.seed(1)
samples <- runMCMC(ClikeMCMC, niter = niter)

Daniel Turek

unread,
Sep 21, 2023, 3:29:09 PM9/21/23
to Oyama, nimble-users
Oyama, it's possible that you could go about fitting your model without writing a "likelihood" variable into the model; usually, the likelihood is instead represented by specifying a distribution for some data (observations) in the model, perhaps as:

for(i in 1:nObservations) {
    dataValues[i] ~ some_distribution(parameters)
}
So it's possible this would be a better way of specifying your likelihood, and allowing the MCMC to operate.

However, taking it at face value that you want to specify a variable in the model to contain the (numeric value of the) likelihood, then yes, there is a mechanism to use this numeric likelihood value in the context of Metropolis-Hastings sampling, and that mechanism is the RW_llFunction (Metropolis-Hastings Random Walk with a custom log-likelihood function) sampler.  You can read more about that in the nimble user manual, or by typing help(samplers) in R.

For now, though, maybe it would be helpful to understand better what your desired model is.  That might help us figure out what approach is better.  Are you able to describe your model a little more?

Thanks,
Daniel


--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/be35e86a-5af6-4c11-b63c-9647f6f32d93n%40googlegroups.com.

Oyama

unread,
Sep 21, 2023, 8:19:34 PM9/21/23
to nimble-users
Dear Daniel 

Thanks for getting back to me so quickly!

I think  RW_llFunction will be  useful for my model.
However I'll tell you a bit more about my model of what I want to do, just in case you can find a better way.

In my model, I do not compare  data-value directly with the parameter from the model. I perform a calculation on the difference between the data-value and the parameter from the model, and the result is another parameter, which is the likelihood of my model.

examCode <- nimbleCode({  
  # prior
  mu ~ dexp(1.0)
  std ~ dgamma(0.1,1.0)

  for (i in 1:N){
    someValue <- ...
    prelikelihood[i] <- dataValues[i] - someValue
    likelihood[i] <- prelikelihood[i]- dnorm(mu,std)
  }
  Likelihoods <-sum(likelihood[1:N])  ##  <--- Is it useful for  RW_llFunction??
})

I hope this explanation has been well communicated.
 I think it's better to use this "Likekihoods" for  RW_llFunction, don' you?

thanks,
Oyama
2023年9月22日金曜日 4:29:09 UTC+9 Daniel Turek:

Daniel Turek

unread,
Sep 26, 2023, 10:16:23 AM9/26/23
to Oyama, nimble-users
Oyama, thanks for sharing a bit more about your model and what you're trying to do.

Yes, I think you could use the RW_llFunction sampler (which would update the mu and/or std parameters separately), or the RW_llFunction_block sampler (if you want to jointy update {mu, std} together).

In order to do this, you would need to create a custom nimbleFunction to provide as the log-likelihood-function (llFunction) argument to the RW_llFunction sampler.  This nimbleFunction would be specialized to the model, and would operate to calculate the value of the Likelihoods term in the model, and return that value.

A brief sketch of your code could look something like:

code <- nimbleCode({...})
constants <- list(...)
data <- list(...)
inits <- list(...)
Rmodel <- nimbleModel(code, constants, data, inits)

llFunction_definition <- nimbleFunction(
    setup = function(model) { },
    run = function() {
        returnType(double())
        return(model$Likelihoods)
    }
)
my_llFunction <- llFunction_definition(Rmodel)

conf <- configureMCMC(Rmodel)
conf$addSampler(type = "RW_llFunction", target = "mu", llFunction = my_llFunction)

Rmcmc <- buildMCMC(conf)
## compile, run MCMC, etc...


Incidentally, is the Likelihoods term (in your model code) giving the value of the likelihood, or the log-likelihood?  And related, do you want the "log=TRUE" argument in your call inside the model to dnorm(mu,std) ?

Also, the way you sketched the code above, either "    someValue <- ..." would have to be indexed by [i], as "    someValue[i] <- ..." if it remains inside the for-loop.  Or alternatively, if the value of "someValue" does not change for each different value of i, then this declaration "    someValue <- ..." should be moved outside of the for-loop.]

I hope this helps get you started down the right track.

Cheers,
Daniel


Reply all
Reply to author
Forward
0 new messages