View source: R/classBayesianSetup.R

createBayesianSetup | R Documentation |

Creates a standardized collection of prior, likelihood and posterior functions, including error checks etc.

createBayesianSetup( likelihood, prior = NULL, priorSampler = NULL, parallel = FALSE, lower = NULL, upper = NULL, best = NULL, names = NULL, parallelOptions = list(variables = "all", packages = "all", dlls = NULL), catchDuplicates = FALSE, plotLower = NULL, plotUpper = NULL, plotBest = NULL )

`likelihood` |
log likelihood density function |

`prior` |
either a prior class (see |

`priorSampler` |
if a prior density (and not a prior class) is provided to prior, the optional prior sampling function can be provided here |

`parallel` |
parallelization option. Default is F. Other options include T, or "external". See details. |

`lower` |
vector with lower prior limits |

`upper` |
vector with upper prior limits |

`best` |
vector with best prior values |

`names` |
optional vector with parameter names |

`parallelOptions` |
list containing three lists. First "packages" determines the R packages necessary to run the likelihood function. Second "variables" the objects in the global environment needed to run the likelihood function and third "dlls" the DLLs needed to run the likelihood function (see Details and Examples). |

`catchDuplicates` |
Logical, determines whether unique parameter combinations should only be evaluated once. Only used when the likelihood accepts a matrix with parameter as columns. |

`plotLower` |
vector with lower limits for plotting |

`plotUpper` |
vector with upper limits for plotting |

`plotBest` |
vector with best values for plotting |

If prior is of class prior (e.g. create with `createPrior`

), priorSampler, lower, upper and best will be ignored.

If prior is a function (log prior density), priorSampler (custom sampler), or lower/upper (uniform sampler) is required.

If prior is NULL, and lower and upper are passed, a uniform prior (see `createUniformPrior`

) will be created with boundaries lower and upper.

For parallelization, Bayesiantools requies that the likelihood can evaluate several parameter vectors (supplied as a matrix) in parallel.

parallel = T means that an automatic parallelization of the likelihood via a standard R socket cluster is attempted, using the function

`generateParallelExecuter`

. By default, of the N cores detected on the computer, N-1 cores are requested. Alternatively, you can provide a integer number to parallel, specifying the cores reserved for the cluster. When the cluster is cluster is created, a copy of your workspace, including DLLs and objects are exported to the cluster workers. Because this can be very inefficient, you can explicitly specify the packages, objects and DLLs that are to be exported via parallelOptions. Using parallel = T requires that the function to be parallelized is well encapsulate, i.e. can run on a shared memory / shared hard disk machine in parallel without interfering with each other.

If automatic parallelization cannot be done (e.g. because dlls are not thread-safe or write to shared disk), and only in this case, you should specify parallel = "external". In this case, it is assumed that the likelihood is programmed such that it accepts a matrix with parameters as columns and the different model runs as rows. It is then up to the user if and how to parallelize this function. This option gives most flexibility to the user, in particular for complicated parallel architecture or shared memory problems.

For more details on parallelization, make sure to read both vignettes, in particular the section on the likelihood in the main vignette, and the section on parallelization in the vignette on interfacing models.

Florian Hartig, Tankred Ott

`checkBayesianSetup`

`createLikelihood`

`createPrior`

ll <- function(x) sum(dnorm(x, log = TRUE)) test <- createBayesianSetup(ll, prior = NULL, priorSampler = NULL, lower = -10, upper = 10) str(test) test$prior$density(0) test$likelihood$density(c(1,1)) test$likelihood$density(1) test$posterior$density(1) test$posterior$density(1, returnAll = TRUE) test$likelihood$density(matrix(rep(1,4), nrow = 2)) #test$posterior$density(matrix(rep(1,4), nrow = 2), returnAll = TRUE) test$likelihood$density(matrix(rep(1,4), nrow = 4)) ## Not run: ## Example of how to use parallelization using the VSEM model # Note that the parallelization produces overhead and is not always # speeding things up. In this example, due to the small # computational cost of the VSEM the parallelization is # most likely to reduce the speed of the sampler. # Creating reference data PAR <- VSEMcreatePAR(1:1000) refPars <- VSEMgetDefaults() refPars[12,] <- c(0.2, 0.001, 1) rownames(refPars)[12] <- "error-sd" referenceData <- VSEM(refPars$best[1:11], PAR) obs = apply(referenceData, 2, function(x) x + rnorm(length(x), sd = abs(x) * refPars$best[12])) # Selecting parameters parSel = c(1:6, 12) ## Builidng the likelihood function likelihood <- function(par, sum = TRUE){ x = refPars$best x[parSel] = par predicted <- VSEM(x[1:11], PAR) diff = c(predicted[,1:3] - obs[,1:3]) llValues = dnorm(diff, sd = max(abs(c(predicted[,1:3])),0.0001) * x[12], log = TRUE) if (sum == False) return(llValues) else return(sum(llValues)) } # Prior prior <- createUniformPrior(lower = refPars$lower[parSel], upper = refPars$upper[parSel]) ## Definition of the packages and objects that are exported to the cluster. # These are the objects that are used in the likelihood function. opts <- list(packages = list("BayesianTools"), variables = list("refPars", "obs", "PAR" ), dlls = NULL) # Create Bayesian Setup BSVSEM <- createBayesianSetup(likelihood, prior, best = refPars$best[parSel], names = rownames(refPars)[parSel], parallel = 2, parallelOptions = opts) ## The bayesianSetup can now be used in the runMCMC function. # Note that not all samplers can make use of parallel # computing. # Remove the Bayesian Setup and close the cluster stopParallel(BSVSEM) rm(BSVSEM) ## End(Not run)

BayesianTools documentation built on Feb. 16, 2023, 8:44 p.m.

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.