Skip to content

Commit

Permalink
AD support. roxygen. refactored dNmixture.
Browse files Browse the repository at this point in the history
  • Loading branch information
perrydv committed Jun 22, 2024
1 parent 3b313e3 commit 41c985e
Show file tree
Hide file tree
Showing 36 changed files with 5,926 additions and 2,712 deletions.
Binary file added .DS_Store
Binary file not shown.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ Collate:
dHMM.R
dOcc.R
dNmixture.R
dNmixtureAD.R
zzz.R
RoxygenNote: 7.2.0
RoxygenNote: 7.3.1
Suggests:
rmarkdown,
knitr,
Expand Down
83 changes: 58 additions & 25 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(dBetaBinom)
export(dBetaBinom_One)
export(dCJS_ss)
export(dCJS_sv)
export(dCJS_vs)
Expand All @@ -19,19 +22,38 @@ export(dDynOcc_vvs)
export(dDynOcc_vvv)
export(dHMM)
export(dHMMo)
export(dNmixture_s)
export(dNmixture_v)
export(dNmixture_BNB_oneObs)
export(dNmixture_BNB_v)
export(dNmixture_BNB_s)
export(dNmixture_BBP_oneObs)
export(dNmixture_BBP_v)
export(dNmixture_BBP_s)
export(dNmixtureAD_BBNB_oneObs)
export(dNmixtureAD_BBNB_s)
export(dNmixtureAD_BBNB_v)
export(dNmixtureAD_BBP_oneObs)
export(dNmixtureAD_BBP_s)
export(dNmixtureAD_BBP_v)
export(dNmixtureAD_BNB_oneObs)
export(dNmixtureAD_BNB_s)
export(dNmixtureAD_BNB_v)
export(dNmixtureAD_s)
export(dNmixtureAD_v)
export(dNmixture_BBNB_oneObs)
export(dNmixture_BBNB_v)
export(dNmixture_BBNB_s)
export(dNmixture_BBNB_steps)
export(dNmixture_BBNB_v)
export(dNmixture_BBP_oneObs)
export(dNmixture_BBP_s)
export(dNmixture_BBP_steps)
export(dNmixture_BBP_v)
export(dNmixture_BNB_oneObs)
export(dNmixture_BNB_s)
export(dNmixture_BNB_steps)
export(dNmixture_BNB_v)
export(dNmixture_s)
export(dNmixture_steps)
export(dNmixture_v)
export(dOcc_s)
export(dOcc_v)
export(nimBetaFun)
export(nimNmixPois_logFac)
export(rBetaBinom)
export(rBetaBinom_One)
export(rCJS_ss)
export(rCJS_sv)
export(rCJS_vs)
Expand All @@ -52,27 +74,38 @@ export(rDynOcc_vvs)
export(rDynOcc_vvv)
export(rHMM)
export(rHMMo)
export(rNmixture_s)
export(rNmixture_v)
export(rNmixture_BNB_oneObs)
export(rNmixture_BNB_v)
export(rNmixture_BNB_s)
export(rNmixture_BBP_oneObs)
export(rNmixture_BBP_v)
export(rNmixture_BBP_s)
export(rNmixtureAD_BBNB_oneObs)
export(rNmixtureAD_BBNB_s)
export(rNmixtureAD_BBNB_v)
export(rNmixtureAD_BBP_oneObs)
export(rNmixtureAD_BBP_s)
export(rNmixtureAD_BBP_v)
export(rNmixtureAD_BNB_oneObs)
export(rNmixtureAD_BNB_s)
export(rNmixtureAD_BNB_v)
export(rNmixtureAD_s)
export(rNmixtureAD_v)
export(rNmixture_BBNB_oneObs)
export(rNmixture_BBNB_v)
export(rNmixture_BBNB_s)
export(nimNmixPois_logFac)
export(rNmixture_BBNB_v)
export(rNmixture_BBP_oneObs)
export(rNmixture_BBP_s)
export(rNmixture_BBP_v)
export(rNmixture_BNB_oneObs)
export(rNmixture_BNB_s)
export(rNmixture_BNB_v)
export(rNmixture_s)
export(rNmixture_v)
export(rOcc_s)
export(rOcc_v)
export(nimBetaFun)
export(dBetaBinom)
export(dBetaBinom_One)
export(rBetaBinom)
export(rBetaBinom_One)
import(nimble)
importFrom(stats,dbinom)
importFrom(stats,dnbinom)
importFrom(stats,dpois)
importFrom(stats,qnbinom)
importFrom(stats,qpois)
importFrom(stats,rbeta)
importFrom(stats,rbinom)
importFrom(stats,rnbinom)
importFrom(stats,rpois)
importFrom(stats,runif)
importFrom("stats", "dpois", "qpois", "rpois", "dnbinom", "rnbinom", "rbeta", "qnbinom")
18 changes: 12 additions & 6 deletions R/dBetaBinom.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ nimBetaFun <- nimbleFunction(
run = function(a = double(0),
b = double(0),
log = logical(0)) {
if (log) return(lgamma(a) + lgamma(b) - lgamma(a + b))
else return(exp(lgamma(a) + lgamma(b) - lgamma(a + b)))
log_ans <- lgamma(a) + lgamma(b) - lgamma(a + b)
if (log) return(log_ans)
else return(exp(log_ans))
returnType(double(0))
})
}, buildDerivs=list(run=list()))

