Skip to content

Commit

Permalink
updated hmc_checkTarget to use checkADsupportForDistribution (#57)
Browse files Browse the repository at this point in the history
* updated hmc_checkTarget to use checkADsupportForDistribution

* removed unnecessary deparse

* fixed minor typo

* trigger testing
  • Loading branch information
danielturek authored Jun 14, 2024
1 parent dee7318 commit 1e336ea
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 16 deletions.
1 change: 1 addition & 0 deletions nimbleHMC/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ Copyright: See COPYRIGHTS file.
Encoding: UTF-8
LazyData: false
RoxygenNote: 7.3.1

17 changes: 1 addition & 16 deletions nimbleHMC/R/HMC_samplers.R
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,7 @@ hmc_checkTarget <- function(model, targetNodes, hmcType) {
dists <- targetDists_unique
ADok <- rep(TRUE, length(dists))
for(i in seq_along(dists)) {
##
## if/when modelDef$checkADsupportForDistribution() is added to core nimble,
## change the entire body of this for-loop to instead be:
## ADoak[i] <- model$getModelDef()$checkADsupportForDistribution(dists[i])
##
## these distributions get re-named to a nimble-version, and won't be found:
if(dists[i] %in% c('dweib', 'dmnorm', 'dmvt', 'dwish', 'dinvwish')) next
##
##
## find the function or this distribution:
nfObj <- get(dists[i], envir = parent.frame(4)) ## this took a bit of an investigation to make work
## is a user-defined distribution:
if(!is.null(environment(nfObj)$nfMethodRCobject)) {
## check for AD support:
ADok[i] <- !isFALSE(environment(nfObj)$nfMethodRCobject[['buildDerivs']])
}
ADok[i] <- model$getModelDef()$checkADsupportForDistribution(dists[i])
}
if(!all(ADok))
stop(paste0(hmcType, ' sampler cannot operate on user-defined distributions which do not support AD calculations. Try using buildDerivs = TRUE in the definition the distributions: ', paste0(dists[!ADok], collapse = ', ')))
Expand Down

0 comments on commit 1e336ea

Please sign in to comment.