From a1f88a074f797c1f489ed9ebea21ab00cc552461 Mon Sep 17 00:00:00 2001 From: jgabry Date: Fri, 14 Feb 2025 10:02:50 -0700 Subject: [PATCH] fix text failures due to hessian --- NEWS.md | 1 + R/fit.R | 26 ++++++++++++-------------- man/fit-method-hessian.Rd | 2 +- man/fit-method-init_model_methods.Rd | 20 ++++++++++++-------- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/NEWS.md b/NEWS.md index d2906ea0..089c75ac 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,7 @@ - `model_params` element of `fit$metadata()` list (`variables` element) - `jacobian_adjustment` argument to `fit$log_prob()` and similar methods (`jacobian` argument) - `output_samples` argument to `model$variational()` (`draws` argument) + - `hessian` argument to `fit$init_model_methods()` (`hessian` method always compiled now) - several arguments to `model$compile()`: - `threads` (`cpp_options = list(stan_threads = TRUE)`) - `compile_hessian_method` (always compiled) diff --git a/R/fit.R b/R/fit.R index 51b05b8f..76a0e972 100644 --- a/R/fit.R +++ b/R/fit.R @@ -314,28 +314,31 @@ CmdStanFit$set("public", name = "init", value = init) #' @aliases init_model_methods #' #' @description The `$init_model_methods()` method compiles and initializes the -#' `log_prob`, `grad_log_prob`, `constrain_variables`, `unconstrain_variables` -#' and `unconstrain_draws` functions. These are then available as methods of -#' the fitted model object. This requires the additional `Rcpp` package, -#' which are not required for fitting models using +#' `log_prob`, `grad_log_prob`, `hessian`, `constrain_variables`, +#' `unconstrain_variables` and `unconstrain_draws` functions. These are then +#' available as methods of the fitted model object. This requires the +#' additional `Rcpp` package, which are not required for fitting models using #' CmdStanR. #' -#' Note: there may be many compiler warnings emitted during compilation but +#' Notes: +#' * There may be many compiler warnings emitted during compilation but #' these can be ignored so long as they are warnings and not errors. +#' * The hessian method relies on higher-order autodiff, which is still +#' experimental. Please report any compilation errors that you encounter. #' #' @param seed (integer) The random seed to use when initializing the model. #' @param verbose (logical) Whether to show verbose logging during compilation. -#' @param hessian (logical) Whether to expose the (experimental) hessian method. #' #' @examples #' \dontrun{ #' fit_mcmc <- cmdstanr_example("logistic", method = "sample", force_recompile = TRUE) +#' # fit_mcmc$init_model_methods() #' } #' @seealso [log_prob()], [grad_log_prob()], [constrain_variables()], #' [unconstrain_variables()], [unconstrain_draws()], [variable_skeleton()], #' [hessian()] #' -init_model_methods <- function(seed = 1, verbose = FALSE, hessian = FALSE) { +init_model_methods <- function(seed = 1, verbose = FALSE) { if (os_is_wsl()) { stop("Additional model methods are not currently available with ", "WSL CmdStan and will not be compiled", @@ -346,13 +349,8 @@ init_model_methods <- function(seed = 1, verbose = FALSE, hessian = FALSE) { stop("Model methods cannot be used with a pre-compiled Stan executable, ", "the model must be compiled again", call. = FALSE) } - if (hessian) { - message("The hessian method relies on higher-order autodiff ", - "which is still experimental. Please report any compilation ", - "errors that you encounter") - } if (is.null(private$model_methods_env_$model_ptr)) { - expose_model_methods(private$model_methods_env_, verbose, hessian) + expose_model_methods(private$model_methods_env_, verbose) } if (!("model_ptr_" %in% ls(private$model_methods_env_))) { initialize_model_pointer(private$model_methods_env_, self$data_file(), seed) @@ -435,7 +433,7 @@ CmdStanFit$set("public", name = "grad_log_prob", value = grad_log_prob) #' @examples #' \dontrun{ #' fit_mcmc <- cmdstanr_example("logistic", method = "sample", force_recompile = TRUE) -#' # fit_mcmc$init_model_methods(hessian = TRUE) +#' # fit_mcmc$init_model_methods() #' # fit_mcmc$hessian(unconstrained_variables = c(0.5, 1.2, 1.1, 2.2)) #' } #' diff --git a/man/fit-method-hessian.Rd b/man/fit-method-hessian.Rd index f4f1ec55..8dfbf03a 100644 --- a/man/fit-method-hessian.Rd +++ b/man/fit-method-hessian.Rd @@ -21,7 +21,7 @@ The \verb{$hessian()} method provides access to the Stan model's \examples{ \dontrun{ fit_mcmc <- cmdstanr_example("logistic", method = "sample", force_recompile = TRUE) -# fit_mcmc$init_model_methods(hessian = TRUE) +# fit_mcmc$init_model_methods() # fit_mcmc$hessian(unconstrained_variables = c(0.5, 1.2, 1.1, 2.2)) } diff --git a/man/fit-method-init_model_methods.Rd b/man/fit-method-init_model_methods.Rd index 64fa8a76..e368bb46 100644 --- a/man/fit-method-init_model_methods.Rd +++ b/man/fit-method-init_model_methods.Rd @@ -6,29 +6,33 @@ \title{Compile additional methods for accessing the model log-probability function and parameter constraining and unconstraining.} \usage{ -init_model_methods(seed = 1, verbose = FALSE, hessian = FALSE) +init_model_methods(seed = 1, verbose = FALSE) } \arguments{ \item{seed}{(integer) The random seed to use when initializing the model.} \item{verbose}{(logical) Whether to show verbose logging during compilation.} - -\item{hessian}{(logical) Whether to expose the (experimental) hessian method.} } \description{ The \verb{$init_model_methods()} method compiles and initializes the -\code{log_prob}, \code{grad_log_prob}, \code{constrain_variables}, \code{unconstrain_variables} -and \code{unconstrain_draws} functions. These are then available as methods of -the fitted model object. This requires the additional \code{Rcpp} package, -which are not required for fitting models using +\code{log_prob}, \code{grad_log_prob}, \code{hessian}, \code{constrain_variables}, +\code{unconstrain_variables} and \code{unconstrain_draws} functions. These are then +available as methods of the fitted model object. This requires the +additional \code{Rcpp} package, which are not required for fitting models using CmdStanR. -Note: there may be many compiler warnings emitted during compilation but +Notes: +\itemize{ +\item There may be many compiler warnings emitted during compilation but these can be ignored so long as they are warnings and not errors. +\item The hessian method relies on higher-order autodiff, which is still +experimental. Please report any compilation errors that you encounter. +} } \examples{ \dontrun{ fit_mcmc <- cmdstanr_example("logistic", method = "sample", force_recompile = TRUE) +# fit_mcmc$init_model_methods() } } \seealso{