#' @rdname dBetaBinom
#' @export
Expand All @@ -77,17 +78,19 @@ dBetaBinom <- nimbleFunction(
shape2 = double(1),
log = integer(0, default = 0)) {
logprob <- 0
lgNp1 <- lgamma(N+1)
for (i in 1:length(x)) {
logprob <- logprob +
nimBetaFun(a = x[i] + shape1[i], b = N - x[i] + shape2[i], log = TRUE) -
nimBetaFun(a = shape1[i], b = shape2[ i], log = TRUE) +
lgamma(N+1) - (lgamma(x[i] + 1) + lgamma(N - x[i] + 1))
lgNp1 - (lgamma(x[i] + 1) + lgamma(N - x[i] + 1))
}

if (log) return(logprob)
return(exp(logprob))
returnType(double(0))
}
},
buildDerivs = list(run = list(ignore = 'i'))
)

#' @rdname dBetaBinom
Expand All @@ -107,12 +110,14 @@ dBetaBinom_One <- nimbleFunction(
if (log) return(logprob)
return(exp(logprob))
returnType(double(0))
}
},
buildDerivs = list(run=list())
)


#' @rdname dBetaBinom
#' @export
#' @importFrom stats rbeta
rBetaBinom <- nimbleFunction(
run = function(n = double(0),
N = double(0),
Expand All @@ -129,6 +134,7 @@ rBetaBinom <- nimbleFunction(

#' @rdname dBetaBinom
#' @export
#' @importFrom stats rbeta
rBetaBinom_One <- nimbleFunction(
run = function(n = double(0),
N = double(0),
Expand Down
13 changes: 13 additions & 0 deletions R/dCJS.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@
#'
#' and so on for each combination of time-dependent and time-independent parameters.
#'
#' @section Notes for use with automatic differentiation:
#'
#' The \code{dCJS_**} distributions should all work for models and algorithms
#' that use nimble's automatic differentiation (AD) system. In that system,
#' some kinds of values are "baked in" (cannot be changed) to the AD calculations
#' from the first call, unless and until the AD calculations are reset. For
#' the \code{dCJS_**} distributions, the lengths of vector inputs and the data
#' (\code{x}) values themselves are baked in. These can be different for different
#' iterations through a for loop (or nimble model declarations with different indices,
#' for example), but the lengths and data values for each specific iteration
#' will be "baked in" after the first call. \bold{In other words, it is assumed that
#' \code{x} are data and are not going to change.}
#'
#' @return
#'
#' For \code{dCJS_**}: the probability (or likelihood) or log probability of observation vector \code{x}.
Expand Down
11 changes: 11 additions & 0 deletions R/dDHMM.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@
#' \code{observedStates[1:T] ~ dDHMMo(initStates[1:S], observationProbs[1:S,
#' 1:O, 1:T], transitionProbs[1:S, 1:S, 1:(T-1)], 1, T)}
#'
#' The \code{dDHMM[o]} distributions should work for models and algorithms that
#' use nimble's automatic differentiation (AD) system. In that system, some
#' kinds of values are "baked in" (cannot be changed) to the AD calculations
#' from the first call, unless and until the AD calculations are reset. For the
#' \code{dDHMM[o]} distributions, the sizes of the inputs and the data (\code{x})
#' values themselves are baked in. These can be different for different
#' iterations through a for loop (or nimble model declarations with different
#' indices, for example), but the sizes and data values for each specific
#' iteration will be "baked in" after the first call. \bold{In other words, it
#' is assumed that \code{x} are data and are not going to change.}
#'
#' @return For \code{dDHMM} and \code{dDHMMo}: the probability (or likelihood)
#' or log probability of observation vector \code{x}. For \code{rDHMM} and
#' \code{rDHMMo}: a simulated detection history, \code{x}.
Expand Down
14 changes: 14 additions & 0 deletions R/dDynOcc.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@
#' probPersist = persistence_prob,
#' probColonize = colonization_prob[1:(T-1)], p = p[1:T, 1:O])}
#'
#' @section Notes for use with automatic differentiation:
#'
#' The \code{dDynOcc_***} distributions should all work for models and
#' algorithms that use nimble's automatic differentiation (AD) system. In that
#' system, some kinds of values are "baked in" (cannot be changed) to the AD
#' calculations from the first call, unless and until the AD calculations are
#' reset. For the \code{dDynOcc_***} distributions, the lengths or dimensions of
#' vector and/or matrix inputs and the \code{start} and \code{end} values
#' themselves are baked in. These can be different for different iterations
#' through a for loop (or nimble model declarations with different indices, for
#' example), but the for each specific iteration will be "baked in" after the
#' first call. \bold{It is safest if one can assume that \code{x} are data and
#' are not going to change.}
#'
#' @return
#' For \code{dDynOcc_***}: the probability (or likelihood) or log probability
#' of observation vector \code{x}.
Expand Down
13 changes: 13 additions & 0 deletions R/dHMM.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@
#' \code{observedStates[1:T] ~ dHMMo(initStates[1:S], observationProbs[1:S,
#' 1:O, 1:T], transitionProbs[1:S, 1:S], 1, T)}
#'
#' @section Notes for use with automatic differentiation:
#'
#' The \code{dHMM[o]} distributions should work for models and algorithms that
#' use nimble's automatic differentiation (AD) system. In that system, some
#' kinds of values are "baked in" (cannot be changed) to the AD calculations
#' from the first call, unless and until the AD calculations are reset. For the
#' \code{dHMM[o]} distributions, the sizes of the inputs and the data (\code{x})
#' values themselves are baked in. These can be different for different
#' iterations through a for loop (or nimble model declarations with different
#' indices, for example), but the sizes and data values for each specific
#' iteration will be "baked in" after the first call. \bold{In other words, it
#' is assumed that \code{x} are data and are not going to change.}
#'
#' @return For \code{dHMM} and \code{dHMMo}: the probability (or likelihood) or
#' log probability of observation vector \code{x}.
#'
Expand Down
Loading

0 comments on commit 41c985e

Please sign in to comment.