Skip to content

Commit 37878f5

Browse files
authored
Merge b47a327 into 407db8c
2 parents 407db8c + b47a327 commit 37878f5

File tree

6 files changed

+63
-35
lines changed

6 files changed

+63
-35
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: cmdstanr
22
Title: R Interface to 'CmdStan'
3-
Version: 0.6.0
3+
Version: 0.6.0.9000
44
Date: 2023-07-25
55
Authors@R:
66
c(person(given = "Jonah", family = "Gabry", role = c("aut", "cre"),

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# cmdstanr 0.6.0.9000
2+
3+
Items for next release
4+
15
# cmdstanr 0.6.0
26

37
### Major new features

R/args.R

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ OptimizeArgs <- R6::R6Class(
374374
public = list(
375375
method = "optimize",
376376
initialize = function(iter = NULL,
377+
jacobian = NULL,
377378
algorithm = NULL,
378379
init_alpha = NULL,
379380
tol_obj = NULL,
@@ -382,8 +383,9 @@ OptimizeArgs <- R6::R6Class(
382383
tol_rel_grad = NULL,
383384
tol_param = NULL,
384385
history_size = NULL) {
385-
self$algorithm <- algorithm
386386
self$iter <- iter
387+
self$jacobian <- jacobian
388+
self$algorithm <- algorithm
387389
self$init_alpha <- init_alpha
388390
self$tol_obj <- tol_obj
389391
self$tol_rel_obj <- tol_rel_obj
@@ -407,6 +409,7 @@ OptimizeArgs <- R6::R6Class(
407409
}
408410
new_args <- list(
409411
"method=optimize",
412+
.make_arg("jacobian"),
410413
.make_arg("iter"),
411414
.make_arg("algorithm"),
412415
.make_arg("init_alpha"),
@@ -548,7 +551,7 @@ validate_cmdstan_args <- function(self) {
548551
checkmate::assert_integerish(self$refresh, lower = 0, null.ok = TRUE)
549552
checkmate::assert_integerish(self$sig_figs, lower = 1, upper = 18, null.ok = TRUE)
550553
if (!is.null(self$sig_figs) && cmdstan_version() < "2.25") {
551-
warning("The 'sig_figs' argument is only supported with cmdstan 2.25+ and will be ignored!")
554+
warning("The 'sig_figs' argument is only supported with cmdstan 2.25+ and will be ignored!", call. = FALSE)
552555
}
553556
if (!is.null(self$refresh)) {
554557
self$refresh <- as.integer(self$refresh)
@@ -669,6 +672,14 @@ validate_sample_args <- function(self, num_procs) {
669672
validate_optimize_args <- function(self) {
670673
checkmate::assert_subset(self$algorithm, empty.ok = TRUE,
671674
choices = c("bfgs", "lbfgs", "newton"))
675+
checkmate::assert_flag(self$jacobian, null.ok = TRUE)
676+
if (!is.null(self$jacobian)) {
677+
if (cmdstan_version() < "2.32") {
678+
warning("The 'jacobian' argument is only supported with cmdstan 2.32+ and will be ignored!", call. = FALSE)
679+
}
680+
self$jacobian <- as.integer(self$jacobian)
681+
}
682+
672683
checkmate::assert_integerish(self$iter, lower = 1, null.ok = TRUE, len = 1)
673684
if (!is.null(self$iter)) {
674685
self$iter <- as.integer(self$iter)

R/model.R

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,23 +1346,17 @@ CmdStanModel$set("public", name = "sample_mpi", value = sample_mpi)
13461346
#' @family CmdStanModel methods
13471347
#'
13481348
#' @description The `$optimize()` method of a [`CmdStanModel`] object runs
1349-
#' Stan's optimizer to obtain a posterior mode (penalized maximum likelihood)
1350-
#' estimate.
1349+
#' Stan's optimizer to obtain a (penalized) maximum likelihood estimate or a
1350+
#' maximum a posteriori estimate (if `jacobian=TRUE`). See the
1351+
#' [Maximum Likelihood Estimation](https://mc-stan.org/docs/cmdstan-guide/maximum-likelihood-estimation.html)
1352+
#' section of the CmdStan User's Guide for more details.
13511353
#'
13521354
#' Any argument left as `NULL` will default to the default value used by the
1353-
#' installed version of CmdStan. See the
1354-
#' [CmdStan User’s Guide](https://mc-stan.org/docs/cmdstan-guide/)
1355-
#' for more details.
1356-
#'
1357-
#' @details CmdStan can find the posterior mode (assuming there is one). If the
1358-
#' posterior is not convex, there is no guarantee Stan will be able to find
1359-
#' the global mode as opposed to a local optimum of log probability. For
1360-
#' optimization, the mode is calculated without the Jacobian adjustment for
1361-
#' constrained variables, which shifts the mode due to the change of
1362-
#' variables. Thus modes correspond to modes of the model as written.
1363-
#'
1364-
#' -- [*CmdStan User's Guide*](https://mc-stan.org/docs/cmdstan-guide/)
1365-
#'
1355+
#' installed version of CmdStan. See the [CmdStan User’s
1356+
#' Guide](https://mc-stan.org/docs/cmdstan-guide/) for more details on the
1357+
#' default arguments. The default values can also be obtained by checking the
1358+
#' metadata of an example model, e.g.,
1359+
#' `cmdstanr_example(method="optimize")$metadata()`.
13661360
#' @template model-common-args
13671361
#' @param threads (positive integer) If the model was
13681362
#' [compiled][model-method-compile] with threading support, the number of
@@ -1374,6 +1368,12 @@ CmdStanModel$set("public", name = "sample_mpi", value = sample_mpi)
13741368
#' for `"lbfgs"` and `"bfgs`. For their default values and more details see
13751369
#' the CmdStan User's Guide. The default values can also be obtained by
13761370
#' running `cmdstanr_example(method="optimize")$metadata()`.
1371+
#' @param jacobian (logical) Whether or not to use the Jacobian adjustment for
1372+
#' constrained variables. By default this is `FALSE`, meaning optimization
1373+
#' yields the (regularized) maximum likelihood estimate. Setting it to `TRUE`
1374+
#' yields the maximum a posteriori estimate. See the
1375+
#' [Maximum Likelihood Estimation](https://mc-stan.org/docs/cmdstan-guide/maximum-likelihood-estimation.html)
1376+
#' section of the CmdStan User's Guide for more details.
13771377
#' @param init_alpha (positive real) The initial step size parameter.
13781378
#' @param tol_obj (positive real) Convergence tolerance on changes in objective function value.
13791379
#' @param tol_rel_obj (positive real) Convergence tolerance on relative changes in objective function value.
@@ -1399,6 +1399,7 @@ optimize <- function(data = NULL,
13991399
threads = NULL,
14001400
opencl_ids = NULL,
14011401
algorithm = NULL,
1402+
jacobian = FALSE,
14021403
init_alpha = NULL,
14031404
iter = NULL,
14041405
tol_obj = NULL,
@@ -1418,6 +1419,7 @@ optimize <- function(data = NULL,
14181419
}
14191420
optimize_args <- OptimizeArgs$new(
14201421
algorithm = algorithm,
1422+
jacobian = jacobian,
14211423
init_alpha = init_alpha,
14221424
iter = iter,
14231425
tol_obj = tol_obj,

man/model-method-optimize.Rd

Lines changed: 16 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-model-optimize.R

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ ok_arg_values <- list(
1313
algorithm = "lbfgs",
1414
iter = 100,
1515
init_alpha = 0.002,
16-
save_latent_dynamics = FALSE
16+
save_latent_dynamics = FALSE,
17+
jacobian = TRUE
1718
)
1819

1920
# using any of these should cause optimize() to error
@@ -25,7 +26,8 @@ bad_arg_values <- list(
2526
algorithm = "NOT_AN_ALGORITHM",
2627
iter = -20,
2728
init_alpha = -20,
28-
save_latent_dynamics = "NOT_LOGICAL"
29+
save_latent_dynamics = "NOT_LOGICAL",
30+
jacobian = 30
2931
)
3032

3133
ok_arg_sci_nota_values <- list(
@@ -142,3 +144,11 @@ test_that("optimize() method runs when the stan file is removed", {
142144
mod_tmp$optimize(data = data_list)
143145
)
144146
})
147+
148+
test_that("optimize() recognizes new jacobian argument", {
149+
fit <- mod$optimize(data = data_list, jacobian = FALSE)
150+
expect_equal(fit$metadata()$jacobian, 0)
151+
152+
fit2 <- mod$optimize(data = data_list, jacobian = TRUE)
153+
expect_equal(fit2$metadata()$jacobian, 1)
154+
})

0 commit comments

Comments
 (0)