florianhartig / BayesianTools

General-Purpose MCMC and SMC Samplers and Tools for Bayesian Statistics
https://cran.r-project.org/web/packages/BayesianTools/index.html
115 stars 29 forks source link

Pass on extra arguments to the likelihood #234

Open DJackCai opened 2 years ago

DJackCai commented 2 years ago

Dear Florian

I was trying to use the DREAMzs algorithm to calibrate a land surface model for soil moisture estimation. The land surface model setting implies that ancillary forcing data (e.g. precipitation, temperature) and observed soil moisture data are needed to simulate the model and define the likelihood function.

Normally in the modelling workflow, I would put the forcing data and observed data as function arguments in addition to parameters - this allows the coupling with an external function that extracts the ancillary data from the data storage and thus allows generalisable implementation for different sites.

However, the createBayesianSetup in BayesianTools doesn't seem to allow me to pass additional arguments of forcing data and observed data, nor does runMCMC - there is no "..." feature in these functions.

In comparison, for the older dream package, we can pass additional modelling arguments by populating the " FUN.pars " argument: dream(FUN, func.type, pars, FUN.pars = list(), INIT = LHSInit, INIT.pars = list(), control = list(), measurement)

So I just wonder if there are any ways in the package to pass on extra arguments (other than parameters to be calibrated) to the likelihood function?

Your assistance would be really appreciated.

Jack

woodwards commented 2 years ago

I believe the only way to do it at present it to pass extra information in as global variables. In R, if a function can't find a variable in its own environment, it looks in the parent environment. So any variables in the global environment are available inside the function. e.g.

print(environment()) a <- "myglobal"

f <- function(x){ print(environment()) print(paste(x, "+", a)) }

f("myargument")

http://adv-r.had.co.nz/Environments.html

lirui0321 commented 11 months ago

I absolutely like the package. It makes my life easy when analyzing uncertainty associated with estimates using a MCMC approach. It would be great if the package allows us to pass extra argument to the likelihood function in a future version. It may be just a personal preference, but I feel that it makes code flows better when I write small modular functions rather than a big function that lumps everything into it.

florianhartig commented 11 months ago

Hi DJackCai / all,

apologies, it seems I forgot responding to this issue. Note that this is identical to #200, so see discussion there.

As @woodwards says, in the current version of BT, this can be done by accessing a global variable. So, define your data outside the likelihood, then you should be able to switch.

I personally never saw the need for passing data arguments to the likelihood, but given that so many people want to use it, I'm happy to consider if this can be done with the next revision of BT.

Best F

lirui0321 commented 11 months ago

Hi Florian,

Thanks for your feedback! I just write a wrapper function that glues everything into my likelihood function for now. I would highly appreciate it if you could consider adding this feature in next revision of BT :)

Rui