Skip to content

Commit

Permalink
rename argument silent -> strict (#648)
Browse files Browse the repository at this point in the history
* rename argument silent -> strict

* example requires non-strictness

* re-render docs

* fix typo

Co-authored-by: James Azam <[email protected]>

---------

Co-authored-by: James Azam <[email protected]>
  • Loading branch information
sbfnk and jamesmbaazam authored May 2, 2024
1 parent 0a1d58e commit ccdc197
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion R/create.R
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ create_stan_args <- function(stan = stan_opts(),
create_stan_delays <- function(..., time_points = 1L) {
delays <- list(...)
## discretise
delays <- map(list(...), discretise)
delays <- map(delays, discretise, strict = FALSE)
## convolve where appropriate
delays <- map(delays, collapse)
## apply tolerance
Expand Down
19 changes: 10 additions & 9 deletions R/dist_spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,10 @@ max.dist_spec <- function(x, ...) {
#' (i.e. those with finite support and constant parameters).
#' @title Discretise a <dist_spec>
#' @param x A `<dist_spec>`
#' @param silent Logical; if `TRUE` then any distribution that can't be
#' discretised will be returned as is. If `FALSE` then an error will be
#' thrown.
#' @param strict Logical; If `TRUE` (default) an error will be thrown if a
#' distribution cannot be discretised (e.g., because no finite maximum has been
#' specified or parameters are uncertain). If `FALSE` then any distribution
#' that cannot be discretised will be returned as is.
#' @return A `<dist_spec>` where all distributions with constant parameters are
#' nonparametric.
#' @export
Expand All @@ -420,14 +421,14 @@ max.dist_spec <- function(x, ...) {
#' dist2 <- LogNormal(mean = Normal(3, 0.5), sd = Normal(2, 0.5), max = 20)
#'
#' # The maxf the sum of two distributions
#' discretise(dist1 + dist2)
discretise <- function(x, silent = TRUE) {
#' discretise(dist1 + dist2, strict = FALSE)
discretise <- function(x, strict = TRUE) {
if (!is(x, "dist_spec")) {
stop("Can only discretise a <dist_spec>.")
}
## check max
max_x <- max(x)
if (any(is.infinite(max_x)) && !silent) {
if (any(is.infinite(max_x)) && strict) {
stop("Cannot discretise a distribution with infinite support.")
}
## discretise
Expand All @@ -445,12 +446,12 @@ discretise <- function(x, silent = TRUE) {
))
z$distribution <- "nonparametric"
return(z)
} else if (silent) {
return(y)
} else {
} else if (strict) {
stop(
"Cannot discretise a distribution with uncertain parameters."
)
} else {
return(y)
}
}
})
Expand Down
13 changes: 7 additions & 6 deletions man/discretise.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tests/testthat/test-dist_spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ test_that("dist_spec returns correct output for fixed lognormal distribution", {

test_that("dist_spec returns correct output for uncertain gamma distribution", {
result <- discretise(
Gamma(shape = Normal(3, 0.5), rate = Normal(2, 0.5), max = 19)
Gamma(shape = Normal(3, 0.5), rate = Normal(2, 0.5), max = 19),
strict = FALSE
)
expect_equal(result[[1]]$parameters$shape[[1]]$parameters$mean, 3)
expect_equal(result[[1]]$parameters$shape[[1]]$parameters$sd, 0.5)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-epinow.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test_that("epinow produces expected output when run with default settings", {
control = list(adapt_delta = 0.8)
),
logs = NULL, verbose = FALSE
)
)
)))

expect_equal(names(out), expected_out)
Expand Down

0 comments on commit ccdc197

Please sign in to comment.