From 658c29f0854f0f75d0b15c27b16681bbf097a130 Mon Sep 17 00:00:00 2001 From: Daniel Turek Date: Wed, 24 Jan 2024 15:36:37 -0500 Subject: [PATCH] another attempt --- nimbleHMC/R/HMC_samplers.R | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/nimbleHMC/R/HMC_samplers.R b/nimbleHMC/R/HMC_samplers.R index 57d5f7c..3ae1228 100644 --- a/nimbleHMC/R/HMC_samplers.R +++ b/nimbleHMC/R/HMC_samplers.R @@ -142,6 +142,7 @@ hmc_checkTarget <- function(model, targetNodes, hmcType) { ## dists <- model$getDistribution(targetNodes) ###### delete after here: + dists dist <- dists[1] if(dist %in% c('ddistNoAD', 'ddistAD')) { print('AAAAAAA') @@ -160,15 +161,24 @@ hmc_checkTarget <- function(model, targetNodes, hmcType) { print('FFFFFFF') } ##### stop deleting here - ADok <- sapply(dists, - function(dist) { - ## this took some detective work to find the right environment: - nfObj <- eval(as.name(dist), envir = parent.frame(6)) - if(!is.null(environment(nfObj)$nfMethodRCobject)) { - ## user-defined: - return(!isFALSE(environment(nfObj)$nfMethodRCobject[['buildDerivs']])) - } else return(TRUE) ## non-user-defined - }) + ADok <- rep(TRUE, length(dists)) + for(i in seq_along(dists)) { + nfObj <- get(dists[i], envir = parent.frame(4)) + if(!is.null(environment(nfObj)$nfMethodRCobject)) { + ## is a user-defined distribution: + ADok[i] <- !isFALSE(environment(nfObj)$nfMethodRCobject[['buildDerivs']]) + } + } + ADok + ##ADok <- sapply(dists, + ## function(dist) { + ## ## this took some detective work to find the right environment: + ## nfObj <- eval(as.name(dist), envir = parent.frame(6)) + ## if(!is.null(environment(nfObj)$nfMethodRCobject)) { + ## ## user-defined: + ## return(!isFALSE(environment(nfObj)$nfMethodRCobject[['buildDerivs']])) + ## } else return(TRUE) ## non-user-defined + ## }) 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 = ', '))) }