diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md new file mode 100644 index 000000000..f28c8b714 --- /dev/null +++ b/.github/CONTRIBUTING.md @@ -0,0 +1,56 @@ +# Contributing to cmdstanr + +This outlines how to propose a change to cmdstanr and is based on similar +instructions for tidyverse packages, including the contributing guidelines +generated by `usethis::use_tidy_contributing()`. + +## Fixing typos + +You can fix typos, spelling mistakes, or grammatical errors in the documentation +directly using the GitHub web interface, as long as the changes are made in the +_source_ file. This generally means you'll need to edit +[roxygen2 comments](https://roxygen2.r-lib.org/articles/roxygen2.html) in an `.R`, +not a `.Rd` file. You can find the `.R` file that generates the `.Rd` by reading +the comment in the first line. + +## Bigger changes + +If you want to make a bigger change, it's a good idea to first file an issue and +make sure someone from the team agrees that it’s needed. If you’ve found a bug, +please file an issue that illustrates the bug with a minimal reproducible +example (see e.g. the [tidyverse reprex instructions](https://www.tidyverse.org/help/#reprex)). +The tidyverse guide on [how to create a great issue](https://code-review.tidyverse.org/issues/) +has more advice. + +### Pull request process + +If you are new to creating pull requests here are some tips. Using the functions +from the `usethis` package is not required but can be helpful if this process is +new to you. + +* Fork the package and clone onto your computer. If you haven't done this before, we recommend using `usethis::create_from_github("stan-dev/cmdstanr", fork = TRUE)`. + +* Install all development dependencies with `devtools::install_dev_deps()`, and then make sure the package passes R CMD check by running `devtools::check()`. + If R CMD check doesn't pass cleanly, it's a good idea to ask for help before continuing. +* Create a Git branch for your pull request (PR). We recommend using `usethis::pr_init("brief-description-of-change")`. + +* Make your changes, commit to git, and then create a PR by running `usethis::pr_push()`, and following the prompts in your browser. + The title of your PR should briefly describe the change. + The body of your PR should contain `Fixes #issue-number`. + +* For user-facing changes, add a bullet to the top of `NEWS.md` (i.e. just below the first header). Follow the style already used in `NEWS.md`. + +### Code style + +* New code should attempt to follow the style used in the package. When in doubt follow the tidyverse [style guide](https://style.tidyverse.org). + +* We use [roxygen2](https://cran.r-project.org/package=roxygen2), with [Markdown syntax](https://cran.r-project.org/web/packages/roxygen2/vignettes/rd-formatting.html), for documentation. + +* We use [testthat](https://cran.r-project.org/package=testthat) for unit tests. + Contributions with test cases included are easier to accept. + +## Code of Conduct + +Please note that the cmdstanr project follows the Stan project's +[Code of Conduct](https://discourse.mc-stan.org/t/announcing-our-new-stan-code-of-conduct/23764). +By contributing to this project you agree to abide by its terms. diff --git a/.github/workflows/R-CMD-check-wsl.yaml b/.github/workflows/R-CMD-check-wsl.yaml index 4e2241f2d..df3c423f6 100644 --- a/.github/workflows/R-CMD-check-wsl.yaml +++ b/.github/workflows/R-CMD-check-wsl.yaml @@ -37,11 +37,11 @@ jobs: - uses: actions/checkout@v4 - - uses: r-lib/actions/setup-r@v2.6.4 + - uses: r-lib/actions/setup-r@v2.6.5 with: r-version: 'release' rtools-version: '42' - - uses: r-lib/actions/setup-pandoc@v2.6.4 + - uses: r-lib/actions/setup-pandoc@v2.6.5 - name: Query dependencies run: | @@ -98,7 +98,7 @@ jobs: - name: Upload check results if: failure() - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: wsl-backend-results path: check diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index f5e0f438f..63d06d9dc 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -57,11 +57,11 @@ jobs: sudo apt-get install -y libcurl4-openssl-dev || true sudo apt-get install -y openmpi-bin openmpi-common libopenmpi-dev || true - - uses: r-lib/actions/setup-r@v2.6.4 + - uses: r-lib/actions/setup-r@v2.6.5 with: r-version: ${{ matrix.config.r }} rtools-version: ${{ matrix.config.rtools }} - - uses: r-lib/actions/setup-pandoc@v2.6.4 + - uses: r-lib/actions/setup-pandoc@v2.6.5 - name: Query dependencies run: | @@ -113,7 +113,7 @@ jobs: - name: Upload check results if: failure() - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: ${{ runner.os }}-r${{ matrix.config.r }}-results path: check diff --git a/.github/workflows/Test-coverage.yaml b/.github/workflows/Test-coverage.yaml index fcbbecd1e..54870fd15 100644 --- a/.github/workflows/Test-coverage.yaml +++ b/.github/workflows/Test-coverage.yaml @@ -34,8 +34,8 @@ jobs: if: "!startsWith(github.ref, 'refs/tags/') && github.ref != 'refs/heads/master'" - uses: actions/checkout@v4 - - uses: r-lib/actions/setup-r@v2.6.4 - - uses: r-lib/actions/setup-pandoc@v2.6.4 + - uses: r-lib/actions/setup-r@v2.6.5 + - uses: r-lib/actions/setup-pandoc@v2.6.5 - name: Install Ubuntu dependencies run: | @@ -85,12 +85,12 @@ jobs: steps: - uses: actions/checkout@v4 - - uses: r-lib/actions/setup-r@v2.6.4 + - uses: r-lib/actions/setup-r@v2.6.5 with: r-version: 'release' rtools-version: '42' - - uses: r-lib/actions/setup-pandoc@v2.6.4 + - uses: r-lib/actions/setup-pandoc@v2.6.5 - name: Query dependencies run: | diff --git a/.github/workflows/cmdstan-tarball-check.yaml b/.github/workflows/cmdstan-tarball-check.yaml index bdcbc0f65..bbb894ebb 100644 --- a/.github/workflows/cmdstan-tarball-check.yaml +++ b/.github/workflows/cmdstan-tarball-check.yaml @@ -40,12 +40,12 @@ jobs: sudo apt-get install -y libcurl4-openssl-dev || true sudo apt-get install -y openmpi-bin openmpi-common libopenmpi-dev || true - - uses: r-lib/actions/setup-r@v2.6.4 + - uses: r-lib/actions/setup-r@v2.6.5 with: r-version: ${{ matrix.config.r }} rtools-version: ${{ matrix.config.rtools }} - - uses: r-lib/actions/setup-pandoc@v2.6.4 + - uses: r-lib/actions/setup-pandoc@v2.6.5 - name: Query dependencies run: | @@ -95,7 +95,7 @@ jobs: - name: Upload check results if: failure() - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: ${{ runner.os }}-r${{ matrix.config.r }}-results path: check diff --git a/DESCRIPTION b/DESCRIPTION index e6f376a45..61b823d09 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: cmdstanr Title: R Interface to 'CmdStan' -Version: 0.6.1.9000 -Date: 2023-08-25 +Version: 0.7.0.9000 +Date: 2023-12-13 Authors@R: c(person(given = "Jonah", family = "Gabry", role = c("aut", "cre"), email = "jsg2201@columbia.edu"), @@ -16,7 +16,8 @@ Authors@R: person(given = c("William", "Michael"), family = "Landau", role = "ctb", email = "will.landau@gmail.com", comment = c(ORCID = "0000-0003-1878-3253")), person(given = "Jacob", family = "Socolar", role = "ctb"), - person(given = "Martin", family = "Modrák", role = "ctb")) + person(given = "Martin", family = "Modrák", role = "ctb"), + person(given = "Steve", family = "Bronder", role = "ctb")) Description: A lightweight interface to 'Stan' . The 'CmdStanR' interface is an alternative to 'RStan' that calls the command line interface for compilation and running algorithms instead of interfacing @@ -44,6 +45,7 @@ Imports: rlang (>= 0.4.7) Suggests: bayesplot, + ggplot2, knitr (>= 1.37), loo (>= 2.0.0), rmarkdown, diff --git a/NAMESPACE b/NAMESPACE index 4ab6a168e..c8a6217dc 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,6 +4,7 @@ S3method(as_draws,CmdStanGQ) S3method(as_draws,CmdStanLaplace) S3method(as_draws,CmdStanMCMC) S3method(as_draws,CmdStanMLE) +S3method(as_draws,CmdStanPathfinder) S3method(as_draws,CmdStanVB) export(as_cmdstan_fit) export(as_draws) diff --git a/NEWS.md b/NEWS.md index 6818a8038..d1b65065a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,31 @@ -# cmdstanr 0.6.1.9000 +# cmdstanr 0.7.0.9000 + +Items for next release go here + +# cmdstanr 0.7.0 + +## Major new features + +* New `laplace` method by @jgabry in #800 +* New `pathfinder` method by @SteveBronder in #848 + +## Other improvements and bug fixes + +* Add missing link to diagnose method in CmdStanModel doc by @jgabry in #833 +* Improvements to compile tests by @martinmodrak in #836 +* Changed the delay behavior in wsl_installed by @martinmodrak in #839 +* Update array syntax in website vignette by @andrjohns in #841 +* Compatibility fixes for cmdstan 2.33+ by @jgabry in #843 +* Suggest format method after error due to old syntax by @jgabry in #852 +* Clarifications in R-markdown vignette by @jgcolman in #854 +* Update linux/wsl detection for install arch by @andrjohns in #856 +* Fix handling of single-length inits for containers by @andrjohns in #857 +* Add support/tests for exposing functions with tuples by @andrjohns in #860 +* Add support/tests for exporting functions with complex types by @andrjohns in #861 +* Add option for installing from release archive by @andrjohns in #866 +* Improve Pathfinder doc by @avehtari in #875 +* Rename `jacobian_adjustment` argument to `jacobian` by @jgabry in #879 +* Fix get_cmdstan_flags('STANCFLAGS') in recursive make by @pearsonca in #881 # cmdstanr 0.6.1 diff --git a/R/args.R b/R/args.R index 4ac5ab769..123022a54 100644 --- a/R/args.R +++ b/R/args.R @@ -16,6 +16,7 @@ #' * `OptimizeArgs`: stores arguments specific to `method=optimize`. #' * `LaplaceArgs`: stores arguments specific to `method=laplace`. #' * `VariationalArgs`: stores arguments specific to `method=variational` +#' * `PathfinderArgs`: stores arguments specific to `method=pathfinder` #' * `GenerateQuantitiesArgs`: stores arguments specific to `method=generate_quantities` #' * `DiagnoseArgs`: stores arguments specific to `method=diagnose` #' @@ -41,7 +42,8 @@ CmdStanArgs <- R6::R6Class( output_basename = NULL, sig_figs = NULL, opencl_ids = NULL, - model_variables = NULL) { + model_variables = NULL, + num_threads = NULL) { self$model_name <- model_name self$stan_code <- stan_code @@ -82,6 +84,7 @@ CmdStanArgs <- R6::R6Class( } self$init <- init self$opencl_ids <- opencl_ids + self$num_threads = NULL self$method_args$validate(num_procs = length(self$proc_ids)) self$validate() }, @@ -179,6 +182,9 @@ CmdStanArgs <- R6::R6Class( if (!is.null(self$opencl_ids)) { args$opencl <- c("opencl", paste0("platform=", self$opencl_ids[1]), paste0("device=", self$opencl_ids[2])) } + if (!is.null(self$num_threads)) { + num_threads <- c(args$output, paste0("num_threads=", self$num_threads)) + } args <- do.call(c, append(args, list(use.names = FALSE))) self$method_args$compose(idx, args) }, @@ -541,6 +547,75 @@ VariationalArgs <- R6::R6Class( ) ) +# PathfinderArgs --------------------------------------------------------- + +PathfinderArgs <- R6::R6Class( + "PathfinderArgs", + lock_objects = FALSE, + public = list( + method = "pathfinder", + initialize = function(init_alpha = NULL, + tol_obj = NULL, + tol_rel_obj = NULL, + tol_grad = NULL, + tol_rel_grad = NULL, + tol_param = NULL, + history_size = NULL, + single_path_draws = NULL, + draws = NULL, + num_paths = NULL, + max_lbfgs_iters = NULL, + num_elbo_draws = NULL, + save_single_paths = NULL) { + self$init_alpha <- init_alpha + self$tol_obj <- tol_obj + self$tol_rel_obj <- tol_rel_obj + self$tol_grad <- tol_grad + self$tol_rel_grad <- tol_rel_grad + self$tol_param <- tol_param + self$history_size <- history_size + self$num_psis_draws <- draws + self$num_draws <- single_path_draws + self$num_paths <- num_paths + self$max_lbfgs_iters <- max_lbfgs_iters + self$num_elbo_draws <- num_elbo_draws + self$save_single_paths <- save_single_paths + invisible(self) + }, + + validate = function(num_procs) { + validate_pathfinder_args(self) + }, + + # Compose arguments to CmdStan command for pathfinder-specific + # non-default arguments. Works the same way as compose for sampler args, + # but `idx` (multiple pathfinders are handled in cmdstan) + compose = function(idx = NULL, args = NULL) { + .make_arg <- function(arg_name) { + compose_arg(self, arg_name, idx = NULL) + } + new_args <- list( + "method=pathfinder", + .make_arg("init_alpha"), + .make_arg("tol_obj"), + .make_arg("tol_rel_obj"), + .make_arg("tol_grad"), + .make_arg("tol_rel_grad"), + .make_arg("tol_param"), + .make_arg("history_size"), + .make_arg("num_psis_draws"), + .make_arg("num_draws"), + .make_arg("num_paths"), + .make_arg("max_lbfgs_iters"), + .make_arg("num_elbo_draws"), + .make_arg("save_single_paths") + ) + new_args <- do.call(c, new_args) + c(args, new_args) + } + ) +) + # DiagnoseArgs ------------------------------------------------------------- DiagnoseArgs <- R6::R6Class( @@ -854,6 +929,59 @@ validate_variational_args <- function(self) { invisible(TRUE) } +#' Validate arguments for pathfinder inference +#' @noRd +#' @param self A `PathfinderArgs` object. +#' @return `TRUE` invisibly unless an error is thrown. +validate_pathfinder_args <- function(self) { + + checkmate::assert_integerish(self$max_lbfgs_iters, lower = 1, null.ok = TRUE, len = 1) + if (!is.null(self$max_lbfgs_iters)) { + self$iter <- as.integer(self$max_lbfgs_iters) + } + checkmate::assert_integerish(self$num_paths, lower = 1, null.ok = TRUE, + len = 1) + if (!is.null(self$num_paths)) { + self$num_paths <- as.integer(self$num_paths) + } + checkmate::assert_integerish(self$num_draws, lower = 1, null.ok = TRUE, + len = 1, .var.name = "single_path_draws") + if (!is.null(self$num_draws)) { + self$num_draws <- as.integer(self$num_draws) + } + checkmate::assert_integerish(self$num_psis_draws, lower = 1, null.ok = TRUE, + len = 1, .var.name = "draws") + if (!is.null(self$num_psis_draws)) { + self$num_psis_draws <- as.integer(self$num_psis_draws) + } + checkmate::assert_integerish(self$num_elbo_draws, lower = 1, null.ok = TRUE, len = 1) + if (!is.null(self$num_elbo_draws)) { + self$num_elbo_draws <- as.integer(self$num_elbo_draws) + } + if (!is.null(self$save_single_paths) && is.logical(self$save_single_paths)) { + self$save_single_paths = as.integer(self$save_single_paths) + } + checkmate::assert_integerish(self$save_single_paths, null.ok = TRUE, + lower = 0, upper = 1, len = 1) + if (!is.null(self$save_single_paths)) { + self$save_single_paths <- 0 + } + + + # check args only available for lbfgs and bfgs + bfgs_args <- c("init_alpha", "tol_obj", "tol_rel_obj", "tol_grad", "tol_rel_grad", "tol_param") + for (arg in bfgs_args) { + checkmate::assert_number(self[[arg]], .var.name = arg, lower = 0, null.ok = TRUE) + } + + if (!is.null(self$history_size)) { + checkmate::assert_integerish(self$history_size, lower = 1, len = 1, null.ok = FALSE) + self$history_size <- as.integer(self$history_size) + } + + invisible(TRUE) +} + # Validation helpers ------------------------------------------------------ diff --git a/R/csv.R b/R/csv.R index 330958504..f2e674f81 100644 --- a/R/csv.R +++ b/R/csv.R @@ -280,6 +280,10 @@ read_cmdstan_csv <- function(files, if (length(variables) > 0) { draws_list_id <- length(draws) + 1 warmup_draws_list_id <- length(warmup_draws) + 1 + if (metadata$method == "pathfinder") { + metadata$variables = union(metadata$sampler_diagnostics, metadata$variables) + variables = union(metadata$sampler_diagnostics, variables) + } suppressWarnings( draws[[draws_list_id]] <- data.table::fread( cmd = fread_cmd, @@ -445,6 +449,21 @@ read_cmdstan_csv <- function(files, metadata = metadata, generated_quantities = draws ) + } else if (metadata$method == "pathfinder") { + if (is.null(format)) { + format <- "draws_matrix" + } + as_draws_format <- as_draws_format_fun(format) + if (length(draws) == 0) { + pathfinder_draws <- NULL + } else { + pathfinder_draws <- do.call(as_draws_format, list(draws[[1]][, colnames(draws[[1]]), drop = FALSE])) + posterior::variables(pathfinder_draws) <- repaired_variables + } + list( + metadata = metadata, + draws = pathfinder_draws + ) } } @@ -477,6 +496,7 @@ as_cmdstan_fit <- function(files, check_diagnostics = TRUE, format = getOption(" "sample" = CmdStanMCMC_CSV$new(csv_contents, files, check_diagnostics), "optimize" = CmdStanMLE_CSV$new(csv_contents, files), "variational" = CmdStanVB_CSV$new(csv_contents, files), + "pathfinder" = CmdStanPathfinder_CSV$new(csv_contents, files), "laplace" = CmdStanLaplace_CSV$new(csv_contents, files) ) } @@ -576,6 +596,23 @@ CmdStanVB_CSV <- R6::R6Class( private = list(output_files_ = NULL) ) +CmdStanPathfinder_CSV <- R6::R6Class( + classname = "CmdStanPathfinder_CSV", + inherit = CmdStanPathfinder, + public = list( + initialize = function(csv_contents, files) { + private$output_files_ <- files + private$draws_ <- csv_contents$draws + private$metadata_ <- csv_contents$metadata + }, + output_files = function(...) { + private$output_files_ + } + ), + private = list(output_files_ = NULL) +) + + # these methods are unavailable because there's no CmdStanRun object unavailable_methods_CmdStanFit_CSV <- c( "cmdstan_diagnose", "cmdstan_summary", @@ -642,7 +679,7 @@ read_csv_metadata <- function(csv_file) { "\"" ) } else { - fread_cmd <- paste0("grep '^[#a-zA-Z]' --color=never '", csv_file, "'") + fread_cmd <- paste0("grep '^[#a-zA-Z]' --color=never '", path.expand(csv_file), "'") } suppressWarnings( metadata <- data.table::fread( diff --git a/R/example.R b/R/example.R index 924c961f9..5bb98ae3e 100644 --- a/R/example.R +++ b/R/example.R @@ -50,7 +50,7 @@ #' cmdstanr_example <- function(example = c("logistic", "schools", "schools_ncp"), - method = c("sample", "optimize", "laplace", "variational", "diagnose"), + method = c("sample", "optimize", "laplace", "variational", "pathfinder", "diagnose"), ..., quiet = TRUE, force_recompile = getOption("cmdstanr_force_recompile", default = FALSE)) { diff --git a/R/fit.R b/R/fit.R index 71aa889e7..b1c99e97a 100644 --- a/R/fit.R +++ b/R/fit.R @@ -362,12 +362,13 @@ CmdStanFit$set("public", name = "init_model_methods", value = init_model_methods #' #' @name fit-method-log_prob #' @aliases log_prob -#' @description The `$log_prob()` method provides access to the Stan model's `log_prob` function +#' @description The `$log_prob()` method provides access to the Stan model's +#' `log_prob` function. #' -#' @param unconstrained_variables (numeric) A vector of unconstrained parameters -#' to be passed to `log_prob`. -#' @param jacobian_adjustment (logical) Whether to include the log-density -#' adjustments from un/constraining variables. +#' @param unconstrained_variables (numeric) A vector of unconstrained parameters. +#' @param jacobian (logical) Whether to include the log-density adjustments from +#' un/constraining variables. +#' @param jacobian_adjustment Deprecated. Please use `jacobian` instead. #' #' @examples #' \dontrun{ @@ -380,7 +381,11 @@ CmdStanFit$set("public", name = "init_model_methods", value = init_model_methods #' [unconstrain_variables()], [unconstrain_draws()], [variable_skeleton()], #' [hessian()] #' -log_prob <- function(unconstrained_variables, jacobian_adjustment = TRUE) { +log_prob <- function(unconstrained_variables, jacobian = TRUE, jacobian_adjustment = NULL) { + if (!is.null(jacobian_adjustment)) { + warning("'jacobian_adjustment' is deprecated. Please use 'jacobian' instead.", call. = FALSE) + jacobian <- jacobian_adjustment + } if (is.null(private$model_methods_env_$model_ptr)) { stop("The method has not been compiled, please call `init_model_methods()` first", call. = FALSE) @@ -390,7 +395,7 @@ log_prob <- function(unconstrained_variables, jacobian_adjustment = TRUE) { length(unconstrained_variables), " were provided!", call. = FALSE) } private$model_methods_env_$log_prob(private$model_methods_env_$model_ptr_, - unconstrained_variables, jacobian_adjustment) + unconstrained_variables, jacobian) } CmdStanFit$set("public", name = "log_prob", value = log_prob) @@ -401,11 +406,7 @@ CmdStanFit$set("public", name = "log_prob", value = log_prob) #' @aliases grad_log_prob #' @description The `$grad_log_prob()` method provides access to the Stan #' model's `log_prob` function and its derivative. -#' -#' @param unconstrained_variables (numeric) A vector of unconstrained parameters -#' to be passed to `grad_log_prob`. -#' @param jacobian_adjustment (logical) Whether to include the log-density -#' adjustments from un/constraining variables. +#' @inheritParams fit-method-log_prob #' #' @examples #' \dontrun{ @@ -418,7 +419,11 @@ CmdStanFit$set("public", name = "log_prob", value = log_prob) #' [unconstrain_variables()], [unconstrain_draws()], [variable_skeleton()], #' [hessian()] #' -grad_log_prob <- function(unconstrained_variables, jacobian_adjustment = TRUE) { +grad_log_prob <- function(unconstrained_variables, jacobian = TRUE, jacobian_adjustment = NULL) { + if (!is.null(jacobian_adjustment)) { + warning("'jacobian_adjustment' is deprecated. Please use 'jacobian' instead.", call. = FALSE) + jacobian <- jacobian_adjustment + } if (is.null(private$model_methods_env_$model_ptr)) { stop("The method has not been compiled, please call `init_model_methods()` first", call. = FALSE) @@ -428,7 +433,7 @@ grad_log_prob <- function(unconstrained_variables, jacobian_adjustment = TRUE) { length(unconstrained_variables), " were provided!", call. = FALSE) } private$model_methods_env_$grad_log_prob(private$model_methods_env_$model_ptr_, - unconstrained_variables, jacobian_adjustment) + unconstrained_variables, jacobian) } CmdStanFit$set("public", name = "grad_log_prob", value = grad_log_prob) @@ -439,11 +444,7 @@ CmdStanFit$set("public", name = "grad_log_prob", value = grad_log_prob) #' @aliases hessian #' @description The `$hessian()` method provides access to the Stan model's #' `log_prob`, its derivative, and its hessian. -#' -#' @param unconstrained_variables (numeric) A vector of unconstrained parameters -#' to be passed to `hessian`. -#' @param jacobian_adjustment (logical) Whether to include the log-density -#' adjustments from un/constraining variables. +#' @inheritParams fit-method-log_prob #' #' @examples #' \dontrun{ @@ -456,7 +457,11 @@ CmdStanFit$set("public", name = "grad_log_prob", value = grad_log_prob) #' [unconstrain_variables()], [unconstrain_draws()], [variable_skeleton()], #' [hessian()] #' -hessian <- function(unconstrained_variables, jacobian_adjustment = TRUE) { +hessian <- function(unconstrained_variables, jacobian = TRUE, jacobian_adjustment = NULL) { + if (!is.null(jacobian_adjustment)) { + warning("'jacobian_adjustment' is deprecated. Please use 'jacobian' instead.", call. = FALSE) + jacobian <- jacobian_adjustment + } if (is.null(private$model_methods_env_$model_ptr)) { stop("The method has not been compiled, please call `init_model_methods()` first", call. = FALSE) @@ -466,7 +471,7 @@ hessian <- function(unconstrained_variables, jacobian_adjustment = TRUE) { length(unconstrained_variables), " were provided!", call. = FALSE) } private$model_methods_env_$hessian(private$model_methods_env_$model_ptr_, - unconstrained_variables, jacobian_adjustment) + unconstrained_variables, jacobian) } CmdStanFit$set("public", name = "hessian", value = hessian) @@ -2024,6 +2029,80 @@ CmdStanVB <- R6::R6Class( ) CmdStanVB$set("public", name = "lp_approx", value = lp_approx) +# CmdStanPathfinder --------------------------------------------------------------- +#' CmdStanPathfinder objects +#' +#' @name CmdStanPathfinder +#' @family fitted model objects +#' @template seealso-docs +#' +#' @description A `CmdStanPathfinder` object is the fitted model object returned by the +#' [`$pathfinder()`][model-method-pathfinder] method of a +#' [`CmdStanModel`] object. +#' +#' @section Methods: `CmdStanPathfinder` objects have the following associated methods, +#' all of which have their own (linked) documentation pages. +#' +#' ## Extract contents of fitted model object +#' +#' |**Method**|**Description**| +#' |:----------|:---------------| +#' [`$draws()`][fit-method-draws] | Return approximate posterior draws as a [`draws_matrix`][posterior::draws_matrix]. | +#' [`$lp()`][fit-method-lp] | Return the total log probability density (`target`) computed in the model block of the Stan program. | +#' [`$lp_approx()`][fit-method-lp] | Return the log density of the approximation to the posterior. | +#' [`$init()`][fit-method-init] | Return user-specified initial values. | +#' [`$metadata()`][fit-method-metadata] | Return a list of metadata gathered from the CmdStan CSV files. | +#' [`$code()`][fit-method-code] | Return Stan code as a character vector. | +#' +#' ## Summarize inferences +#' +#' |**Method**|**Description**| +#' |:----------|:---------------| +#' [`$summary()`][fit-method-summary] | Run [`posterior::summarise_draws()`][posterior::draws_summary]. | +#' [`$cmdstan_summary()`][fit-method-cmdstan_summary] | Run and print CmdStan's `bin/stansummary`. | +#' +#' ## Save fitted model object and temporary files +#' +#' |**Method**|**Description**| +#' |:----------|:---------------| +#' [`$save_object()`][fit-method-save_object] | Save fitted model object to a file. | +#' [`$save_output_files()`][fit-method-save_output_files] | Save output CSV files to a specified location. | +#' [`$save_data_file()`][fit-method-save_data_file] | Save JSON data file to a specified location. | +#' [`$save_latent_dynamics_files()`][fit-method-save_latent_dynamics_files] | Save diagnostic CSV files to a specified location. | +#' +#' ## Report run times, console output, return codes +#' +#' |**Method**|**Description**| +#' |:----------|:---------------| +#' [`$time()`][fit-method-time] | Report the total run time. | +#' [`$output()`][fit-method-output] | Pretty print the output that was printed to the console. | +#' [`$return_codes()`][fit-method-return_codes] | Return the return codes from the CmdStan runs. | +#' +CmdStanPathfinder <- R6::R6Class( + classname = "CmdStanPathfinder", + inherit = CmdStanFit, + public = list(), + private = list( + # inherits draws_ and metadata_ slots from CmdStanFit + read_csv_ = function(format = getOption("cmdstanr_draws_format", "draws_matrix")) { + if (!length(self$output_files(include_failed = FALSE))) { + stop("Pathfinder failed. Unable to retrieve the draws.", call. = FALSE) + } + csv_contents <- read_cmdstan_csv(self$output_files(), format = format) + private$draws_ <- csv_contents$draws + private$metadata_ <- csv_contents$metadata + invisible(self) + } + ) +) + +#' @rdname fit-method-lp +lp_approx <- function() { + as.numeric(self$draws()[, "lp_approx__"]) +} +CmdStanPathfinder$set("public", name = "lp_approx", value = lp_approx) + + # CmdStanGQ --------------------------------------------------------------- #' CmdStanGQ objects @@ -2290,3 +2369,9 @@ as_draws.CmdStanVB <- function(x, ...) { as_draws.CmdStanGQ <- function(x, ...) { x$draws(...) } + +#' @rdname as_draws.CmdStanMCMC +#' @export +as_draws.CmdStanPathfinder <- function(x, ...) { + x$draws(...) +} diff --git a/R/install.R b/R/install.R index 25497763a..7d92f5e95 100644 --- a/R/install.R +++ b/R/install.R @@ -51,6 +51,10 @@ #' The URL should point to the tarball (`.tar.gz.` file) itself, e.g., #' `release_url="https://github.com/stan-dev/cmdstan/releases/download/v2.25.0/cmdstan-2.25.0.tar.gz"`. #' If both `version` and `release_url` are specified then `version` will be used. +#' @param release_file (string) A file path to a CmdStan release tar.gz file +#' downloaded from the releases page: . +#' For example: `release_file=""./cmdstan-2.33.1.tar.gz"`. If `release_file` is +#' specified then both `release_url` and `version` will be ignored. #' @param cpp_options (list) Any makefile flags/variables to be written to #' the `make/local` file. For example, `list("CXX" = "clang++")` will force #' the use of clang for compilation. @@ -82,6 +86,7 @@ install_cmdstan <- function(dir = NULL, timeout = 1200, version = NULL, release_url = NULL, + release_file = NULL, cpp_options = list(), check_toolchain = TRUE, wsl = FALSE) { @@ -118,7 +123,15 @@ install_cmdstan <- function(dir = NULL, dir <- repair_path(dir) assert_dir_exists(dir, access = "rwx") } - if (!is.null(version)) { + if (!is.null(release_file)) { + if (!is.null(release_url) || !is.null(version)) { + warning("release_file and release_url/version shouldn't both be specified!", + "\nrelease_url/version will be ignored.", call. = FALSE) + } + + release_url <- release_file + } + if (!is.null(version) && is.null(release_file)) { if (!is.null(release_url)) { warning("version and release_url shouldn't both be specified!", "\nrelease_url will be ignored.", call. = FALSE) @@ -155,18 +168,22 @@ install_cmdstan <- function(dir = NULL, if (!check_install_dir(dir_cmdstan, overwrite)) { return(invisible(NULL)) } - tar_downloaded <- download_with_retries(download_url, dest_file, quiet = quiet) - if (inherits(tar_downloaded, "try-error")) { - error_msg <- paste("Download of CmdStan failed with error:", - attr(tar_downloaded, "condition")$message) - if (!is.null(version)) { - error_msg <- paste0(error_msg, "\nPlease check if the supplied version number is valid.") - } else if (!is.null(release_url)) { - error_msg <- paste0(error_msg, "\nPlease check if the supplied release URL is valid.") + if (is.null(release_file)) { + tar_downloaded <- download_with_retries(download_url, dest_file, quiet = quiet) + if (inherits(tar_downloaded, "try-error")) { + error_msg <- paste("Download of CmdStan failed with error:", + attr(tar_downloaded, "condition")$message) + if (!is.null(version)) { + error_msg <- paste0(error_msg, "\nPlease check if the supplied version number is valid.") + } else if (!is.null(release_url)) { + error_msg <- paste0(error_msg, "\nPlease check if the supplied release URL is valid.") + } + stop(error_msg, call. = FALSE) } - stop(error_msg, call. = FALSE) + message("* Download complete") + } else { + file.copy(release_file, dest_file) } - message("* Download complete") message("* Unpacking archive...") if (wsl) { # Significantly faster to use WSL to untar the downloaded archive, as there are @@ -381,10 +398,10 @@ github_download_url <- function(version_number) { } # get version number of latest release -latest_released_version <- function(quiet=TRUE) { +latest_released_version <- function(quiet=TRUE, ...) { dest_file <- tempfile(pattern = "releases-", fileext = ".json") download_url <- "https://api.github.com/repos/stan-dev/cmdstan/releases/latest" - release_list_downloaded <- download_with_retries(download_url, dest_file, quiet = quiet) + release_list_downloaded <- download_with_retries(download_url, dest_file, quiet = quiet, ...) if (inherits(release_list_downloaded, "try-error")) { stop("GitHub download of release list failed with error: ", attr(release_list_downloaded, "condition")$message, @@ -762,7 +779,7 @@ cmdstan_arch_suffix <- function(version = NULL) { } else { arch <- R.version$arch } - + if (any(grepl(arch, c("x86_64", "i686")))) { return(NULL) } diff --git a/R/model.R b/R/model.R index c061e9ac7..7f298b118 100644 --- a/R/model.R +++ b/R/model.R @@ -1,6 +1,6 @@ #' Create a new CmdStanModel object #' -#' @description \if{html}{\figure{logo.png}{options: width="25px"}} +#' @description \if{html}{\figure{logo.png}{options: width=25}} #' Create a new [`CmdStanModel`] object from a file containing a Stan program #' or from an existing Stan executable. The [`CmdStanModel`] object stores the #' path to a Stan program and compiled executable (once created), and provides @@ -93,19 +93,26 @@ #' fit_optim <- mod$optimize(data = my_data_file, seed = 123) #' fit_optim$summary() #' -#' # Run 'optimize' again with 'jacobian=TRUE' and then draw from laplace approximation +#' # Run 'optimize' again with 'jacobian=TRUE' and then draw from Laplace approximation #' # to the posterior #' fit_optim <- mod$optimize(data = my_data_file, jacobian = TRUE) #' fit_laplace <- mod$laplace(data = my_data_file, mode = fit_optim, draws = 2000) #' fit_laplace$summary() #' -#' # Run 'variational' method to approximate the posterior (default is meanfield ADVI) +#' # Run 'variational' method to use ADVI to approximate posterior #' fit_vb <- mod$variational(data = stan_data, seed = 123) #' fit_vb$summary() -#' -#' # Plot approximate posterior using bayesplot #' mcmc_hist(fit_vb$draws("theta")) #' +#' # Run 'pathfinder' method, a new alternative to the variational method +#' fit_pf <- mod$pathfinder(data = stan_data, seed = 123) +#' fit_pf$summary() +#' mcmc_hist(fit_pf$draws("theta")) +#' +#' # Run 'pathfinder' again with more paths, fewer draws per path, +#' # better covariance approximation, and fewer LBFGSs iterations +#' fit_pf <- mod$pathfinder(data = stan_data, num_paths=10, single_path_draws=40, +#' history_size=50, max_lbfgs_iters=100) #' #' # Specifying initial values as a function #' fit_mcmc_w_init_fun <- mod$sample( @@ -205,6 +212,7 @@ cmdstan_model <- function(stan_file = NULL, exe_file = NULL, compile = TRUE, ... #' [`$sample_mpi()`][model-method-sample_mpi] | Run CmdStan's `"sample"` method with [MPI](https://mc-stan.org/math/mpi.html), return [`CmdStanMCMC`] object. | #' [`$optimize()`][model-method-optimize] | Run CmdStan's `"optimize"` method, return [`CmdStanMLE`] object. | #' [`$variational()`][model-method-variational] | Run CmdStan's `"variational"` method, return [`CmdStanVB`] object. | +#' [`$pathfinder()`][model-method-pathfinder] | Run CmdStan's `"pathfinder"` method, return [`CmdStanPathfinder`] object. | #' [`$generate_quantities()`][model-method-generate-quantities] | Run CmdStan's `"generate quantities"` method, return [`CmdStanGQ`] object. | #' #' @template seealso-docs @@ -373,7 +381,7 @@ CmdStanModel <- R6::R6Class( #' @param pedantic (logical) Should pedantic mode be turned on? The default is #' `FALSE`. Pedantic mode attempts to warn you about potential issues in your #' Stan program beyond syntax errors. For details see the [*Pedantic mode* -#' chapter](https://mc-stan.org/docs/reference-manual/pedantic-mode.html) in +#' chapter](https://mc-stan.org/docs/stan-users-guide/pedantic-mode.html) in #' the Stan Reference Manual. **Note:** to do a pedantic check for a model #' without compiling it or for a model that is already compiled the #' [`$check_syntax()`][model-method-check_syntax] method can be used instead. @@ -789,7 +797,7 @@ CmdStanModel$set("public", name = "variables", value = variables) #' @param pedantic (logical) Should pedantic mode be turned on? The default is #' `FALSE`. Pedantic mode attempts to warn you about potential issues in your #' Stan program beyond syntax errors. For details see the [*Pedantic mode* -#' chapter](https://mc-stan.org/docs/reference-manual/pedantic-mode.html) in +#' chapter](https://mc-stan.org/docs/stan-users-guide/pedantic-mode.html) in #' the Stan Reference Manual. #' @param include_paths (character vector) Paths to directories where Stan #' should look for files specified in `#include` directives in the Stan @@ -1265,7 +1273,7 @@ CmdStanModel$set("public", name = "sample", value = sample) #' #' @description The `$sample_mpi()` method of a [`CmdStanModel`] object is #' identical to the `$sample()` method but with support for -#' [MPI](https://mc-stan.org/math/mpi.html). The target audience for MPI are +#' MPI (message passing interface). The target audience for MPI are #' those with large computer clusters. For other users, the #' [`$sample()`][model-method-sample] method provides both parallelization of #' chains and threading support for within-chain parallelization. @@ -1305,8 +1313,8 @@ CmdStanModel$set("public", name = "sample", value = sample) #' @return A [`CmdStanMCMC`] object. #' #' @template seealso-docs -#' @seealso The Stan Math Library's MPI documentation -#' ([mc-stan.org/math/mpi](https://mc-stan.org/math/mpi.html)) for more +#' @seealso The Stan Math Library's documentation +#' ([mc-stan.org/math](https://mc-stan.org/math/)) for more #' details on MPI support in Stan. #' #' @examples @@ -1426,8 +1434,8 @@ CmdStanModel$set("public", name = "sample_mpi", value = sample_mpi) #' @description The `$optimize()` method of a [`CmdStanModel`] object runs #' Stan's optimizer to obtain a (penalized) maximum likelihood estimate or a #' maximum a posteriori estimate (if `jacobian=TRUE`). See the -#' [Maximum Likelihood Estimation](https://mc-stan.org/docs/cmdstan-guide/maximum-likelihood-estimation.html) -#' section of the CmdStan User's Guide for more details. +#' [CmdStan User's Guide](https://mc-stan.org/docs/cmdstan-guide/index.html) +#' for more details. #' #' Any argument left as `NULL` will default to the default value used by the #' installed version of CmdStan. See the [CmdStan User’s @@ -1536,7 +1544,7 @@ optimize <- function(data = NULL, CmdStanModel$set("public", name = "optimize", value = optimize) -#' Run Stan's laplace algorithm +#' Run Stan's Laplace algorithm #' #' @name model-method-laplace #' @aliases laplace @@ -1550,13 +1558,11 @@ CmdStanModel$set("public", name = "optimize", value = optimize) #' likelihood estimate (MLE), the sample provides an estimate of the standard #' error of the likelihood. Whether the mode is the MAP or MLE depends on #' the value of the `jacobian` argument when running optimization. See the -#' [Laplace Sampling](https://mc-stan.org/docs/cmdstan-guide/laplace-sampling.html) -#' section of the CmdStan User's Guide for more details. +#' [CmdStan User’s Guide](https://mc-stan.org/docs/cmdstan-guide/) +#' for more details. #' #' Any argument left as `NULL` will default to the default value used by the -#' installed version of CmdStan. See the -#' [CmdStan User’s Guide](https://mc-stan.org/docs/cmdstan-guide/) -#' for more details on the default arguments. +#' installed version of CmdStan. #' #' @template model-common-args #' @inheritParams model-method-optimize @@ -1707,21 +1713,17 @@ CmdStanModel$set("public", name = "laplace", value = laplace) #' @family CmdStanModel methods #' #' @description The `$variational()` method of a [`CmdStanModel`] object runs -#' Stan's variational Bayes (ADVI) algorithms. -#' -#' Any argument left as `NULL` will default to the default value used by the -#' installed version of CmdStan. See the +#' Stan's Automatic Differentiation Variational Inference (ADVI) algorithms. +#' The approximation is a Gaussian in the unconstrained variable space. Stan +#' implements two ADVI algorithms: the `algorithm="meanfield"` option uses a +#' fully factorized Gaussian for the approximation; the `algorithm="fullrank"` +#' option uses a Gaussian with a full-rank covariance matrix for the +#' approximation. See the #' [CmdStan User’s Guide](https://mc-stan.org/docs/cmdstan-guide/) #' for more details. #' -#' @details CmdStan can fit a variational approximation to the posterior. The -#' approximation is a Gaussian in the unconstrained variable space. Stan -#' implements two variational algorithms. The `algorithm="meanfield"` option -#' uses a fully factorized Gaussian for the approximation. The -#' `algorithm="fullrank"` option uses a Gaussian with a full-rank covariance -#' matrix for the approximation. -#' -#' -- [*CmdStan Interface User's Guide*](https://github.com/stan-dev/cmdstan/releases/latest) +#' Any argument left as `NULL` will default to the default value used by the +#' installed version of CmdStan. #' #' @template model-common-args #' @param threads (positive integer) If the model was @@ -1821,6 +1823,138 @@ variational <- function(data = NULL, } CmdStanModel$set("public", name = "variational", value = variational) + +#' Run Stan's Pathfinder Variational Inference Algorithm +#' +#' @name model-method-pathfinder +#' @aliases pathfinder +#' @family CmdStanModel methods +#' +#' @description The `$pathfinder()` method of a [`CmdStanModel`] object runs +#' Stan's Pathfinder algorithms. Pathfinder is a variational method for +#' approximately sampling from differentiable log densities. Starting from a +#' random initialization, Pathfinder locates normal approximations +#' to the target density along a quasi-Newton optimization path in +#' the unconstrained space, with local covariance estimated using +#' the negative inverse Hessian estimates produced by the LBFGS +#' optimizer. Pathfinder selects the normal approximation with the +#' lowest estimated Kullback-Leibler (KL) divergence to the true +#' posterior. Finally Pathfinder draws from that normal +#' approximation and returns the draws transformed to the +#' constrained scale. See the +#' [CmdStan User’s Guide](https://mc-stan.org/docs/cmdstan-guide/) +#' for more details. +#' +#' Any argument left as `NULL` will default to the default value used by the +#' installed version of CmdStan +#' +#' @template model-common-args +#' @param num_threads (positive integer) If the model was +#' [compiled][model-method-compile] with threading support, the number of +#' threads to use in parallelized sections (e.g., for multi-path pathfinder +#' as well as `reduce_sum`). +#' @param init_alpha (positive real) The initial step size parameter. +#' @param tol_obj (positive real) Convergence tolerance on changes in objective function value. +#' @param tol_rel_obj (positive real) Convergence tolerance on relative changes in objective function value. +#' @param tol_grad (positive real) Convergence tolerance on the norm of the gradient. +#' @param tol_rel_grad (positive real) Convergence tolerance on the relative norm of the gradient. +#' @param tol_param (positive real) Convergence tolerance on changes in parameter value. +#' @param history_size (positive integer) The size of the history used when +#' approximating the Hessian. +#' @param single_path_draws (positive integer) Number of draws a single +#' pathfinder should return. The number of draws PSIS sampling samples from +#' will be equal to `single_path_draws * num_paths`. +#' @param draws (positive integer) Number of draws to return after performing +#' pareto smooted importance sampling (PSIS). This should be smaller than +#' `single_path_draws * num_paths` (future versions of CmdStan will throw a +#' warning). +#' @param num_paths (positive integer) Number of single pathfinders to run. +#' @param max_lbfgs_iters (positive integer) The maximum number of iterations +#' for LBFGS. +#' @param num_elbo_draws (positive integer) Number of draws to make when +#' calculating the ELBO of the approximation at each iteration of LBFGS. +#' @param save_single_paths (logical) Whether to save the results of single +#' pathfinder runs in multi-pathfinder. +#' @return A [`CmdStanPathfinder`] object. +#' +#' @template seealso-docs +#' @inherit cmdstan_model examples +#' +pathfinder <- function(data = NULL, + seed = NULL, + refresh = NULL, + init = NULL, + save_latent_dynamics = FALSE, + output_dir = NULL, + output_basename = NULL, + sig_figs = NULL, + opencl_ids = NULL, + num_threads = NULL, + init_alpha = NULL, + tol_obj = NULL, + tol_rel_obj = NULL, + tol_grad = NULL, + tol_rel_grad = NULL, + tol_param = NULL, + history_size = NULL, + single_path_draws = NULL, + draws = NULL, + num_paths = NULL, + max_lbfgs_iters = NULL, + num_elbo_draws = NULL, + save_single_paths = NULL) { + procs <- CmdStanProcs$new( + num_procs = 1, + show_stdout_messages = (is.null(refresh) || refresh != 0), + threads_per_proc = assert_valid_threads(num_threads, self$cpp_options()) + ) + model_variables <- NULL + if (is_variables_method_supported(self)) { + model_variables <- self$variables() + } + pathfinder_args <- PathfinderArgs$new( + init_alpha = init_alpha, + tol_obj = tol_obj, + tol_rel_obj = tol_rel_obj, + tol_grad = tol_grad, + tol_rel_grad = tol_rel_grad, + tol_param = tol_param, + history_size = history_size, + draws = draws, + single_path_draws = single_path_draws, + num_paths = num_paths, + max_lbfgs_iters = max_lbfgs_iters, + num_elbo_draws = num_elbo_draws, + save_single_paths = save_single_paths + ) + args <- CmdStanArgs$new( + method_args = pathfinder_args, + stan_file = self$stan_file(), + stan_code = suppressWarnings(self$code()), + model_methods_env = private$model_methods_env_, + standalone_env = self$functions, + model_name = self$model_name(), + exe_file = self$exe_file(), + proc_ids = 1, + data_file = process_data(data, model_variables), + save_latent_dynamics = save_latent_dynamics, + seed = seed, + init = init, + refresh = refresh, + output_dir = output_dir, + output_basename = output_basename, + sig_figs = sig_figs, + opencl_ids = assert_valid_opencl(opencl_ids, self$cpp_options()), + model_variables = model_variables, + num_threads = num_threads + ) + runset <- CmdStanRun$new(args, procs) + runset$run_cmdstan() + CmdStanPathfinder$new(runset) +} +CmdStanModel$set("public", name = "pathfinder", value = pathfinder) + + #' Run Stan's standalone generated quantities method #' #' @name model-method-generate-quantities diff --git a/R/run.R b/R/run.R index b674a05e0..16a0c97ba 100644 --- a/R/run.R +++ b/R/run.R @@ -262,7 +262,7 @@ CmdStanRun <- R6::R6Class( }, time = function() { - if (self$method() %in% c("laplace", "optimize", "variational")) { + if (self$method() %in% c("laplace", "optimize", "variational", "pathfinder")) { time <- list(total = self$procs$total_time()) } else if (self$method() == "generate_quantities") { chain_time <- data.frame( @@ -518,6 +518,10 @@ CmdStanRun$set("private", name = "run_generate_quantities_", value = .run_genera if (procs$proc_state(id = id) > 3) { successful_fit <- TRUE } + } else if (self$method() == "pathfinder") { + if (procs$proc_state(id = id) > 3 | procs$get_proc(id)$get_exit_status() == 0) { + successful_fit <- TRUE + } } else if (procs$get_proc(id)$get_exit_status() == 0) { successful_fit <- TRUE } @@ -533,6 +537,7 @@ CmdStanRun$set("private", name = "run_generate_quantities_", value = .run_genera CmdStanRun$set("private", name = "run_optimize_", value = .run_other) CmdStanRun$set("private", name = "run_laplace_", value = .run_other) CmdStanRun$set("private", name = "run_variational_", value = .run_other) +CmdStanRun$set("private", name = "run_pathfinder_", value = .run_other) .run_diagnose <- function() { procs <- self$procs diff --git a/R/utils.R b/R/utils.R index be03344b9..00fe3faeb 100644 --- a/R/utils.R +++ b/R/utils.R @@ -678,7 +678,7 @@ get_cmdstan_flags <- function(flag_name) { cmdstan_path <- cmdstanr::cmdstan_path() flags <- wsl_compatible_run( command = "make", - args = c(paste0("print-", flag_name)), + args = c("-s", paste0("print-", flag_name)), wd = cmdstan_path )$stdout diff --git a/R/zzz.R b/R/zzz.R index 1da0fc2c6..0a2552582 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -14,7 +14,7 @@ startup_messages <- function() { default = identical(tolower(Sys.getenv("CMDSTANR_NO_VER_CHECK")), "true") )) if (!skip_version_check) { - latest_version <- try(suppressWarnings(latest_released_version()), silent = TRUE) + latest_version <- try(suppressWarnings(latest_released_version(retries = 0)), silent = TRUE) current_version <- try(cmdstan_version(), silent = TRUE) if (!inherits(latest_version, "try-error") && !inherits(current_version, "try-error") diff --git a/README.md b/README.md index 27956fec4..989c2830e 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ releases. * Modularity: CmdStanR runs Stan's algorithms and lets downstream modules do the analysis. -* Flexible [BSD-3 license](https://opensource.org/licenses/BSD-3-Clause). +* Flexible [BSD-3 license](https://opensource.org/license/bsd-3-clause/). ### Installation @@ -61,14 +61,14 @@ CmdStanR_](https://mc-stan.org/cmdstanr/articles/cmdstanr.html) vignette. ### Contributing -There is a lot of work still to be done and contributions are very welcome! -If you are interested in contributing please comment on an open issue -or open a new one if none are applicable. +There is a lot of work still to be done and we welcome contributions from +anyone! If you are interested in contributing please comment on an open issue or +open a new one if none are applicable. ### License CmdStanR, like CmdStan and the core Stan C++ code, is licensed under the following licenses: -- Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause) +- Code: BSD 3-clause (https://opensource.org/license/bsd-3-clause/) - Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/) diff --git a/_pkgdown.yml b/_pkgdown.yml index d3786f7d4..b9f89cf3d 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -102,6 +102,7 @@ reference: - CmdStanMLE - CmdStanLaplace - CmdStanVB + - CmdStanPathfinder - CmdStanGQ - CmdStanDiagnose - starts_with("fit-method") diff --git a/docs/404.html b/docs/404.html index 9dcc60bed..4499f2e68 100644 --- a/docs/404.html +++ b/docs/404.html @@ -39,7 +39,7 @@ cmdstanr - 0.6.1 + 0.7.0 diff --git a/docs/CONTRIBUTING.html b/docs/CONTRIBUTING.html new file mode 100644 index 000000000..ce8b14b16 --- /dev/null +++ b/docs/CONTRIBUTING.html @@ -0,0 +1,162 @@ + +Contributing to cmdstanr • cmdstanr + + +
+
+ + + +
+
+ + +
+ +

This outlines how to propose a change to cmdstanr and is based on similar instructions for tidyverse packages, including the contributing guidelines generated by usethis::use_tidy_contributing().

+
+

Fixing typos

+

You can fix typos, spelling mistakes, or grammatical errors in the documentation directly using the GitHub web interface, as long as the changes are made in the source file. This generally means you’ll need to edit roxygen2 comments in an .R, not a .Rd file. You can find the .R file that generates the .Rd by reading the comment in the first line.

+
+
+

Bigger changes

+

If you want to make a bigger change, it’s a good idea to first file an issue and make sure someone from the team agrees that it’s needed. If you’ve found a bug, please file an issue that illustrates the bug with a minimal reproducible example (see e.g. the tidyverse reprex instructions). The tidyverse guide on how to create a great issue has more advice.

+
+

Pull request process

+

If you are new to creating pull requests here are some tips. Using the functions from the usethis package is not required but can be helpful if this process is new to you.

+
  • Fork the package and clone onto your computer. If you haven’t done this before, we recommend using usethis::create_from_github("stan-dev/cmdstanr", fork = TRUE).

  • +
  • Install all development dependencies with devtools::install_dev_deps(), and then make sure the package passes R CMD check by running devtools::check(). If R CMD check doesn’t pass cleanly, it’s a good idea to ask for help before continuing.

  • +
  • Create a Git branch for your pull request (PR). We recommend using usethis::pr_init("brief-description-of-change").

  • +
  • Make your changes, commit to git, and then create a PR by running usethis::pr_push(), and following the prompts in your browser. The title of your PR should briefly describe the change. The body of your PR should contain Fixes #issue-number.

  • +
  • For user-facing changes, add a bullet to the top of NEWS.md (i.e. just below the first header). Follow the style already used in NEWS.md.

  • +
+
+

Code style

+
  • New code should attempt to follow the style used in the package. When in doubt follow the tidyverse style guide.

  • +
  • We use roxygen2, with Markdown syntax, for documentation.

  • +
  • We use testthat for unit tests. Contributions with test cases included are easier to accept.

  • +
+
+
+

Code of Conduct

+

Please note that the cmdstanr project follows the Stan project’s Code of Conduct. By contributing to this project you agree to abide by its terms.

+
+
+ +
+ + + +
+ + + +
+ +
+

Site built with pkgdown 2.0.7.

+
+ +
+ + + + + + + + diff --git a/docs/LICENSE-text.html b/docs/LICENSE-text.html index 31783b5cc..8f4c7007f 100644 --- a/docs/LICENSE-text.html +++ b/docs/LICENSE-text.html @@ -17,7 +17,7 @@ cmdstanr - 0.6.1 + 0.7.0 diff --git a/docs/LICENSE.html b/docs/LICENSE.html index 614717854..9cfe0b946 100644 --- a/docs/LICENSE.html +++ b/docs/LICENSE.html @@ -17,7 +17,7 @@ cmdstanr - 0.6.1 + 0.7.0 diff --git a/docs/articles/articles-online-only/opencl.html b/docs/articles/articles-online-only/opencl.html index c2af01ec9..af47c06b3 100644 --- a/docs/articles/articles-online-only/opencl.html +++ b/docs/articles/articles-online-only/opencl.html @@ -26,6 +26,8 @@ + +
+
-
-

-Introduction

+
+

Introduction +

This vignette demonstrates how to use the OpenCL capabilities of CmdStan with CmdStanR. The functionality described in this vignette requires CmdStan 2.26.1 or newer.

@@ -160,13 +162,13 @@

using profiling, which was introduced in Stan version 2.26.0.

-
-

-OpenCL runtime

+
+

OpenCL runtime +

OpenCL is supported on most modern CPUs and GPUs. In order to use OpenCL in CmdStanR, an OpenCL runtime for the target device must be installed. A guide for the most common devices is available in the -CmdStan manual’s chapter +CmdStan manual’s chapter on parallelization.

In case of using Windows, CmdStan requires the OpenCL.lib to compile the model. If you experience issue @@ -175,17 +177,17 @@

OpenCL.lib file on your system. If you are using CUDA, the path should be similar to the one listed here.

-path_to_opencl_lib <- "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.5/lib/x64"
-cpp_options = list(
-  paste0("LDFLAGS+= -L\"",path_to_opencl_lib,"\" -lOpenCL")
-)
-
-cmdstanr::cmdstan_make_local(cpp_options = cpp_options)
-cmdstanr::rebuild_cmdstan()
+path_to_opencl_lib <- "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.5/lib/x64" +cpp_options = list( + paste0("LDFLAGS+= -L\"",path_to_opencl_lib,"\" -lOpenCL") +) + +cmdstanr::cmdstan_make_local(cpp_options = cpp_options) +cmdstanr::rebuild_cmdstan()

-
-

-Compiling a model with OpenCL

+
+

Compiling a model with OpenCL +

By default, models in CmdStanR are compiled without OpenCL support. Once OpenCL support is enabled, a CmdStan model will make use of OpenCL if the functions in the model support it. Technically no @@ -200,7 +202,7 @@

int<lower=1> k; int<lower=0> n; matrix[n, k] X; - int y[n]; + array[n] int y; } parameters { vector[k] beta; @@ -213,30 +215,30 @@

}

Some fake data will be useful to run this model:

-library(cmdstanr)
-
-# Generate some fake data
-n <- 250000
-k <- 20
-X <- matrix(rnorm(n * k), ncol = k)
-y <- rbinom(n, size = 1, prob = plogis(3 * X[,1] - 2 * X[,2] + 1))
-mdata <- list(k = k, n = n, y = y, X = X)
+library(cmdstanr) + +# Generate some fake data +n <- 250000 +k <- 20 +X <- matrix(rnorm(n * k), ncol = k) +y <- rbinom(n, size = 1, prob = plogis(3 * X[,1] - 2 * X[,2] + 1)) +mdata <- list(k = k, n = n, y = y, X = X)

In this model, most of the computation will be handled by the bernoulli_logit_glm_lpmf function. Because this is a supported GPU function, it should be possible to accelerate it with -OpenCL. Check here for a +OpenCL. Check here for a list of functions with OpenCL support.

To build the model with OpenCL support, add cpp_options = list(stan_opencl = TRUE) at the compilation step.

-# Compile the model with STAN_OPENCL=TRUE
-mod_cl <- cmdstan_model("opencl-files/bernoulli_logit_glm.stan",
-                        cpp_options = list(stan_opencl = TRUE))
+# Compile the model with STAN_OPENCL=TRUE +mod_cl <- cmdstan_model("opencl-files/bernoulli_logit_glm.stan", + cpp_options = list(stan_opencl = TRUE))
-
-

-Running models with OpenCL

+
+

Running models with OpenCL +

Running models with OpenCL requires specifying the OpenCL platform and device on which to run the model (there can be multiple). If the system has one GPU and no OpenCL CPU runtime, the platform and device @@ -245,10 +247,10 @@

devices are available.

On an Ubuntu system with both CPU and GPU OpenCL support, clinfo -l outputs:

-
Platform #0: AMD Accelerated Parallel Processing
- `-- Device #0: gfx906+sram-ecc
-Platform #1: Intel(R) CPU Runtime for OpenCL(TM) Applications
- `-- Device #0: Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
+
Platform #0: AMD Accelerated Parallel Processing
+ `-- Device #0: gfx906+sram-ecc
+Platform #1: Intel(R) CPU Runtime for OpenCL(TM) Applications
+ `-- Device #0: Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz

On this system the GPU is platform ID 0 and device ID 0, while the CPU is platform ID 1, device ID 0. These can be specified with the opencl_ids argument when running a model. The @@ -256,38 +258,17 @@

first element is the platform ID and the second argument is the device ID.

-fit_cl <- mod_cl$sample(data = mdata, chains = 4, parallel_chains = 4,
-                        opencl_ids = c(0, 0), refresh = 0)
-
Running MCMC with 4 parallel chains...
-
-Chain 4 finished in 96.7 seconds.
-Chain 1 finished in 97.9 seconds.
-Chain 2 finished in 98.6 seconds.
-Chain 3 finished in 98.8 seconds.
-
-All 4 chains finished successfully.
-Mean chain execution time: 98.0 seconds.
-Total execution time: 103.0 seconds.
+fit_cl <- mod_cl$sample(data = mdata, chains = 4, parallel_chains = 4, + opencl_ids = c(0, 0), refresh = 0)

We’ll also run a version without OpenCL and compare the run times.

-
-# no OpenCL version
-mod <- cmdstan_model("opencl-files/bernoulli_logit_glm.stan", force_recompile = TRUE)
-fit_cpu <- mod$sample(data = mdata, chains = 4, parallel_chains = 4, refresh = 0)
-
Running MCMC with 4 parallel chains...
-
-Chain 3 finished in 487.9 seconds.
-Chain 2 finished in 491.8 seconds.
-Chain 1 finished in 514.9 seconds.
-Chain 4 finished in 518.4 seconds.
-
-All 4 chains finished successfully.
-Mean chain execution time: 503.2 seconds.
-Total execution time: 521.9 seconds.
+
+# no OpenCL version
+mod <- cmdstan_model("opencl-files/bernoulli_logit_glm.stan", force_recompile = TRUE)
+fit_cpu <- mod$sample(data = mdata, chains = 4, parallel_chains = 4, refresh = 0)

The speedup of the OpenCL model is:

-
-fit_cpu$time()$total / fit_cl$time()$total
-
[1] 5.065968
+
+fit_cpu$time()$total / fit_cl$time()$total

This speedup will be determined by the particular GPU/CPU used, the input problem sizes (data as well as parameters) and if the model uses functions that can be run on the GPU or other OpenCL devices.

@@ -305,11 +286,13 @@

-

Site built with pkgdown 1.6.1.

+

+

Site built with pkgdown 2.0.7.

@@ -318,5 +301,7 @@

+ + diff --git a/docs/articles/cmdstanr-internals.html b/docs/articles/cmdstanr-internals.html index 035f2e91e..cc89ad19c 100644 --- a/docs/articles/cmdstanr-internals.html +++ b/docs/articles/cmdstanr-internals.html @@ -40,7 +40,7 @@ cmdstanr - 0.6.0 + 0.7.0

@@ -183,10 +183,10 @@

Immediate compilation
 mod$stan_file()
-
[1] "/Users/jgabry/.cmdstan/cmdstan-2.32.2/examples/bernoulli/bernoulli.stan"
+
[1] "/Users/jgabry/.cmdstan/cmdstan-2.33.1/examples/bernoulli/bernoulli.stan"
 mod$exe_file()
-
[1] "/Users/jgabry/.cmdstan/cmdstan-2.32.2/examples/bernoulli/bernoulli"
+
[1] "/Users/jgabry/.cmdstan/cmdstan-2.33.1/examples/bernoulli/bernoulli"

Subsequently, if you create a CmdStanModel object from the same Stan file then compilation will be skipped (assuming the file hasn’t changed).

@@ -220,7 +220,7 @@

Delayed compilation
 mod$compile()
 mod$exe_file()

-
[1] "/Users/jgabry/.cmdstan/cmdstan-2.32.2/examples/bernoulli/bernoulli"
+
[1] "/Users/jgabry/.cmdstan/cmdstan-2.33.1/examples/bernoulli/bernoulli"

Pedantic check @@ -250,7 +250,7 @@

Pedantic check

For the latest information on the checks performed in pedantic mode -see the Pedantic +see the Pedantic mode chapter in the Stan Reference Manual.

Pedantic mode is available when compiling the model or when using the separate $check_syntax() method of a @@ -278,7 +278,7 @@

Pedantic check$compile() method directly if using the delayed compilation approach described above).

mod_pedantic <- cmdstan_model(stan_file_pedantic, pedantic = TRUE)
-Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpbI1mWj/model-1134e453cbe1d.stan', line 11, column 14: A
+Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/model-74281eb75594.stan', line 11, column 14: A
     poisson distribution is given parameter lambda as a rate parameter
     (argument 1), but lambda was not constrained to be strictly positive.
 Warning: The parameter lambda has no priors. This means either no prior is
@@ -288,7 +288,7 @@ 

Pedantic checkpedantic argument to the $check_syntax() method.

mod_pedantic$check_syntax(pedantic = TRUE)
-Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpbI1mWj/model_febb1e69c7387a0e64cf13583e078104.stan', line 11, column 14: A
+Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/model_febb1e69c7387a0e64cf13583e078104.stan', line 11, column 14: A
     poisson distribution is given parameter lambda as a rate parameter
     (argument 1), but lambda was not constrained to be strictly positive.
 Warning: The parameter lambda has no priors. This means either no prior is
@@ -305,7 +305,7 @@ 

Pedantic check mod_pedantic <- cmdstan_model(stan_file_pedantic, compile = FALSE) mod_pedantic$check_syntax(pedantic = TRUE) -Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpbI1mWj/model_febb1e69c7387a0e64cf13583e078104.stan', line 11, column 14: A +Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/model_febb1e69c7387a0e64cf13583e078104.stan', line 11, column 14: A poisson distribution is given parameter lambda as a rate parameter (argument 1), but lambda was not constrained to be strictly positive. Warning: The parameter lambda has no priors. This means either no prior is @@ -495,10 +495,10 @@

Default temporary files
 fit$output_files()
-
[1] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpbI1mWj/bernoulli-202308170903-1-3cf14f.csv"
-[2] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpbI1mWj/bernoulli-202308170903-2-3cf14f.csv"
-[3] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpbI1mWj/bernoulli-202308170903-3-3cf14f.csv"
-[4] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpbI1mWj/bernoulli-202308170903-4-3cf14f.csv"
+
[1] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/bernoulli-202312131009-1-37e810.csv"
+[2] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/bernoulli-202312131009-2-37e810.csv"
+[3] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/bernoulli-202312131009-3-37e810.csv"
+[4] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/bernoulli-202312131009-4-37e810.csv"

These files will be lost if you end your R session or if you remove the fit object and force (or wait for) garbage collection.

@@ -510,8 +510,8 @@

Default temporary filesrm(fit) gc()

          used (Mb) gc trigger  (Mb) limit (Mb) max used  (Mb)
-Ncells 1275914 68.2    2632161 140.6         NA  2060504 110.1
-Vcells 2251167 17.2    8388608  64.0      32768  3439587  26.3
+Ncells 1260600 67.4 2436292 130.2 NA 2436292 130.2 +Vcells 2212085 16.9 8388608 64.0 32768 3459642 26.4

 file.exists(files)
[1] FALSE FALSE FALSE FALSE
@@ -561,14 +561,14 @@

Lazy CSV readingLazy CSV reading

After we call a method that requires the draws then if we reexamine @@ -623,14 +623,14 @@

Lazy CSV readingLazy CSV readingLazy CSV reading

For models with many parameters, transformed parameters, or generated @@ -686,9 +686,9 @@

read_cmdstan_csv()read_cmdstan_csv()read_cmdstan_csv()read_cmdstan_csv()Saving and fit <- mod$sample(data = data_list, save_latent_dynamics = TRUE)

 fit$latent_dynamics_files()
-
[1] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpbI1mWj/bernoulli-diagnostic-202308170903-1-33a5bd.csv"
-[2] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpbI1mWj/bernoulli-diagnostic-202308170903-2-33a5bd.csv"
-[3] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpbI1mWj/bernoulli-diagnostic-202308170903-3-33a5bd.csv"
-[4] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpbI1mWj/bernoulli-diagnostic-202308170903-4-33a5bd.csv"
+
[1] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/bernoulli-diagnostic-202312131009-1-425883.csv"
+[2] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/bernoulli-diagnostic-202312131009-2-425883.csv"
+[3] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/bernoulli-diagnostic-202312131009-3-425883.csv"
+[4] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/bernoulli-diagnostic-202312131009-4-425883.csv"
 # read one of the files in
 x <- utils::read.csv(fit$latent_dynamics_files()[1], comment.char = "#")
 head(x)
      lp__ accept_stat__ stepsize__ treedepth__ n_leapfrog__ divergent__
-1 -6.81485      0.964189   0.890649           1            3           0
-2 -6.82429      0.975056   0.890649           2            3           0
-3 -6.89143      0.994187   0.890649           2            3           0
-4 -7.74322      0.864876   0.890649           2            7           0
-5 -7.53356      1.000000   0.890649           1            1           0
-6 -7.72985      0.976133   0.890649           1            1           0
-  energy__     theta   p_theta   g_theta
-1  7.72945 -0.859513  1.883460  0.569292
-2  6.99952 -0.843502  0.824417  0.609572
-3  6.89942 -0.751210 -0.176090  0.846694
-4  7.75914 -2.133320 -0.248490 -1.729200
-5  7.78044 -2.006620 -0.978554 -1.577890
-6  7.79615 -2.125570  0.507119 -1.720370
+1 -9.73111 1.000000 0.932232 2 3 0 +2 -6.75346 1.000000 0.932232 1 3 0 +3 -8.48107 0.665681 0.932232 1 3 0 +4 -7.74234 1.000000 0.932232 1 1 0 +5 -6.82333 1.000000 0.932232 2 3 0 +6 -6.82333 0.784555 0.932232 1 3 0 + energy__ theta p_theta g_theta +1 10.97150 0.3939970 -2.155610 4.166930 +2 9.05119 -1.0295100 -2.933880 0.158161 +3 8.61284 0.0530292 0.702591 3.159050 +4 8.45793 -0.2148440 1.637290 2.357940 +5 7.76204 -1.3633100 1.875250 -0.555575 +6 8.04989 -1.3633100 -2.143570 -0.555575

The column lp__ is also provided via fit$draws(), and the columns accept_stat__, stepsize__, treedepth__, @@ -818,13 +818,13 @@

Saving and unique to the latent dynamics file.

 head(x[, c("theta", "p_theta", "g_theta")])
-
      theta   p_theta   g_theta
-1 -0.859513  1.883460  0.569292
-2 -0.843502  0.824417  0.609572
-3 -0.751210 -0.176090  0.846694
-4 -2.133320 -0.248490 -1.729200
-5 -2.006620 -0.978554 -1.577890
-6 -2.125570  0.507119 -1.720370
+
       theta   p_theta   g_theta
+1  0.3939970 -2.155610  4.166930
+2 -1.0295100 -2.933880  0.158161
+3  0.0530292  0.702591  3.159050
+4 -0.2148440  1.637290  2.357940
+5 -1.3633100  1.875250 -0.555575
+6 -1.3633100 -2.143570 -0.555575

Our model has a single parameter theta and the three columns above correspond to theta in the unconstrained space (theta on the constrained @@ -872,16 +872,16 @@

Troubleshooting and debuggingmod <- cmdstan_model(stan_file, force_recompile = TRUE)

Running make \
-  /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpbI1mWj/model-1134e4997b2fe \
+  /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/model-742871ce8467 \
   "STANCFLAGS +=  --name='bernoulli_model'"
 
 --- Translating Stan model to C++ code ---
-bin/stanc --name='bernoulli_model' --o=/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpbI1mWj/model-1134e4997b2fe.hpp /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpbI1mWj/model-1134e4997b2fe.stan
+bin/stanc --name='bernoulli_model' --o=/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/model-742871ce8467.hpp /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/model-742871ce8467.stan
 
 --- Compiling, linking C++ code ---
-clang++ -std=c++1y -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare -D_REENTRANT -Wno-ignored-attributes      -I stan/lib/stan_math/lib/tbb_2020.3/include    -O3 -I src -I stan/src -I stan/lib/rapidjson_1.1.0/ -I lib/CLI11-1.9.1/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.4.0 -I stan/lib/stan_math/lib/boost_1.78.0 -I stan/lib/stan_math/lib/sundials_6.1.1/include -I stan/lib/stan_math/lib/sundials_6.1.1/src/sundials    -DBOOST_DISABLE_ASSERTS          -c -include-pch stan/src/stan/model/model_header.hpp.gch -x c++ -o /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpbI1mWj/model-1134e4997b2fe.o /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpbI1mWj/model-1134e4997b2fe.hpp
-clang++ -std=c++1y -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare -D_REENTRANT -Wno-ignored-attributes      -I stan/lib/stan_math/lib/tbb_2020.3/include    -O3 -I src -I stan/src -I stan/lib/rapidjson_1.1.0/ -I lib/CLI11-1.9.1/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.4.0 -I stan/lib/stan_math/lib/boost_1.78.0 -I stan/lib/stan_math/lib/sundials_6.1.1/include -I stan/lib/stan_math/lib/sundials_6.1.1/src/sundials    -DBOOST_DISABLE_ASSERTS               -Wl,-L,"/Users/jgabry/.cmdstan/cmdstan-2.32.2/stan/lib/stan_math/lib/tbb" -Wl,-rpath,"/Users/jgabry/.cmdstan/cmdstan-2.32.2/stan/lib/stan_math/lib/tbb"        /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpbI1mWj/model-1134e4997b2fe.o src/cmdstan/main.o       -Wl,-L,"/Users/jgabry/.cmdstan/cmdstan-2.32.2/stan/lib/stan_math/lib/tbb" -Wl,-rpath,"/Users/jgabry/.cmdstan/cmdstan-2.32.2/stan/lib/stan_math/lib/tbb"     stan/lib/stan_math/lib/sundials_6.1.1/lib/libsundials_nvecserial.a stan/lib/stan_math/lib/sundials_6.1.1/lib/libsundials_cvodes.a stan/lib/stan_math/lib/sundials_6.1.1/lib/libsundials_idas.a stan/lib/stan_math/lib/sundials_6.1.1/lib/libsundials_kinsol.a  stan/lib/stan_math/lib/tbb/libtbb.dylib stan/lib/stan_math/lib/tbb/libtbbmalloc.dylib stan/lib/stan_math/lib/tbb/libtbbmalloc_proxy.dylib -o /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpbI1mWj/model-1134e4997b2fe
-rm -f /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpbI1mWj/model-1134e4997b2fe.o
+clang++ -Wno-deprecated-declarations -Wno-deprecated-declarations -std=c++1y -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare -D_REENTRANT -Wno-ignored-attributes -I stan/lib/stan_math/lib/tbb_2020.3/include -O3 -I src -I stan/src -I stan/lib/rapidjson_1.1.0/ -I lib/CLI11-1.9.1/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.4.0 -I stan/lib/stan_math/lib/boost_1.78.0 -I stan/lib/stan_math/lib/sundials_6.1.1/include -I stan/lib/stan_math/lib/sundials_6.1.1/src/sundials -DBOOST_DISABLE_ASSERTS -c -include-pch stan/src/stan/model/model_header_13_0.hpp.gch -x c++ -o /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/model-742871ce8467.o /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/model-742871ce8467.hpp +clang++ -Wno-deprecated-declarations -Wno-deprecated-declarations -std=c++1y -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare -D_REENTRANT -Wno-ignored-attributes -I stan/lib/stan_math/lib/tbb_2020.3/include -O3 -I src -I stan/src -I stan/lib/rapidjson_1.1.0/ -I lib/CLI11-1.9.1/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.4.0 -I stan/lib/stan_math/lib/boost_1.78.0 -I stan/lib/stan_math/lib/sundials_6.1.1/include -I stan/lib/stan_math/lib/sundials_6.1.1/src/sundials -DBOOST_DISABLE_ASSERTS -Wl,-L,"/Users/jgabry/.cmdstan/cmdstan-2.33.1/stan/lib/stan_math/lib/tbb" -Wl,-rpath,"/Users/jgabry/.cmdstan/cmdstan-2.33.1/stan/lib/stan_math/lib/tbb" /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/model-742871ce8467.o src/cmdstan/main.o -Wl,-L,"/Users/jgabry/.cmdstan/cmdstan-2.33.1/stan/lib/stan_math/lib/tbb" -Wl,-rpath,"/Users/jgabry/.cmdstan/cmdstan-2.33.1/stan/lib/stan_math/lib/tbb" stan/lib/stan_math/lib/sundials_6.1.1/lib/libsundials_nvecserial.a stan/lib/stan_math/lib/sundials_6.1.1/lib/libsundials_cvodes.a stan/lib/stan_math/lib/sundials_6.1.1/lib/libsundials_idas.a stan/lib/stan_math/lib/sundials_6.1.1/lib/libsundials_kinsol.a stan/lib/stan_math/lib/tbb/libtbb.dylib stan/lib/stan_math/lib/tbb/libtbbmalloc.dylib stan/lib/stan_math/lib/tbb/libtbbmalloc_proxy.dylib -o /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/model-742871ce8467 +rm -f /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/model-742871ce8467.o
 fit <- mod$sample(
   data = data_list,
@@ -891,11 +891,11 @@ 

Troubleshooting and debugging)

Running MCMC with 1 chain...
 
-Running ./bernoulli 'id=1' random 'seed=2144173564' data \
-  'file=/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpbI1mWj/standata-1134e60662723.json' \
+Running ./bernoulli 'id=1' random 'seed=90958316' data \
+  'file=/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/standata-742874baeb02.json' \
   output \
-  'file=/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpbI1mWj/bernoulli-202308170903-1-93b959.csv' \
-  'profile_file=/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpbI1mWj/bernoulli-profile-202308170903-1-09cf37.csv' \
+  'file=/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/bernoulli-202312131009-1-4db120.csv' \
+  'profile_file=/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/bernoulli-profile-202312131009-1-1c1f83.csv' \
   'method=sample' 'num_samples=100' 'num_warmup=100' 'save_warmup=0' \
   'algorithm=hmc' 'engine=nuts' adapt 'engaged=1'
 Chain 1 method = sample (Default) 
@@ -925,16 +925,16 @@ 

Troubleshooting and debugging - +
@@ -49,7 +49,7 @@
  • - +
  • @@ -64,7 +64,7 @@
  • - +
    - - + +

    Introduction

    @@ -214,10 +214,10 @@

    Installing CmdStancmdstan_version():

    -
    [1] "/Users/jgabry/.cmdstan/cmdstan-2.32.2"
    +
    [1] "/Users/jgabry/.cmdstan/cmdstan-2.33.1"
    -
    [1] "2.32.2"
    +
    [1] "2.33.1"

    Compiling a model @@ -254,7 +254,7 @@

    Compiling a model
     mod$exe_file()

    -
    [1] "/Users/jgabry/.cmdstan/cmdstan-2.32.2/examples/bernoulli/bernoulli"
    +
    [1] "/Users/jgabry/.cmdstan/cmdstan-2.33.1/examples/bernoulli/bernoulli"

    Running MCMC @@ -277,30 +277,30 @@

    Running MCMC)

    Running MCMC with 4 parallel chains...
     
    -Chain 1 Iteration:    1 / 2000 [  0%]  (Warmup)
    -Chain 1 Iteration:  500 / 2000 [ 25%]  (Warmup)
    -Chain 1 Iteration: 1000 / 2000 [ 50%]  (Warmup)
    -Chain 1 Iteration: 1001 / 2000 [ 50%]  (Sampling)
    -Chain 1 Iteration: 1500 / 2000 [ 75%]  (Sampling)
    -Chain 1 Iteration: 2000 / 2000 [100%]  (Sampling)
    -Chain 2 Iteration:    1 / 2000 [  0%]  (Warmup)
    -Chain 2 Iteration:  500 / 2000 [ 25%]  (Warmup)
    -Chain 2 Iteration: 1000 / 2000 [ 50%]  (Warmup)
    -Chain 2 Iteration: 1001 / 2000 [ 50%]  (Sampling)
    -Chain 2 Iteration: 1500 / 2000 [ 75%]  (Sampling)
    -Chain 2 Iteration: 2000 / 2000 [100%]  (Sampling)
    -Chain 3 Iteration:    1 / 2000 [  0%]  (Warmup)
    -Chain 3 Iteration:  500 / 2000 [ 25%]  (Warmup)
    -Chain 3 Iteration: 1000 / 2000 [ 50%]  (Warmup)
    -Chain 3 Iteration: 1001 / 2000 [ 50%]  (Sampling)
    -Chain 3 Iteration: 1500 / 2000 [ 75%]  (Sampling)
    -Chain 3 Iteration: 2000 / 2000 [100%]  (Sampling)
    -Chain 4 Iteration:    1 / 2000 [  0%]  (Warmup)
    -Chain 4 Iteration:  500 / 2000 [ 25%]  (Warmup)
    -Chain 4 Iteration: 1000 / 2000 [ 50%]  (Warmup)
    -Chain 4 Iteration: 1001 / 2000 [ 50%]  (Sampling)
    -Chain 4 Iteration: 1500 / 2000 [ 75%]  (Sampling)
    -Chain 4 Iteration: 2000 / 2000 [100%]  (Sampling)
    +Chain 1 Iteration:    1 / 2000 [  0%]  (Warmup) 
    +Chain 1 Iteration:  500 / 2000 [ 25%]  (Warmup) 
    +Chain 1 Iteration: 1000 / 2000 [ 50%]  (Warmup) 
    +Chain 1 Iteration: 1001 / 2000 [ 50%]  (Sampling) 
    +Chain 1 Iteration: 1500 / 2000 [ 75%]  (Sampling) 
    +Chain 1 Iteration: 2000 / 2000 [100%]  (Sampling) 
    +Chain 2 Iteration:    1 / 2000 [  0%]  (Warmup) 
    +Chain 2 Iteration:  500 / 2000 [ 25%]  (Warmup) 
    +Chain 2 Iteration: 1000 / 2000 [ 50%]  (Warmup) 
    +Chain 2 Iteration: 1001 / 2000 [ 50%]  (Sampling) 
    +Chain 2 Iteration: 1500 / 2000 [ 75%]  (Sampling) 
    +Chain 2 Iteration: 2000 / 2000 [100%]  (Sampling) 
    +Chain 3 Iteration:    1 / 2000 [  0%]  (Warmup) 
    +Chain 3 Iteration:  500 / 2000 [ 25%]  (Warmup) 
    +Chain 3 Iteration: 1000 / 2000 [ 50%]  (Warmup) 
    +Chain 3 Iteration: 1001 / 2000 [ 50%]  (Sampling) 
    +Chain 3 Iteration: 1500 / 2000 [ 75%]  (Sampling) 
    +Chain 3 Iteration: 2000 / 2000 [100%]  (Sampling) 
    +Chain 4 Iteration:    1 / 2000 [  0%]  (Warmup) 
    +Chain 4 Iteration:  500 / 2000 [ 25%]  (Warmup) 
    +Chain 4 Iteration: 1000 / 2000 [ 50%]  (Warmup) 
    +Chain 4 Iteration: 1001 / 2000 [ 50%]  (Sampling) 
    +Chain 4 Iteration: 1500 / 2000 [ 75%]  (Sampling) 
    +Chain 4 Iteration: 2000 / 2000 [100%]  (Sampling) 
     Chain 1 finished in 0.0 seconds.
     Chain 2 finished in 0.0 seconds.
     Chain 3 finished in 0.0 seconds.
    @@ -308,7 +308,7 @@ 

    Running MCMCSampler diagnostic warnings a suffers from divergences.

    -
    Warning: 67 of 4000 (2.0%) transitions ended with a divergence.
    +
    Chain 4 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
    +
    Chain 4 Exception: normal_lpdf: Scale parameter is 0, but must be positive! (in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmpl3ZPjU/model-76ac71f3c1fe.stan', line 14, column 2 to column 41)
    +
    Chain 4 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
    +
    Chain 4 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
    +
    Chain 4 
    +
    Warning: 149 of 4000 (4.0%) transitions ended with a divergence.
     See https://mc-stan.org/misc/warnings for details.

    After fitting there is a warning about divergences. We can also regenerate this warning message later using fit$diagnostic_summary().

    -
    +
     diagnostics <- fit_with_warning$diagnostic_summary()
    -
    Warning: 67 of 4000 (2.0%) transitions ended with a divergence.
    +
    Warning: 149 of 4000 (4.0%) transitions ended with a divergence.
     See https://mc-stan.org/misc/warnings for details.
    -
    +
     print(diagnostics)
    $num_divergent
    -[1]  6 35 24  2
    +[1] 11 54 32 52
     
     $num_max_treedepth
     [1] 0 0 0 0
     
     $ebfmi
    -[1] 0.30 0.33 0.24 0.35
    -
    +[1] 0.40 0.36 0.37 0.30
    +
     # number of divergences reported in warning is the sum of the per chain values
     sum(diagnostics$num_divergent)
    -
    [1] 67
    +
    [1] 149

    CmdStan’s diagnose utility @@ -539,7 +544,7 @@

    Create a stanfit object
    +
     

    @@ -548,98 +553,211 @@

    Running optimization and

    CmdStanR also supports running Stan’s optimization algorithms and its algorithms for variational approximation of full Bayesian inference. -These are run via the $optimize() and -$variational() methods, which are called in a similar way -to the $sample() method demonstrated above.

    +These are run via the $optimize(), $laplace(), +$variational(), and $pathfinder() methods, +which are called in a similar way to the $sample() method +demonstrated above.

    Optimization

    We can find the (penalized) maximum likelihood estimate (MLE) using $optimize().

    -
    +
     fit_mle <- mod$optimize(data = data_list, seed = 123)
    -
    Initial log joint probability = -9.51104
    -    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
    -       6      -5.00402   0.000103557   2.55661e-07           1           1        9
    -Optimization terminated normally:
    -  Convergence detected: relative gradient magnitude is below tolerance
    +
    Initial log joint probability = -9.51104 
    +    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes  
    +       6      -5.00402   0.000103557   2.55661e-07           1           1        9    
    +Optimization terminated normally:  
    +  Convergence detected: relative gradient magnitude is below tolerance 
     Finished in  0.2 seconds.
    -
    +
     fit_mle$print() # includes lp__ (log prob calculated by Stan program)
     variable estimate
         lp__     -5.00
         theta     0.20
    -
    +
     fit_mle$mle("theta")
    theta 
       0.2 

    Here’s a plot comparing the penalized MLE to the posterior distribution of theta.

    -
    +
     mcmc_hist(fit$draws("theta")) +
       vline_at(fit_mle$mle("theta"), size = 1.5)

    +

    For optimization, by default the mode is calculated without the +Jacobian adjustment for constrained variables, which shifts the mode due +to the change of variables. To include the Jacobian adjustment and +obtain a maximum a posteriori (MAP) estimate set +jacobian=TRUE. See the Maximum +Likelihood Estimation section of the CmdStan User’s Guide for more +details.

    +
    +fit_map <- mod$optimize(
    +  data = data_list,
    +  jacobian = TRUE,
    +  seed = 123
    +)
    +
    Initial log joint probability = -11.0088 
    +    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes  
    +       5      -6.74802   0.000938344   1.39149e-05           1           1        8    
    +Optimization terminated normally:  
    +  Convergence detected: relative gradient magnitude is below tolerance 
    +Finished in  0.1 seconds.
    -

    Variational Bayes +

    Laplace Approximation

    -

    We can run Stan’s experimental variational Bayes algorithm (ADVI) -using the $variational() -method.

    -
    -fit_vb <- mod$variational(data = data_list, seed = 123, output_samples = 4000)
    -
    ------------------------------------------------------------
    -EXPERIMENTAL ALGORITHM:
    -  This procedure has not been thoroughly tested and may be unstable
    -  or buggy. The interface is subject to change.
    -------------------------------------------------------------
    -Gradient evaluation took 6e-06 seconds
    -1000 transitions using 10 leapfrog steps per transition would take 0.06 seconds.
    -Adjust your expectations accordingly!
    -Begin eta adaptation.
    -Iteration:   1 / 250 [  0%]  (Adaptation)
    -Iteration:  50 / 250 [ 20%]  (Adaptation)
    -Iteration: 100 / 250 [ 40%]  (Adaptation)
    -Iteration: 150 / 250 [ 60%]  (Adaptation)
    -Iteration: 200 / 250 [ 80%]  (Adaptation)
    -Success! Found best value [eta = 1] earlier than expected.
    -Begin stochastic gradient ascent.
    -  iter             ELBO   delta_ELBO_mean   delta_ELBO_med   notes
    -   100           -6.262             1.000            1.000
    -   200           -6.263             0.500            1.000
    -   300           -6.307             0.336            0.007   MEDIAN ELBO CONVERGED
    -Drawing a sample of size 4000 from the approximate posterior...
    -COMPLETED.
    +

    The $laplace() +method produces a sample from a normal approximation centered at the +mode of a distribution in the unconstrained space. If the mode is a MAP +estimate, the samples provide an estimate of the mean and standard +deviation of the posterior distribution. If the mode is the MLE, the +sample provides an estimate of the standard error of the likelihood. +Whether the mode is the MAP or MLE depends on the value of the +jacobian argument when running optimization. See the Laplace +Sampling chapter of the CmdStan User’s Guide for more details.

    +

    Here we pass in the fit_map object from above as the +mode argument. If mode is omitted then +optimization will be run internally before taking draws from the normal +approximation.

    +
    +fit_laplace <- mod$laplace(
    +    mode = fit_map,
    +    draws = 4000,
    +    data = data_list,
    +    seed = 123,
    +    refresh = 1000
    +  )
    +
    Calculating Hessian 
    +Calculating inverse of Cholesky factor 
    +Generating draws 
    +iteration: 0 
    +iteration: 1000 
    +iteration: 2000 
    +iteration: 3000 
     Finished in  0.1 seconds.
    -
    +
    +fit_laplace$print("theta")
    +
     variable mean median   sd  mad   q5  q95
    +    theta 0.27   0.25 0.12 0.12 0.10 0.50
    +
    +mcmc_hist(fit_laplace$draws("theta"), binwidth = 0.025)
    +

    +
    +
    +

    Variational (ADVI) +

    +

    We can run Stan’s experimental Automatic Differentiation Variational +Inference (ADVI) using the $variational() +method. For details on the ADVI algorithm see the CmdStan +User’s Guide.

    +
    +fit_vb <- mod$variational(
    +  data = data_list,
    +  seed = 123,
    +  draws = 4000
    +)
    +
    ------------------------------------------------------------ 
    +EXPERIMENTAL ALGORITHM: 
    +  This procedure has not been thoroughly tested and may be unstable 
    +  or buggy. The interface is subject to change. 
    +------------------------------------------------------------ 
    +Gradient evaluation took 5e-06 seconds 
    +1000 transitions using 10 leapfrog steps per transition would take 0.05 seconds. 
    +Adjust your expectations accordingly! 
    +Begin eta adaptation. 
    +Iteration:   1 / 250 [  0%]  (Adaptation) 
    +Iteration:  50 / 250 [ 20%]  (Adaptation) 
    +Iteration: 100 / 250 [ 40%]  (Adaptation) 
    +Iteration: 150 / 250 [ 60%]  (Adaptation) 
    +Iteration: 200 / 250 [ 80%]  (Adaptation) 
    +Success! Found best value [eta = 1] earlier than expected. 
    +Begin stochastic gradient ascent. 
    +  iter             ELBO   delta_ELBO_mean   delta_ELBO_med   notes  
    +   100           -6.262             1.000            1.000 
    +   200           -6.263             0.500            1.000 
    +   300           -6.307             0.336            0.007   MEDIAN ELBO CONVERGED 
    +Drawing a sample of size 4000 from the approximate posterior...  
    +COMPLETED. 
    +Finished in  0.1 seconds.
    +
     fit_vb$print("theta")
     variable mean median   sd  mad   q5  q95
         theta 0.27   0.25 0.12 0.12 0.10 0.49
    -

    The $draws() method can be used to access the -approximate posterior draws. Let’s extract the draws, make the same plot -we made after MCMC, and compare the two. In this trivial example the -distributions look quite similar, although the variational approximation -slightly underestimates the posterior standard deviation.

    -
    -mcmc_hist(fit$draws("theta"), binwidth = 0.025)
    -
    -Posterior from MCMC

    -Posterior from MCMC -

    -
    -
    +
     mcmc_hist(fit_vb$draws("theta"), binwidth = 0.025)
    -
    -Posterior from variational

    -Posterior from variational -

    +

    -

    For more details on the $optimize() and -$variational() methods, follow these links to their +

    +

    Variational (Pathfinder) +

    +

    Stan version 2.33 introduced a new variational method called +Pathfinder, which is intended to be faster and more stable than ADVI. +For details on how Pathfinder works see the section in the CmdStan +User’s Guide. Pathfinder is run using the $pathfinder() +method.

    +
    +fit_pf <- mod$pathfinder(
    +  data = data_list,
    +  seed = 123,
    +  draws = 4000
    +)
    +
    Path [1] :Initial log joint density = -11.008832 
    +Path [1] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
    +              5      -6.748e+00      9.383e-04   1.391e-05    1.000e+00  1.000e+00       126 -6.264e+00 -6.264e+00                   
    +Path [1] :Best Iter: [3] ELBO (-6.195408) evaluations: (126) 
    +Path [2] :Initial log joint density = -7.318450 
    +Path [2] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
    +              4      -6.748e+00      5.414e-03   1.618e-04    1.000e+00  1.000e+00       101 -6.251e+00 -6.251e+00                   
    +Path [2] :Best Iter: [3] ELBO (-6.229174) evaluations: (101) 
    +Path [3] :Initial log joint density = -12.374612 
    +Path [3] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
    +              5      -6.748e+00      1.419e-03   2.837e-05    1.000e+00  1.000e+00       126 -6.199e+00 -6.199e+00                   
    +Path [3] :Best Iter: [5] ELBO (-6.199185) evaluations: (126) 
    +Path [4] :Initial log joint density = -13.009824 
    +Path [4] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
    +              5      -6.748e+00      1.677e-03   3.885e-05    1.000e+00  1.000e+00       126 -6.173e+00 -6.173e+00                   
    +Path [4] :Best Iter: [5] ELBO (-6.172860) evaluations: (126) 
    +Total log probability function evaluations:4379 
    +Finished in  0.1 seconds.
    +
    +fit_pf$print("theta")
    +
     variable mean median   sd  mad   q5  q95
    +    theta 0.25   0.24 0.12 0.12 0.08 0.47
    +

    Let’s extract the draws, make the same plot we made after running the +other algorithms, and compare them all. approximation, and compare them +all. In this simple example the distributions are quite similar, but +this will not always be the case for more challenging problems.

    +
    +mcmc_hist(fit_pf$draws("theta"), binwidth = 0.025) +
    +  ggplot2::labs(subtitle = "Approximate posterior from pathfinder") +
    +  ggplot2::xlim(0, 1)
    +

    +
    +mcmc_hist(fit_vb$draws("theta"), binwidth = 0.025) +
    +  ggplot2::labs(subtitle = "Approximate posterior from variational") +
    +  ggplot2::xlim(0, 1)
    +

    +
    +mcmc_hist(fit_laplace$draws("theta"), binwidth = 0.025) +
    +  ggplot2::labs(subtitle = "Approximate posterior from Laplace") +
    +  ggplot2::xlim(0, 1)
    +

    +
    +mcmc_hist(fit$draws("theta"), binwidth = 0.025) +
    +  ggplot2::labs(subtitle = "Posterior from MCMC") +
    +  ggplot2::xlim(0, 1)
    +

    +

    For more details on the $optimize(), +$laplace(), $variational(), and +pathfinder() methods, follow these links to their documentation pages.

    @@ -650,7 +768,7 @@

    Saving fitted model objects
    +
     fit$save_object(file = "fit.RDS")
     
     # can be read back in using readRDS
    @@ -662,7 +780,7 @@ 

    Saving fitted model objects$save_object() and replace saveRDS with the much faster qsave() function from the qs package.

    -
    +
     # Load CmdStan output files into the fitted model object.
     fit$draws() # Load posterior draws into the object.
     try(fit$sampler_diagnostics(), silent = TRUE) # Load sampler diagnostics.
    @@ -678,7 +796,7 @@ 

    Saving fitted model objects
    +
     # Load posterior draws into the fitted model object and omit other output.
     fit$draws()
     
    @@ -711,9 +829,12 @@ 

    Advantages of RStanDeveloping +using CmdStanR.)

  • Avoids use of R6 classes, which may result in more familiar syntax for many R users.

  • +
  • CRAN binaries available for Mac and Windows.

  • @@ -781,10 +902,10 @@

    Additional resources cmdstanr - 0.6.0 + 0.7.0

    @@ -118,8 +118,6 @@

    More details

    R Markdown CmdStan Engine
    -
    Handling deprecated Stan features with the canonicalizer in CmdStanR
    -
    Profiling Stan programs with CmdStanR
    Running Stan on the GPU with OpenCL
    diff --git a/docs/articles/posterior.html b/docs/articles/posterior.html index 24f6b6a34..bf1156f81 100644 --- a/docs/articles/posterior.html +++ b/docs/articles/posterior.html @@ -40,7 +40,7 @@ cmdstanr - 0.6.0 + 0.7.0

    @@ -140,78 +140,90 @@

    Working with Posteriors

    -

    Summary statistics

    -

    We can easily customise the summary statistics reported by +

    We can easily customize the summary statistics reported by $summary() and $print().

    -fit <- cmdstanr::cmdstanr_example("schools", method = "sample")
    -fit$summary()
    -
    Warning: 302 of 4000 (8.0%) transitions ended with a divergence.
    +fit <- cmdstanr::cmdstanr_example("schools", method = "sample")
    +
    Warning: 411 of 4000 (10.0%) transitions ended with a divergence.
     See https://mc-stan.org/misc/warnings for details.
    Warning: 1 of 4 chains had an E-BFMI less than 0.2.
     See https://mc-stan.org/misc/warnings for details.
    -
       variable  mean median  sd mad      q5 q95 rhat ess_bulk ess_tail
    -1      lp__ -56.7  -57.1 6.1 6.4 -66.008 -46  1.1       33       19
    -2        mu   6.6    6.7 4.2 4.5   0.025  13  1.0      124      879
    -3       tau   4.7    3.9 3.6 3.4   0.790  12  1.1       33       22
    -4  theta[1]   9.1    8.5 6.8 6.0  -0.082  21  1.0      163      405
    -5  theta[2]   7.0    6.9 5.5 5.8  -1.456  16  1.0      236     1810
    -6  theta[3]   5.7    6.1 6.4 6.2  -4.835  16  1.0      313     1466
    -7  theta[4]   6.8    6.9 5.9 5.8  -2.355  16  1.0      246     1356
    -8  theta[5]   4.9    5.2 5.8 5.5  -5.038  13  1.0      219      932
    -9  theta[6]   5.7    5.9 5.8 5.8  -4.073  15  1.0      278     1208
    -10 theta[7]   8.9    8.6 6.0 5.7   0.094  19  1.0      176      352
    -11 theta[8]   7.0    7.1 6.5 5.9  -3.088  18  1.0      317     1709
    +
    +fit$summary()
    +
       variable       mean     median       sd      mad         q5       q95
    +1      lp__ -55.581147 -56.536200 7.135791 7.809299 -66.108490 -44.17410
    +2        mu   6.053657   5.516030 3.777403 3.597633  -0.119312  12.18247
    +3       tau   4.456447   3.456865 3.641807 3.645828   0.778481  11.91315
    +4  theta[1]   8.436941   7.287125 6.350694 4.628344   0.249281  20.38983
    +5  theta[2]   6.522999   5.993620 5.308671 4.494072  -1.760367  15.64359
    +6  theta[3]   5.147676   4.349235 6.014958 5.082679  -5.803791  14.17618
    +7  theta[4]   6.189408   5.541310 5.578086 4.501730  -2.557880  15.63053
    +8  theta[5]   4.669689   4.952550 5.338198 4.271882  -5.029484  12.96141
    +9  theta[6]   5.123046   4.853110 5.623562 4.579796  -4.797084  13.70336
    +10 theta[7]   8.283195   7.324155 5.703079 4.912514   1.112880  18.94623
    +11 theta[8]   6.499894   5.594240 6.114406 4.839688  -2.747390  16.75831
    +       rhat   ess_bulk    ess_tail
    +1  1.348190   9.585696          NA
    +2  1.094243 190.870204  910.104440
    +3  1.305410  10.562073    4.833756
    +4  1.037209 205.819798 1097.512010
    +5  1.234963 615.265251 1158.714996
    +6  1.270209 429.358164 1118.125260
    +7  1.060915 288.566093 1331.398737
    +8  1.265317 356.169676 1352.544578
    +9  1.281493 482.412281 1311.140711
    +10 1.041368  75.113061  938.699910
    +11 1.069623 436.937773 1068.232198

    By default all variables are summaries with the follow functions:

    -
    +
     
    [1] "mean"      "median"    "sd"        "mad"       "quantile2"
    -

    To change the variables summarised, we use the variables argument

    -
    +

    To change the variables summarized, we use the variables argument

    +
     fit$summary(variables = c("mu", "tau"))
    -
      variable mean median  sd mad    q5 q95 rhat ess_bulk ess_tail
    -1       mu  6.6    6.7 4.2 4.5 0.025  13  1.0      124      879
    -2      tau  4.7    3.9 3.6 3.4 0.790  12  1.1       33       22
    +
      variable     mean   median       sd      mad        q5      q95     rhat
    +1       mu 6.053657 5.516030 3.777403 3.597633 -0.119312 12.18247 1.094243
    +2      tau 4.456447 3.456865 3.641807 3.645828  0.778481 11.91315 1.305410
    +   ess_bulk   ess_tail
    +1 190.87020 910.104440
    +2  10.56207   4.833756

    We can additionally change which functions are used

    -
    +
     fit$summary(variables = c("mu", "tau"), mean, sd)
    -
      variable mean  sd
    -1       mu  6.6 4.2
    -2      tau  4.7 3.6
    -

    To summarise all variables with non-default functions, it is +

      variable     mean       sd
    +1       mu 6.053657 3.777403
    +2      tau 4.456447 3.641807
    +

    To summarize all variables with non-default functions, it is necessary to set explicitly set the variables argument, either to NULL or the full vector of variable names.

    -
    -fit$metadata()$model_params
    -fit$summary(variables = NULL, "mean", "median")
    +
    +fit$metadata()$model_params
     [1] "lp__"     "mu"       "tau"      "theta[1]" "theta[2]" "theta[3]"
      [7] "theta[4]" "theta[5]" "theta[6]" "theta[7]" "theta[8]"
    -
       variable  mean median
    -1      lp__ -56.7  -57.1
    -2        mu   6.6    6.7
    -3       tau   4.7    3.9
    -4  theta[1]   9.1    8.5
    -5  theta[2]   7.0    6.9
    -6  theta[3]   5.7    6.1
    -7  theta[4]   6.8    6.9
    -8  theta[5]   4.9    5.2
    -9  theta[6]   5.7    5.9
    -10 theta[7]   8.9    8.6
    -11 theta[8]   7.0    7.1
    -

    Summary functions can be specified by character string, function, or -using a formula (or anything else supported by [rlang::as_function]). If -these arguments are named, those names will be used in the tibble -output. If the summary results are named they will take precedence.

    -my_sd <- function(x) c(My_SD = sd(x))
    +fit$summary(variables = NULL, "mean", "median")
    +
       variable       mean     median
    +1      lp__ -55.581147 -56.536200
    +2        mu   6.053657   5.516030
    +3       tau   4.456447   3.456865
    +4  theta[1]   8.436941   7.287125
    +5  theta[2]   6.522999   5.993620
    +6  theta[3]   5.147676   4.349235
    +7  theta[4]   6.189408   5.541310
    +8  theta[5]   4.669689   4.952550
    +9  theta[6]   5.123046   4.853110
    +10 theta[7]   8.283195   7.324155
    +11 theta[8]   6.499894   5.594240
    +

    Summary functions can be specified by character string, function, or +using a formula (or anything else supported by +rlang::as_function()). If these arguments are named, those +names will be used in the tibble output. If the summary results are +named they will take precedence.

    +
    +my_sd <- function(x) c(My_SD = sd(x))
     fit$summary(
       c("mu", "tau"), 
       MEAN = mean, 
    @@ -220,48 +232,47 @@ 

    Summary statistics~quantile(.x, probs = c(0.1, 0.9)), Minimum = function(x) min(x) )

    -
      variable MEAN median My_SD 10%  90% Minimum
    -1       mu  6.6    6.7   4.2 1.3 11.7  -11.23
    -2      tau  4.7    3.9   3.6 1.1  9.6    0.53
    +
      variable     MEAN   median    My_SD      10%       90%   Minimum
    +1       mu 6.053657 5.516030 3.777403 1.565533 10.863700 -8.554010
    +2      tau 4.456447 3.456865 3.641807 0.778481  9.666494  0.774844

    Arguments to all summary functions can also be specified with .args.

    -
    +
     fit$summary(c("mu", "tau"), quantile, .args = list(probs = c(0.025, .05, .95, .975)))
    -
      variable  2.5%    5% 95% 97.5%
    -1       mu -1.17 0.025  13    15
    -2      tau  0.59 0.790  12    13
    +
      variable      2.5%        5%      95%    97.5%
    +1       mu -1.614829 -0.119312 12.18247 13.54298
    +2      tau  0.778481  0.778481 11.91315 13.43694

    The summary functions are applied to the array of sample values, with dimension iter_samplingxchains.

    -
    +
     fit$summary(variables = NULL, dim, colMeans)
    -
       variable dim.1 dim.2     1     2     3     4
    -1      lp__  1000     4 -58.0 -55.7 -55.7 -57.3
    -2        mu  1000     4   6.9   7.5   5.4   6.8
    -3       tau  1000     4   5.2   4.3   4.4   4.9
    -4  theta[1]  1000     4  10.0   9.7   7.6   9.1
    -5  theta[2]  1000     4   7.1   8.0   5.8   7.2
    -6  theta[3]  1000     4   5.7   6.6   4.5   5.9
    -7  theta[4]  1000     4   7.2   7.7   5.6   6.7
    -8  theta[5]  1000     4   4.9   6.0   4.0   4.9
    -9  theta[6]  1000     4   5.7   6.7   4.8   5.7
    -10 theta[7]  1000     4   9.3   9.5   7.5   9.2
    -11 theta[8]  1000     4   7.0   8.0   5.9   7.0
    +
       variable dim.1 dim.2          1          2          3          4
    +1      lp__  1000     4 -56.698590 -58.592226 -56.911658 -50.122112
    +2        mu  1000     4   6.836467   6.338743   6.214918   4.824501
    +3       tau  1000     4   4.751132   5.540196   4.617587   2.916872
    +4  theta[1]  1000     4   9.139180   9.470688   8.631066   6.506831
    +5  theta[2]  1000     4   7.088238   6.756565   6.691301   5.555891
    +6  theta[3]  1000     4   6.087235   5.430155   5.208329   3.864985
    +7  theta[4]  1000     4   6.963234   6.883517   6.322756   4.588127
    +8  theta[5]  1000     4   5.312623   4.530775   4.623091   4.212267
    +9  theta[6]  1000     4   5.778286   4.999767   5.497782   4.216348
    +10 theta[7]  1000     4   9.354323   9.367796   8.483796   5.926863
    +11 theta[8]  1000     4   7.269928   7.165762   6.670007   4.893880

    For this reason users may have unexpected results if they use stats::var() directly, as it will return a covariance matrix. An alternative is the distributional::variance() function, which can also be accessed via posterior::variance().

    -
    -fit$summary(c("mu", "tau"), posterior::variance, ~var(as.vector(.x)))
    +
    +fit$summary(c("mu", "tau"), posterior::variance, ~var(as.vector(.x)))
      variable posterior::variance ~var(as.vector(.x))
    -1       mu                  18                  18
    -2      tau                  13                  13
    +1 mu 14.26877 14.26877 +2 tau 13.26276 13.26276

    Summary functions need not be numeric, but these won’t work with $print().

    -
    +
     strict_pos <- function(x) if (all(x > 0)) "yes" else "no"
    -fit$summary(variables = NULL, "Strictly Positive" = strict_pos)
    -# fit$print(variables = NULL, "Strictly Positive" = strict_pos)
    +fit$summary(variables = NULL, "Strictly Positive" = strict_pos)
       variable Strictly Positive
     1      lp__                no
     2        mu                no
    @@ -274,6 +285,8 @@ 

    Summary statistics
    +# fit$print(variables = NULL, "Strictly Positive" = strict_pos)

    For more information, see posterior::summarise_draws(), which is called by $summary().

    @@ -286,49 +299,49 @@

    Extracting posterior draws/samplesdraws_array and draws_df formats, but the posterior package supports other useful formats as well.

    -
    +
    -
     'draws_array' num [1:1000, 1:4, 1:11] -66.1 -68.2 -67.1 -62.4 -65.6 ...
    +
     'draws_array' num [1:1000, 1:4, 1:11] -61.7 -60.5 -61.8 -60.5 -66.8 ...
      - attr(*, "dimnames")=List of 3
       ..$ iteration: chr [1:1000] "1" "2" "3" "4" ...
       ..$ chain    : chr [1:4] "1" "2" "3" "4"
       ..$ variable : chr [1:11] "lp__" "mu" "tau" "theta[1]" ...
    -
    +
     # draws x variables data frame
     draws_df <- fit$draws(format = "df")
     str(draws_df)
    draws_df [4,000 × 14] (S3: draws_df/draws/tbl_df/tbl/data.frame)
    - $ lp__      : num [1:4000] -66.1 -68.2 -67.1 -62.4 -65.6 ...
    - $ mu        : num [1:4000] -2.42 9.44 2.99 2.91 6.73 ...
    - $ tau       : num [1:4000] 12.21 6.46 17.66 8.04 8.8 ...
    - $ theta[1]  : num [1:4000] 5.57 11.03 -2.77 1.5 8.91 ...
    - $ theta[2]  : num [1:4000] 6.97 3.31 6.77 12.84 5.79 ...
    - $ theta[3]  : num [1:4000] 8.21 15.21 -8.08 -5.34 -19.54 ...
    - $ theta[4]  : num [1:4000] 19.75 19.47 -7.42 -5.76 7.54 ...
    - $ theta[5]  : num [1:4000] -4.12 -5.77 6.01 5.63 -3.23 ...
    - $ theta[6]  : num [1:4000] -4.03 2.55 2.99 2.86 15.21 ...
    - $ theta[7]  : num [1:4000] -0.186 -2.004 10.11 7.803 14.427 ...
    - $ theta[8]  : num [1:4000] 0.0702 -3.005 11.0116 14.5279 14.1928 ...
    + $ lp__      : num [1:4000] -61.7 -60.5 -61.8 -60.5 -66.8 ...
    + $ mu        : num [1:4000] 8.86 6.18 9.7 4.97 5.16 ...
    + $ tau       : num [1:4000] 5.6 8.77 7.58 7.37 11.97 ...
    + $ theta[1]  : num [1:4000] 16.716 14.062 6.67 6.25 -0.404 ...
    + $ theta[2]  : num [1:4000] 4.377 6.173 15.608 0.638 10.449 ...
    + $ theta[3]  : num [1:4000] 11.59 9.27 11.11 3.74 13.42 ...
    + $ theta[4]  : num [1:4000] 13.68 12.51 7.77 6.48 10.16 ...
    + $ theta[5]  : num [1:4000] -0.877 1.959 7.197 2.097 3.229 ...
    + $ theta[6]  : num [1:4000] -5.02 -4.95 5.66 5.24 14.26 ...
    + $ theta[7]  : num [1:4000] 13.38 17.44 13.69 6.65 13.77 ...
    + $ theta[8]  : num [1:4000] 10.46 5.48 -6.15 20.92 31.79 ...
      $ .chain    : int [1:4000] 1 1 1 1 1 1 1 1 1 1 ...
      $ .iteration: int [1:4000] 1 2 3 4 5 6 7 8 9 10 ...
      $ .draw     : int [1:4000] 1 2 3 4 5 6 7 8 9 10 ...
    -
    +
     print(draws_df)
    # A draws_df: 1000 iterations, 4 chains, and 11 variables
        lp__   mu  tau theta[1] theta[2] theta[3] theta[4] theta[5]
    -1   -66 -2.4 12.2      5.6     6.97      8.2    19.75    -4.12
    -2   -68  9.4  6.5     11.0     3.31     15.2    19.47    -5.77
    -3   -67  3.0 17.7     -2.8     6.77     -8.1    -7.42     6.01
    -4   -62  2.9  8.0      1.5    12.84     -5.3    -5.76     5.63
    -5   -66  6.7  8.8      8.9     5.79    -19.5     7.54    -3.23
    -6   -64  5.3 11.4     18.5    13.37     -1.4    15.97    -0.61
    -7   -60  7.3  9.1      8.6     7.82      3.5     0.34    -2.00
    -8   -60  6.3  8.5      7.7     0.51      7.5    -0.99     1.51
    -9   -59  1.9  6.9      3.0     8.63      1.4     3.70     4.72
    -10  -63  9.1  9.3     16.0     5.77      3.9     4.14   -10.34
    +1   -62  8.9  5.6     16.7     4.38     11.6   13.676    -0.88
    +2   -61  6.2  8.8     14.1     6.17      9.3   12.515     1.96
    +3   -62  9.7  7.6      6.7    15.61     11.1    7.770     7.20
    +4   -60  5.0  7.4      6.3     0.64      3.7    6.477     2.10
    +5   -67  5.2 12.0     -0.4    10.45     13.4   10.162     3.23
    +6   -63 11.1  7.9     24.6    11.68     18.8   -1.606     3.79
    +7   -61 14.7  6.1     28.9    11.15     15.3    6.780     9.95
    +8   -63 13.4  5.7     13.7    21.03      7.2   20.051    12.99
    +9   -64 16.0  8.3     20.2     2.65     11.3   19.000     3.51
    +10  -59  2.9  4.5      4.2    11.37      2.5   -0.057     6.30
     # ... with 3990 more draws, and 3 more variables
     # ... hidden reserved variables {'.chain', '.iteration', '.draw'}

    To convert an existing draws object to a different format use the diff --git a/docs/articles/profiling.html b/docs/articles/profiling.html index 0bb97c2aa..dfbed9b18 100644 --- a/docs/articles/profiling.html +++ b/docs/articles/profiling.html @@ -40,7 +40,7 @@ cmdstanr - 0.6.0 + 0.7.0

    @@ -250,11 +250,11 @@

    Accessing the profiling info fit$profiles()

    [[1]]
             name   thread_id total_time forward_time reverse_time chain_stack
    -1 likelihood 0x102122e00 0.71089600   0.58314100    0.1277560       51969
    -2     priors 0x102122e00 0.00482875   0.00293865    0.0018901       34646
    +1 likelihood 0x118759e00 0.69970800   0.57529800   0.12441000       51468
    +2     priors 0x118759e00 0.00446571   0.00270154   0.00176417       34312
       no_chain_stack autodiff_calls no_autodiff_calls
    -1       34646000          17323                 1
    -2              0          17323                 1
    +1 34329156 17156 1 +2 34312 17156 1

    The total_time column is the total time spent inside a given profile statement. It is clear that the vast majority of time is spent in the likelihood function.

    @@ -297,11 +297,11 @@

    Comparing to a faster versio fit_glm$profiles()

    [[1]]
             name   thread_id total_time forward_time reverse_time chain_stack
    -1 likelihood 0x1066bde00 0.45516500   0.45357200   0.00159287       17695
    -2     priors 0x1066bde00 0.00399743   0.00242302   0.00157441       35390
    +1 likelihood 0x106ce4e00 0.38400000   0.38264800   0.00135175       52203
    +2     priors 0x106ce4e00 0.00356564   0.00237053   0.00119511       34802
       no_chain_stack autodiff_calls no_autodiff_calls
    -1              0          17695                 1
    -2              0          17695                 1
    +1 17401 17401 1 +2 34802 17401 1

    We can see from the total_time column that this is much faster than the previous model.

    @@ -324,7 +324,7 @@

    Per-gradient timings, and memory profile_chain_1 <- fit$profiles()[[1]] per_gradient_timing <- profile_chain_1$total_time/profile_chain_1$autodiff_calls print(per_gradient_timing) # two elements for the two profile statements in the model

    -
    [1] 4.103770e-05 2.787479e-07
    +
    [1] 4.078503e-05 2.603002e-07

    Accessing and saving the profile files @@ -335,7 +335,7 @@

    Accessing and saving the profile $profile_files().

     fit$profile_files()
    -
    [1] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmppMVUoY/model_6580008f67848265f3dfd0e7ae3b0600-profile-202307251456-1-5100ca.csv"
    +
    [1] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpJRtaoq/model_6580008f67848265f3dfd0e7ae3b0600-profile-202312131010-1-95e407.csv"

    These can be saved to a more permanent location with the $save_profile_files() method.

    diff --git a/docs/articles/r-markdown.html b/docs/articles/r-markdown.html
    index c403854e2..23c674be2 100644
    --- a/docs/articles/r-markdown.html
    +++ b/docs/articles/r-markdown.html
    @@ -40,7 +40,7 @@
           
           
             cmdstanr
    -        0.6.1
    +        0.7.0
           
         
    @@ -229,7 +229,7 @@

    Example#> #> All 4 chains finished successfully. #> Mean chain execution time: 0.0 seconds. -#> Total execution time: 0.6 seconds. +#> Total execution time: 0.7 seconds. print(fit) #> variable mean median sd mad q5 q95 rhat ess_bulk ess_tail diff --git a/docs/authors.html b/docs/authors.html index 3b3e08e88..8c3fd6459 100644 --- a/docs/authors.html +++ b/docs/authors.html @@ -17,7 +17,7 @@ cmdstanr - 0.6.1 + 0.7.0

    @@ -139,6 +139,14 @@

    Authors

    Jacob Socolar. Contributor.

    +
  • +

    Martin Modrák. Contributor. +

    +
  • +
  • +

    Steve Bronder. Contributor. +

    +
  • diff --git a/docs/index.html b/docs/index.html index 12811cade..1e06b71ed 100644 --- a/docs/index.html +++ b/docs/index.html @@ -45,7 +45,7 @@ cmdstanr - 0.6.1 + 0.7.0
    @@ -156,7 +156,7 @@

    Goals
  • A clean interface to Stan services so that CmdStanR can keep up with Stan releases.

  • R code that doesn’t interface directly with C++, only calls compiled executables.

  • Modularity: CmdStanR runs Stan’s algorithms and lets downstream modules do the analysis.

  • -
  • Flexible BSD-3 license.

  • +
  • Flexible BSD-3 license.

  • @@ -184,14 +184,14 @@

    Installing CmdStan

    Contributing

    -

    There is a lot of work still to be done and contributions are very welcome! If you are interested in contributing please comment on an open issue or open a new one if none are applicable.

    +

    There is a lot of work still to be done and we welcome contributions from anyone! If you are interested in contributing please comment on an open issue or open a new one if none are applicable.

    License

    CmdStanR, like CmdStan and the core Stan C++ code, is licensed under the following licenses:

    @@ -216,6 +216,12 @@

    License

    +
    +

    Community

    + +

    Citation

    diff --git a/docs/news/index.html b/docs/news/index.html index 526e35b5f..f702a0449 100644 --- a/docs/news/index.html +++ b/docs/news/index.html @@ -17,7 +17,7 @@ cmdstanr - 0.6.1 + 0.7.0
    @@ -102,6 +102,32 @@

    Changelog

    Source: NEWS.md

    +
    + +
    +

    Major new features

    +
    +
    +

    Other improvements and bug fixes

    +
    • Add missing link to diagnose method in CmdStanModel doc by @jgabry in #833
    • +
    • Improvements to compile tests by @martinmodrak in #836
    • +
    • Changed the delay behavior in wsl_installed by @martinmodrak in #839
    • +
    • Update array syntax in website vignette by @andrjohns in #841
    • +
    • Compatibility fixes for cmdstan 2.33+ by @jgabry in #843
    • +
    • Suggest format method after error due to old syntax by @jgabry in #852
    • +
    • Clarifications in R-markdown vignette by @jgcolman in #854
    • +
    • Update linux/wsl detection for install arch by @andrjohns in #856
    • +
    • Fix handling of single-length inits for containers by @andrjohns in #857
    • +
    • Add support/tests for exposing functions with tuples by @andrjohns in #860
    • +
    • Add support/tests for exporting functions with complex types by @andrjohns in #861
    • +
    • Add option for installing from release archive by @andrjohns in #866
    • +
    • Improve Pathfinder doc by @avehtari in #875
    • +
    • Rename jacobian_adjustment argument to jacobian by @jgabry in #879
    • +
    • Fix get_cmdstan_flags(‘STANCFLAGS’) in recursive make by @pearsonca in #881
    • +
    +
    • Store return codes instead of always querying exit status by @jgabry in #798
    • @@ -289,7 +315,7 @@

      Bug fixes

      New features

      -
      • CSV reading is now faster by using data.table::fread(). (#318)

      • +
        • CSV reading is now faster by using data.table::fread(). (#318)

        • install_cmdstan() gains argument version for specifying which version of CmdStan to install. (#300, #308)

        • New function check_cmdstan_toolchain() that checks if the appropriate toolchains are available. (#289)

        • $sample() method for CmdStanModel objects gains argument chain_ids for specifying custom chain IDs. (#319)

        • diff --git a/docs/pkgdown.yml b/docs/pkgdown.yml index 39643d5e4..14ff6a178 100644 --- a/docs/pkgdown.yml +++ b/docs/pkgdown.yml @@ -3,14 +3,12 @@ pkgdown: 2.0.7 pkgdown_sha: ~ articles: opencl: articles-online-only/opencl.html - settings-knitr: children/settings-knitr.html cmdstanr-internals: cmdstanr-internals.html cmdstanr: cmdstanr.html - deprecations: deprecations.html posterior: posterior.html profiling: profiling.html r-markdown: r-markdown.html -last_built: 2023-07-25T20:34Z +last_built: 2023-12-13T17:00Z urls: reference: https://mc-stan.org/cmdstanr/reference article: https://mc-stan.org/cmdstanr/articles diff --git a/docs/pull_request_template.html b/docs/pull_request_template.html index 8c12eee3e..dabe9968f 100644 --- a/docs/pull_request_template.html +++ b/docs/pull_request_template.html @@ -17,7 +17,7 @@ cmdstanr - 0.6.1 + 0.7.0

    diff --git a/docs/reference/CmdStanDiagnose.html b/docs/reference/CmdStanDiagnose.html index 4046c2686..e0d66d56d 100644 --- a/docs/reference/CmdStanDiagnose.html +++ b/docs/reference/CmdStanDiagnose.html @@ -18,7 +18,7 @@ cmdstanr - 0.6.0 + 0.7.0
    @@ -123,8 +123,10 @@

    See also

  • CmdStan User’s Guide: mc-stan.org/docs/cmdstan-guide

  • Other fitted model objects: CmdStanGQ, +CmdStanLaplace, CmdStanMCMC, CmdStanMLE, +CmdStanPathfinder, CmdStanVB

    @@ -135,11 +137,11 @@

    Examples

    # retrieve the gradients test$gradients() -#> param_idx value model finite_diff error -#> 1 0 -0.185026 11.68660 11.68660 -2.89634e-09 -#> 2 1 -1.579220 12.33760 12.33760 2.86842e-08 -#> 3 2 -0.391481 4.28029 4.28029 -2.55717e-08 -#> 4 3 -0.580364 18.08200 18.08200 -1.15854e-08 +#> param_idx value model finite_diff error +#> 1 0 -1.501560 34.56550 34.56550 4.58586e-08 +#> 2 1 -1.199130 1.69981 1.69981 -1.08299e-09 +#> 3 2 1.255420 -23.66640 -23.66640 -2.23140e-08 +#> 4 3 -0.452023 12.80740 12.80740 -8.57077e-09 # }
    diff --git a/docs/reference/CmdStanGQ.html b/docs/reference/CmdStanGQ.html index 24ddc1db4..6506d9afc 100644 --- a/docs/reference/CmdStanGQ.html +++ b/docs/reference/CmdStanGQ.html @@ -19,7 +19,7 @@ cmdstanr - 0.6.0 + 0.7.0
    @@ -141,8 +141,10 @@

    See also

  • CmdStan User’s Guide: mc-stan.org/docs/cmdstan-guide

  • Other fitted model objects: CmdStanDiagnose, +CmdStanLaplace, CmdStanMCMC, CmdStanMLE, +CmdStanPathfinder, CmdStanVB

    @@ -153,7 +155,7 @@

    Examples

    mcmc_program <- write_stan_file( "data { int<lower=0> N; - int<lower=0,upper=1> y[N]; + array[N] int<lower=0,upper=1> y; } parameters { real<lower=0,upper=1> theta; @@ -163,11 +165,6 @@

    Examples

    }" ) mod_mcmc <- cmdstan_model(mcmc_program) -#> Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/model-16218796affe.stan', line 3, column 4: Declaration -#> of arrays by placing brackets after a variable name is deprecated and -#> will be removed in Stan 2.33.0. Instead use the array keyword before the -#> type. This can be changed automatically using the auto-format flag to -#> stanc data <- list(N = 10, y = c(1,1,0,0,0,1,0,1,0,0)) fit_mcmc <- mod_mcmc$sample(data = data, seed = 123, refresh = 0) @@ -180,7 +177,7 @@

    Examples

    #> #> All 4 chains finished successfully. #> Mean chain execution time: 0.0 seconds. -#> Total execution time: 0.6 seconds. +#> Total execution time: 0.7 seconds. #> # stan program for standalone generated quantities @@ -188,27 +185,17 @@

    Examples

    gq_program <- write_stan_file( "data { int<lower=0> N; - int<lower=0,upper=1> y[N]; + array[N] int<lower=0,upper=1> y; } parameters { real<lower=0,upper=1> theta; } generated quantities { - int y_rep[N] = bernoulli_rng(rep_vector(theta, N)); + array[N] int y_rep = bernoulli_rng(rep_vector(theta, N)); }" ) mod_gq <- cmdstan_model(gq_program) -#> Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/model-162182f764088.stan', line 3, column 4: Declaration -#> of arrays by placing brackets after a variable name is deprecated and -#> will be removed in Stan 2.33.0. Instead use the array keyword before the -#> type. This can be changed automatically using the auto-format flag to -#> stanc -#> Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/model-162182f764088.stan', line 9, column 4: Declaration -#> of arrays by placing brackets after a variable name is deprecated and -#> will be removed in Stan 2.33.0. Instead use the array keyword before the -#> type. This can be changed automatically using the auto-format flag to -#> stanc fit_gq <- mod_gq$generate_quantities(fit_mcmc, data = data, seed = 123) #> Running standalone generated quantities after 4 MCMC chains, 1 chain at a time ... #> @@ -228,7 +215,7 @@

    Examples

    #> ..$ variable : chr [1:10] "y_rep[1]" "y_rep[2]" "y_rep[3]" "y_rep[4]" ... library(posterior) -#> This is posterior version 1.4.1 +#> This is posterior version 1.5.0 #> #> Attaching package: ‘posterior’ #> The following objects are masked from ‘package:stats’: diff --git a/docs/reference/CmdStanLaplace.html b/docs/reference/CmdStanLaplace.html new file mode 100644 index 000000000..56d9c8d39 --- /dev/null +++ b/docs/reference/CmdStanLaplace.html @@ -0,0 +1,174 @@ + +CmdStanLaplace objects — CmdStanLaplace • cmdstanr + + +
    +
    + + + +
    +
    + + +
    +

    A CmdStanLaplace object is the fitted model object returned by the +$laplace() method of a +CmdStanModel object.

    +
    + + +
    +

    Methods

    +

    CmdStanLaplace objects have the following associated methods, +all of which have their own (linked) documentation pages.

    +

    Extract contents of fitted model object

    +
    MethodDescription
    $draws()Return approximate posterior draws as a draws_matrix.
    $mode()Return the mode as a CmdStanMLE object.
    $lp()Return the total log probability density (target) computed in the model block of the Stan program.
    $lp_approx()Return the log density of the approximation to the posterior.
    $init()Return user-specified initial values.
    $metadata()Return a list of metadata gathered from the CmdStan CSV files.
    $code()Return Stan code as a character vector.
    + +
    +

    Summarize inferences

    +
    MethodDescription
    $summary()Run posterior::summarise_draws().
    + +
    +

    Save fitted model object and temporary files

    +
    MethodDescription
    $save_object()Save fitted model object to a file.
    $save_output_files()Save output CSV files to a specified location.
    $save_data_file()Save JSON data file to a specified location.
    $save_latent_dynamics_files()Save diagnostic CSV files to a specified location.
    + +
    +

    Report run times, console output, return codes

    +
    MethodDescription
    $time()Report the run time of the Laplace sampling step.
    $output()Pretty print the output that was printed to the console.
    $return_codes()Return the return codes from the CmdStan runs.
    + +
    +
    +

    See also

    +

    The CmdStanR website +(mc-stan.org/cmdstanr) for online +documentation and tutorials.

    +

    The Stan and CmdStan documentation:

    Other fitted model objects: +CmdStanDiagnose, +CmdStanGQ, +CmdStanMCMC, +CmdStanMLE, +CmdStanPathfinder, +CmdStanVB

    +
    + +
    + +
    + + +
    + +
    +

    Site built with pkgdown 2.0.7.

    +
    + +
    + + + + + + + + diff --git a/docs/reference/CmdStanMCMC.html b/docs/reference/CmdStanMCMC.html index e7ecaec5d..4b2ed95bc 100644 --- a/docs/reference/CmdStanMCMC.html +++ b/docs/reference/CmdStanMCMC.html @@ -20,7 +20,7 @@ cmdstanr - 0.6.0 + 0.7.0 @@ -148,7 +148,9 @@

    See also

    Other fitted model objects: CmdStanDiagnose, CmdStanGQ, +CmdStanLaplace, CmdStanMLE, +CmdStanPathfinder, CmdStanVB

    diff --git a/docs/reference/CmdStanMLE.html b/docs/reference/CmdStanMLE.html index 1eb913bf5..e2d322da9 100644 --- a/docs/reference/CmdStanMLE.html +++ b/docs/reference/CmdStanMLE.html @@ -18,7 +18,7 @@ cmdstanr - 0.6.0 + 0.7.0 @@ -144,7 +144,9 @@

    See also

    Other fitted model objects: CmdStanDiagnose, CmdStanGQ, +CmdStanLaplace, CmdStanMCMC, +CmdStanPathfinder, CmdStanVB

    diff --git a/docs/reference/CmdStanModel-3.png b/docs/reference/CmdStanModel-3.png index e985d6949..8d58780fd 100644 Binary files a/docs/reference/CmdStanModel-3.png and b/docs/reference/CmdStanModel-3.png differ diff --git a/docs/reference/CmdStanModel.html b/docs/reference/CmdStanModel.html index 9d9cdf30d..6b2ba8d72 100644 --- a/docs/reference/CmdStanModel.html +++ b/docs/reference/CmdStanModel.html @@ -20,7 +20,7 @@ cmdstanr - 0.6.0 + 0.7.0 @@ -131,7 +131,7 @@

    Diagnostics

    Model fitting

    -
    MethodDescription
    $sample()Run CmdStan's "sample" method, return CmdStanMCMC object.
    $sample_mpi()Run CmdStan's "sample" method with MPI, return CmdStanMCMC object.
    $optimize()Run CmdStan's "optimize" method, return CmdStanMLE object.
    $variational()Run CmdStan's "variational" method, return CmdStanVB object.
    $generate_quantities()Run CmdStan's "generate quantities" method, return CmdStanGQ object.
    +
    MethodDescription
    $sample()Run CmdStan's "sample" method, return CmdStanMCMC object.
    $sample_mpi()Run CmdStan's "sample" method with MPI, return CmdStanMCMC object.
    $optimize()Run CmdStan's "optimize" method, return CmdStanMLE object.
    $variational()Run CmdStan's "variational" method, return CmdStanVB object.
    $pathfinder()Run CmdStan's "pathfinder" method, return CmdStanPathfinder object.
    $generate_quantities()Run CmdStan's "generate quantities" method, return CmdStanGQ object.
    @@ -149,15 +149,6 @@

    Examples

    # \dontrun{
     library(cmdstanr)
     library(posterior)
    -#> This is posterior version 1.4.1.9000
    -#> 
    -#> Attaching package: ‘posterior’
    -#> The following objects are masked from ‘package:stats’:
    -#> 
    -#>     mad, sd, var
    -#> The following objects are masked from ‘package:base’:
    -#> 
    -#>     %in%, match
     library(bayesplot)
     #> This is bayesplot version 1.10.0
     #> - Online documentation and vignettes at mc-stan.org/bayesplot
    @@ -177,13 +168,12 @@ 

    Examples

    # Otherwise use the `path` argument to specify the location of your # CmdStan installation.) set_cmdstan_path(path = NULL) -#> CmdStan path set to: /Users/jgabry/.cmdstan/cmdstan-2.32.2 +#> CmdStan path set to: /Users/jgabry/.cmdstan/cmdstan-2.33.1 # Create a CmdStanModel object from a Stan program, # here using the example model that comes with CmdStan file <- file.path(cmdstan_path(), "examples/bernoulli/bernoulli.stan") mod <- cmdstan_model(file) -#> Model executable is up to date! mod$print() #> data { #> int<lower=0> N; @@ -269,6 +259,18 @@

    Examples

    #> 1 lp__ -7.30 -7.03 0.721 0.380 -8.82 -6.75 1.00 902. 1006. #> 2 theta 0.247 0.233 0.122 0.129 0.0786 0.470 1.00 762. 712. +# Check sampling diagnostics +fit_mcmc$diagnostic_summary() +#> $num_divergent +#> [1] 0 0 +#> +#> $num_max_treedepth +#> [1] 0 0 +#> +#> $ebfmi +#> [1] 1.017555 1.250490 +#> + # Get posterior draws draws <- fit_mcmc$draws() print(draws) @@ -317,51 +319,8 @@

    Examples

    #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. -# Call CmdStan's diagnose and stansummary utilities -fit_mcmc$cmdstan_diagnose() -#> Processing csv files: /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpGdLwVH/bernoulli-202308230921-1-6956c8.csv, /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpGdLwVH/bernoulli-202308230921-2-6956c8.csv -#> -#> Checking sampler transitions treedepth. -#> Treedepth satisfactory for all transitions. -#> -#> Checking sampler transitions for divergences. -#> No divergent transitions found. -#> -#> Checking E-BFMI - sampler transitions HMC potential energy. -#> E-BFMI satisfactory. -#> -#> Effective sample size satisfactory. -#> -#> Split R-hat values satisfactory all parameters. -#> -#> Processing complete, no problems detected. -fit_mcmc$cmdstan_summary() -#> Inference for Stan model: bernoulli_model -#> 2 chains: each with iter=(1000,1000); warmup=(0,0); thin=(1,1); 2000 iterations saved. -#> -#> Warmup took (0.0040, 0.0040) seconds, 0.0080 seconds total -#> Sampling took (0.011, 0.012) seconds, 0.023 seconds total -#> -#> Mean MCSE StdDev 5% 50% 95% N_Eff N_Eff/s R_hat -#> -#> lp__ -7.3 2.6e-02 0.72 -8.8 -7.0 -6.8 781 33958 1.0 -#> accept_stat__ 0.92 8.3e-03 0.13 0.64 0.97 1.0 235 10198 1.0e+00 -#> stepsize__ 0.95 7.9e-02 0.079 0.87 1.0 1.0 1.0 44 2.0e+13 -#> treedepth__ 1.4 1.1e-02 0.48 1.0 1.0 2.0 1874 81476 1.0e+00 -#> n_leapfrog__ 2.5 1.4e-01 1.3 1.0 3.0 3.0 89 3874 1.0e+00 -#> divergent__ 0.00 nan 0.00 0.00 0.00 0.00 nan nan nan -#> energy__ 7.8 3.6e-02 1.00 6.8 7.5 9.6 775 33684 1.0e+00 -#> -#> theta 0.25 4.3e-03 0.12 0.079 0.23 0.47 796 34623 1.0 -#> -#> Samples were drawn using hmc with nuts. -#> For each parameter, N_Eff is a crude measure of effective sample size, -#> and R_hat is the potential scale reduction factor on split chains (at -#> convergence, R_hat=1). - # For models fit using MCMC, if you like working with RStan's stanfit objects # then you can create one with rstan::read_stan_csv() - # stanfit <- rstan::read_stan_csv(fit_mcmc$output_files()) @@ -374,8 +333,7 @@

    Examples

    #> 6 -5.00402 0.000103557 2.55661e-07 1 1 9 #> Optimization terminated normally: #> Convergence detected: relative gradient magnitude is below tolerance -#> Finished in 0.1 seconds. - +#> Finished in 0.2 seconds. fit_optim$summary() #> # A tibble: 2 × 2 #> variable estimate @@ -383,16 +341,57 @@

    Examples

    #> 1 lp__ -5.00 #> 2 theta 0.2 +# Run 'optimize' again with 'jacobian=TRUE' and then draw from Laplace approximation +# to the posterior +fit_optim <- mod$optimize(data = my_data_file, jacobian = TRUE) +#> Initial log joint probability = -6.92942 +#> Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes +#> 4 -6.74802 0.000269426 5.11368e-05 0.9249 0.9249 7 +#> Optimization terminated normally: +#> Convergence detected: relative gradient magnitude is below tolerance +#> Finished in 0.1 seconds. +fit_laplace <- mod$laplace(data = my_data_file, mode = fit_optim, draws = 2000) +#> Calculating Hessian +#> Calculating inverse of Cholesky factor +#> Generating draws +#> iteration: 0 +#> iteration: 100 +#> iteration: 200 +#> iteration: 300 +#> iteration: 400 +#> iteration: 500 +#> iteration: 600 +#> iteration: 700 +#> iteration: 800 +#> iteration: 900 +#> iteration: 1000 +#> iteration: 1100 +#> iteration: 1200 +#> iteration: 1300 +#> iteration: 1400 +#> iteration: 1500 +#> iteration: 1600 +#> iteration: 1700 +#> iteration: 1800 +#> iteration: 1900 +#> Finished in 0.1 seconds. +fit_laplace$summary() +#> # A tibble: 3 × 7 +#> variable mean median sd mad q5 q95 +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> +#> 1 lp__ -7.22 -6.97 0.652 0.296 -8.51 -6.75 +#> 2 lp_approx__ -0.492 -0.223 0.677 0.302 -1.91 -0.00222 +#> 3 theta 0.265 0.246 0.120 0.122 0.0952 0.484 -# Run 'variational' method to approximate the posterior (default is meanfield ADVI) +# Run 'variational' method to use ADVI to approximate posterior fit_vb <- mod$variational(data = stan_data, seed = 123) #> ------------------------------------------------------------ #> EXPERIMENTAL ALGORITHM: #> This procedure has not been thoroughly tested and may be unstable #> or buggy. The interface is subject to change. #> ------------------------------------------------------------ -#> Gradient evaluation took 5e-06 seconds -#> 1000 transitions using 10 leapfrog steps per transition would take 0.05 seconds. +#> Gradient evaluation took 6e-06 seconds +#> 1000 transitions using 10 leapfrog steps per transition would take 0.06 seconds. #> Adjust your expectations accordingly! #> Begin eta adaptation. #> Iteration: 1 / 250 [ 0%] (Adaptation) @@ -409,7 +408,6 @@

    Examples

    #> Drawing a sample of size 1000 from the approximate posterior... #> COMPLETED. #> Finished in 0.1 seconds. - fit_vb$summary() #> # A tibble: 3 × 7 #> variable mean median sd mad q5 q95 @@ -417,12 +415,87 @@

    Examples

    #> 1 lp__ -7.18 -6.94 0.588 0.259 -8.36 -6.75 #> 2 lp_approx__ -0.515 -0.221 0.692 0.303 -2.06 -0.00257 #> 3 theta 0.263 0.246 0.115 0.113 0.106 0.481 - -# Plot approximate posterior using bayesplot mcmc_hist(fit_vb$draws("theta")) #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. +# Run 'pathfinder' method, a new alternative to the variational method +fit_pf <- mod$pathfinder(data = stan_data, seed = 123) +#> Path [1] :Initial log joint density = -11.008832 +#> Path [1] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 9.383e-04 1.391e-05 1.000e+00 1.000e+00 126 -6.264e+00 -6.264e+00 +#> Path [1] :Best Iter: [3] ELBO (-6.195408) evaluations: (126) +#> Path [2] :Initial log joint density = -7.318450 +#> Path [2] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 4 -6.748e+00 5.414e-03 1.618e-04 1.000e+00 1.000e+00 101 -6.251e+00 -6.251e+00 +#> Path [2] :Best Iter: [3] ELBO (-6.229174) evaluations: (101) +#> Path [3] :Initial log joint density = -12.374612 +#> Path [3] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.419e-03 2.837e-05 1.000e+00 1.000e+00 126 -6.199e+00 -6.199e+00 +#> Path [3] :Best Iter: [5] ELBO (-6.199185) evaluations: (126) +#> Path [4] :Initial log joint density = -13.009824 +#> Path [4] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.677e-03 3.885e-05 1.000e+00 1.000e+00 126 -6.173e+00 -6.173e+00 +#> Path [4] :Best Iter: [5] ELBO (-6.172860) evaluations: (126) +#> Total log probability function evaluations:4379 +#> Finished in 0.1 seconds. +fit_pf$summary() +#> # A tibble: 3 × 7 +#> variable mean median sd mad q5 q95 +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> +#> 1 lp_approx__ -1.08 -0.728 0.886 0.304 -2.71 -0.511 +#> 2 lp__ -7.26 -6.96 0.738 0.297 -8.72 -6.75 +#> 3 theta 0.249 0.230 0.120 0.121 0.0854 0.471 +mcmc_hist(fit_pf$draws("theta")) +#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. + + +# Run 'pathfinder' again with more paths, fewer draws per path, +# better covariance approximation, and fewer LBFGSs iterations +fit_pf <- mod$pathfinder(data = stan_data, num_paths=10, single_path_draws=40, + history_size=50, max_lbfgs_iters=100) +#> Path [1] :Initial log joint density = -6.860971 +#> Path [1] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 4 -6.748e+00 7.999e-04 6.710e-06 1.000e+00 1.000e+00 101 -6.266e+00 -6.266e+00 +#> Path [1] :Best Iter: [2] ELBO (-6.159468) evaluations: (101) +#> Path [2] :Initial log joint density = -15.050783 +#> Path [2] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 2.035e-03 6.058e-05 1.000e+00 1.000e+00 126 -6.231e+00 -6.231e+00 +#> Path [2] :Best Iter: [2] ELBO (-6.179167) evaluations: (126) +#> Path [3] :Initial log joint density = -12.609181 +#> Path [3] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.519e-03 3.222e-05 1.000e+00 1.000e+00 126 -6.224e+00 -6.224e+00 +#> Path [3] :Best Iter: [3] ELBO (-6.177380) evaluations: (126) +#> Path [4] :Initial log joint density = -7.551395 +#> Path [4] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.242e-04 5.362e-07 1.000e+00 1.000e+00 126 -6.267e+00 -6.267e+00 +#> Path [4] :Best Iter: [4] ELBO (-6.163367) evaluations: (126) +#> Path [5] :Initial log joint density = -7.623161 +#> Path [5] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.410e-04 6.577e-07 1.000e+00 1.000e+00 126 -6.242e+00 -6.242e+00 +#> Path [5] :Best Iter: [5] ELBO (-6.241791) evaluations: (126) +#> Path [6] :Initial log joint density = -12.604877 +#> Path [6] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.517e-03 3.214e-05 1.000e+00 1.000e+00 126 -6.220e+00 -6.220e+00 +#> Path [6] :Best Iter: [4] ELBO (-6.150438) evaluations: (126) +#> Path [7] :Initial log joint density = -6.754804 +#> Path [7] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 3 -6.748e+00 1.079e-03 3.589e-05 1.000e+00 1.000e+00 76 -6.230e+00 -6.230e+00 +#> Path [7] :Best Iter: [2] ELBO (-6.228353) evaluations: (76) +#> Path [8] :Initial log joint density = -6.839688 +#> Path [8] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 4 -6.748e+00 6.089e-04 4.257e-06 1.000e+00 1.000e+00 101 -6.279e+00 -6.279e+00 +#> Path [8] :Best Iter: [3] ELBO (-6.230942) evaluations: (101) +#> Path [9] :Initial log joint density = -7.590990 +#> Path [9] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.335e-04 6.020e-07 1.000e+00 1.000e+00 126 -6.260e+00 -6.260e+00 +#> Path [9] :Best Iter: [4] ELBO (-6.210494) evaluations: (126) +#> Path [10] :Initial log joint density = -11.878141 +#> Path [10] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.194e-03 2.066e-05 1.000e+00 1.000e+00 126 -6.255e+00 -6.255e+00 +#> Path [10] :Best Iter: [2] ELBO (-6.222876) evaluations: (126) +#> Total log probability function evaluations:1310 +#> Finished in 0.1 seconds. # Specifying initial values as a function fit_mcmc_w_init_fun <- mod$sample( diff --git a/docs/reference/CmdStanPathfinder.html b/docs/reference/CmdStanPathfinder.html new file mode 100644 index 000000000..3da6af493 --- /dev/null +++ b/docs/reference/CmdStanPathfinder.html @@ -0,0 +1,174 @@ + +CmdStanPathfinder objects — CmdStanPathfinder • cmdstanr + + +
    +
    + + + +
    +
    + + +
    +

    A CmdStanPathfinder object is the fitted model object returned by the +$pathfinder() method of a +CmdStanModel object.

    +
    + + +
    +

    Methods

    +

    CmdStanPathfinder objects have the following associated methods, +all of which have their own (linked) documentation pages.

    +

    Extract contents of fitted model object

    +
    MethodDescription
    $draws()Return approximate posterior draws as a draws_matrix.
    $lp()Return the total log probability density (target) computed in the model block of the Stan program.
    $lp_approx()Return the log density of the approximation to the posterior.
    $init()Return user-specified initial values.
    $metadata()Return a list of metadata gathered from the CmdStan CSV files.
    $code()Return Stan code as a character vector.
    + +
    +

    Summarize inferences

    +
    MethodDescription
    $summary()Run posterior::summarise_draws().
    $cmdstan_summary()Run and print CmdStan's bin/stansummary.
    + +
    +

    Save fitted model object and temporary files

    +
    MethodDescription
    $save_object()Save fitted model object to a file.
    $save_output_files()Save output CSV files to a specified location.
    $save_data_file()Save JSON data file to a specified location.
    $save_latent_dynamics_files()Save diagnostic CSV files to a specified location.
    + +
    +

    Report run times, console output, return codes

    +
    MethodDescription
    $time()Report the total run time.
    $output()Pretty print the output that was printed to the console.
    $return_codes()Return the return codes from the CmdStan runs.
    + +
    +
    +

    See also

    +

    The CmdStanR website +(mc-stan.org/cmdstanr) for online +documentation and tutorials.

    +

    The Stan and CmdStan documentation:

    Other fitted model objects: +CmdStanDiagnose, +CmdStanGQ, +CmdStanLaplace, +CmdStanMCMC, +CmdStanMLE, +CmdStanVB

    +
    + +
    + +
    + + +
    + +
    +

    Site built with pkgdown 2.0.7.

    +
    + +
    + + + + + + + + diff --git a/docs/reference/CmdStanVB.html b/docs/reference/CmdStanVB.html index 51c738a30..b3ea83472 100644 --- a/docs/reference/CmdStanVB.html +++ b/docs/reference/CmdStanVB.html @@ -19,7 +19,7 @@ cmdstanr - 0.6.0 + 0.7.0
    @@ -146,8 +146,10 @@

    See also

    Other fitted model objects: CmdStanDiagnose, CmdStanGQ, +CmdStanLaplace, CmdStanMCMC, -CmdStanMLE

    +CmdStanMLE, +CmdStanPathfinder

    diff --git a/docs/reference/Rplot001.png b/docs/reference/Rplot001.png index 6823093a5..17a358060 100644 Binary files a/docs/reference/Rplot001.png and b/docs/reference/Rplot001.png differ diff --git a/docs/reference/Rplot003.png b/docs/reference/Rplot003.png new file mode 100644 index 000000000..21e6f50ef Binary files /dev/null and b/docs/reference/Rplot003.png differ diff --git a/docs/reference/as_draws.CmdStanMCMC.html b/docs/reference/as_draws.CmdStanMCMC.html index 6be705dc4..ab9c18eca 100644 --- a/docs/reference/as_draws.CmdStanMCMC.html +++ b/docs/reference/as_draws.CmdStanMCMC.html @@ -19,7 +19,7 @@ cmdstanr - 0.6.0 + 0.7.0 @@ -118,10 +118,16 @@

    Create a draws object from a CmdStanR fitted model object

    # S3 method for CmdStanMLE as_draws(x, ...) +# S3 method for CmdStanLaplace +as_draws(x, ...) + # S3 method for CmdStanVB as_draws(x, ...) # S3 method for CmdStanGQ +as_draws(x, ...) + +# S3 method for CmdStanPathfinder as_draws(x, ...)
    @@ -152,41 +158,41 @@

    Examples

    #> #> chain #> iteration 1 2 3 4 -#> 1 -66 -65 -65 -64 -#> 2 -66 -65 -64 -66 -#> 3 -65 -66 -65 -66 -#> 4 -65 -65 -66 -65 -#> 5 -67 -66 -66 -67 +#> 1 -66 -67 -65 -67 +#> 2 -65 -65 -64 -67 +#> 3 -66 -66 -65 -64 +#> 4 -65 -65 -65 -66 +#> 5 -67 -67 -66 -65 #> #> , , variable = alpha #> #> chain -#> iteration 1 2 3 4 -#> 1 0.37 0.21 0.66 0.314 -#> 2 0.34 0.35 0.28 0.748 -#> 3 0.58 0.25 0.41 0.023 -#> 4 0.20 0.37 0.45 0.297 -#> 5 0.24 0.37 0.72 0.328 +#> iteration 1 2 3 4 +#> 1 0.37 0.0232 0.30 0.19 +#> 2 0.35 0.1359 0.50 0.56 +#> 3 0.39 0.0021 0.22 0.42 +#> 4 0.42 0.2264 0.22 0.21 +#> 5 0.30 0.6367 0.19 0.24 #> #> , , variable = beta[1] #> #> chain #> iteration 1 2 3 4 -#> 1 -0.54 -0.69 -0.71 -0.55 -#> 2 -0.98 -0.91 -0.67 -0.70 -#> 3 -0.77 -0.30 -0.82 -0.63 -#> 4 -0.41 -0.66 -1.07 -0.75 -#> 5 -0.26 -0.78 -0.72 -0.45 +#> 1 -0.21 -0.70 -0.87 -0.93 +#> 2 -0.42 -0.81 -0.58 -0.38 +#> 3 -1.01 -0.83 -0.81 -0.49 +#> 4 -0.95 -0.45 -0.67 -1.03 +#> 5 -0.16 -0.96 -0.73 -0.89 #> #> , , variable = beta[2] #> #> chain -#> iteration 1 2 3 4 -#> 1 -0.738 -0.427 -0.183 -0.256 -#> 2 -0.545 -0.076 -0.220 -0.433 -#> 3 -0.168 -0.494 -0.400 -0.201 -#> 4 -0.281 -0.029 -0.388 0.072 -#> 5 0.054 -0.460 -0.027 0.122 +#> iteration 1 2 3 4 +#> 1 -0.3841 0.08 -0.574 -0.73 +#> 2 -0.2843 -0.27 -0.376 0.12 +#> 3 -0.0223 -0.13 -0.065 -0.11 +#> 4 0.0016 -0.28 -0.100 -0.29 +#> 5 -0.5327 -0.66 -0.644 -0.16 #> #> # ... with 995 more iterations, and 101 more variables @@ -200,64 +206,65 @@

    Examples

    #> [1] 0.37 ± 0.22 #> #> $beta: rvar<1000,4>[3] mean ± sd: -#> [1] -0.67 ± 0.25 -0.27 ± 0.22 0.67 ± 0.27 +#> [1] -0.66 ± 0.25 -0.27 ± 0.23 0.67 ± 0.26 #> #> $log_lik: rvar<1000,4>[100] mean ± sd: -#> [1] -0.517 ± 0.101 -0.404 ± 0.151 -0.497 ± 0.215 -0.451 ± 0.152 -#> [5] -1.177 ± 0.282 -0.595 ± 0.194 -0.639 ± 0.124 -0.281 ± 0.134 -#> [9] -0.696 ± 0.170 -0.736 ± 0.229 -0.284 ± 0.127 -0.503 ± 0.249 -#> [13] -0.654 ± 0.209 -0.361 ± 0.173 -0.281 ± 0.106 -0.278 ± 0.087 -#> [17] -1.585 ± 0.286 -0.482 ± 0.109 -0.234 ± 0.076 -0.114 ± 0.078 -#> [21] -0.214 ± 0.088 -0.572 ± 0.150 -0.333 ± 0.139 -0.137 ± 0.067 -#> [25] -0.455 ± 0.121 -1.516 ± 0.346 -0.310 ± 0.123 -0.447 ± 0.085 -#> [29] -0.723 ± 0.222 -0.697 ± 0.195 -0.492 ± 0.165 -0.426 ± 0.108 -#> [33] -0.410 ± 0.128 -0.064 ± 0.050 -0.584 ± 0.182 -0.327 ± 0.132 -#> [37] -0.697 ± 0.220 -0.313 ± 0.145 -0.180 ± 0.109 -0.682 ± 0.131 -#> [41] -1.124 ± 0.254 -0.931 ± 0.201 -0.413 ± 0.269 -1.172 ± 0.186 -#> [45] -0.360 ± 0.119 -0.582 ± 0.132 -0.307 ± 0.130 -0.326 ± 0.084 -#> [49] -0.321 ± 0.080 -1.289 ± 0.340 -0.290 ± 0.095 -0.834 ± 0.146 -#> [53] -0.405 ± 0.132 -0.371 ± 0.144 -0.387 ± 0.137 -0.322 ± 0.189 -#> [57] -0.657 ± 0.118 -0.947 ± 0.345 -1.358 ± 0.345 -0.977 ± 0.161 -#> [61] -0.542 ± 0.101 -0.881 ± 0.324 -0.118 ± 0.073 -0.899 ± 0.249 -#> [65] -1.999 ± 0.597 -0.509 ± 0.135 -0.278 ± 0.082 -1.056 ± 0.235 -#> [69] -0.437 ± 0.085 -0.641 ± 0.239 -0.608 ± 0.208 -0.462 ± 0.171 -#> [73] -1.479 ± 0.361 -0.947 ± 0.192 -1.146 ± 0.401 -0.373 ± 0.139 -#> [77] -0.875 ± 0.138 -0.489 ± 0.178 -0.762 ± 0.186 -0.544 ± 0.202 -#> [81] -0.164 ± 0.099 -0.225 ± 0.139 -0.345 ± 0.082 -0.277 ± 0.091 -#> [85] -0.131 ± 0.075 -1.126 ± 0.328 -0.822 ± 0.130 -0.773 ± 0.243 -#> [89] -1.276 ± 0.323 -0.261 ± 0.132 -0.386 ± 0.129 -1.490 ± 0.344 -#> [93] -0.740 ± 0.216 -0.319 ± 0.088 -0.391 ± 0.113 -1.566 ± 0.280 -#> [97] -0.433 ± 0.103 -1.050 ± 0.364 -0.693 ± 0.143 -0.392 ± 0.099 +#> [1] -0.518 ± 0.099 -0.405 ± 0.147 -0.501 ± 0.220 -0.451 ± 0.153 +#> [5] -1.173 ± 0.276 -0.592 ± 0.185 -0.640 ± 0.123 -0.280 ± 0.135 +#> [9] -0.693 ± 0.163 -0.740 ± 0.234 -0.283 ± 0.124 -0.500 ± 0.241 +#> [13] -0.655 ± 0.206 -0.364 ± 0.175 -0.282 ± 0.106 -0.278 ± 0.087 +#> [17] -1.582 ± 0.288 -0.481 ± 0.107 -0.235 ± 0.076 -0.115 ± 0.079 +#> [21] -0.215 ± 0.088 -0.570 ± 0.144 -0.332 ± 0.140 -0.139 ± 0.067 +#> [25] -0.456 ± 0.121 -1.513 ± 0.345 -0.309 ± 0.120 -0.447 ± 0.083 +#> [29] -0.722 ± 0.227 -0.694 ± 0.189 -0.489 ± 0.157 -0.427 ± 0.106 +#> [33] -0.412 ± 0.126 -0.065 ± 0.052 -0.585 ± 0.184 -0.327 ± 0.135 +#> [37] -0.700 ± 0.227 -0.314 ± 0.150 -0.182 ± 0.110 -0.681 ± 0.126 +#> [41] -1.124 ± 0.248 -0.928 ± 0.195 -0.412 ± 0.267 -1.169 ± 0.185 +#> [45] -0.362 ± 0.119 -0.583 ± 0.128 -0.307 ± 0.129 -0.327 ± 0.084 +#> [49] -0.322 ± 0.079 -1.286 ± 0.334 -0.291 ± 0.096 -0.833 ± 0.144 +#> [53] -0.404 ± 0.127 -0.373 ± 0.143 -0.388 ± 0.134 -0.322 ± 0.195 +#> [57] -0.658 ± 0.117 -0.951 ± 0.353 -1.351 ± 0.337 -0.975 ± 0.159 +#> [61] -0.542 ± 0.099 -0.876 ± 0.314 -0.119 ± 0.075 -0.901 ± 0.247 +#> [65] -1.995 ± 0.581 -0.511 ± 0.134 -0.279 ± 0.083 -1.057 ± 0.233 +#> [69] -0.438 ± 0.083 -0.638 ± 0.235 -0.609 ± 0.207 -0.463 ± 0.169 +#> [73] -1.476 ± 0.363 -0.944 ± 0.194 -1.139 ± 0.389 -0.376 ± 0.140 +#> [77] -0.875 ± 0.136 -0.489 ± 0.174 -0.764 ± 0.190 -0.545 ± 0.196 +#> [81] -0.165 ± 0.100 -0.226 ± 0.138 -0.346 ± 0.081 -0.278 ± 0.091 +#> [85] -0.132 ± 0.075 -1.120 ± 0.315 -0.822 ± 0.127 -0.773 ± 0.237 +#> [89] -1.270 ± 0.314 -0.263 ± 0.136 -0.388 ± 0.129 -1.484 ± 0.335 +#> [93] -0.738 ± 0.218 -0.321 ± 0.088 -0.391 ± 0.110 -1.562 ± 0.280 +#> [97] -0.433 ± 0.102 -1.052 ± 0.372 -0.693 ± 0.139 -0.393 ± 0.098 #> posterior::as_draws_list(fit) #> # A draws_list: 1000 iterations, 4 chains, and 105 variables #> #> [chain = 1] #> $lp__ -#> [1] -66 -66 -65 -65 -67 -65 -64 -64 -66 -64 +#> [1] -66 -65 -66 -65 -67 -65 -65 -64 -67 -69 #> #> $alpha -#> [1] 0.37 0.34 0.58 0.20 0.24 0.48 0.36 0.35 0.21 0.35 +#> [1] 0.373 0.355 0.389 0.419 0.300 0.178 0.629 0.288 0.016 -0.075 #> #> $`beta[1]` -#> [1] -0.54 -0.98 -0.77 -0.41 -0.26 -0.80 -0.62 -0.74 -0.55 -0.51 +#> [1] -0.21 -0.42 -1.01 -0.95 -0.16 -0.51 -0.69 -0.67 -0.74 -0.71 #> #> $`beta[2]` -#> [1] -0.738 -0.545 -0.168 -0.281 0.054 -0.315 -0.204 -0.103 0.067 -0.446 +#> [1] -0.3841 -0.2843 -0.0223 0.0016 -0.5327 -0.3040 -0.3392 -0.2792 0.0976 +#> [10] 0.1916 #> #> #> [chain = 2] #> $lp__ -#> [1] -65 -65 -66 -65 -66 -68 -66 -68 -65 -65 +#> [1] -67 -65 -66 -65 -67 -67 -67 -64 -68 -66 #> #> $alpha -#> [1] 0.211 0.348 0.245 0.371 0.366 0.050 0.135 -0.055 0.417 0.247 +#> [1] 0.0232 0.1359 0.0021 0.2264 0.6367 0.7152 0.4707 0.3940 0.0634 0.4544 #> #> $`beta[1]` -#> [1] -0.69 -0.91 -0.30 -0.66 -0.78 -0.47 -0.86 -0.85 -0.32 -0.72 +#> [1] -0.70 -0.81 -0.83 -0.45 -0.96 -0.34 -1.17 -0.57 -0.62 -0.62 #> #> $`beta[2]` -#> [1] -0.427 -0.076 -0.494 -0.029 -0.460 0.199 -0.272 0.096 -0.201 -0.249 +#> [1] 0.08 -0.27 -0.13 -0.28 -0.66 -0.57 -0.21 -0.20 -0.47 -0.67 #> #> # ... with 990 more iterations, and 2 more chains, and 101 more variables # } diff --git a/docs/reference/as_mcmc.list.html b/docs/reference/as_mcmc.list.html index 62098ec93..f0a3fe218 100644 --- a/docs/reference/as_mcmc.list.html +++ b/docs/reference/as_mcmc.list.html @@ -23,7 +23,7 @@ cmdstanr - 0.6.0 + 0.7.0 diff --git a/docs/reference/cmdstan_default_install_path.html b/docs/reference/cmdstan_default_install_path.html index 45d262399..e2ca8ac80 100644 --- a/docs/reference/cmdstan_default_install_path.html +++ b/docs/reference/cmdstan_default_install_path.html @@ -17,7 +17,7 @@ cmdstanr - 0.6.0 + 0.7.0 diff --git a/docs/reference/cmdstan_default_path.html b/docs/reference/cmdstan_default_path.html index 624f12f6f..01f43b73f 100644 --- a/docs/reference/cmdstan_default_path.html +++ b/docs/reference/cmdstan_default_path.html @@ -18,7 +18,7 @@ cmdstanr - 0.6.0 + 0.7.0 diff --git a/docs/reference/cmdstan_model-1.png b/docs/reference/cmdstan_model-1.png index 27fd88217..0aa41c0e9 100644 Binary files a/docs/reference/cmdstan_model-1.png and b/docs/reference/cmdstan_model-1.png differ diff --git a/docs/reference/cmdstan_model-2.png b/docs/reference/cmdstan_model-2.png index 4ed9fe5c5..835a8a667 100644 Binary files a/docs/reference/cmdstan_model-2.png and b/docs/reference/cmdstan_model-2.png differ diff --git a/docs/reference/cmdstan_model-3.png b/docs/reference/cmdstan_model-3.png index e985d6949..8d58780fd 100644 Binary files a/docs/reference/cmdstan_model-3.png and b/docs/reference/cmdstan_model-3.png differ diff --git a/docs/reference/cmdstan_model.html b/docs/reference/cmdstan_model.html index e466aec34..a94e571ba 100644 --- a/docs/reference/cmdstan_model.html +++ b/docs/reference/cmdstan_model.html @@ -23,7 +23,7 @@ cmdstanr - 0.6.0 + 0.7.0 @@ -185,7 +185,7 @@

    Examples

    # Otherwise use the `path` argument to specify the location of your # CmdStan installation.) set_cmdstan_path(path = NULL) -#> CmdStan path set to: /Users/jgabry/.cmdstan/cmdstan-2.32.2 +#> CmdStan path set to: /Users/jgabry/.cmdstan/cmdstan-2.33.1 # Create a CmdStanModel object from a Stan program, # here using the example model that comes with CmdStan @@ -272,10 +272,22 @@

    Examples

    fit_mcmc$summary() #> # A tibble: 2 × 10 #> variable mean median sd mad q5 q95 rhat ess_bulk ess_tail -#> <chr> <num> <num> <num> <num> <num> <num> <num> <num> <num> +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 lp__ -7.30 -7.03 0.721 0.380 -8.82 -6.75 1.00 902. 1006. #> 2 theta 0.247 0.233 0.122 0.129 0.0786 0.470 1.00 762. 712. +# Check sampling diagnostics +fit_mcmc$diagnostic_summary() +#> $num_divergent +#> [1] 0 0 +#> +#> $num_max_treedepth +#> [1] 0 0 +#> +#> $ebfmi +#> [1] 1.017555 1.250490 +#> + # Get posterior draws draws <- fit_mcmc$draws() print(draws) @@ -324,51 +336,8 @@

    Examples

    #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. -# Call CmdStan's diagnose and stansummary utilities -fit_mcmc$cmdstan_diagnose() -#> Processing csv files: /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/bernoulli-202307251434-1-79f79a.csv, /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/bernoulli-202307251434-2-79f79a.csv -#> -#> Checking sampler transitions treedepth. -#> Treedepth satisfactory for all transitions. -#> -#> Checking sampler transitions for divergences. -#> No divergent transitions found. -#> -#> Checking E-BFMI - sampler transitions HMC potential energy. -#> E-BFMI satisfactory. -#> -#> Effective sample size satisfactory. -#> -#> Split R-hat values satisfactory all parameters. -#> -#> Processing complete, no problems detected. -fit_mcmc$cmdstan_summary() -#> Inference for Stan model: bernoulli_model -#> 2 chains: each with iter=(1000,1000); warmup=(0,0); thin=(1,1); 2000 iterations saved. -#> -#> Warmup took (0.0040, 0.0030) seconds, 0.0070 seconds total -#> Sampling took (0.011, 0.011) seconds, 0.022 seconds total -#> -#> Mean MCSE StdDev 5% 50% 95% N_Eff N_Eff/s R_hat -#> -#> lp__ -7.3 2.6e-02 0.72 -8.8 -7.0 -6.8 781 35502 1.0 -#> accept_stat__ 0.92 8.3e-03 0.13 0.64 0.97 1.0 235 10662 1.0e+00 -#> stepsize__ 0.95 7.9e-02 0.079 0.87 1.0 1.0 1.0 46 2.0e+13 -#> treedepth__ 1.4 1.1e-02 0.48 1.0 1.0 2.0 1874 85179 1.0e+00 -#> n_leapfrog__ 2.5 1.4e-01 1.3 1.0 3.0 3.0 89 4050 1.0e+00 -#> divergent__ 0.00 nan 0.00 0.00 0.00 0.00 nan nan nan -#> energy__ 7.8 3.6e-02 1.00 6.8 7.5 9.6 775 35215 1.0e+00 -#> -#> theta 0.25 4.3e-03 0.12 0.079 0.23 0.47 796 36197 1.0 -#> -#> Samples were drawn using hmc with nuts. -#> For each parameter, N_Eff is a crude measure of effective sample size, -#> and R_hat is the potential scale reduction factor on split chains (at -#> convergence, R_hat=1). - # For models fit using MCMC, if you like working with RStan's stanfit objects # then you can create one with rstan::read_stan_csv() - # stanfit <- rstan::read_stan_csv(fit_mcmc$output_files()) @@ -382,24 +351,64 @@

    Examples

    #> Optimization terminated normally: #> Convergence detected: relative gradient magnitude is below tolerance #> Finished in 0.1 seconds. - fit_optim$summary() #> # A tibble: 2 × 2 #> variable estimate -#> <chr> <num> +#> <chr> <dbl> #> 1 lp__ -5.00 #> 2 theta 0.2 +# Run 'optimize' again with 'jacobian=TRUE' and then draw from Laplace approximation +# to the posterior +fit_optim <- mod$optimize(data = my_data_file, jacobian = TRUE) +#> Initial log joint probability = -17.5452 +#> Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes +#> 5 -6.74802 0.00115356 2.90728e-05 1 1 8 +#> Optimization terminated normally: +#> Convergence detected: relative gradient magnitude is below tolerance +#> Finished in 0.1 seconds. +fit_laplace <- mod$laplace(data = my_data_file, mode = fit_optim, draws = 2000) +#> Calculating Hessian +#> Calculating inverse of Cholesky factor +#> Generating draws +#> iteration: 0 +#> iteration: 100 +#> iteration: 200 +#> iteration: 300 +#> iteration: 400 +#> iteration: 500 +#> iteration: 600 +#> iteration: 700 +#> iteration: 800 +#> iteration: 900 +#> iteration: 1000 +#> iteration: 1100 +#> iteration: 1200 +#> iteration: 1300 +#> iteration: 1400 +#> iteration: 1500 +#> iteration: 1600 +#> iteration: 1700 +#> iteration: 1800 +#> iteration: 1900 +#> Finished in 0.1 seconds. +fit_laplace$summary() +#> # A tibble: 3 × 7 +#> variable mean median sd mad q5 q95 +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> +#> 1 lp__ -7.27 -7.00 0.705 0.338 -8.72 -6.75 +#> 2 lp_approx__ -0.527 -0.251 0.714 0.340 -2.02 -0.00142 +#> 3 theta 0.270 0.251 0.126 0.125 0.0959 0.509 -# Run 'variational' method to approximate the posterior (default is meanfield ADVI) +# Run 'variational' method to use ADVI to approximate posterior fit_vb <- mod$variational(data = stan_data, seed = 123) #> ------------------------------------------------------------ #> EXPERIMENTAL ALGORITHM: #> This procedure has not been thoroughly tested and may be unstable #> or buggy. The interface is subject to change. #> ------------------------------------------------------------ -#> Gradient evaluation took 6e-06 seconds -#> 1000 transitions using 10 leapfrog steps per transition would take 0.06 seconds. +#> Gradient evaluation took 5e-06 seconds +#> 1000 transitions using 10 leapfrog steps per transition would take 0.05 seconds. #> Adjust your expectations accordingly! #> Begin eta adaptation. #> Iteration: 1 / 250 [ 0%] (Adaptation) @@ -416,20 +425,94 @@

    Examples

    #> Drawing a sample of size 1000 from the approximate posterior... #> COMPLETED. #> Finished in 0.1 seconds. - fit_vb$summary() #> # A tibble: 3 × 7 #> variable mean median sd mad q5 q95 -#> <chr> <num> <num> <num> <num> <num> <num> +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 lp__ -7.18 -6.94 0.588 0.259 -8.36 -6.75 #> 2 lp_approx__ -0.515 -0.221 0.692 0.303 -2.06 -0.00257 #> 3 theta 0.263 0.246 0.115 0.113 0.106 0.481 - -# Plot approximate posterior using bayesplot mcmc_hist(fit_vb$draws("theta")) #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. +# Run 'pathfinder' method, a new alternative to the variational method +fit_pf <- mod$pathfinder(data = stan_data, seed = 123) +#> Path [1] :Initial log joint density = -11.008832 +#> Path [1] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 9.383e-04 1.391e-05 1.000e+00 1.000e+00 126 -6.264e+00 -6.264e+00 +#> Path [1] :Best Iter: [3] ELBO (-6.195408) evaluations: (126) +#> Path [2] :Initial log joint density = -7.318450 +#> Path [2] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 4 -6.748e+00 5.414e-03 1.618e-04 1.000e+00 1.000e+00 101 -6.251e+00 -6.251e+00 +#> Path [2] :Best Iter: [3] ELBO (-6.229174) evaluations: (101) +#> Path [3] :Initial log joint density = -12.374612 +#> Path [3] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.419e-03 2.837e-05 1.000e+00 1.000e+00 126 -6.199e+00 -6.199e+00 +#> Path [3] :Best Iter: [5] ELBO (-6.199185) evaluations: (126) +#> Path [4] :Initial log joint density = -13.009824 +#> Path [4] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.677e-03 3.885e-05 1.000e+00 1.000e+00 126 -6.173e+00 -6.173e+00 +#> Path [4] :Best Iter: [5] ELBO (-6.172860) evaluations: (126) +#> Total log probability function evaluations:4379 +#> Finished in 0.1 seconds. +fit_pf$summary() +#> # A tibble: 3 × 7 +#> variable mean median sd mad q5 q95 +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> +#> 1 lp_approx__ -1.08 -0.728 0.886 0.304 -2.71 -0.511 +#> 2 lp__ -7.26 -6.96 0.738 0.297 -8.72 -6.75 +#> 3 theta 0.249 0.230 0.120 0.121 0.0854 0.471 +mcmc_hist(fit_pf$draws("theta")) +#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. + + +# Run 'pathfinder' again with more paths, fewer draws per path, +# better covariance approximation, and fewer LBFGSs iterations +fit_pf <- mod$pathfinder(data = stan_data, num_paths=10, single_path_draws=40, + history_size=50, max_lbfgs_iters=100) +#> Path [1] :Initial log joint density = -11.515672 +#> Path [1] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.064e-03 1.692e-05 1.000e+00 1.000e+00 126 -6.254e+00 -6.254e+00 +#> Path [1] :Best Iter: [3] ELBO (-6.213658) evaluations: (126) +#> Path [2] :Initial log joint density = -9.189304 +#> Path [2] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 3.749e-04 3.327e-06 1.000e+00 1.000e+00 126 -6.289e+00 -6.289e+00 +#> Path [2] :Best Iter: [4] ELBO (-6.214534) evaluations: (126) +#> Path [3] :Initial log joint density = -13.270061 +#> Path [3] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.767e-03 4.305e-05 1.000e+00 1.000e+00 126 -6.276e+00 -6.276e+00 +#> Path [3] :Best Iter: [3] ELBO (-6.225363) evaluations: (126) +#> Path [4] :Initial log joint density = -6.748213 +#> Path [4] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 2 -6.748e+00 1.004e-02 7.315e-05 1.000e+00 1.000e+00 51 -6.200e+00 -6.200e+00 +#> Path [4] :Best Iter: [2] ELBO (-6.200114) evaluations: (51) +#> Path [5] :Initial log joint density = -7.368682 +#> Path [5] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 5.377e-04 9.870e-07 1.000e+00 1.000e+00 126 -6.276e+00 -6.276e+00 +#> Path [5] :Best Iter: [3] ELBO (-6.167297) evaluations: (126) +#> Path [6] :Initial log joint density = -9.465368 +#> Path [6] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 4.613e-04 4.599e-06 1.000e+00 1.000e+00 126 -6.238e+00 -6.238e+00 +#> Path [6] :Best Iter: [3] ELBO (-6.185454) evaluations: (126) +#> Path [7] :Initial log joint density = -6.856712 +#> Path [7] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 4 -6.748e+00 1.028e-04 1.386e-05 9.452e-01 9.452e-01 101 -6.242e+00 -6.242e+00 +#> Path [7] :Best Iter: [3] ELBO (-6.159687) evaluations: (101) +#> Path [8] :Initial log joint density = -7.085030 +#> Path [8] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 4 -6.748e+00 3.058e-03 6.268e-05 1.000e+00 1.000e+00 101 -6.221e+00 -6.221e+00 +#> Path [8] :Best Iter: [4] ELBO (-6.221460) evaluations: (101) +#> Path [9] :Initial log joint density = -7.443992 +#> Path [9] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 9.951e-05 3.747e-07 1.000e+00 1.000e+00 126 -6.228e+00 -6.228e+00 +#> Path [9] :Best Iter: [2] ELBO (-6.206717) evaluations: (126) +#> Path [10] :Initial log joint density = -6.751684 +#> Path [10] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 3 -6.748e+00 5.950e-04 1.461e-05 1.000e+00 1.000e+00 76 -6.209e+00 -6.209e+00 +#> Path [10] :Best Iter: [3] ELBO (-6.209430) evaluations: (76) +#> Total log probability function evaluations:1235 +#> Finished in 0.1 seconds. # Specifying initial values as a function fit_mcmc_w_init_fun <- mod$sample( diff --git a/docs/reference/cmdstanr-package-3.png b/docs/reference/cmdstanr-package-3.png index e985d6949..8d58780fd 100644 Binary files a/docs/reference/cmdstanr-package-3.png and b/docs/reference/cmdstanr-package-3.png differ diff --git a/docs/reference/cmdstanr-package.html b/docs/reference/cmdstanr-package.html index 5cd82a34f..072311bb7 100644 --- a/docs/reference/cmdstanr-package.html +++ b/docs/reference/cmdstanr-package.html @@ -19,7 +19,7 @@ cmdstanr - 0.6.0 + 0.7.0 @@ -134,9 +134,11 @@

    Different ways of interfac

    Advantages of RStan

    @@ -189,13 +191,12 @@

    Examples

    # Otherwise use the `path` argument to specify the location of your # CmdStan installation.) set_cmdstan_path(path = NULL) -#> CmdStan path set to: /Users/jgabry/.cmdstan/cmdstan-2.32.2 +#> CmdStan path set to: /Users/jgabry/.cmdstan/cmdstan-2.33.1 # Create a CmdStanModel object from a Stan program, # here using the example model that comes with CmdStan file <- file.path(cmdstan_path(), "examples/bernoulli/bernoulli.stan") mod <- cmdstan_model(file) -#> Model executable is up to date! mod$print() #> data { #> int<lower=0> N; @@ -277,10 +278,22 @@

    Examples

    fit_mcmc$summary() #> # A tibble: 2 × 10 #> variable mean median sd mad q5 q95 rhat ess_bulk ess_tail -#> <chr> <num> <num> <num> <num> <num> <num> <num> <num> <num> +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 lp__ -7.30 -7.03 0.721 0.380 -8.82 -6.75 1.00 902. 1006. #> 2 theta 0.247 0.233 0.122 0.129 0.0786 0.470 1.00 762. 712. +# Check sampling diagnostics +fit_mcmc$diagnostic_summary() +#> $num_divergent +#> [1] 0 0 +#> +#> $num_max_treedepth +#> [1] 0 0 +#> +#> $ebfmi +#> [1] 1.017555 1.250490 +#> + # Get posterior draws draws <- fit_mcmc$draws() print(draws) @@ -329,51 +342,8 @@

    Examples

    #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. -# Call CmdStan's diagnose and stansummary utilities -fit_mcmc$cmdstan_diagnose() -#> Processing csv files: /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmp9Z3PNu/bernoulli-202307271729-1-67e85d.csv, /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmp9Z3PNu/bernoulli-202307271729-2-67e85d.csv -#> -#> Checking sampler transitions treedepth. -#> Treedepth satisfactory for all transitions. -#> -#> Checking sampler transitions for divergences. -#> No divergent transitions found. -#> -#> Checking E-BFMI - sampler transitions HMC potential energy. -#> E-BFMI satisfactory. -#> -#> Effective sample size satisfactory. -#> -#> Split R-hat values satisfactory all parameters. -#> -#> Processing complete, no problems detected. -fit_mcmc$cmdstan_summary() -#> Inference for Stan model: bernoulli_model -#> 2 chains: each with iter=(1000,1000); warmup=(0,0); thin=(1,1); 2000 iterations saved. -#> -#> Warmup took (0.0040, 0.0030) seconds, 0.0070 seconds total -#> Sampling took (0.011, 0.011) seconds, 0.022 seconds total -#> -#> Mean MCSE StdDev 5% 50% 95% N_Eff N_Eff/s R_hat -#> -#> lp__ -7.3 2.6e-02 0.72 -8.8 -7.0 -6.8 781 35502 1.0 -#> accept_stat__ 0.92 8.3e-03 0.13 0.64 0.97 1.0 235 10662 1.0e+00 -#> stepsize__ 0.95 7.9e-02 0.079 0.87 1.0 1.0 1.0 46 2.0e+13 -#> treedepth__ 1.4 1.1e-02 0.48 1.0 1.0 2.0 1874 85179 1.0e+00 -#> n_leapfrog__ 2.5 1.4e-01 1.3 1.0 3.0 3.0 89 4050 1.0e+00 -#> divergent__ 0.00 nan 0.00 0.00 0.00 0.00 nan nan nan -#> energy__ 7.8 3.6e-02 1.00 6.8 7.5 9.6 775 35215 1.0e+00 -#> -#> theta 0.25 4.3e-03 0.12 0.079 0.23 0.47 796 36197 1.0 -#> -#> Samples were drawn using hmc with nuts. -#> For each parameter, N_Eff is a crude measure of effective sample size, -#> and R_hat is the potential scale reduction factor on split chains (at -#> convergence, R_hat=1). - # For models fit using MCMC, if you like working with RStan's stanfit objects # then you can create one with rstan::read_stan_csv() - # stanfit <- rstan::read_stan_csv(fit_mcmc$output_files()) @@ -387,24 +357,64 @@

    Examples

    #> Optimization terminated normally: #> Convergence detected: relative gradient magnitude is below tolerance #> Finished in 0.1 seconds. - fit_optim$summary() #> # A tibble: 2 × 2 #> variable estimate -#> <chr> <num> +#> <chr> <dbl> #> 1 lp__ -5.00 #> 2 theta 0.2 +# Run 'optimize' again with 'jacobian=TRUE' and then draw from Laplace approximation +# to the posterior +fit_optim <- mod$optimize(data = my_data_file, jacobian = TRUE) +#> Initial log joint probability = -18.9282 +#> Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes +#> 5 -6.74802 0.00032939 4.62604e-06 1 1 8 +#> Optimization terminated normally: +#> Convergence detected: relative gradient magnitude is below tolerance +#> Finished in 0.1 seconds. +fit_laplace <- mod$laplace(data = my_data_file, mode = fit_optim, draws = 2000) +#> Calculating Hessian +#> Calculating inverse of Cholesky factor +#> Generating draws +#> iteration: 0 +#> iteration: 100 +#> iteration: 200 +#> iteration: 300 +#> iteration: 400 +#> iteration: 500 +#> iteration: 600 +#> iteration: 700 +#> iteration: 800 +#> iteration: 900 +#> iteration: 1000 +#> iteration: 1100 +#> iteration: 1200 +#> iteration: 1300 +#> iteration: 1400 +#> iteration: 1500 +#> iteration: 1600 +#> iteration: 1700 +#> iteration: 1800 +#> iteration: 1900 +#> Finished in 0.1 seconds. +fit_laplace$summary() +#> # A tibble: 3 × 7 +#> variable mean median sd mad q5 q95 +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> +#> 1 lp__ -7.23 -6.97 0.683 0.304 -8.57 -6.75 +#> 2 lp_approx__ -0.496 -0.225 0.743 0.305 -1.91 -0.00217 +#> 3 theta 0.264 0.247 0.120 0.119 0.0967 0.492 -# Run 'variational' method to approximate the posterior (default is meanfield ADVI) +# Run 'variational' method to use ADVI to approximate posterior fit_vb <- mod$variational(data = stan_data, seed = 123) #> ------------------------------------------------------------ #> EXPERIMENTAL ALGORITHM: #> This procedure has not been thoroughly tested and may be unstable #> or buggy. The interface is subject to change. #> ------------------------------------------------------------ -#> Gradient evaluation took 5e-06 seconds -#> 1000 transitions using 10 leapfrog steps per transition would take 0.05 seconds. +#> Gradient evaluation took 6e-06 seconds +#> 1000 transitions using 10 leapfrog steps per transition would take 0.06 seconds. #> Adjust your expectations accordingly! #> Begin eta adaptation. #> Iteration: 1 / 250 [ 0%] (Adaptation) @@ -421,20 +431,94 @@

    Examples

    #> Drawing a sample of size 1000 from the approximate posterior... #> COMPLETED. #> Finished in 0.1 seconds. - fit_vb$summary() #> # A tibble: 3 × 7 #> variable mean median sd mad q5 q95 -#> <chr> <num> <num> <num> <num> <num> <num> +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 lp__ -7.18 -6.94 0.588 0.259 -8.36 -6.75 #> 2 lp_approx__ -0.515 -0.221 0.692 0.303 -2.06 -0.00257 #> 3 theta 0.263 0.246 0.115 0.113 0.106 0.481 - -# Plot approximate posterior using bayesplot mcmc_hist(fit_vb$draws("theta")) #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. +# Run 'pathfinder' method, a new alternative to the variational method +fit_pf <- mod$pathfinder(data = stan_data, seed = 123) +#> Path [1] :Initial log joint density = -11.008832 +#> Path [1] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 9.383e-04 1.391e-05 1.000e+00 1.000e+00 126 -6.264e+00 -6.264e+00 +#> Path [1] :Best Iter: [3] ELBO (-6.195408) evaluations: (126) +#> Path [2] :Initial log joint density = -7.318450 +#> Path [2] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 4 -6.748e+00 5.414e-03 1.618e-04 1.000e+00 1.000e+00 101 -6.251e+00 -6.251e+00 +#> Path [2] :Best Iter: [3] ELBO (-6.229174) evaluations: (101) +#> Path [3] :Initial log joint density = -12.374612 +#> Path [3] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.419e-03 2.837e-05 1.000e+00 1.000e+00 126 -6.199e+00 -6.199e+00 +#> Path [3] :Best Iter: [5] ELBO (-6.199185) evaluations: (126) +#> Path [4] :Initial log joint density = -13.009824 +#> Path [4] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.677e-03 3.885e-05 1.000e+00 1.000e+00 126 -6.173e+00 -6.173e+00 +#> Path [4] :Best Iter: [5] ELBO (-6.172860) evaluations: (126) +#> Total log probability function evaluations:4379 +#> Finished in 0.1 seconds. +fit_pf$summary() +#> # A tibble: 3 × 7 +#> variable mean median sd mad q5 q95 +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> +#> 1 lp_approx__ -1.08 -0.728 0.886 0.304 -2.71 -0.511 +#> 2 lp__ -7.26 -6.96 0.738 0.297 -8.72 -6.75 +#> 3 theta 0.249 0.230 0.120 0.121 0.0854 0.471 +mcmc_hist(fit_pf$draws("theta")) +#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. + + +# Run 'pathfinder' again with more paths, fewer draws per path, +# better covariance approximation, and fewer LBFGSs iterations +fit_pf <- mod$pathfinder(data = stan_data, num_paths=10, single_path_draws=40, + history_size=50, max_lbfgs_iters=100) +#> Path [1] :Initial log joint density = -12.637372 +#> Path [1] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.531e-03 3.268e-05 1.000e+00 1.000e+00 126 -6.214e+00 -6.214e+00 +#> Path [1] :Best Iter: [5] ELBO (-6.213940) evaluations: (126) +#> Path [2] :Initial log joint density = -11.196746 +#> Path [2] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 9.877e-04 1.507e-05 1.000e+00 1.000e+00 126 -6.283e+00 -6.283e+00 +#> Path [2] :Best Iter: [4] ELBO (-6.223987) evaluations: (126) +#> Path [3] :Initial log joint density = -10.337120 +#> Path [3] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 7.410e-04 9.634e-06 1.000e+00 1.000e+00 126 -6.236e+00 -6.236e+00 +#> Path [3] :Best Iter: [2] ELBO (-6.179583) evaluations: (126) +#> Path [4] :Initial log joint density = -12.005851 +#> Path [4] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.253e-03 2.257e-05 1.000e+00 1.000e+00 126 -6.159e+00 -6.159e+00 +#> Path [4] :Best Iter: [5] ELBO (-6.159209) evaluations: (126) +#> Path [5] :Initial log joint density = -11.377707 +#> Path [5] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.032e-03 1.614e-05 1.000e+00 1.000e+00 126 -6.206e+00 -6.206e+00 +#> Path [5] :Best Iter: [2] ELBO (-6.202552) evaluations: (126) +#> Path [6] :Initial log joint density = -7.049866 +#> Path [6] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 4 -6.748e+00 2.694e-03 5.075e-05 1.000e+00 1.000e+00 101 -6.203e+00 -6.203e+00 +#> Path [6] :Best Iter: [4] ELBO (-6.203218) evaluations: (101) +#> Path [7] :Initial log joint density = -9.219130 +#> Path [7] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 3.841e-04 3.455e-06 1.000e+00 1.000e+00 126 -6.185e+00 -6.185e+00 +#> Path [7] :Best Iter: [5] ELBO (-6.185156) evaluations: (126) +#> Path [8] :Initial log joint density = -17.192426 +#> Path [8] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.354e-03 3.651e-05 1.000e+00 1.000e+00 126 -6.204e+00 -6.204e+00 +#> Path [8] :Best Iter: [4] ELBO (-6.168586) evaluations: (126) +#> Path [9] :Initial log joint density = -9.491732 +#> Path [9] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 4.697e-04 4.731e-06 1.000e+00 1.000e+00 126 -6.247e+00 -6.247e+00 +#> Path [9] :Best Iter: [3] ELBO (-6.220590) evaluations: (126) +#> Path [10] :Initial log joint density = -6.750036 +#> Path [10] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 3 -6.748e+00 3.322e-04 6.069e-06 1.000e+00 1.000e+00 76 -6.214e+00 -6.214e+00 +#> Path [10] :Best Iter: [2] ELBO (-6.188430) evaluations: (76) +#> Total log probability function evaluations:1335 +#> Finished in 0.1 seconds. # Specifying initial values as a function fit_mcmc_w_init_fun <- mod$sample( diff --git a/docs/reference/cmdstanr_example.html b/docs/reference/cmdstanr_example.html index 8bc2b49bb..8d377b8f0 100644 --- a/docs/reference/cmdstanr_example.html +++ b/docs/reference/cmdstanr_example.html @@ -17,7 +17,7 @@ cmdstanr - 0.6.0 + 0.7.0
    @@ -110,7 +110,7 @@

    Fit models for use in examples

    cmdstanr_example(
       example = c("logistic", "schools", "schools_ncp"),
    -  method = c("sample", "optimize", "variational", "diagnose"),
    +  method = c("sample", "optimize", "laplace", "variational", "pathfinder", "diagnose"),
       ...,
       quiet = TRUE,
       force_recompile = getOption("cmdstanr_force_recompile", default = FALSE)
    @@ -149,8 +149,7 @@ 

    Arguments

    force_recompile
    -

    Passed to the model-method-compile -method.

    +

    Passed to the $compile() method.

    @@ -186,26 +185,24 @@

    Examples

    #> } #> } fit_logistic_mcmc <- cmdstanr_example("logistic", chains = 2) -#> Model executable is up to date! fit_logistic_mcmc$summary() #> # A tibble: 105 × 10 -#> variable mean median sd mad q5 q95 rhat ess_bulk -#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> -#> 1 lp__ -66.0 -65.7 1.46 1.25 -68.9 -64.3 1.00 786. -#> 2 alpha 0.376 0.373 0.215 0.210 0.0346 0.747 1.00 1698. -#> 3 beta[1] -0.673 -0.671 0.261 0.249 -1.11 -0.251 1.00 1889. -#> 4 beta[2] -0.271 -0.267 0.228 0.239 -0.656 0.0921 1.00 2052. -#> 5 beta[3] 0.677 0.668 0.264 0.264 0.261 1.13 1.00 2070. -#> 6 log_lik[1] -0.515 -0.510 0.0982 0.0971 -0.685 -0.366 1.00 1843. -#> 7 log_lik[2] -0.402 -0.381 0.147 0.141 -0.670 -0.196 1.00 1893. -#> 8 log_lik[3] -0.496 -0.456 0.221 0.205 -0.905 -0.212 1.00 1969. -#> 9 log_lik[4] -0.449 -0.434 0.148 0.153 -0.721 -0.233 1.00 1958. -#> 10 log_lik[5] -1.18 -1.16 0.273 0.267 -1.65 -0.760 1.00 1991. +#> variable mean median sd mad q5 q95 rhat ess_bulk +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> +#> 1 lp__ -66.0 -65.6 1.45 1.20 -68.7 -64.3 1.00 934. +#> 2 alpha 0.384 0.386 0.218 0.218 0.0341 0.748 1.00 1959. +#> 3 beta[1] -0.667 -0.661 0.247 0.248 -1.08 -0.273 1.00 1840. +#> 4 beta[2] -0.275 -0.273 0.231 0.231 -0.662 0.114 1.00 1954. +#> 5 beta[3] 0.687 0.685 0.271 0.269 0.254 1.13 1.00 2212. +#> 6 log_lik[1] -0.515 -0.509 0.101 0.0967 -0.698 -0.360 1.00 2038. +#> 7 log_lik[2] -0.402 -0.380 0.150 0.140 -0.673 -0.195 1.00 2565. +#> 8 log_lik[3] -0.498 -0.467 0.223 0.212 -0.922 -0.201 1.00 2174. +#> 9 log_lik[4] -0.449 -0.430 0.154 0.148 -0.731 -0.232 0.999 1785. +#> 10 log_lik[5] -1.19 -1.17 0.283 0.281 -1.69 -0.772 1.00 2119. #> # ℹ 95 more rows #> # ℹ 1 more variable: ess_tail <dbl> fit_logistic_optim <- cmdstanr_example("logistic", method = "optimize") -#> Model executable is up to date! fit_logistic_optim$summary() #> # A tibble: 105 × 2 #> variable estimate @@ -223,21 +220,20 @@

    Examples

    #> # ℹ 95 more rows fit_logistic_vb <- cmdstanr_example("logistic", method = "variational") -#> Model executable is up to date! fit_logistic_vb$summary() #> # A tibble: 106 × 7 -#> variable mean median sd mad q5 q95 -#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> -#> 1 lp__ -66.0 -65.6 1.51 1.24 -69.1 -64.3 -#> 2 lp_approx__ -2.01 -1.67 1.46 1.19 -4.72 -0.370 -#> 3 alpha 0.343 0.336 0.198 0.203 0.0276 0.671 -#> 4 beta[1] -0.664 -0.666 0.268 0.258 -1.08 -0.215 -#> 5 beta[2] -0.285 -0.281 0.193 0.194 -0.607 0.0295 -#> 6 beta[3] 0.720 0.706 0.285 0.282 0.277 1.23 -#> 7 log_lik[1] -0.534 -0.529 0.0945 0.0944 -0.703 -0.391 -#> 8 log_lik[2] -0.378 -0.365 0.147 0.143 -0.632 -0.172 -#> 9 log_lik[3] -0.508 -0.477 0.200 0.201 -0.879 -0.237 -#> 10 log_lik[4] -0.418 -0.405 0.131 0.132 -0.652 -0.232 +#> variable mean median sd mad q5 q95 +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> +#> 1 lp__ -66.5 -66.0 1.82 1.62 -69.9 -64.3 +#> 2 lp_approx__ -1.96 -1.62 1.41 1.19 -4.62 -0.318 +#> 3 alpha 0.503 0.492 0.219 0.222 0.144 0.876 +#> 4 beta[1] -0.582 -0.568 0.301 0.288 -1.10 -0.0916 +#> 5 beta[2] -0.278 -0.275 0.185 0.183 -0.592 0.0118 +#> 6 beta[3] 0.688 0.696 0.301 0.307 0.211 1.18 +#> 7 log_lik[1] -0.480 -0.475 0.0981 0.0994 -0.648 -0.332 +#> 8 log_lik[2] -0.466 -0.443 0.190 0.185 -0.836 -0.197 +#> 9 log_lik[3] -0.470 -0.443 0.195 0.188 -0.838 -0.209 +#> 10 log_lik[4] -0.495 -0.480 0.152 0.148 -0.776 -0.277 #> # ℹ 96 more rows print_example_program("schools") @@ -258,24 +254,23 @@

    Examples

    #> target += normal_lpdf(y | theta, sigma); #> } fit_schools_mcmc <- cmdstanr_example("schools") -#> Model executable is up to date! -#> Warning: 211 of 4000 (5.0%) transitions ended with a divergence. +#> Warning: 146 of 4000 (4.0%) transitions ended with a divergence. #> See https://mc-stan.org/misc/warnings for details. fit_schools_mcmc$summary() #> # A tibble: 11 × 10 #> variable mean median sd mad q5 q95 rhat ess_bulk ess_tail #> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> -#> 1 lp__ -57.8 -58.4 5.90 5.92 -66.5 -46.9 1.03 134. 91.1 -#> 2 mu 6.59 6.58 4.19 4.02 -0.239 13.4 1.01 605. 759. -#> 3 tau 5.32 4.54 3.72 3.56 0.991 12.7 1.02 129. 68.3 -#> 4 theta[1] 9.40 8.66 6.82 5.74 -0.443 21.5 1.01 543. 1393. -#> 5 theta[2] 6.95 6.63 5.60 5.01 -2.06 15.9 1.01 912. 1777. -#> 6 theta[3] 5.44 5.77 6.76 5.78 -5.92 15.4 1.01 1075. 2014. -#> 7 theta[4] 6.74 6.67 5.84 5.21 -3.08 16.3 1.01 1107. 1798. -#> 8 theta[5] 4.67 4.90 5.73 5.26 -5.35 13.2 1.02 853. 1100. -#> 9 theta[6] 5.53 5.51 6.11 5.41 -5.04 15.0 1.01 1068. 1575. -#> 10 theta[7] 9.19 8.53 6.04 5.70 0.489 20.1 1.01 474. 1994. -#> 11 theta[8] 7.09 7.01 6.55 5.52 -3.34 17.8 1.02 820. 1987. +#> 1 lp__ -58.4 -58.5 5.06 5.42 -66.4 -50.2 1.02 139. 71.0 +#> 2 mu 6.36 6.39 4.24 4.27 -0.684 13.0 1.01 487. 345. +#> 3 tau 5.42 4.61 3.53 3.33 1.25 12.3 1.02 136. 67.5 +#> 4 theta[1] 9.11 8.41 7.20 6.29 -1.35 21.8 1.01 774. 601. +#> 5 theta[2] 6.65 6.69 5.88 5.59 -2.86 16.2 1.01 1011. 2093. +#> 6 theta[3] 5.38 5.61 6.74 6.18 -5.83 15.9 1.01 863. 1953. +#> 7 theta[4] 6.59 6.59 6.01 5.73 -2.94 16.6 1.01 946. 2109. +#> 8 theta[5] 4.37 4.72 5.66 5.51 -5.63 12.8 1.01 745. 2276. +#> 9 theta[6] 5.28 5.49 5.96 5.60 -4.99 14.6 1.01 879. 1751. +#> 10 theta[7] 9.06 8.65 6.17 5.72 -0.151 20.1 1.01 678. 1072. +#> 11 theta[8] 6.91 6.84 6.86 6.09 -4.10 17.8 1.01 939. 2286. print_example_program("schools_ncp") #> data { @@ -298,55 +293,41 @@

    Examples

    #> target += normal_lpdf(y | theta, sigma); #> } fit_schools_ncp_mcmc <- cmdstanr_example("schools_ncp") -#> Model executable is up to date! fit_schools_ncp_mcmc$summary() #> # A tibble: 19 × 10 #> variable mean median sd mad q5 q95 rhat ess_bulk ess_tail #> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> -#> 1 lp__ -46.9 -4.66e+1 2.45 2.38 -51.4 -43.4 1.00 1684. 2222. -#> 2 mu 6.53 6.60e+0 4.23 4.29 -0.526 13.3 1.00 2821. 2399. -#> 3 tau 4.78 4.00e+0 3.65 3.45 0.323 12.0 1.00 2114. 2038. -#> 4 theta_r… 0.379 4.09e-1 0.987 0.963 -1.27 1.95 1.00 3181. 2648. -#> 5 theta_r… 0.0512 7.13e-2 0.909 0.893 -1.45 1.56 1.00 3571. 2470. -#> 6 theta_r… -0.155 -1.61e-1 0.953 0.950 -1.71 1.40 1.00 3651. 2384. -#> 7 theta_r… 0.0143 9.01e-3 0.888 0.872 -1.39 1.49 1.00 3590. 2857. -#> 8 theta_r… -0.266 -2.77e-1 0.898 0.888 -1.71 1.23 1.00 3506. 2698. -#> 9 theta_r… -0.144 -1.44e-1 0.924 0.895 -1.66 1.36 1.00 3308. 2961. -#> 10 theta_r… 0.361 3.56e-1 0.954 0.946 -1.25 1.89 1.00 3656. 2540. -#> 11 theta_r… 0.0480 3.80e-2 0.959 0.953 -1.52 1.68 1.00 4024. 2776. -#> 12 theta[1] 9.07 8.48e+0 6.90 5.95 -0.696 21.7 1.00 3565. 2983. -#> 13 theta[2] 6.86 6.81e+0 5.56 5.13 -2.16 15.9 1.00 4325. 2944. -#> 14 theta[3] 5.45 5.83e+0 6.39 5.75 -5.69 15.0 1.00 3605. 3177. -#> 15 theta[4] 6.67 6.67e+0 5.72 5.16 -2.73 16.2 0.999 4154. 3160. -#> 16 theta[5] 4.93 5.18e+0 5.62 5.36 -4.69 13.4 1.00 3410. 2956. -#> 17 theta[6] 5.62 5.85e+0 5.82 5.33 -4.24 14.9 1.00 3805. 3138. -#> 18 theta[7] 8.96 8.44e+0 6.20 5.60 -0.266 20.2 1.00 3687. 3063. -#> 19 theta[8] 6.92 6.75e+0 6.59 5.75 -3.69 17.7 1.00 3285. 3072. +#> 1 lp__ -46.9 -4.66e+1 2.49 2.36 -51.6 -43.6 1.00 1229. 1844. +#> 2 mu 6.43 6.46e+0 4.08 3.93 -0.238 13.1 1.00 2985. 2197. +#> 3 tau 4.74 4.03e+0 3.70 3.41 0.334 11.9 1.00 1724. 1496. +#> 4 theta_r… 0.351 3.53e-1 0.979 0.955 -1.26 1.96 1.00 3404. 2385. +#> 5 theta_r… 0.0484 6.67e-2 0.924 0.913 -1.49 1.53 1.00 3680. 2435. +#> 6 theta_r… -0.131 -1.50e-1 0.962 0.938 -1.71 1.50 1.00 3067. 2557. +#> 7 theta_r… 0.0221 5.18e-3 0.919 0.905 -1.49 1.59 1.00 3748. 2583. +#> 8 theta_r… -0.273 -3.14e-1 0.931 0.892 -1.77 1.34 1.00 4139. 2891. +#> 9 theta_r… -0.157 -1.57e-1 0.930 0.929 -1.69 1.36 1.00 3560. 2797. +#> 10 theta_r… 0.363 4.01e-1 0.924 0.911 -1.15 1.87 1.00 3687. 2941. +#> 11 theta_r… 0.0558 7.25e-2 0.951 0.924 -1.55 1.65 1.00 4077. 2555. +#> 12 theta[1] 8.85 8.09e+0 6.85 5.77 -0.638 21.2 1.00 2994. 2531. +#> 13 theta[2] 6.79 6.75e+0 5.59 5.01 -2.46 15.9 1.00 4593. 3367. +#> 14 theta[3] 5.40 5.88e+0 6.64 5.63 -5.94 15.5 1.00 3448. 3181. +#> 15 theta[4] 6.65 6.62e+0 5.69 5.09 -2.25 16.0 1.00 4537. 3155. +#> 16 theta[5] 4.78 5.06e+0 5.54 5.26 -4.69 13.2 1.00 3996. 3191. +#> 17 theta[6] 5.44 5.63e+0 5.97 5.29 -4.66 14.8 1.00 3934. 2877. +#> 18 theta[7] 8.83 8.37e+0 5.98 5.56 0.141 19.4 1.00 4104. 3336. +#> 19 theta[8] 6.89 6.73e+0 6.34 5.33 -3.03 17.1 1.00 3988. 3025. # optimization fails for hierarchical model cmdstanr_example("schools", "optimize", quiet = FALSE) -#> Model executable is up to date! -#> Initial log joint probability = -74.0754 +#> Initial log joint probability = -56.2449 #> Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes -#> 99 114.137 0.279807 1.66573e+09 0.5386 0.1135 171 +#> 99 121.706 0.109668 2.48002e+09 1 1 174 #> Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes -#> 189 249.295 0.0236622 1.57992e+16 1e-12 0.001 399 LS failed, Hessian reset -#> Optimization terminated with error: -#> Line search failed to achieve a sufficient decrease, no more progress can be made -#> Finished in 0.1 seconds. -#> variable estimate -#> lp__ 249.29 -#> mu 0.95 -#> tau 0.00 -#> theta[1] 0.95 -#> theta[2] 0.95 -#> theta[3] 0.95 -#> theta[4] 0.95 -#> theta[5] 0.95 -#> theta[6] 0.95 -#> theta[7] 0.95 -#> -#> # showing 10 of 11 rows (change via 'max_rows' argument or 'cmdstanr_max_rows' option) +#> 187 244.785 0.253817 8.00296 1e-12 0.001 400 LS failed, Hessian reset +#> Line search failed to achieve a sufficient decrease, no more progress can be made +#> Chain 1 Optimization terminated with error: +#> Warning: Fitting finished unexpectedly! Use the $output() method for more information. +#> Error: Fitting failed. Unable to print. # }
    diff --git a/docs/reference/draws_to_csv.html b/docs/reference/draws_to_csv.html index 08c28b5ef..355224d76 100644 --- a/docs/reference/draws_to_csv.html +++ b/docs/reference/draws_to_csv.html @@ -20,7 +20,7 @@ cmdstanr - 0.6.0 + 0.7.0
    @@ -169,10 +169,10 @@

    Examples

    draws_csv_files <- draws_to_csv(draws) print(draws_csv_files) -#> [1] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T//RtmpFBtN6X/fittedParams-202307251435-1-8744f9.csv" -#> [2] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T//RtmpFBtN6X/fittedParams-202307251435-2-8744f9.csv" -#> [3] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T//RtmpFBtN6X/fittedParams-202307251435-3-8744f9.csv" -#> [4] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T//RtmpFBtN6X/fittedParams-202307251435-4-8744f9.csv" +#> [1] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T//RtmpAMFHpW/fittedParams-202312131001-1-1dd4e6.csv" +#> [2] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T//RtmpAMFHpW/fittedParams-202312131001-2-1dd4e6.csv" +#> [3] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T//RtmpAMFHpW/fittedParams-202312131001-3-1dd4e6.csv" +#> [4] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T//RtmpAMFHpW/fittedParams-202312131001-4-1dd4e6.csv" # draws_csv_files <- draws_to_csv(draws, # sampler_diagnostic = sampler_diagnostics, diff --git a/docs/reference/eng_cmdstan.html b/docs/reference/eng_cmdstan.html index f39ce5cec..3430c9d75 100644 --- a/docs/reference/eng_cmdstan.html +++ b/docs/reference/eng_cmdstan.html @@ -22,7 +22,7 @@ cmdstanr - 0.6.0 + 0.7.0 diff --git a/docs/reference/fit-method-cmdstan_summary.html b/docs/reference/fit-method-cmdstan_summary.html index 7aaf67169..47d7f8f7f 100644 --- a/docs/reference/fit-method-cmdstan_summary.html +++ b/docs/reference/fit-method-cmdstan_summary.html @@ -28,7 +28,7 @@ cmdstanr - 0.6.0 + 0.7.0 @@ -149,7 +149,7 @@

    Examples

    # \dontrun{
     fit <- cmdstanr_example("logistic")
     fit$cmdstan_diagnose()
    -#> Processing csv files: /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/logistic-202307251435-1-7b6c9f.csv, /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/logistic-202307251435-2-7b6c9f.csv, /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/logistic-202307251435-3-7b6c9f.csv, /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/logistic-202307251435-4-7b6c9f.csv
    +#> Processing csv files: /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/logistic-202312131001-1-2718e9.csv, /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/logistic-202312131001-2-2718e9.csv, /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/logistic-202312131001-3-2718e9.csv, /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/logistic-202312131001-4-2718e9.csv
     #> 
     #> Checking sampler transitions treedepth.
     #> Treedepth satisfactory for all transitions.
    @@ -169,123 +169,123 @@ 

    Examples

    #> Inference for Stan model: logistic_model #> 4 chains: each with iter=(1000,1000,1000,1000); warmup=(0,0,0,0); thin=(1,1,1,1); 4000 iterations saved. #> -#> Warmup took (0.022, 0.022, 0.022, 0.021) seconds, 0.087 seconds total -#> Sampling took (0.069, 0.072, 0.068, 0.066) seconds, 0.28 seconds total +#> Warmup took (0.022, 0.023, 0.023, 0.023) seconds, 0.091 seconds total +#> Sampling took (0.071, 0.070, 0.070, 0.070) seconds, 0.28 seconds total #> #> Mean MCSE StdDev 5% 50% 95% N_Eff N_Eff/s R_hat #> -#> lp__ -6.6e+01 3.3e-02 1.4 -69 -6.6e+01 -6.4e+01 1875 6818 1.0 -#> accept_stat__ 0.91 1.3e-03 0.099 0.71 0.95 1.0 5487 19952 1.0e+00 -#> stepsize__ 0.75 2.8e-02 0.040 0.69 0.76 0.80 2.0 7.3 1.2e+13 -#> treedepth__ 2.4 9.4e-03 0.53 2.0 2.0 3.0 3168 11520 1.0e+00 -#> n_leapfrog__ 5.3 3.3e-02 2.0 3.0 7.0 7.0 3698 13446 1.0e+00 +#> lp__ -6.6e+01 3.1e-02 1.4 -69 -6.6e+01 -6.4e+01 2120 7545 1.0 +#> accept_stat__ 0.91 1.4e-03 0.10 0.69 0.95 1.0 5437 19350 1.0e+00 +#> stepsize__ 0.74 2.1e-02 0.030 0.70 0.75 0.78 2.0 7.1 1.1e+13 +#> treedepth__ 2.4 8.8e-03 0.53 2.0 2.0 3.0 3601 12815 1.0e+00 +#> n_leapfrog__ 5.3 3.3e-02 2.0 3.0 7.0 7.0 3773 13428 1.0e+00 #> divergent__ 0.00 nan 0.00 0.00 0.00 0.00 nan nan nan -#> energy__ 68 5.2e-02 2.0 65 68 72 1501 5457 1.0e+00 +#> energy__ 68 4.9e-02 2.0 65 68 72 1750 6227 1.0e+00 #> -#> alpha 3.8e-01 3.3e-03 0.22 0.031 3.8e-01 7.3e-01 4281 15567 1.00 -#> beta[1] -6.6e-01 3.9e-03 0.25 -1.1 -6.6e-01 -2.5e-01 4206 15294 1.00 -#> beta[2] -2.8e-01 3.7e-03 0.23 -0.65 -2.8e-01 1.0e-01 3812 13860 1.0 -#> beta[3] 6.8e-01 4.4e-03 0.27 0.25 6.8e-01 1.1e+00 3764 13688 1.00 -#> log_lik[1] -5.2e-01 1.5e-03 0.097 -0.69 -5.1e-01 -3.7e-01 4137 15045 1.0 -#> log_lik[2] -4.0e-01 2.4e-03 0.15 -0.69 -3.8e-01 -2.0e-01 4070 14798 1.00 -#> log_lik[3] -5.0e-01 3.5e-03 0.22 -0.90 -4.7e-01 -2.1e-01 3974 14450 1.0 -#> log_lik[4] -4.5e-01 2.4e-03 0.15 -0.72 -4.3e-01 -2.4e-01 3841 13967 1.00 -#> log_lik[5] -1.2e+00 4.3e-03 0.28 -1.7 -1.2e+00 -7.6e-01 4262 15497 1.00 -#> log_lik[6] -5.9e-01 2.8e-03 0.19 -0.94 -5.7e-01 -3.1e-01 4639 16869 1.00 -#> log_lik[7] -6.4e-01 1.8e-03 0.13 -0.86 -6.3e-01 -4.5e-01 4605 16746 1.00 -#> log_lik[8] -2.8e-01 2.3e-03 0.14 -0.54 -2.5e-01 -1.1e-01 3527 12826 1.0 -#> log_lik[9] -6.9e-01 2.6e-03 0.17 -0.99 -6.8e-01 -4.3e-01 4522 16442 1.00 -#> log_lik[10] -7.4e-01 3.7e-03 0.23 -1.2 -7.2e-01 -4.1e-01 3966 14423 1.0 -#> log_lik[11] -2.8e-01 2.1e-03 0.13 -0.52 -2.6e-01 -1.1e-01 3682 13390 1.0 -#> log_lik[12] -5.0e-01 3.9e-03 0.24 -0.93 -4.5e-01 -1.9e-01 3842 13973 1.00 -#> log_lik[13] -6.6e-01 3.1e-03 0.21 -1.0 -6.4e-01 -3.6e-01 4585 16673 1.00 -#> log_lik[14] -3.7e-01 2.8e-03 0.17 -0.70 -3.4e-01 -1.4e-01 3858 14029 1.0 -#> log_lik[15] -2.8e-01 1.7e-03 0.11 -0.48 -2.7e-01 -1.4e-01 3977 14463 1.0 -#> log_lik[16] -2.8e-01 1.5e-03 0.087 -0.44 -2.6e-01 -1.5e-01 3495 12708 1.0 -#> log_lik[17] -1.6e+00 5.1e-03 0.29 -2.1 -1.6e+00 -1.1e+00 3184 11579 1.0 -#> log_lik[18] -4.8e-01 1.7e-03 0.11 -0.68 -4.7e-01 -3.1e-01 4019 14615 1.0 -#> log_lik[19] -2.3e-01 1.3e-03 0.075 -0.37 -2.2e-01 -1.3e-01 3432 12479 1.0 -#> log_lik[20] -1.1e-01 1.3e-03 0.080 -0.27 -9.4e-02 -2.9e-02 3626 13186 1.0 -#> log_lik[21] -2.1e-01 1.5e-03 0.088 -0.38 -2.0e-01 -9.4e-02 3506 12749 1.0 -#> log_lik[22] -5.7e-01 2.3e-03 0.15 -0.84 -5.5e-01 -3.4e-01 4293 15611 1.0 -#> log_lik[23] -3.3e-01 2.4e-03 0.14 -0.60 -3.0e-01 -1.4e-01 3628 13194 1.0 -#> log_lik[24] -1.4e-01 1.1e-03 0.066 -0.26 -1.2e-01 -5.2e-02 3504 12742 1.0 -#> log_lik[25] -4.5e-01 1.8e-03 0.12 -0.67 -4.4e-01 -2.8e-01 4234 15396 1.00 -#> log_lik[26] -1.5e+00 5.5e-03 0.34 -2.1 -1.5e+00 -1.0e+00 3760 13672 1.0 -#> log_lik[27] -3.1e-01 2.0e-03 0.12 -0.54 -2.9e-01 -1.4e-01 3688 13412 1.0 -#> log_lik[28] -4.5e-01 1.3e-03 0.083 -0.59 -4.4e-01 -3.2e-01 4007 14572 1.0 -#> log_lik[29] -7.2e-01 3.4e-03 0.22 -1.1 -7.0e-01 -4.0e-01 4367 15880 1.00 -#> log_lik[30] -6.9e-01 3.1e-03 0.20 -1.0 -6.7e-01 -4.1e-01 4146 15077 1.0 -#> log_lik[31] -4.9e-01 2.5e-03 0.17 -0.80 -4.6e-01 -2.5e-01 4260 15491 1.0 -#> log_lik[32] -4.2e-01 1.6e-03 0.11 -0.61 -4.2e-01 -2.7e-01 4253 15465 1.00 -#> log_lik[33] -4.1e-01 2.0e-03 0.13 -0.65 -4.0e-01 -2.3e-01 4207 15297 1.00 -#> log_lik[34] -6.3e-02 8.6e-04 0.051 -0.16 -4.9e-02 -1.3e-02 3530 12835 1.00 -#> log_lik[35] -5.9e-01 2.6e-03 0.18 -0.91 -5.7e-01 -3.2e-01 4846 17624 1.00 -#> log_lik[36] -3.2e-01 2.2e-03 0.13 -0.57 -3.0e-01 -1.5e-01 3788 13775 1.0 -#> log_lik[37] -7.0e-01 3.4e-03 0.22 -1.1 -6.8e-01 -3.8e-01 4249 15451 1.0 -#> log_lik[38] -3.1e-01 2.4e-03 0.15 -0.60 -2.8e-01 -1.2e-01 3822 13898 1.00 -#> log_lik[39] -1.8e-01 1.8e-03 0.11 -0.40 -1.6e-01 -5.3e-02 3734 13579 1.0 -#> log_lik[40] -6.8e-01 2.0e-03 0.13 -0.91 -6.7e-01 -4.8e-01 4407 16026 1.00 -#> log_lik[41] -1.1e+00 4.2e-03 0.26 -1.6 -1.1e+00 -7.4e-01 3931 14294 1.0 -#> log_lik[42] -9.3e-01 3.1e-03 0.20 -1.3 -9.1e-01 -6.2e-01 4217 15335 1.00 -#> log_lik[43] -4.1e-01 4.0e-03 0.27 -0.92 -3.5e-01 -1.0e-01 4495 16345 1.00 -#> log_lik[44] -1.2e+00 2.9e-03 0.18 -1.5 -1.2e+00 -8.9e-01 3801 13820 1.0 -#> log_lik[45] -3.6e-01 1.9e-03 0.12 -0.58 -3.5e-01 -1.9e-01 3946 14348 1.0 -#> log_lik[46] -5.8e-01 2.0e-03 0.13 -0.81 -5.7e-01 -3.8e-01 4290 15599 1.00 -#> log_lik[47] -3.0e-01 2.1e-03 0.13 -0.55 -2.8e-01 -1.3e-01 3827 13916 1.00 -#> log_lik[48] -3.2e-01 1.3e-03 0.082 -0.47 -3.2e-01 -2.0e-01 3733 13576 1.0 -#> log_lik[49] -3.2e-01 1.3e-03 0.079 -0.46 -3.1e-01 -2.0e-01 3527 12825 1.0 -#> log_lik[50] -1.3e+00 5.1e-03 0.33 -1.9 -1.3e+00 -8.0e-01 4204 15288 1.00 -#> log_lik[51] -2.9e-01 1.5e-03 0.094 -0.46 -2.8e-01 -1.6e-01 4007 14571 1.00 -#> log_lik[52] -8.3e-01 2.2e-03 0.14 -1.1 -8.2e-01 -6.2e-01 4159 15122 1.00 -#> log_lik[53] -4.0e-01 2.1e-03 0.13 -0.65 -3.8e-01 -2.1e-01 4104 14922 1.0 -#> log_lik[54] -3.7e-01 2.2e-03 0.14 -0.63 -3.6e-01 -1.8e-01 4025 14635 1.0 -#> log_lik[55] -3.9e-01 2.2e-03 0.14 -0.64 -3.7e-01 -2.0e-01 4074 14813 1.00 -#> log_lik[56] -3.2e-01 3.2e-03 0.19 -0.70 -2.7e-01 -9.4e-02 3722 13536 1.0 -#> log_lik[57] -6.6e-01 1.7e-03 0.12 -0.86 -6.5e-01 -4.8e-01 4340 15782 1.00 -#> log_lik[58] -9.5e-01 5.2e-03 0.35 -1.6 -9.1e-01 -4.6e-01 4377 15915 1.00 -#> log_lik[59] -1.4e+00 5.6e-03 0.35 -2.0 -1.3e+00 -8.4e-01 3916 14239 1.00 -#> log_lik[60] -9.8e-01 2.4e-03 0.16 -1.2 -9.7e-01 -7.3e-01 4048 14720 1.00 -#> log_lik[61] -5.4e-01 1.5e-03 0.097 -0.71 -5.3e-01 -3.9e-01 4267 15517 1.00 -#> log_lik[62] -8.7e-01 4.9e-03 0.32 -1.5 -8.3e-01 -4.3e-01 4186 15223 1.00 -#> log_lik[63] -1.2e-01 1.3e-03 0.074 -0.26 -9.7e-02 -3.2e-02 3420 12438 1.0 -#> log_lik[64] -9.1e-01 4.0e-03 0.26 -1.4 -8.8e-01 -5.3e-01 4216 15333 1.0 -#> log_lik[65] -2.0e+00 1.0e-02 0.60 -3.1 -2.0e+00 -1.1e+00 3486 12678 1.00 -#> log_lik[66] -5.1e-01 2.0e-03 0.13 -0.75 -5.0e-01 -3.1e-01 4559 16578 1.00 -#> log_lik[67] -2.8e-01 1.4e-03 0.081 -0.42 -2.7e-01 -1.6e-01 3503 12740 1.0 -#> log_lik[68] -1.1e+00 3.9e-03 0.24 -1.5 -1.1e+00 -7.0e-01 3902 14189 1.0 -#> log_lik[69] -4.4e-01 1.3e-03 0.082 -0.58 -4.3e-01 -3.1e-01 4099 14906 1.0 -#> log_lik[70] -6.4e-01 3.8e-03 0.24 -1.1 -6.0e-01 -3.0e-01 4023 14629 1.0 -#> log_lik[71] -6.1e-01 3.0e-03 0.21 -0.99 -5.9e-01 -3.2e-01 4800 17455 1.00 -#> log_lik[72] -4.7e-01 2.7e-03 0.18 -0.80 -4.4e-01 -2.2e-01 4327 15734 1.00 -#> log_lik[73] -1.5e+00 6.0e-03 0.37 -2.1 -1.5e+00 -9.4e-01 3679 13379 1.00 -#> log_lik[74] -9.5e-01 3.0e-03 0.19 -1.3 -9.4e-01 -6.6e-01 4126 15004 1.0 -#> log_lik[75] -1.1e+00 6.0e-03 0.39 -1.8 -1.1e+00 -5.8e-01 4255 15474 1.00 -#> log_lik[76] -3.8e-01 2.2e-03 0.14 -0.63 -3.6e-01 -1.8e-01 3886 14131 1.0 -#> log_lik[77] -8.8e-01 2.1e-03 0.14 -1.1 -8.7e-01 -6.6e-01 4420 16075 1.00 -#> log_lik[78] -4.9e-01 2.6e-03 0.17 -0.80 -4.7e-01 -2.5e-01 4490 16326 1.00 -#> log_lik[79] -7.7e-01 2.9e-03 0.19 -1.1 -7.5e-01 -4.9e-01 4123 14992 1.00 -#> log_lik[80] -5.4e-01 3.0e-03 0.20 -0.92 -5.2e-01 -2.6e-01 4287 15591 1.00 -#> log_lik[81] -1.6e-01 1.6e-03 0.099 -0.36 -1.4e-01 -4.9e-02 3918 14248 1.00 -#> log_lik[82] -2.3e-01 2.3e-03 0.14 -0.51 -1.9e-01 -6.4e-02 3867 14061 1.00 -#> log_lik[83] -3.4e-01 1.3e-03 0.080 -0.49 -3.4e-01 -2.3e-01 3809 13853 1.0 -#> log_lik[84] -2.8e-01 1.5e-03 0.090 -0.45 -2.7e-01 -1.5e-01 3815 13872 1.0 -#> log_lik[85] -1.3e-01 1.3e-03 0.075 -0.28 -1.2e-01 -4.4e-02 3631 13202 1.0 -#> log_lik[86] -1.1e+00 5.2e-03 0.33 -1.7 -1.1e+00 -6.4e-01 4016 14603 1.00 -#> log_lik[87] -8.2e-01 1.9e-03 0.13 -1.0 -8.2e-01 -6.3e-01 4341 15786 1.00 -#> log_lik[88] -7.8e-01 3.7e-03 0.25 -1.2 -7.5e-01 -4.3e-01 4546 16530 1.00 -#> log_lik[89] -1.3e+00 5.2e-03 0.32 -1.8 -1.3e+00 -8.0e-01 3869 14070 1.00 -#> log_lik[90] -2.6e-01 2.1e-03 0.13 -0.51 -2.3e-01 -9.4e-02 4086 14857 1.00 -#> log_lik[91] -3.9e-01 2.0e-03 0.13 -0.63 -3.8e-01 -2.0e-01 4332 15753 1.00 -#> log_lik[92] -1.5e+00 5.5e-03 0.34 -2.1 -1.5e+00 -9.7e-01 3795 13801 1.0 -#> log_lik[93] -7.3e-01 3.5e-03 0.22 -1.1 -7.1e-01 -4.2e-01 3875 14091 1.00 -#> log_lik[94] -3.2e-01 1.4e-03 0.086 -0.48 -3.1e-01 -1.9e-01 3797 13806 1.0 -#> log_lik[95] -3.9e-01 1.9e-03 0.11 -0.60 -3.7e-01 -2.2e-01 3692 13426 1.0 -#> log_lik[96] -1.6e+00 4.9e-03 0.28 -2.1 -1.6e+00 -1.1e+00 3247 11808 1.0 -#> log_lik[97] -4.3e-01 1.6e-03 0.10 -0.61 -4.2e-01 -2.8e-01 4122 14989 1.0 -#> log_lik[98] -1.1e+00 5.5e-03 0.37 -1.7 -1.0e+00 -5.3e-01 4388 15957 1.00 -#> log_lik[99] -7.0e-01 2.2e-03 0.15 -0.95 -6.9e-01 -4.8e-01 4345 15799 1.00 -#> log_lik[100] -3.9e-01 1.5e-03 0.096 -0.56 -3.8e-01 -2.5e-01 4119 14977 1.0 +#> alpha 3.8e-01 3.5e-03 0.22 0.026 3.8e-01 7.5e-01 3978 14156 1.00 +#> beta[1] -6.7e-01 4.1e-03 0.26 -1.1 -6.6e-01 -2.6e-01 3779 13447 1.0 +#> beta[2] -2.7e-01 3.5e-03 0.23 -0.64 -2.7e-01 1.0e-01 4061 14450 1.0 +#> beta[3] 6.7e-01 4.3e-03 0.27 0.24 6.7e-01 1.1e+00 3879 13804 1.00 +#> log_lik[1] -5.1e-01 1.5e-03 0.097 -0.68 -5.1e-01 -3.7e-01 3951 14060 1.00 +#> log_lik[2] -4.1e-01 2.3e-03 0.15 -0.69 -3.9e-01 -1.9e-01 4167 14830 1.00 +#> log_lik[3] -4.9e-01 3.3e-03 0.21 -0.88 -4.6e-01 -2.1e-01 4010 14272 1.00 +#> log_lik[4] -4.5e-01 2.5e-03 0.15 -0.72 -4.4e-01 -2.4e-01 3751 13350 1.0 +#> log_lik[5] -1.2e+00 4.4e-03 0.28 -1.7 -1.2e+00 -7.6e-01 4068 14476 1.0 +#> log_lik[6] -5.9e-01 3.0e-03 0.19 -0.96 -5.7e-01 -3.2e-01 4275 15213 1.0 +#> log_lik[7] -6.4e-01 2.0e-03 0.13 -0.86 -6.4e-01 -4.5e-01 4183 14885 1.0 +#> log_lik[8] -2.8e-01 2.3e-03 0.14 -0.55 -2.5e-01 -1.1e-01 3659 13021 1.0 +#> log_lik[9] -6.9e-01 2.7e-03 0.17 -1.0 -6.7e-01 -4.4e-01 4128 14690 1.0 +#> log_lik[10] -7.3e-01 3.6e-03 0.22 -1.1 -7.1e-01 -4.1e-01 3894 13857 1.00 +#> log_lik[11] -2.8e-01 2.1e-03 0.13 -0.54 -2.6e-01 -1.1e-01 3822 13601 1.00 +#> log_lik[12] -5.0e-01 3.8e-03 0.24 -0.95 -4.6e-01 -1.9e-01 3952 14065 1.00 +#> log_lik[13] -6.6e-01 3.3e-03 0.21 -1.0 -6.4e-01 -3.5e-01 4181 14878 1.0 +#> log_lik[14] -3.6e-01 2.6e-03 0.17 -0.67 -3.2e-01 -1.4e-01 4021 14308 1.00 +#> log_lik[15] -2.8e-01 1.7e-03 0.10 -0.47 -2.6e-01 -1.3e-01 3858 13730 1.00 +#> log_lik[16] -2.8e-01 1.5e-03 0.091 -0.44 -2.7e-01 -1.5e-01 3706 13189 1.00 +#> log_lik[17] -1.6e+00 5.0e-03 0.30 -2.1 -1.6e+00 -1.1e+00 3612 12854 1.0 +#> log_lik[18] -4.8e-01 1.8e-03 0.11 -0.68 -4.7e-01 -3.1e-01 4081 14523 1.0 +#> log_lik[19] -2.3e-01 1.3e-03 0.079 -0.37 -2.2e-01 -1.2e-01 3751 13347 1.0 +#> log_lik[20] -1.1e-01 1.3e-03 0.078 -0.26 -9.4e-02 -2.8e-02 3798 13516 1.00 +#> log_lik[21] -2.1e-01 1.5e-03 0.091 -0.39 -2.0e-01 -9.3e-02 3626 12906 1.00 +#> log_lik[22] -5.7e-01 2.4e-03 0.15 -0.86 -5.5e-01 -3.5e-01 4061 14452 1.0 +#> log_lik[23] -3.3e-01 2.3e-03 0.15 -0.61 -3.1e-01 -1.4e-01 3872 13780 1.0 +#> log_lik[24] -1.4e-01 1.1e-03 0.069 -0.27 -1.2e-01 -5.1e-02 3743 13321 1.00 +#> log_lik[25] -4.6e-01 1.9e-03 0.12 -0.68 -4.5e-01 -2.8e-01 3975 14145 1.0 +#> log_lik[26] -1.5e+00 5.8e-03 0.35 -2.1 -1.5e+00 -1.0e+00 3699 13164 1.0 +#> log_lik[27] -3.1e-01 2.0e-03 0.13 -0.55 -2.9e-01 -1.4e-01 3831 13633 1.00 +#> log_lik[28] -4.4e-01 1.4e-03 0.086 -0.59 -4.4e-01 -3.1e-01 3970 14130 1.0 +#> log_lik[29] -7.3e-01 3.4e-03 0.22 -1.1 -7.1e-01 -4.0e-01 4258 15153 1.0 +#> log_lik[30] -6.9e-01 3.1e-03 0.20 -1.1 -6.8e-01 -4.0e-01 4121 14666 1.00 +#> log_lik[31] -4.9e-01 2.6e-03 0.17 -0.81 -4.7e-01 -2.5e-01 4046 14398 1.0 +#> log_lik[32] -4.2e-01 1.7e-03 0.11 -0.62 -4.1e-01 -2.6e-01 3986 14185 1.0 +#> log_lik[33] -4.1e-01 2.0e-03 0.13 -0.65 -4.0e-01 -2.2e-01 4122 14669 1.0 +#> log_lik[34] -6.5e-02 9.1e-04 0.054 -0.17 -5.0e-02 -1.3e-02 3544 12612 1.0 +#> log_lik[35] -5.9e-01 2.8e-03 0.18 -0.92 -5.7e-01 -3.2e-01 4411 15696 1.0 +#> log_lik[36] -3.3e-01 2.2e-03 0.14 -0.58 -3.0e-01 -1.4e-01 4001 14239 1.0 +#> log_lik[37] -6.9e-01 3.4e-03 0.22 -1.1 -6.7e-01 -3.7e-01 4045 14394 1.0 +#> log_lik[38] -3.2e-01 2.4e-03 0.15 -0.60 -2.9e-01 -1.2e-01 3854 13717 1.0 +#> log_lik[39] -1.8e-01 1.7e-03 0.11 -0.38 -1.5e-01 -5.2e-02 3915 13934 1.00 +#> log_lik[40] -6.8e-01 2.1e-03 0.13 -0.92 -6.7e-01 -4.8e-01 4145 14752 1.0 +#> log_lik[41] -1.1e+00 4.2e-03 0.26 -1.6 -1.1e+00 -7.3e-01 3929 13983 1.0 +#> log_lik[42] -9.3e-01 3.2e-03 0.20 -1.3 -9.1e-01 -6.3e-01 3967 14116 1.0 +#> log_lik[43] -4.1e-01 4.0e-03 0.26 -0.91 -3.4e-01 -1.0e-01 4191 14913 1.0 +#> log_lik[44] -1.2e+00 3.1e-03 0.19 -1.5 -1.2e+00 -9.0e-01 3709 13201 1.00 +#> log_lik[45] -3.6e-01 1.8e-03 0.11 -0.56 -3.4e-01 -1.9e-01 4009 14267 1.00 +#> log_lik[46] -5.9e-01 2.0e-03 0.13 -0.82 -5.8e-01 -3.8e-01 4248 15119 1.00 +#> log_lik[47] -3.1e-01 2.2e-03 0.13 -0.55 -2.9e-01 -1.3e-01 3696 13153 1.0 +#> log_lik[48] -3.2e-01 1.4e-03 0.085 -0.47 -3.2e-01 -2.0e-01 3847 13691 1.0 +#> log_lik[49] -3.2e-01 1.3e-03 0.082 -0.46 -3.1e-01 -2.0e-01 3716 13223 1.00 +#> log_lik[50] -1.3e+00 5.4e-03 0.33 -1.9 -1.3e+00 -8.1e-01 3859 13734 1.0 +#> log_lik[51] -2.9e-01 1.6e-03 0.096 -0.46 -2.8e-01 -1.5e-01 3714 13218 1.0 +#> log_lik[52] -8.4e-01 2.2e-03 0.14 -1.1 -8.3e-01 -6.2e-01 3985 14182 1.00 +#> log_lik[53] -4.0e-01 2.1e-03 0.13 -0.66 -3.9e-01 -2.1e-01 3970 14127 1.00 +#> log_lik[54] -3.7e-01 2.2e-03 0.14 -0.62 -3.5e-01 -1.8e-01 4008 14263 1.00 +#> log_lik[55] -3.9e-01 2.2e-03 0.14 -0.65 -3.7e-01 -1.9e-01 4107 14614 1.00 +#> log_lik[56] -3.2e-01 3.2e-03 0.20 -0.70 -2.8e-01 -9.3e-02 3745 13326 1.0 +#> log_lik[57] -6.5e-01 1.8e-03 0.12 -0.85 -6.5e-01 -4.8e-01 3918 13943 1.0 +#> log_lik[58] -9.4e-01 5.3e-03 0.35 -1.6 -9.0e-01 -4.5e-01 4317 15364 1.0 +#> log_lik[59] -1.4e+00 5.7e-03 0.35 -2.0 -1.3e+00 -8.4e-01 3801 13527 1.0 +#> log_lik[60] -9.8e-01 2.5e-03 0.16 -1.3 -9.7e-01 -7.4e-01 3902 13886 1.00 +#> log_lik[61] -5.4e-01 1.5e-03 0.097 -0.70 -5.3e-01 -3.9e-01 4078 14513 1.00 +#> log_lik[62] -8.9e-01 5.0e-03 0.31 -1.5 -8.5e-01 -4.4e-01 3852 13708 1.00 +#> log_lik[63] -1.2e-01 1.3e-03 0.077 -0.27 -9.9e-02 -3.2e-02 3330 11849 1.00 +#> log_lik[64] -9.0e-01 3.8e-03 0.25 -1.4 -8.8e-01 -5.3e-01 4445 15817 1.00 +#> log_lik[65] -2.0e+00 9.8e-03 0.61 -3.1 -2.0e+00 -1.1e+00 3783 13462 1.00 +#> log_lik[66] -5.1e-01 2.1e-03 0.13 -0.74 -4.9e-01 -3.1e-01 4154 14782 1.0 +#> log_lik[67] -2.8e-01 1.4e-03 0.085 -0.43 -2.7e-01 -1.6e-01 3827 13618 1.0 +#> log_lik[68] -1.1e+00 3.8e-03 0.24 -1.5 -1.0e+00 -6.8e-01 4102 14597 1.00 +#> log_lik[69] -4.3e-01 1.4e-03 0.085 -0.58 -4.3e-01 -3.0e-01 3842 13673 1.0 +#> log_lik[70] -6.4e-01 3.6e-03 0.24 -1.1 -6.1e-01 -3.1e-01 4371 15555 1.00 +#> log_lik[71] -6.1e-01 3.2e-03 0.21 -0.99 -5.9e-01 -3.1e-01 4401 15663 1.0 +#> log_lik[72] -4.6e-01 2.7e-03 0.17 -0.79 -4.5e-01 -2.2e-01 4214 14996 1.0 +#> log_lik[73] -1.5e+00 6.1e-03 0.37 -2.1 -1.4e+00 -9.2e-01 3671 13065 1.0 +#> log_lik[74] -9.5e-01 3.0e-03 0.19 -1.3 -9.4e-01 -6.7e-01 3944 14035 1.00 +#> log_lik[75] -1.2e+00 6.4e-03 0.39 -1.8 -1.1e+00 -5.9e-01 3678 13088 1.00 +#> log_lik[76] -3.7e-01 2.1e-03 0.13 -0.62 -3.5e-01 -1.8e-01 3976 14151 1.00 +#> log_lik[77] -8.8e-01 2.2e-03 0.14 -1.1 -8.8e-01 -6.6e-01 4034 14357 1.0 +#> log_lik[78] -4.8e-01 2.7e-03 0.17 -0.79 -4.6e-01 -2.4e-01 4097 14581 1.00 +#> log_lik[79] -7.6e-01 2.9e-03 0.18 -1.1 -7.4e-01 -4.8e-01 3889 13840 1.0 +#> log_lik[80] -5.5e-01 3.0e-03 0.20 -0.91 -5.2e-01 -2.7e-01 4407 15683 1.00 +#> log_lik[81] -1.7e-01 1.7e-03 0.10 -0.37 -1.4e-01 -4.8e-02 3780 13450 1.0 +#> log_lik[82] -2.3e-01 2.3e-03 0.14 -0.51 -2.0e-01 -6.3e-02 3943 14031 1.00 +#> log_lik[83] -3.4e-01 1.3e-03 0.081 -0.48 -3.4e-01 -2.2e-01 3766 13404 1.00 +#> log_lik[84] -2.7e-01 1.5e-03 0.090 -0.44 -2.6e-01 -1.4e-01 3767 13405 1.00 +#> log_lik[85] -1.3e-01 1.2e-03 0.075 -0.28 -1.1e-01 -4.1e-02 3780 13453 1.00 +#> log_lik[86] -1.1e+00 5.0e-03 0.32 -1.7 -1.1e+00 -6.4e-01 4173 14850 1.00 +#> log_lik[87] -8.3e-01 2.0e-03 0.13 -1.1 -8.2e-01 -6.2e-01 4183 14887 1.00 +#> log_lik[88] -7.8e-01 3.8e-03 0.24 -1.2 -7.5e-01 -4.2e-01 4157 14794 1.0 +#> log_lik[89] -1.3e+00 5.1e-03 0.32 -1.9 -1.2e+00 -7.9e-01 3879 13805 1.00 +#> log_lik[90] -2.6e-01 2.2e-03 0.14 -0.52 -2.4e-01 -9.4e-02 4008 14262 1.0 +#> log_lik[91] -3.9e-01 2.1e-03 0.13 -0.63 -3.8e-01 -2.1e-01 4144 14746 1.0 +#> log_lik[92] -1.5e+00 5.6e-03 0.34 -2.1 -1.5e+00 -9.7e-01 3829 13625 1.00 +#> log_lik[93] -7.5e-01 3.4e-03 0.21 -1.1 -7.2e-01 -4.4e-01 3888 13835 1.00 +#> log_lik[94] -3.2e-01 1.4e-03 0.087 -0.47 -3.1e-01 -1.9e-01 3803 13533 1.00 +#> log_lik[95] -3.9e-01 1.9e-03 0.12 -0.60 -3.8e-01 -2.2e-01 3889 13841 1.0 +#> log_lik[96] -1.6e+00 4.8e-03 0.29 -2.1 -1.6e+00 -1.1e+00 3601 12815 1.00 +#> log_lik[97] -4.3e-01 1.6e-03 0.10 -0.61 -4.2e-01 -2.7e-01 4067 14472 1.0 +#> log_lik[98] -1.0e+00 5.6e-03 0.37 -1.7 -1.0e+00 -5.2e-01 4342 15452 1.0 +#> log_lik[99] -7.0e-01 2.2e-03 0.15 -0.96 -6.9e-01 -4.7e-01 4351 15484 1.00 +#> log_lik[100] -3.9e-01 1.5e-03 0.097 -0.55 -3.8e-01 -2.4e-01 4019 14301 1.00 #> #> Samples were drawn using hmc with nuts. #> For each parameter, N_Eff is a crude measure of effective sample size, diff --git a/docs/reference/fit-method-code.html b/docs/reference/fit-method-code.html index 5dc9cdc20..e2080e073 100644 --- a/docs/reference/fit-method-code.html +++ b/docs/reference/fit-method-code.html @@ -17,7 +17,7 @@ cmdstanr - 0.6.0 + 0.7.0
    diff --git a/docs/reference/fit-method-constrain_variables.html b/docs/reference/fit-method-constrain_variables.html index 79cdb61a9..122d2d4b9 100644 --- a/docs/reference/fit-method-constrain_variables.html +++ b/docs/reference/fit-method-constrain_variables.html @@ -18,7 +18,7 @@ cmdstanr - 0.6.0 + 0.7.0 @@ -145,7 +145,6 @@

    See also

    Examples

    # \dontrun{
     fit_mcmc <- cmdstanr_example("logistic", method = "sample", force_recompile = TRUE)
    -#> Compiling Stan program...
     fit_mcmc$init_model_methods()
     #> Compiling additional model methods...
     fit_mcmc$constrain_variables(unconstrained_variables = c(0.5, 1.2, 1.1, 2.2))
    diff --git a/docs/reference/fit-method-diagnostic_summary.html b/docs/reference/fit-method-diagnostic_summary.html
    index c7c0ce326..53c45c1c7 100644
    --- a/docs/reference/fit-method-diagnostic_summary.html
    +++ b/docs/reference/fit-method-diagnostic_summary.html
    @@ -24,7 +24,7 @@
           
           
             cmdstanr
    -        0.6.0
    +        0.7.0
           
         
    @@ -161,29 +161,29 @@

    See also

    Examples

    # \dontrun{
     fit <- cmdstanr_example("schools")
    -#> Warning: 104 of 4000 (3.0%) transitions ended with a divergence.
    +#> Warning: 91 of 4000 (2.0%) transitions ended with a divergence.
     #> See https://mc-stan.org/misc/warnings for details.
     fit$diagnostic_summary()
    -#> Warning: 104 of 4000 (3.0%) transitions ended with a divergence.
    +#> Warning: 91 of 4000 (2.0%) transitions ended with a divergence.
     #> See https://mc-stan.org/misc/warnings for details.
     #> $num_divergent
    -#> [1] 47 52  3  2
    +#> [1] 12 18 11 50
     #> 
     #> $num_max_treedepth
     #> [1] 0 0 0 0
     #> 
     #> $ebfmi
    -#> [1] 0.3009803 0.3412086 0.3406291 0.2824946
    +#> [1] 0.3003970 0.2974182 0.3438194 0.4920025
     #> 
     fit$diagnostic_summary(quiet = TRUE)
     #> $num_divergent
    -#> [1] 47 52  3  2
    +#> [1] 12 18 11 50
     #> 
     #> $num_max_treedepth
     #> [1] 0 0 0 0
     #> 
     #> $ebfmi
    -#> [1] 0.3009803 0.3412086 0.3406291 0.2824946
    +#> [1] 0.3003970 0.2974182 0.3438194 0.4920025
     #> 
     # }
     
    diff --git a/docs/reference/fit-method-draws-1.png b/docs/reference/fit-method-draws-1.png
    index 74c1b4fb5..a6899fc42 100644
    Binary files a/docs/reference/fit-method-draws-1.png and b/docs/reference/fit-method-draws-1.png differ
    diff --git a/docs/reference/fit-method-draws-2.png b/docs/reference/fit-method-draws-2.png
    index 9bd5910ac..fb93238cc 100644
    Binary files a/docs/reference/fit-method-draws-2.png and b/docs/reference/fit-method-draws-2.png differ
    diff --git a/docs/reference/fit-method-draws.html b/docs/reference/fit-method-draws.html
    index 0fd6cc766..b257cb5d6 100644
    --- a/docs/reference/fit-method-draws.html
    +++ b/docs/reference/fit-method-draws.html
    @@ -22,7 +22,7 @@
           
           
             cmdstanr
    -        0.6.0
    +        0.7.0
           
         
    @@ -203,7 +203,7 @@

    Examples

    dim(draws) #> [1] 1000 4 105 str(draws) -#> 'draws_array' num [1:1000, 1:4, 1:105] -66.2 -64.2 -65.5 -66.1 -64.4 ... +#> 'draws_array' num [1:1000, 1:4, 1:105] -64.2 -65.3 -65.3 -64.7 -64.8 ... #> - attr(*, "dimnames")=List of 3 #> ..$ iteration: chr [1:1000] "1" "2" "3" "4" ... #> ..$ chain : chr [1:4] "1" "2" "3" "4" @@ -215,12 +215,12 @@

    Examples

    #> # A draws_matrix: 6 iterations, 1 chains, and 105 variables #> variable #> draw lp__ alpha beta[1] beta[2] beta[3] log_lik[1] log_lik[2] log_lik[3] -#> 1 -66 0.098 -0.85 -0.59 0.74 -0.65 -0.30 -0.84 -#> 2 -64 0.291 -0.57 -0.31 0.52 -0.55 -0.45 -0.58 -#> 3 -65 0.185 -0.32 -0.28 0.82 -0.65 -0.32 -0.62 -#> 4 -66 0.223 -0.38 -0.14 0.97 -0.62 -0.25 -0.44 -#> 5 -64 0.313 -0.65 -0.14 0.83 -0.53 -0.28 -0.37 -#> 6 -64 0.338 -0.74 -0.20 0.80 -0.52 -0.30 -0.39 +#> 1 -64 0.28 -0.73 -0.16 0.73 -0.53 -0.30 -0.39 +#> 2 -65 0.19 -0.42 -0.13 0.37 -0.59 -0.48 -0.53 +#> 3 -65 0.61 -0.93 -0.25 0.83 -0.40 -0.34 -0.30 +#> 4 -65 0.42 -0.81 -0.32 0.94 -0.50 -0.28 -0.41 +#> 5 -65 0.36 -0.41 -0.20 0.39 -0.52 -0.56 -0.52 +#> 6 -67 0.55 -0.24 -0.43 0.24 -0.49 -0.87 -0.75 #> # ... with 97 more variables # or can specify 'format' argument to avoid manual conversion @@ -230,12 +230,12 @@

    Examples

    #> # A draws_matrix: 6 iterations, 1 chains, and 105 variables #> variable #> draw lp__ alpha beta[1] beta[2] beta[3] log_lik[1] log_lik[2] log_lik[3] -#> 1 -66 0.098 -0.85 -0.59 0.74 -0.65 -0.30 -0.84 -#> 2 -64 0.291 -0.57 -0.31 0.52 -0.55 -0.45 -0.58 -#> 3 -65 0.185 -0.32 -0.28 0.82 -0.65 -0.32 -0.62 -#> 4 -66 0.223 -0.38 -0.14 0.97 -0.62 -0.25 -0.44 -#> 5 -64 0.313 -0.65 -0.14 0.83 -0.53 -0.28 -0.37 -#> 6 -64 0.338 -0.74 -0.20 0.80 -0.52 -0.30 -0.39 +#> 1 -64 0.28 -0.73 -0.16 0.73 -0.53 -0.30 -0.39 +#> 2 -65 0.19 -0.42 -0.13 0.37 -0.59 -0.48 -0.53 +#> 3 -65 0.61 -0.93 -0.25 0.83 -0.40 -0.34 -0.30 +#> 4 -65 0.42 -0.81 -0.32 0.94 -0.50 -0.28 -0.41 +#> 5 -65 0.36 -0.41 -0.20 0.39 -0.52 -0.56 -0.52 +#> 6 -67 0.55 -0.24 -0.43 0.24 -0.49 -0.87 -0.75 #> # ... with 97 more variables # can select specific parameters @@ -244,12 +244,12 @@

    Examples

    #> , , variable = alpha #> #> chain -#> iteration 1 2 3 4 -#> 1 0.098 0.043 0.30 0.119 -#> 2 0.291 0.133 0.26 0.609 -#> 3 0.185 0.612 0.11 0.483 -#> 4 0.223 0.561 0.43 0.842 -#> 5 0.313 0.463 0.21 0.044 +#> iteration 1 2 3 4 +#> 1 0.28 0.27 0.62 0.10 +#> 2 0.19 0.70 0.49 0.71 +#> 3 0.61 0.26 0.89 0.14 +#> 4 0.42 0.39 0.58 0.39 +#> 5 0.36 0.39 0.58 0.48 #> #> # ... with 995 more iterations fit$draws("beta") # selects entire vector beta @@ -258,31 +258,31 @@

    Examples

    #> #> chain #> iteration 1 2 3 4 -#> 1 -0.85 -1.03 -1.02 -0.77 -#> 2 -0.57 -0.76 -0.92 -0.44 -#> 3 -0.32 -0.65 -0.94 -0.31 -#> 4 -0.38 -0.95 -0.54 -0.49 -#> 5 -0.65 -0.91 -0.28 -0.75 +#> 1 -0.73 -0.46 -0.82 -0.55 +#> 2 -0.42 -0.92 -1.18 -1.08 +#> 3 -0.93 -0.52 -0.63 -0.42 +#> 4 -0.81 -0.86 -0.30 -0.72 +#> 5 -0.41 -0.86 -0.30 -0.86 #> #> , , variable = beta[2] #> #> chain -#> iteration 1 2 3 4 -#> 1 -0.59 -0.120 -0.206 0.053 -#> 2 -0.31 -0.066 -0.353 -0.424 -#> 3 -0.28 -0.552 0.039 -0.239 -#> 4 -0.14 -0.550 -0.715 -0.513 -#> 5 -0.14 -0.153 0.147 -0.444 +#> iteration 1 2 3 4 +#> 1 -0.16 -0.31 -0.88 -0.18 +#> 2 -0.13 -0.27 -0.69 -0.21 +#> 3 -0.25 -0.39 -0.38 -0.37 +#> 4 -0.32 -0.43 -0.52 -0.36 +#> 5 -0.20 -0.43 -0.52 -0.42 #> #> , , variable = beta[3] #> #> chain #> iteration 1 2 3 4 -#> 1 0.74 0.61 0.61 0.58 -#> 2 0.52 0.57 0.52 0.83 -#> 3 0.82 0.72 0.68 1.21 -#> 4 0.97 0.59 0.74 1.03 -#> 5 0.83 0.57 0.46 0.52 +#> 1 0.73 0.28 0.87 0.93 +#> 2 0.37 1.26 0.75 0.45 +#> 3 0.83 0.28 0.88 0.83 +#> 4 0.94 0.64 0.71 0.48 +#> 5 0.39 0.64 0.71 0.41 #> #> # ... with 995 more iterations fit$draws(c("alpha", "beta[2]")) @@ -290,22 +290,22 @@

    Examples

    #> , , variable = alpha #> #> chain -#> iteration 1 2 3 4 -#> 1 0.098 0.043 0.30 0.119 -#> 2 0.291 0.133 0.26 0.609 -#> 3 0.185 0.612 0.11 0.483 -#> 4 0.223 0.561 0.43 0.842 -#> 5 0.313 0.463 0.21 0.044 +#> iteration 1 2 3 4 +#> 1 0.28 0.27 0.62 0.10 +#> 2 0.19 0.70 0.49 0.71 +#> 3 0.61 0.26 0.89 0.14 +#> 4 0.42 0.39 0.58 0.39 +#> 5 0.36 0.39 0.58 0.48 #> #> , , variable = beta[2] #> #> chain -#> iteration 1 2 3 4 -#> 1 -0.59 -0.120 -0.206 0.053 -#> 2 -0.31 -0.066 -0.353 -0.424 -#> 3 -0.28 -0.552 0.039 -0.239 -#> 4 -0.14 -0.550 -0.715 -0.513 -#> 5 -0.14 -0.153 0.147 -0.444 +#> iteration 1 2 3 4 +#> 1 -0.16 -0.31 -0.88 -0.18 +#> 2 -0.13 -0.27 -0.69 -0.21 +#> 3 -0.25 -0.39 -0.38 -0.37 +#> 4 -0.32 -0.43 -0.52 -0.36 +#> 5 -0.20 -0.43 -0.52 -0.42 #> #> # ... with 995 more iterations @@ -323,21 +323,21 @@

    Examples

    #> # A draws_matrix: 6 iterations, 1 chains, and 3 variables #> variable #> draw beta[1] beta[2] beta[3] -#> 1 -0.66 0.3552 1.03 -#> 2 -0.39 -0.0109 0.32 -#> 3 -0.65 -0.8123 0.59 -#> 4 -0.48 0.0005 0.86 -#> 5 -0.60 -0.4373 0.48 -#> 6 -1.04 -0.0197 1.13 +#> 1 -0.62 -0.485 0.63 +#> 2 -0.34 0.188 0.77 +#> 3 -0.63 -0.236 0.90 +#> 4 -0.65 -0.392 0.76 +#> 5 -0.57 0.028 0.44 +#> 6 -0.58 -0.299 0.69 head(fit$draws("beta", format = "df")) #> # A draws_df: 6 iterations, 1 chains, and 3 variables #> beta[1] beta[2] beta[3] -#> 1 -0.66 0.3552 1.03 -#> 2 -0.39 -0.0109 0.32 -#> 3 -0.65 -0.8123 0.59 -#> 4 -0.48 0.0005 0.86 -#> 5 -0.60 -0.4373 0.48 -#> 6 -1.04 -0.0197 1.13 +#> 1 -0.62 -0.485 0.63 +#> 2 -0.34 0.188 0.77 +#> 3 -0.63 -0.236 0.90 +#> 4 -0.65 -0.392 0.76 +#> 5 -0.57 0.028 0.44 +#> 6 -0.58 -0.299 0.69 #> # ... hidden reserved variables {'.chain', '.iteration', '.draw'} # } diff --git a/docs/reference/fit-method-grad_log_prob.html b/docs/reference/fit-method-grad_log_prob.html index 41213cd70..e6dae257f 100644 --- a/docs/reference/fit-method-grad_log_prob.html +++ b/docs/reference/fit-method-grad_log_prob.html @@ -20,7 +20,7 @@ cmdstanr - 0.6.0 + 0.7.0 @@ -113,19 +113,26 @@

    Calculate the log-probability and the gradient w.r.t. each input for a
    -
    grad_log_prob(unconstrained_variables, jacobian_adjustment = TRUE)
    +
    grad_log_prob(
    +  unconstrained_variables,
    +  jacobian = TRUE,
    +  jacobian_adjustment = NULL
    +)

    Arguments

    unconstrained_variables
    -

    (numeric) A vector of unconstrained parameters -to be passed to grad_log_prob.

    +

    (numeric) A vector of unconstrained parameters.

    + + +
    jacobian
    +

    (logical) Whether to include the log-density adjustments from +un/constraining variables.

    jacobian_adjustment
    -

    (logical) Whether to include the log-density -adjustments from un/constraining variables.

    +

    Deprecated. Please use jacobian instead.

    @@ -139,7 +146,6 @@

    See also

    Examples

    # \dontrun{
     fit_mcmc <- cmdstanr_example("logistic", method = "sample", force_recompile = TRUE)
    -#> Compiling Stan program...
     fit_mcmc$init_model_methods()
     #> Compiling additional model methods...
     fit_mcmc$grad_log_prob(unconstrained_variables = c(0.5, 1.2, 1.1, 2.2))
    diff --git a/docs/reference/fit-method-gradients.html b/docs/reference/fit-method-gradients.html
    index e022df320..c682dd044 100644
    --- a/docs/reference/fit-method-gradients.html
    +++ b/docs/reference/fit-method-gradients.html
    @@ -18,7 +18,7 @@
           
           
             cmdstanr
    -        0.6.0
    +        0.7.0
           
         
    @@ -131,11 +131,11 @@

    Examples

    # retrieve the gradients test$gradients() -#> param_idx value model finite_diff error -#> 1 0 0.788454 -2.90866 -2.90866 4.89447e-09 -#> 2 1 0.538042 -17.17950 -17.17950 2.76466e-08 -#> 3 2 -1.725840 16.99630 16.99630 8.87535e-10 -#> 4 3 1.854370 -7.63726 -7.63726 -8.95156e-09 +#> param_idx value model finite_diff error +#> 1 0 1.083910 -6.21416 -6.21416 3.58858e-08 +#> 2 1 1.800640 -33.18970 -33.18970 4.38607e-08 +#> 3 2 0.683175 -19.70780 -19.70780 1.73640e-08 +#> 4 3 1.857330 -9.17992 -9.17992 1.12563e-08 # }
    diff --git a/docs/reference/fit-method-hessian.html b/docs/reference/fit-method-hessian.html index eaecb8c8b..12bcc5e81 100644 --- a/docs/reference/fit-method-hessian.html +++ b/docs/reference/fit-method-hessian.html @@ -20,7 +20,7 @@ cmdstanr - 0.6.0 + 0.7.0 @@ -113,19 +113,22 @@

    Calculate the log-probability , the gradient w.r.t. each input, and the hess
    -
    hessian(unconstrained_variables, jacobian_adjustment = TRUE)
    +
    hessian(unconstrained_variables, jacobian = TRUE, jacobian_adjustment = TRUE)

    Arguments

    unconstrained_variables
    -

    (numeric) A vector of unconstrained parameters -to be passed to hessian.

    +

    (numeric) A vector of unconstrained parameters.

    + + +
    jacobian
    +

    (logical) Whether to include the log-density adjustments from +un/constraining variables.

    jacobian_adjustment
    -

    (logical) Whether to include the log-density -adjustments from un/constraining variables.

    +

    Deprecated. Please use jacobian instead.

    @@ -139,7 +142,6 @@

    See also

    Examples

    # \dontrun{
     fit_mcmc <- cmdstanr_example("logistic", method = "sample", force_recompile = TRUE)
    -#> Compiling Stan program...
     # fit_mcmc$init_model_methods(hessian = TRUE)
     # fit_mcmc$hessian(unconstrained_variables = c(0.5, 1.2, 1.1, 2.2))
     # }
    diff --git a/docs/reference/fit-method-init.html b/docs/reference/fit-method-init.html
    index e66e48080..955ed4b1f 100644
    --- a/docs/reference/fit-method-init.html
    +++ b/docs/reference/fit-method-init.html
    @@ -22,7 +22,7 @@
           
           
             cmdstanr
    -        0.6.0
    +        0.7.0
           
         
    @@ -140,11 +140,11 @@

    Examples

    str(fit$init()) #> List of 2 #> $ :List of 2 -#> ..$ alpha: num -1.25 -#> ..$ beta : num [1:3] -2.2653 0.0495 0.8638 +#> ..$ alpha: num -0.538 +#> ..$ beta : num [1:3] -0.0289 1.8267 1.6042 #> $ :List of 2 -#> ..$ alpha: num -0.172 -#> ..$ beta : num [1:3] 0.437 0.285 1.047 +#> ..$ alpha: num -1.4 +#> ..$ beta : num [1:3] -1.313 -1.04 0.224 # partial inits (only specifying for a subset of parameters) init_list <- list( diff --git a/docs/reference/fit-method-init_model_methods.html b/docs/reference/fit-method-init_model_methods.html index 0b0aa7dff..d27c21709 100644 --- a/docs/reference/fit-method-init_model_methods.html +++ b/docs/reference/fit-method-init_model_methods.html @@ -26,7 +26,7 @@ cmdstanr - 0.6.0 + 0.7.0
    @@ -153,7 +153,6 @@

    See also

    Examples

    # \dontrun{
     fit_mcmc <- cmdstanr_example("logistic", method = "sample", force_recompile = TRUE)
    -#> Compiling Stan program...
     fit_mcmc$init_model_methods()
     #> Compiling additional model methods...
     # }
    diff --git a/docs/reference/fit-method-inv_metric.html b/docs/reference/fit-method-inv_metric.html
    index cca5c0ce6..3705aecd0 100644
    --- a/docs/reference/fit-method-inv_metric.html
    +++ b/docs/reference/fit-method-inv_metric.html
    @@ -17,7 +17,7 @@
           
           
             cmdstanr
    -        0.6.0
    +        0.7.0
           
         
    @@ -140,75 +140,75 @@

    Examples

    fit$inv_metric() #> $`1` #> [,1] [,2] [,3] [,4] -#> [1,] 0.0431002 0.0000000 0.0000000 0.0000000 -#> [2,] 0.0000000 0.0583438 0.0000000 0.0000000 -#> [3,] 0.0000000 0.0000000 0.0531465 0.0000000 -#> [4,] 0.0000000 0.0000000 0.0000000 0.0786797 +#> [1,] 0.0435508 0.0000000 0.0000000 0.0000000 +#> [2,] 0.0000000 0.0607469 0.0000000 0.0000000 +#> [3,] 0.0000000 0.0000000 0.0503494 0.0000000 +#> [4,] 0.0000000 0.0000000 0.0000000 0.0672705 #> #> $`2` #> [,1] [,2] [,3] [,4] -#> [1,] 0.0454991 0.0000000 0.0000000 0.0000000 -#> [2,] 0.0000000 0.0809962 0.0000000 0.0000000 -#> [3,] 0.0000000 0.0000000 0.0496686 0.0000000 -#> [4,] 0.0000000 0.0000000 0.0000000 0.0688616 +#> [1,] 0.0532065 0.0000000 0.0000000 0.0000000 +#> [2,] 0.0000000 0.0574785 0.0000000 0.0000000 +#> [3,] 0.0000000 0.0000000 0.0512835 0.0000000 +#> [4,] 0.0000000 0.0000000 0.0000000 0.0641874 #> #> $`3` -#> [,1] [,2] [,3] [,4] -#> [1,] 0.0406769 0.000000 0.0000000 0.0000000 -#> [2,] 0.0000000 0.054686 0.0000000 0.0000000 -#> [3,] 0.0000000 0.000000 0.0544764 0.0000000 -#> [4,] 0.0000000 0.000000 0.0000000 0.0675343 +#> [,1] [,2] [,3] [,4] +#> [1,] 0.0502858 0.0000000 0.0000000 0.0000000 +#> [2,] 0.0000000 0.0629888 0.0000000 0.0000000 +#> [3,] 0.0000000 0.0000000 0.0512933 0.0000000 +#> [4,] 0.0000000 0.0000000 0.0000000 0.0661664 #> #> $`4` -#> [,1] [,2] [,3] [,4] -#> [1,] 0.0434081 0.0000000 0.0000000 0.0000000 -#> [2,] 0.0000000 0.0669723 0.0000000 0.0000000 -#> [3,] 0.0000000 0.0000000 0.0466104 0.0000000 -#> [4,] 0.0000000 0.0000000 0.0000000 0.0654703 +#> [,1] [,2] [,3] [,4] +#> [1,] 0.0453724 0.000000 0.00000 0.0000000 +#> [2,] 0.0000000 0.061371 0.00000 0.0000000 +#> [3,] 0.0000000 0.000000 0.04017 0.0000000 +#> [4,] 0.0000000 0.000000 0.00000 0.0682251 #> fit$inv_metric(matrix=FALSE) #> $`1` -#> [1] 0.0431002 0.0583438 0.0531465 0.0786797 +#> [1] 0.0435508 0.0607469 0.0503494 0.0672705 #> #> $`2` -#> [1] 0.0454991 0.0809962 0.0496686 0.0688616 +#> [1] 0.0532065 0.0574785 0.0512835 0.0641874 #> #> $`3` -#> [1] 0.0406769 0.0546860 0.0544764 0.0675343 +#> [1] 0.0502858 0.0629888 0.0512933 0.0661664 #> #> $`4` -#> [1] 0.0434081 0.0669723 0.0466104 0.0654703 +#> [1] 0.0453724 0.0613710 0.0401700 0.0682251 #> fit <- cmdstanr_example("logistic", metric = "dense_e") fit$inv_metric() #> $`1` -#> [,1] [,2] [,3] [,4] -#> [1,] 4.31917e-02 0.00404353 0.00504066 9.28594e-06 -#> [2,] 4.04353e-03 0.05741780 -0.00262368 -9.31332e-03 -#> [3,] 5.04066e-03 -0.00262368 0.04816930 -1.40772e-02 -#> [4,] 9.28594e-06 -0.00931332 -0.01407720 7.29028e-02 +#> [,1] [,2] [,3] [,4] +#> [1,] 0.04487240 -0.00665557 0.00248036 -0.00336473 +#> [2,] -0.00665557 0.05394230 -0.00841737 -0.00815855 +#> [3,] 0.00248036 -0.00841737 0.04945140 -0.01015960 +#> [4,] -0.00336473 -0.00815855 -0.01015960 0.07289150 #> #> $`2` -#> [,1] [,2] [,3] [,4] -#> [1,] 0.05586860 -0.00758105 0.00327655 0.00186063 -#> [2,] -0.00758105 0.06515060 -0.00821290 -0.00560473 -#> [3,] 0.00327655 -0.00821290 0.05351870 -0.01152510 -#> [4,] 0.00186063 -0.00560473 -0.01152510 0.06850340 +#> [,1] [,2] [,3] [,4] +#> [1,] 0.044891100 -0.00756532 0.00352846 -0.000462971 +#> [2,] -0.007565320 0.06064960 -0.00809279 -0.004813840 +#> [3,] 0.003528460 -0.00809279 0.05892730 -0.016947100 +#> [4,] -0.000462971 -0.00481384 -0.01694710 0.063878300 #> #> $`3` -#> [,1] [,2] [,3] [,4] -#> [1,] 0.05023130 0.00119935 0.00439778 -0.00486186 -#> [2,] 0.00119935 0.06410140 -0.00267351 -0.01118110 -#> [3,] 0.00439778 -0.00267351 0.04945990 -0.01316990 -#> [4,] -0.00486186 -0.01118110 -0.01316990 0.07295030 +#> [,1] [,2] [,3] [,4] +#> [1,] 0.047806100 0.000761202 0.00579535 0.00494546 +#> [2,] 0.000761202 0.062074500 -0.00488086 -0.01014510 +#> [3,] 0.005795350 -0.004880860 0.05170230 -0.00609765 +#> [4,] 0.004945460 -0.010145100 -0.00609765 0.07081120 #> #> $`4` -#> [,1] [,2] [,3] [,4] -#> [1,] 0.05218850 -0.00108995 0.00814572 0.00609612 -#> [2,] -0.00108995 0.06646310 -0.00283644 -0.01047740 -#> [3,] 0.00814572 -0.00283644 0.05347060 -0.01085170 -#> [4,] 0.00609612 -0.01047740 -0.01085170 0.07278000 +#> [,1] [,2] [,3] [,4] +#> [1,] 0.04387350 0.004639300 0.001929550 0.00526756 +#> [2,] 0.00463930 0.064076200 -0.000902685 -0.00609741 +#> [3,] 0.00192955 -0.000902685 0.045911200 -0.00859578 +#> [4,] 0.00526756 -0.006097410 -0.008595780 0.07087270 #> # } diff --git a/docs/reference/fit-method-log_prob.html b/docs/reference/fit-method-log_prob.html index cac3b586d..561ca052b 100644 --- a/docs/reference/fit-method-log_prob.html +++ b/docs/reference/fit-method-log_prob.html @@ -1,5 +1,6 @@ -Calculate the log-probability given a provided vector of unconstrained parameters. — fit-method-log_prob • cmdstanrCalculate the log-probability given a provided vector of unconstrained parameters. — fit-method-log_prob • cmdstanr @@ -17,7 +18,7 @@ cmdstanr - 0.6.0 + 0.7.0 @@ -104,23 +105,27 @@

    Calculate the log-probability given a provided vector of unconstrained param
    -

    The $log_prob() method provides access to the Stan model's log_prob function

    +

    The $log_prob() method provides access to the Stan model's +log_prob function.

    -
    log_prob(unconstrained_variables, jacobian_adjustment = TRUE)
    +
    log_prob(unconstrained_variables, jacobian = TRUE, jacobian_adjustment = NULL)

    Arguments

    unconstrained_variables
    -

    (numeric) A vector of unconstrained parameters -to be passed to log_prob.

    +

    (numeric) A vector of unconstrained parameters.

    + + +
    jacobian
    +

    (logical) Whether to include the log-density adjustments from +un/constraining variables.

    jacobian_adjustment
    -

    (logical) Whether to include the log-density -adjustments from un/constraining variables.

    +

    Deprecated. Please use jacobian instead.

    @@ -134,7 +139,6 @@

    See also

    Examples

    # \dontrun{
     fit_mcmc <- cmdstanr_example("logistic", method = "sample", force_recompile = TRUE)
    -#> Compiling Stan program...
     fit_mcmc$init_model_methods()
     #> Compiling additional model methods...
     fit_mcmc$log_prob(unconstrained_variables = c(0.5, 1.2, 1.1, 2.2))
    diff --git a/docs/reference/fit-method-loo.html b/docs/reference/fit-method-loo.html
    index 95111bd38..54c584664 100644
    --- a/docs/reference/fit-method-loo.html
    +++ b/docs/reference/fit-method-loo.html
    @@ -21,7 +21,7 @@
           
           
             cmdstanr
    -        0.6.0
    +        0.7.0
           
         
    @@ -175,7 +175,6 @@

    Examples

    # \dontrun{ # the "logistic" example model has "log_lik" in generated quantities fit <- cmdstanr_example("logistic") -#> Model executable is up to date! loo_result <- fit$loo(cores = 2) print(loo_result) #> @@ -184,7 +183,7 @@

    Examples

    #> Estimate SE #> elpd_loo -63.7 4.1 #> p_loo 3.9 0.5 -#> looic 127.4 8.3 +#> looic 127.4 8.2 #> ------ #> Monte Carlo SE of elpd_loo is 0.0. #> diff --git a/docs/reference/fit-method-lp-1.png b/docs/reference/fit-method-lp-1.png index ca30aa561..980bd46be 100644 Binary files a/docs/reference/fit-method-lp-1.png and b/docs/reference/fit-method-lp-1.png differ diff --git a/docs/reference/fit-method-lp.html b/docs/reference/fit-method-lp.html index 8aaf6c788..bca9a3297 100644 --- a/docs/reference/fit-method-lp.html +++ b/docs/reference/fit-method-lp.html @@ -2,7 +2,9 @@ Extract log probability (target) — fit-method-lp • cmdstanrReport timing of CmdStan runs — fit-method-time • cmdstanr @@ -19,7 +21,7 @@ cmdstanr - 0.6.0 + 0.7.0
    @@ -108,7 +110,9 @@

    Report timing of CmdStan runs

    Report the run time in seconds. For MCMC additional information is provided about the run times of individual chains and the warmup and -sampling phases.

    +sampling phases. For Laplace approximation the time only include the time +for drawing the approximate sample and does not include the time +taken to run the $optimize() method.

    @@ -136,27 +140,36 @@

    Examples

    fit_mcmc <- cmdstanr_example("logistic", method = "sample") fit_mcmc$time() #> $total -#> [1] 0.553201 +#> [1] 0.554847 #> #> $chains #> chain_id warmup sampling total -#> 1 1 0.021 0.068 0.089 -#> 2 2 0.022 0.070 0.092 -#> 3 3 0.022 0.076 0.098 +#> 1 1 0.023 0.071 0.094 +#> 2 2 0.022 0.066 0.088 +#> 3 3 0.023 0.071 0.094 #> 4 4 0.023 0.071 0.094 #> -fit_mle <- cmdstanr_example("logistic", method = "optimize") +fit_vb <- cmdstanr_example("logistic", method = "variational") +fit_vb$time() +#> $total +#> [1] 0.1302531 +#> + +fit_mle <- cmdstanr_example("logistic", method = "optimize", jacobian = TRUE) fit_mle$time() #> $total -#> [1] 0.1247039 +#> [1] 0.1320548 #> -fit_vb <- cmdstanr_example("logistic", method = "variational") -fit_vb$time() +# use fit_mle to draw samples from laplace approximation +fit_laplace <- cmdstanr_example("logistic", method = "laplace", mode = fit_mle) +fit_laplace$time() # just time for drawing sample not for running optimize #> $total -#> [1] 0.1268399 +#> [1] 0.126792 #> +fit_laplace$time()$total + fit_mle$time()$total # total time +#> [1] 0.2588468 # }
    diff --git a/docs/reference/fit-method-unconstrain_draws.html b/docs/reference/fit-method-unconstrain_draws.html index 3efbaca46..b849e0ba0 100644 --- a/docs/reference/fit-method-unconstrain_draws.html +++ b/docs/reference/fit-method-unconstrain_draws.html @@ -22,7 +22,7 @@ cmdstanr - 0.6.0 + 0.7.0 @@ -143,7 +143,6 @@

    See also

    Examples

    # \dontrun{
     fit_mcmc <- cmdstanr_example("logistic", method = "sample", force_recompile = TRUE)
    -#> Compiling Stan program...
     fit_mcmc$init_model_methods()
     #> Compiling additional model methods...
     
    diff --git a/docs/reference/fit-method-unconstrain_variables.html b/docs/reference/fit-method-unconstrain_variables.html
    index ba0c10ce3..f2b96cabc 100644
    --- a/docs/reference/fit-method-unconstrain_variables.html
    +++ b/docs/reference/fit-method-unconstrain_variables.html
    @@ -18,7 +18,7 @@
           
           
             cmdstanr
    -        0.6.0
    +        0.7.0
           
         
    @@ -131,7 +131,6 @@

    See also

    Examples

    # \dontrun{
     fit_mcmc <- cmdstanr_example("logistic", method = "sample", force_recompile = TRUE)
    -#> Compiling Stan program...
     fit_mcmc$init_model_methods()
     #> Compiling additional model methods...
     fit_mcmc$unconstrain_variables(list(alpha = 0.5, beta = c(0.7, 1.1, 0.2)))
    diff --git a/docs/reference/fit-method-variable_skeleton.html b/docs/reference/fit-method-variable_skeleton.html
    index 0bc3100e1..1fb28b4c3 100644
    --- a/docs/reference/fit-method-variable_skeleton.html
    +++ b/docs/reference/fit-method-variable_skeleton.html
    @@ -1,7 +1,7 @@
     
    -Return the variable skeleton needed by the utils::relist function to re-structure a
    -vector of constrained parameter values to a named list — fit-method-variable_skeleton • cmdstanrReturn the variable skeleton for relist — fit-method-variable_skeleton • cmdstanr
    @@ -19,7 +19,7 @@
           
           
             cmdstanr
    -        0.6.0
    +        0.7.0
           
         
    @@ -100,14 +100,15 @@
    -

    The $variable_skeleton() method returns the variable skeleton

    +

    The $variable_skeleton() method returns the variable skeleton +needed by utils::relist() to re-structure a vector of constrained +parameter values to a named list.

    @@ -117,13 +118,13 @@

    Return the variable skeleton needed by the utils::relist function to re-stru

    Arguments

    transformed_parameters
    -

    (boolean) Whether to include transformed parameters -in the skeleton (defaults to TRUE)

    +

    (logical) Whether to include transformed +parameters in the skeleton (defaults to TRUE).

    generated_quantities
    -

    (boolean) Whether to include generated quantities -in the skeleton (defaults to TRUE)

    +

    (logical) Whether to include generated quantities +in the skeleton (defaults to TRUE).

    @@ -136,11 +137,21 @@

    See also

    Examples

    # \dontrun{
    -fit_mcmc <- cmdstanr_example("logistic", method = "sample")
    +fit_mcmc <- cmdstanr_example("logistic", method = "sample", force_recompile = TRUE)
     fit_mcmc$init_model_methods()
    -#> Error: Model methods cannot be used with a pre-compiled Stan executable, the model must be compiled again
    +#> Compiling additional model methods...
     fit_mcmc$variable_skeleton()
    -#> Error: The method has not been compiled, please call `init_model_methods()` first
    +#> $alpha
    +#> [1] 0
    +#> 
    +#> $beta
    +#> [1] 0 0 0
    +#> 
    +#> $log_lik
    +#>   [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    +#>  [38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    +#>  [75] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    +#> 
     # }
     
     
    diff --git a/docs/reference/index.html b/docs/reference/index.html index ad6c2b705..c1a096fea 100644 --- a/docs/reference/index.html +++ b/docs/reference/index.html @@ -17,7 +17,7 @@ cmdstanr - 0.6.0 + 0.7.0
    @@ -157,10 +157,18 @@

    Running CmdStan from R generate_quantities()

    Run Stan's standalone generated quantities method

    + +

    laplace()

    + +

    Run Stan's Laplace algorithm

    optimize()

    Run Stan's optimization algorithms

    + +

    pathfinder()

    + +

    Run Stan's Pathfinder Variational Inference Algorithm

    sample()

    @@ -193,10 +201,18 @@

    Fitted model objects and methods CmdStanMLE

    CmdStanMLE objects

    + +

    CmdStanLaplace

    + +

    CmdStanLaplace objects

    CmdStanVB

    CmdStanVB objects

    + +

    CmdStanPathfinder

    + +

    CmdStanPathfinder objects

    CmdStanGQ

    @@ -261,7 +277,7 @@

    Fitted model objects and methods

    Leave-one-out cross-validation (LOO-CV)

    -

    lp() lp_approx()

    +

    lp() lp_approx() lp_approx()

    Extract log probability (target)

    @@ -350,7 +366,7 @@

    Other tools for working with CmdSt

    Convert CmdStanMCMC to mcmc.list

    -

    as_draws(<CmdStanMCMC>) as_draws(<CmdStanMLE>) as_draws(<CmdStanVB>) as_draws(<CmdStanGQ>)

    +

    as_draws(<CmdStanMCMC>) as_draws(<CmdStanMLE>) as_draws(<CmdStanLaplace>) as_draws(<CmdStanVB>) as_draws(<CmdStanGQ>) as_draws(<CmdStanPathfinder>)

    Create a draws object from a CmdStanR fitted model object

    diff --git a/docs/reference/install_cmdstan.html b/docs/reference/install_cmdstan.html index 5995ec079..f63b9b783 100644 --- a/docs/reference/install_cmdstan.html +++ b/docs/reference/install_cmdstan.html @@ -34,7 +34,7 @@ cmdstanr - 0.6.0 + 0.7.0

    @@ -150,6 +150,7 @@

    Install CmdStan or clean and rebuild an existing installation

    timeout = 1200, version = NULL, release_url = NULL, + release_file = NULL, cpp_options = list(), check_toolchain = TRUE, wsl = FALSE @@ -217,6 +218,13 @@

    Arguments

    If both version and release_url are specified then version will be used.

    +
    release_file
    +

    (string) A file path to a CmdStan release tar.gz file +downloaded from the releases page: https://github.com/stan-dev/cmdstan/releases. +For example: release_file=""./cmdstan-2.33.1.tar.gz". If release_file is +specified then both release_url and version will be ignored.

    + +
    cpp_options

    (list) Any makefile flags/variables to be written to the make/local file. For example, list("CXX" = "clang++") will force diff --git a/docs/reference/model-method-check_syntax.html b/docs/reference/model-method-check_syntax.html index 1d321677e..6ec6f59e1 100644 --- a/docs/reference/model-method-check_syntax.html +++ b/docs/reference/model-method-check_syntax.html @@ -19,7 +19,7 @@ cmdstanr - 0.6.0 + 0.7.0

    @@ -125,7 +125,7 @@

    Arguments

    pedantic

    (logical) Should pedantic mode be turned on? The default is FALSE. Pedantic mode attempts to warn you about potential issues in your -Stan program beyond syntax errors. For details see the Pedantic mode chapter in +Stan program beyond syntax errors. For details see the Pedantic mode chapter in the Stan Reference Manual.

    @@ -168,7 +168,9 @@

    See also

    model-method-expose_functions, model-method-format, model-method-generate-quantities, +model-method-laplace, model-method-optimize, +model-method-pathfinder, model-method-sample_mpi, model-method-sample, model-method-variables, @@ -181,7 +183,7 @@

    Examples

    file <- write_stan_file(" data { int N; - int y[N]; + array[N] int y; } parameters { // should have <lower=0> but omitting to demonstrate pedantic mode @@ -195,22 +197,12 @@

    Examples

    # the program is syntactically correct, however... mod$check_syntax() -#> Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/model_74b584fd23ea8c78eceda86d1d425fe8.stan', line 4, column 2: Declaration -#> of arrays by placing brackets after a variable name is deprecated and -#> will be removed in Stan 2.33.0. Instead use the array keyword before the -#> type. This can be changed automatically using the auto-format flag to -#> stanc #> Stan program is syntactically correct # pedantic mode will warn that lambda should be constrained to be positive # and that lambda has no prior distribution mod$check_syntax(pedantic = TRUE) -#> Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/model_74b584fd23ea8c78eceda86d1d425fe8.stan', line 4, column 2: Declaration -#> of arrays by placing brackets after a variable name is deprecated and -#> will be removed in Stan 2.33.0. Instead use the array keyword before the -#> type. This can be changed automatically using the auto-format flag to -#> stanc -#> Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/model_74b584fd23ea8c78eceda86d1d425fe8.stan', line 11, column 14: A +#> Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/model_febb1e69c7387a0e64cf13583e078104.stan', line 11, column 14: A #> poisson distribution is given parameter lambda as a rate parameter #> (argument 1), but lambda was not constrained to be strictly positive. #> Warning: The parameter lambda has no priors. This means either no prior is diff --git a/docs/reference/model-method-compile.html b/docs/reference/model-method-compile.html index 63b0fa515..e5a50034d 100644 --- a/docs/reference/model-method-compile.html +++ b/docs/reference/model-method-compile.html @@ -31,7 +31,7 @@ cmdstanr - 0.6.0 + 0.7.0
    @@ -148,6 +148,7 @@

    Compile a Stan program

    compile_model_methods = FALSE, compile_hessian_method = FALSE, compile_standalone = FALSE, + dry_run = FALSE, threads = FALSE )
    @@ -170,7 +171,7 @@

    Arguments

    pedantic

    (logical) Should pedantic mode be turned on? The default is FALSE. Pedantic mode attempts to warn you about potential issues in your -Stan program beyond syntax errors. For details see the Pedantic mode chapter in +Stan program beyond syntax errors. For details see the Pedantic mode chapter in the Stan Reference Manual. Note: to do a pedantic check for a model without compiling it or for a model that is already compiled the $check_syntax() method can be used instead.

    @@ -227,6 +228,11 @@

    Arguments

    $expose_functions() method.

    +
    dry_run
    +

    (logical) If TRUE, the code will do all checks before compilation, +but skip the actual C++ compilation. Used to speedup tests.

    + +
    threads

    Deprecated and will be removed in a future release. Please turn on threading via cpp_options = list(stan_threads = TRUE) instead.

    @@ -261,7 +267,9 @@

    See also

    model-method-expose_functions, model-method-format, model-method-generate-quantities, +model-method-laplace, model-method-optimize, +model-method-pathfinder, model-method-sample_mpi, model-method-sample, model-method-variables, @@ -278,12 +286,12 @@

    Examples

    mod <- cmdstan_model(file, compile = FALSE) mod$compile() mod$exe_file() -#> [1] "/Users/jgabry/.cmdstan/cmdstan-2.32.2/examples/bernoulli/bernoulli" +#> [1] "/Users/jgabry/.cmdstan/cmdstan-2.33.1/examples/bernoulli/bernoulli" # turn on threading support (for using functions that support within-chain parallelization) mod$compile(force_recompile = TRUE, cpp_options = list(stan_threads = TRUE)) mod$exe_file() -#> [1] "/Users/jgabry/.cmdstan/cmdstan-2.32.2/examples/bernoulli/bernoulli" +#> [1] "/Users/jgabry/.cmdstan/cmdstan-2.33.1/examples/bernoulli/bernoulli" # turn on pedantic mode (new in Stan v2.24) file_pedantic <- write_stan_file(" @@ -295,7 +303,7 @@

    Examples

    } ") mod <- cmdstan_model(file_pedantic, pedantic = TRUE) -#> Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/model-1621885c67f.stan', line 6, column 2: Parameter +#> Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/model-5f0d242e1d46.stan', line 6, column 2: Parameter #> sigma is given a exponential distribution, which has strictly positive #> support, but sigma was not constrained to be strictly positive. diff --git a/docs/reference/model-method-diagnose.html b/docs/reference/model-method-diagnose.html index 548b5959b..9a09d3085 100644 --- a/docs/reference/model-method-diagnose.html +++ b/docs/reference/model-method-diagnose.html @@ -21,7 +21,7 @@ cmdstanr - 0.6.0 + 0.7.0 @@ -165,10 +165,10 @@

    Arguments

    write_stan_json() to write R objects to JSON files compatible with CmdStan.

  • A list of lists containing initial values for all or some parameters. For -MCMC the list should contain a sublist for each chain. For optimization and -variational inference there should be just one sublist. The sublists should -have named elements corresponding to the parameters for which you are -specifying initial values. See Examples.

  • +MCMC the list should contain a sublist for each chain. For other model +fitting methods there should be just one sublist. The sublists should have +named elements corresponding to the parameters for which you are specifying +initial values. See Examples.

  • A function that returns a single list with names corresponding to the parameters for which you are specifying initial values. The function can take no arguments or a single argument chain_id. For MCMC, if the function @@ -229,7 +229,9 @@

    See also

    model-method-expose_functions, model-method-format, model-method-generate-quantities, +model-method-laplace, model-method-optimize, +model-method-pathfinder, model-method-sample_mpi, model-method-sample, model-method-variables, @@ -243,11 +245,11 @@

    Examples

    # retrieve the gradients test$gradients() -#> param_idx value model finite_diff error -#> 1 0 -1.310990 28.37700 28.37700 -1.92695e-08 -#> 2 1 -1.931430 8.55341 8.55341 -1.60934e-08 -#> 3 2 0.751196 -12.17180 -12.17180 -2.15070e-08 -#> 4 3 -1.808550 25.15020 25.15020 6.91254e-09 +#> param_idx value model finite_diff error +#> 1 0 -1.0700800 30.35590 30.35590 -8.78525e-09 +#> 2 1 0.5670250 -22.14500 -22.14500 9.99824e-09 +#> 3 2 0.0466329 -10.07860 -10.07860 1.68498e-08 +#> 4 3 1.1724800 -5.09201 -5.09201 2.79421e-10 # } diff --git a/docs/reference/model-method-expose_functions.html b/docs/reference/model-method-expose_functions.html index ad674e0e1..c4f4d0d11 100644 --- a/docs/reference/model-method-expose_functions.html +++ b/docs/reference/model-method-expose_functions.html @@ -25,7 +25,7 @@ cmdstanr - 0.6.0 + 0.7.0 @@ -153,7 +153,9 @@

    See also

    model-method-diagnose, model-method-format, model-method-generate-quantities, +model-method-laplace, model-method-optimize, +model-method-pathfinder, model-method-sample_mpi, model-method-sample, model-method-variables, diff --git a/docs/reference/model-method-format.html b/docs/reference/model-method-format.html index 041779776..3e6ef1005 100644 --- a/docs/reference/model-method-format.html +++ b/docs/reference/model-method-format.html @@ -19,7 +19,7 @@ cmdstanr - 0.6.0 + 0.7.0 @@ -174,7 +174,9 @@

    See also

    model-method-diagnose, model-method-expose_functions, model-method-generate-quantities, +model-method-laplace, model-method-optimize, +model-method-pathfinder, model-method-sample_mpi, model-method-sample, model-method-variables, @@ -184,10 +186,41 @@

    See also

    Examples

    # \dontrun{
    +
    +# Example of fixing old syntax
    +# real x[2] --> array[2] real x;
    +file <- write_stan_file("
    +parameters {
    +  real x[2];
    +}
    +model {
    +  x ~ std_normal();
    +}
    +")
    +
    +# set compile=FALSE then call format to fix old syntax
    +mod <- cmdstan_model(file, compile = FALSE)
    +mod$format(canonicalize = list("deprecations"))
    +#> parameters {
    +#>   array[2] real x;
    +#> }
    +#> model {
    +#>   x ~ std_normal();
    +#> }
    +#> 
    +#> 
    +
    +# overwrite the original file instead of just printing it
    +mod$format(canonicalize = list("deprecations"), overwrite_file = TRUE)
    +#> Old version of the model stored to /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/model_1fc88c86300d78667dd3e476a636c279.stan.bak-20231213100754.
    +mod$compile()
    +
    +
    +# Example of removing unnecessary whitespace
     file <- write_stan_file("
     data {
       int N;
    -  int y[N];
    +  array[N] int y;
     }
     parameters {
       real                     lambda;
    diff --git a/docs/reference/model-method-generate-quantities.html b/docs/reference/model-method-generate-quantities.html
    index 63e57c29a..80a23de27 100644
    --- a/docs/reference/model-method-generate-quantities.html
    +++ b/docs/reference/model-method-generate-quantities.html
    @@ -19,7 +19,7 @@
           
           
             cmdstanr
    -        0.6.0
    +        0.7.0
           
         
    @@ -243,7 +243,9 @@

    See also

    model-method-diagnose, model-method-expose_functions, model-method-format, +model-method-laplace, model-method-optimize, +model-method-pathfinder, model-method-sample_mpi, model-method-sample, model-method-variables, @@ -257,7 +259,7 @@

    Examples

    mcmc_program <- write_stan_file( "data { int<lower=0> N; - int<lower=0,upper=1> y[N]; + array[N] int<lower=0,upper=1> y; } parameters { real<lower=0,upper=1> theta; @@ -287,13 +289,13 @@

    Examples

    gq_program <- write_stan_file( "data { int<lower=0> N; - int<lower=0,upper=1> y[N]; + array[N] int<lower=0,upper=1> y; } parameters { real<lower=0,upper=1> theta; } generated quantities { - int y_rep[N] = bernoulli_rng(rep_vector(theta, N)); + array[N] int y_rep = bernoulli_rng(rep_vector(theta, N)); }" ) diff --git a/docs/reference/model-method-laplace-1.png b/docs/reference/model-method-laplace-1.png new file mode 100644 index 000000000..aaf21c132 Binary files /dev/null and b/docs/reference/model-method-laplace-1.png differ diff --git a/docs/reference/model-method-laplace.html b/docs/reference/model-method-laplace.html new file mode 100644 index 000000000..20880c2f9 --- /dev/null +++ b/docs/reference/model-method-laplace.html @@ -0,0 +1,426 @@ + +Run Stan's Laplace algorithm — model-method-laplace • cmdstanr + + +
    +
    + + + +
    +
    + + +
    +

    The $laplace() method of a CmdStanModel object produces a +sample from a normal approximation centered at the mode of a distribution +in the unconstrained space. If the mode is a maximum a posteriori (MAP) +estimate, the samples provide an estimate of the mean and standard +deviation of the posterior distribution. If the mode is a maximum +likelihood estimate (MLE), the sample provides an estimate of the standard +error of the likelihood. Whether the mode is the MAP or MLE depends on +the value of the jacobian argument when running optimization. See the +CmdStan User’s Guide +for more details.

    +

    Any argument left as NULL will default to the default value used by the +installed version of CmdStan.

    +
    + +
    +
    laplace(
    +  data = NULL,
    +  seed = NULL,
    +  refresh = NULL,
    +  init = NULL,
    +  save_latent_dynamics = FALSE,
    +  output_dir = NULL,
    +  output_basename = NULL,
    +  sig_figs = NULL,
    +  threads = NULL,
    +  opencl_ids = NULL,
    +  mode = NULL,
    +  opt_args = NULL,
    +  jacobian = TRUE,
    +  draws = NULL
    +)
    +
    + +
    +

    Arguments

    +
    data
    +

    (multiple options) The data to use for the variables specified in +the data block of the Stan program. One of the following:

    • A named list of R objects with the names corresponding to variables +declared in the data block of the Stan program. Internally this list is then +written to JSON for CmdStan using write_stan_json(). See +write_stan_json() for details on the conversions performed on R objects +before they are passed to Stan.

    • +
    • A path to a data file compatible with CmdStan (JSON or R dump). See the +appendices in the CmdStan guide for details on using these formats.

    • +
    • NULL or an empty list if the Stan program has no data block.

    • +
    + + +
    seed
    +

    (positive integer(s)) A seed for the (P)RNG to pass to CmdStan. +In the case of multi-chain sampling the single seed will automatically be +augmented by the the run (chain) ID so that each chain uses a different +seed. The exception is the transformed data block, which defaults to +using same seed for all chains so that the same data is generated for all +chains if RNG functions are used. The only time seed should be specified +as a vector (one element per chain) is if RNG functions are used in +transformed data and the goal is to generate different data for each +chain.

    + + +
    refresh
    +

    (non-negative integer) The number of iterations between +printed screen updates. If refresh = 0, only error messages will be +printed.

    + + +
    init
    +

    (multiple options) The initialization method to use for the +variables declared in the parameters block of the Stan program. One of +the following:

    • A real number x>0. This initializes all parameters randomly between +[-x,x] on the unconstrained parameter space.;

    • +
    • The number 0. This initializes all parameters to 0;

    • +
    • A character vector of paths (one per chain) to JSON or Rdump files +containing initial values for all or some parameters. See +write_stan_json() to write R objects to JSON files compatible with +CmdStan.

    • +
    • A list of lists containing initial values for all or some parameters. For +MCMC the list should contain a sublist for each chain. For other model +fitting methods there should be just one sublist. The sublists should have +named elements corresponding to the parameters for which you are specifying +initial values. See Examples.

    • +
    • A function that returns a single list with names corresponding to the +parameters for which you are specifying initial values. The function can +take no arguments or a single argument chain_id. For MCMC, if the function +has argument chain_id it will be supplied with the chain id (from 1 to +number of chains) when called to generate the initial values. See +Examples.

    • +
    + + +
    save_latent_dynamics
    +

    Ignored for this method.

    + + +
    output_dir
    +

    (string) A path to a directory where CmdStan should write +its output CSV files. For interactive use this can typically be left at +NULL (temporary directory) since CmdStanR makes the CmdStan output +(posterior draws and diagnostics) available in R via methods of the fitted +model objects. The behavior of output_dir is as follows:

    • If NULL (the default), then the CSV files are written to a temporary +directory and only saved permanently if the user calls one of the $save_* +methods of the fitted model object (e.g., +$save_output_files()). These temporary +files are removed when the fitted model object is +garbage collected (manually or automatically).

    • +
    • If a path, then the files are created in output_dir with names +corresponding to the defaults used by $save_output_files().

    • +
    + + +
    output_basename
    +

    (string) A string to use as a prefix for the names of +the output CSV files of CmdStan. If NULL (the default), the basename of +the output CSV files will be comprised from the model name, timestamp, and +5 random characters.

    + + +
    sig_figs
    +

    (positive integer) The number of significant figures used +when storing the output values. By default, CmdStan represent the output +values with 6 significant figures. The upper limit for sig_figs is 18. +Increasing this value will result in larger output CSV files and thus an +increased usage of disk space.

    + + +
    threads
    +

    (positive integer) If the model was +compiled with threading support, the number of +threads to use in parallelized sections (e.g., when +using the Stan functions reduce_sum() or map_rect()).

    + + +
    opencl_ids
    +

    (integer vector of length 2) The platform and +device IDs of the OpenCL device to use for fitting. The model must +be compiled with cpp_options = list(stan_opencl = TRUE) for this +argument to have an effect.

    + + +
    mode
    +

    (multiple options) The mode to center the approximation at. One +of the following:

    • A CmdStanMLE object from a previous run of $optimize().

    • +
    • The path to a CmdStan CSV file from running optimization.

    • +
    • NULL, in which case $optimize() will be run +with jacobian=jacobian (see the jacobian argument below).

    • +

    In all cases the total time reported by $time() will be +the time of the Laplace sampling step only and does not include the time +taken to run the $optimize() method.

    + + +
    opt_args
    +

    (named list) A named list of optional arguments to pass to +$optimize() if mode=NULL.

    + + +
    jacobian
    +

    (logical) Whether or not to enable the Jacobian adjustment +for constrained parameters. The default is TRUE. See the +Laplace Sampling +section of the CmdStan User's Guide for more details. If mode is not +NULL then the value of jacobian must match the value used when +optimization was originally run. If mode is NULL then the value of +jacobian specified here is used when running optimization.

    + + +
    draws
    +

    (positive integer) The number of draws to take.

    + +
    +
    +

    Value

    + + +

    A CmdStanLaplace object.

    +
    + + +
    +

    Examples

    +
    # \dontrun{
    +file <- file.path(cmdstan_path(), "examples/bernoulli/bernoulli.stan")
    +mod <- cmdstan_model(file)
    +mod$print()
    +#> data {
    +#>   int<lower=0> N;
    +#>   array[N] int<lower=0,upper=1> y;
    +#> }
    +#> parameters {
    +#>   real<lower=0,upper=1> theta;
    +#> }
    +#> model {
    +#>   theta ~ beta(1,1);  // uniform prior on interval 0,1
    +#>   y ~ bernoulli(theta);
    +#> }
    +
    +stan_data <- list(N = 10, y = c(0,1,0,0,0,0,0,0,0,1))
    +fit_mode <- mod$optimize(data = stan_data, jacobian = TRUE)
    +#> Initial log joint probability = -8.0994 
    +#>     Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes  
    +#>        5      -6.74802   0.000245062   1.61833e-06           1           1        8    
    +#> Optimization terminated normally:  
    +#>   Convergence detected: relative gradient magnitude is below tolerance 
    +#> Finished in  0.1 seconds.
    +fit_laplace <- mod$laplace(data = stan_data, mode = fit_mode)
    +#> Calculating Hessian 
    +#> Calculating inverse of Cholesky factor 
    +#> Generating draws 
    +#> iteration: 0 
    +#> iteration: 100 
    +#> iteration: 200 
    +#> iteration: 300 
    +#> iteration: 400 
    +#> iteration: 500 
    +#> iteration: 600 
    +#> iteration: 700 
    +#> iteration: 800 
    +#> iteration: 900 
    +#> Finished in  0.1 seconds.
    +fit_laplace$summary()
    +#> # A tibble: 3 × 7
    +#>   variable      mean median    sd   mad      q5      q95
    +#>   <chr>        <dbl>  <dbl> <dbl> <dbl>   <dbl>    <dbl>
    +#> 1 lp__        -7.25  -6.97  0.724 0.305 -8.72   -6.75   
    +#> 2 lp_approx__ -0.516 -0.225 0.747 0.306 -1.93   -0.00324
    +#> 3 theta        0.267  0.250 0.124 0.120  0.0954  0.499  
    +
    +# if mode isn't specified optimize is run internally first
    +fit_laplace <- mod$laplace(data = stan_data)
    +#> Initial log joint probability = -15.1067 
    +#>     Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes  
    +#>        5      -6.74802    0.00203152   6.06231e-05           1           1        8    
    +#> Optimization terminated normally:  
    +#>   Convergence detected: relative gradient magnitude is below tolerance 
    +#> Finished in  0.1 seconds.
    +#> Calculating Hessian 
    +#> Calculating inverse of Cholesky factor 
    +#> Generating draws 
    +#> iteration: 0 
    +#> iteration: 100 
    +#> iteration: 200 
    +#> iteration: 300 
    +#> iteration: 400 
    +#> iteration: 500 
    +#> iteration: 600 
    +#> iteration: 700 
    +#> iteration: 800 
    +#> iteration: 900 
    +#> Finished in  0.1 seconds.
    +fit_laplace$summary()
    +#> # A tibble: 3 × 7
    +#>   variable      mean median    sd   mad      q5      q95
    +#>   <chr>        <dbl>  <dbl> <dbl> <dbl>   <dbl>    <dbl>
    +#> 1 lp__        -7.28  -6.98  0.782 0.316 -8.84   -6.75   
    +#> 2 lp_approx__ -0.531 -0.231 0.756 0.320 -1.96   -0.00280
    +#> 3 theta        0.273  0.251 0.128 0.123  0.0984  0.510  
    +
    +# plot approximate posterior
    +bayesplot::mcmc_hist(fit_laplace$draws("theta"))
    +#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
    +
    +# }
    +
    +
    +
    +
    +
    + +
    + + +
    + +
    +

    Site built with pkgdown 2.0.7.

    +
    + +
    + + + + + + + + diff --git a/docs/reference/model-method-optimize-1.png b/docs/reference/model-method-optimize-1.png index 27fd88217..0aa41c0e9 100644 Binary files a/docs/reference/model-method-optimize-1.png and b/docs/reference/model-method-optimize-1.png differ diff --git a/docs/reference/model-method-optimize-2.png b/docs/reference/model-method-optimize-2.png index 4ed9fe5c5..835a8a667 100644 Binary files a/docs/reference/model-method-optimize-2.png and b/docs/reference/model-method-optimize-2.png differ diff --git a/docs/reference/model-method-optimize-3.png b/docs/reference/model-method-optimize-3.png index e985d6949..8d58780fd 100644 Binary files a/docs/reference/model-method-optimize-3.png and b/docs/reference/model-method-optimize-3.png differ diff --git a/docs/reference/model-method-optimize.html b/docs/reference/model-method-optimize.html index b4d1ad67a..78b2a042d 100644 --- a/docs/reference/model-method-optimize.html +++ b/docs/reference/model-method-optimize.html @@ -1,11 +1,14 @@ Run Stan's optimization algorithms — model-method-optimize • cmdstanr @@ -23,7 +26,7 @@ cmdstanr - 0.6.0 + 0.7.0
    @@ -111,12 +114,15 @@

    Run Stan's optimization algorithms

    The $optimize() method of a CmdStanModel object runs -Stan's optimizer to obtain a posterior mode (penalized maximum likelihood) -estimate.

    -

    Any argument left as NULL will default to the default value used by the -installed version of CmdStan. See the -CmdStan User’s Guide +Stan's optimizer to obtain a (penalized) maximum likelihood estimate or a +maximum a posteriori estimate (if jacobian=TRUE). See the +CmdStan User's Guide for more details.

    +

    Any argument left as NULL will default to the default value used by the +installed version of CmdStan. See the CmdStan User’s Guide for more details on the +default arguments. The default values can also be obtained by checking the +metadata of an example model, e.g., +cmdstanr_example(method="optimize")$metadata().

    @@ -132,6 +138,7 @@

    Run Stan's optimization algorithms

    threads = NULL, opencl_ids = NULL, algorithm = NULL, + jacobian = FALSE, init_alpha = NULL, iter = NULL, tol_obj = NULL, @@ -187,10 +194,10 @@

    Arguments

    write_stan_json() to write R objects to JSON files compatible with CmdStan.

  • A list of lists containing initial values for all or some parameters. For -MCMC the list should contain a sublist for each chain. For optimization and -variational inference there should be just one sublist. The sublists should -have named elements corresponding to the parameters for which you are -specifying initial values. See Examples.

  • +MCMC the list should contain a sublist for each chain. For other model +fitting methods there should be just one sublist. The sublists should have +named elements corresponding to the parameters for which you are specifying +initial values. See Examples.

  • A function that returns a single list with names corresponding to the parameters for which you are specifying initial values. The function can take no arguments or a single argument chain_id. For MCMC, if the function @@ -265,6 +272,17 @@

    Arguments

    running cmdstanr_example(method="optimize")$metadata().

    +
    jacobian
    +

    (logical) Whether or not to use the Jacobian adjustment for +constrained variables. For historical reasons, the default is FALSE, meaning optimization +yields the (regularized) maximum likelihood estimate. Setting it to TRUE +yields the maximum a posteriori estimate. See the +Maximum Likelihood Estimation +section of the CmdStan User's Guide for more details. +For use later with $laplace() the jacobian +argument should typically be set to TRUE.

    + +
    init_alpha

    (positive real) The initial step size parameter.

    @@ -303,16 +321,6 @@

    Value

    A CmdStanMLE object.

    - -
    -

    Details

    -

    CmdStan can find the posterior mode (assuming there is one). If the -posterior is not convex, there is no guarantee Stan will be able to find -the global mode as opposed to a local optimum of log probability. For -optimization, the mode is calculated without the Jacobian adjustment for -constrained variables, which shifts the mode due to the change of -variables. Thus modes correspond to modes of the model as written.

    -

    -- CmdStan User's Guide

    See also

    @@ -328,6 +336,8 @@

    See also

    model-method-expose_functions, model-method-format, model-method-generate-quantities, +model-method-laplace, +model-method-pathfinder, model-method-sample_mpi, model-method-sample, model-method-variables, @@ -348,7 +358,7 @@

    Examples

    # Otherwise use the `path` argument to specify the location of your # CmdStan installation.) set_cmdstan_path(path = NULL) -#> CmdStan path set to: /Users/jgabry/.cmdstan/cmdstan-2.32.2 +#> CmdStan path set to: /Users/jgabry/.cmdstan/cmdstan-2.33.1 # Create a CmdStanModel object from a Stan program, # here using the example model that comes with CmdStan @@ -435,10 +445,22 @@

    Examples

    fit_mcmc$summary() #> # A tibble: 2 × 10 #> variable mean median sd mad q5 q95 rhat ess_bulk ess_tail -#> <chr> <num> <num> <num> <num> <num> <num> <num> <num> <num> +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 lp__ -7.30 -7.03 0.721 0.380 -8.82 -6.75 1.00 902. 1006. #> 2 theta 0.247 0.233 0.122 0.129 0.0786 0.470 1.00 762. 712. +# Check sampling diagnostics +fit_mcmc$diagnostic_summary() +#> $num_divergent +#> [1] 0 0 +#> +#> $num_max_treedepth +#> [1] 0 0 +#> +#> $ebfmi +#> [1] 1.017555 1.250490 +#> + # Get posterior draws draws <- fit_mcmc$draws() print(draws) @@ -487,51 +509,8 @@

    Examples

    #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. -# Call CmdStan's diagnose and stansummary utilities -fit_mcmc$cmdstan_diagnose() -#> Processing csv files: /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/bernoulli-202307251438-1-65b170.csv, /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/bernoulli-202307251438-2-65b170.csv -#> -#> Checking sampler transitions treedepth. -#> Treedepth satisfactory for all transitions. -#> -#> Checking sampler transitions for divergences. -#> No divergent transitions found. -#> -#> Checking E-BFMI - sampler transitions HMC potential energy. -#> E-BFMI satisfactory. -#> -#> Effective sample size satisfactory. -#> -#> Split R-hat values satisfactory all parameters. -#> -#> Processing complete, no problems detected. -fit_mcmc$cmdstan_summary() -#> Inference for Stan model: bernoulli_model -#> 2 chains: each with iter=(1000,1000); warmup=(0,0); thin=(1,1); 2000 iterations saved. -#> -#> Warmup took (0.0040, 0.0040) seconds, 0.0080 seconds total -#> Sampling took (0.011, 0.011) seconds, 0.022 seconds total -#> -#> Mean MCSE StdDev 5% 50% 95% N_Eff N_Eff/s R_hat -#> -#> lp__ -7.3 2.6e-02 0.72 -8.8 -7.0 -6.8 781 35502 1.0 -#> accept_stat__ 0.92 8.3e-03 0.13 0.64 0.97 1.0 235 10662 1.0e+00 -#> stepsize__ 0.95 7.9e-02 0.079 0.87 1.0 1.0 1.0 46 2.0e+13 -#> treedepth__ 1.4 1.1e-02 0.48 1.0 1.0 2.0 1874 85179 1.0e+00 -#> n_leapfrog__ 2.5 1.4e-01 1.3 1.0 3.0 3.0 89 4050 1.0e+00 -#> divergent__ 0.00 nan 0.00 0.00 0.00 0.00 nan nan nan -#> energy__ 7.8 3.6e-02 1.00 6.8 7.5 9.6 775 35215 1.0e+00 -#> -#> theta 0.25 4.3e-03 0.12 0.079 0.23 0.47 796 36197 1.0 -#> -#> Samples were drawn using hmc with nuts. -#> For each parameter, N_Eff is a crude measure of effective sample size, -#> and R_hat is the potential scale reduction factor on split chains (at -#> convergence, R_hat=1). - # For models fit using MCMC, if you like working with RStan's stanfit objects # then you can create one with rstan::read_stan_csv() - # stanfit <- rstan::read_stan_csv(fit_mcmc$output_files()) @@ -545,24 +524,64 @@

    Examples

    #> Optimization terminated normally: #> Convergence detected: relative gradient magnitude is below tolerance #> Finished in 0.1 seconds. - fit_optim$summary() #> # A tibble: 2 × 2 #> variable estimate -#> <chr> <num> +#> <chr> <dbl> #> 1 lp__ -5.00 #> 2 theta 0.2 +# Run 'optimize' again with 'jacobian=TRUE' and then draw from Laplace approximation +# to the posterior +fit_optim <- mod$optimize(data = my_data_file, jacobian = TRUE) +#> Initial log joint probability = -7.17234 +#> Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes +#> 5 -6.74802 0.000196979 1.66919e-07 1 1 8 +#> Optimization terminated normally: +#> Convergence detected: relative gradient magnitude is below tolerance +#> Finished in 0.1 seconds. +fit_laplace <- mod$laplace(data = my_data_file, mode = fit_optim, draws = 2000) +#> Calculating Hessian +#> Calculating inverse of Cholesky factor +#> Generating draws +#> iteration: 0 +#> iteration: 100 +#> iteration: 200 +#> iteration: 300 +#> iteration: 400 +#> iteration: 500 +#> iteration: 600 +#> iteration: 700 +#> iteration: 800 +#> iteration: 900 +#> iteration: 1000 +#> iteration: 1100 +#> iteration: 1200 +#> iteration: 1300 +#> iteration: 1400 +#> iteration: 1500 +#> iteration: 1600 +#> iteration: 1700 +#> iteration: 1800 +#> iteration: 1900 +#> Finished in 0.1 seconds. +fit_laplace$summary() +#> # A tibble: 3 × 7 +#> variable mean median sd mad q5 q95 +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> +#> 1 lp__ -7.26 -6.98 0.759 0.313 -8.67 -6.75 +#> 2 lp_approx__ -0.521 -0.232 0.759 0.318 -2.07 -0.00238 +#> 3 theta 0.266 0.248 0.125 0.123 0.0975 0.496 -# Run 'variational' method to approximate the posterior (default is meanfield ADVI) +# Run 'variational' method to use ADVI to approximate posterior fit_vb <- mod$variational(data = stan_data, seed = 123) #> ------------------------------------------------------------ #> EXPERIMENTAL ALGORITHM: #> This procedure has not been thoroughly tested and may be unstable #> or buggy. The interface is subject to change. #> ------------------------------------------------------------ -#> Gradient evaluation took 8e-06 seconds -#> 1000 transitions using 10 leapfrog steps per transition would take 0.08 seconds. +#> Gradient evaluation took 6e-06 seconds +#> 1000 transitions using 10 leapfrog steps per transition would take 0.06 seconds. #> Adjust your expectations accordingly! #> Begin eta adaptation. #> Iteration: 1 / 250 [ 0%] (Adaptation) @@ -579,20 +598,94 @@

    Examples

    #> Drawing a sample of size 1000 from the approximate posterior... #> COMPLETED. #> Finished in 0.1 seconds. - fit_vb$summary() #> # A tibble: 3 × 7 #> variable mean median sd mad q5 q95 -#> <chr> <num> <num> <num> <num> <num> <num> +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 lp__ -7.18 -6.94 0.588 0.259 -8.36 -6.75 #> 2 lp_approx__ -0.515 -0.221 0.692 0.303 -2.06 -0.00257 #> 3 theta 0.263 0.246 0.115 0.113 0.106 0.481 - -# Plot approximate posterior using bayesplot mcmc_hist(fit_vb$draws("theta")) #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. +# Run 'pathfinder' method, a new alternative to the variational method +fit_pf <- mod$pathfinder(data = stan_data, seed = 123) +#> Path [1] :Initial log joint density = -11.008832 +#> Path [1] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 9.383e-04 1.391e-05 1.000e+00 1.000e+00 126 -6.264e+00 -6.264e+00 +#> Path [1] :Best Iter: [3] ELBO (-6.195408) evaluations: (126) +#> Path [2] :Initial log joint density = -7.318450 +#> Path [2] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 4 -6.748e+00 5.414e-03 1.618e-04 1.000e+00 1.000e+00 101 -6.251e+00 -6.251e+00 +#> Path [2] :Best Iter: [3] ELBO (-6.229174) evaluations: (101) +#> Path [3] :Initial log joint density = -12.374612 +#> Path [3] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.419e-03 2.837e-05 1.000e+00 1.000e+00 126 -6.199e+00 -6.199e+00 +#> Path [3] :Best Iter: [5] ELBO (-6.199185) evaluations: (126) +#> Path [4] :Initial log joint density = -13.009824 +#> Path [4] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.677e-03 3.885e-05 1.000e+00 1.000e+00 126 -6.173e+00 -6.173e+00 +#> Path [4] :Best Iter: [5] ELBO (-6.172860) evaluations: (126) +#> Total log probability function evaluations:4379 +#> Finished in 0.1 seconds. +fit_pf$summary() +#> # A tibble: 3 × 7 +#> variable mean median sd mad q5 q95 +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> +#> 1 lp_approx__ -1.08 -0.728 0.886 0.304 -2.71 -0.511 +#> 2 lp__ -7.26 -6.96 0.738 0.297 -8.72 -6.75 +#> 3 theta 0.249 0.230 0.120 0.121 0.0854 0.471 +mcmc_hist(fit_pf$draws("theta")) +#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. + + +# Run 'pathfinder' again with more paths, fewer draws per path, +# better covariance approximation, and fewer LBFGSs iterations +fit_pf <- mod$pathfinder(data = stan_data, num_paths=10, single_path_draws=40, + history_size=50, max_lbfgs_iters=100) +#> Path [1] :Initial log joint density = -16.937625 +#> Path [1] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.488e-03 4.162e-05 1.000e+00 1.000e+00 126 -6.227e+00 -6.227e+00 +#> Path [1] :Best Iter: [2] ELBO (-6.211714) evaluations: (126) +#> Path [2] :Initial log joint density = -6.798413 +#> Path [2] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 3 -6.748e+00 3.762e-03 4.627e-05 9.519e-01 9.519e-01 76 -6.195e+00 -6.195e+00 +#> Path [2] :Best Iter: [3] ELBO (-6.194747) evaluations: (76) +#> Path [3] :Initial log joint density = -7.054440 +#> Path [3] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 4 -6.748e+00 2.741e-03 5.225e-05 1.000e+00 1.000e+00 101 -6.187e+00 -6.187e+00 +#> Path [3] :Best Iter: [4] ELBO (-6.186757) evaluations: (101) +#> Path [4] :Initial log joint density = -7.467690 +#> Path [4] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.049e-04 4.080e-07 1.000e+00 1.000e+00 126 -6.224e+00 -6.224e+00 +#> Path [4] :Best Iter: [2] ELBO (-6.213804) evaluations: (126) +#> Path [5] :Initial log joint density = -14.817294 +#> Path [5] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 2.041e-03 5.999e-05 1.000e+00 1.000e+00 126 -6.271e+00 -6.271e+00 +#> Path [5] :Best Iter: [3] ELBO (-6.188344) evaluations: (126) +#> Path [6] :Initial log joint density = -7.375208 +#> Path [6] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 5.530e-04 1.038e-06 1.000e+00 1.000e+00 126 -6.229e+00 -6.229e+00 +#> Path [6] :Best Iter: [2] ELBO (-6.203592) evaluations: (126) +#> Path [7] :Initial log joint density = -6.748023 +#> Path [7] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 2 -6.748e+00 9.521e-04 6.579e-07 1.000e+00 1.000e+00 51 -6.231e+00 -6.231e+00 +#> Path [7] :Best Iter: [2] ELBO (-6.231405) evaluations: (51) +#> Path [8] :Initial log joint density = -11.312958 +#> Path [8] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.017e-03 1.576e-05 1.000e+00 1.000e+00 126 -6.304e+00 -6.304e+00 +#> Path [8] :Best Iter: [3] ELBO (-6.185214) evaluations: (126) +#> Path [9] :Initial log joint density = -6.881067 +#> Path [9] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 4 -6.748e+00 9.878e-04 9.538e-06 1.000e+00 1.000e+00 101 -6.173e+00 -6.173e+00 +#> Path [9] :Best Iter: [4] ELBO (-6.173184) evaluations: (101) +#> Path [10] :Initial log joint density = -7.173191 +#> Path [10] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.980e-04 1.685e-07 1.000e+00 1.000e+00 126 -6.176e+00 -6.176e+00 +#> Path [10] :Best Iter: [5] ELBO (-6.175625) evaluations: (126) +#> Total log probability function evaluations:1235 +#> Finished in 0.1 seconds. # Specifying initial values as a function fit_mcmc_w_init_fun <- mod$sample( diff --git a/docs/reference/model-method-pathfinder-1.png b/docs/reference/model-method-pathfinder-1.png new file mode 100644 index 000000000..0aa41c0e9 Binary files /dev/null and b/docs/reference/model-method-pathfinder-1.png differ diff --git a/docs/reference/model-method-pathfinder-2.png b/docs/reference/model-method-pathfinder-2.png new file mode 100644 index 000000000..835a8a667 Binary files /dev/null and b/docs/reference/model-method-pathfinder-2.png differ diff --git a/docs/reference/model-method-pathfinder-3.png b/docs/reference/model-method-pathfinder-3.png new file mode 100644 index 000000000..8d58780fd Binary files /dev/null and b/docs/reference/model-method-pathfinder-3.png differ diff --git a/docs/reference/model-method-pathfinder.html b/docs/reference/model-method-pathfinder.html new file mode 100644 index 000000000..e5caa27c5 --- /dev/null +++ b/docs/reference/model-method-pathfinder.html @@ -0,0 +1,828 @@ + +Run Stan's Pathfinder Variational Inference Algorithm — model-method-pathfinder • cmdstanr + + +
    +
    + + + +
    +
    + + +
    +

    The $pathfinder() method of a CmdStanModel object runs +Stan's Pathfinder algorithms. Pathfinder is a variational method for +approximately sampling from differentiable log densities. Starting from a +random initialization, Pathfinder locates normal approximations +to the target density along a quasi-Newton optimization path in +the unconstrained space, with local covariance estimated using +the negative inverse Hessian estimates produced by the LBFGS +optimizer. Pathfinder selects the normal approximation with the +lowest estimated Kullback-Leibler (KL) divergence to the true +posterior. Finally Pathfinder draws from that normal +approximation and returns the draws transformed to the +constrained scale. See the +CmdStan User’s Guide +for more details.

    +

    Any argument left as NULL will default to the default value used by the +installed version of CmdStan

    +
    + +
    +
    pathfinder(
    +  data = NULL,
    +  seed = NULL,
    +  refresh = NULL,
    +  init = NULL,
    +  save_latent_dynamics = FALSE,
    +  output_dir = NULL,
    +  output_basename = NULL,
    +  sig_figs = NULL,
    +  opencl_ids = NULL,
    +  num_threads = NULL,
    +  init_alpha = NULL,
    +  tol_obj = NULL,
    +  tol_rel_obj = NULL,
    +  tol_grad = NULL,
    +  tol_rel_grad = NULL,
    +  tol_param = NULL,
    +  history_size = NULL,
    +  single_path_draws = NULL,
    +  draws = NULL,
    +  num_paths = NULL,
    +  max_lbfgs_iters = NULL,
    +  num_elbo_draws = NULL,
    +  save_single_paths = NULL
    +)
    +
    + +
    +

    Arguments

    +
    data
    +

    (multiple options) The data to use for the variables specified in +the data block of the Stan program. One of the following:

    • A named list of R objects with the names corresponding to variables +declared in the data block of the Stan program. Internally this list is then +written to JSON for CmdStan using write_stan_json(). See +write_stan_json() for details on the conversions performed on R objects +before they are passed to Stan.

    • +
    • A path to a data file compatible with CmdStan (JSON or R dump). See the +appendices in the CmdStan guide for details on using these formats.

    • +
    • NULL or an empty list if the Stan program has no data block.

    • +
    + + +
    seed
    +

    (positive integer(s)) A seed for the (P)RNG to pass to CmdStan. +In the case of multi-chain sampling the single seed will automatically be +augmented by the the run (chain) ID so that each chain uses a different +seed. The exception is the transformed data block, which defaults to +using same seed for all chains so that the same data is generated for all +chains if RNG functions are used. The only time seed should be specified +as a vector (one element per chain) is if RNG functions are used in +transformed data and the goal is to generate different data for each +chain.

    + + +
    refresh
    +

    (non-negative integer) The number of iterations between +printed screen updates. If refresh = 0, only error messages will be +printed.

    + + +
    init
    +

    (multiple options) The initialization method to use for the +variables declared in the parameters block of the Stan program. One of +the following:

    • A real number x>0. This initializes all parameters randomly between +[-x,x] on the unconstrained parameter space.;

    • +
    • The number 0. This initializes all parameters to 0;

    • +
    • A character vector of paths (one per chain) to JSON or Rdump files +containing initial values for all or some parameters. See +write_stan_json() to write R objects to JSON files compatible with +CmdStan.

    • +
    • A list of lists containing initial values for all or some parameters. For +MCMC the list should contain a sublist for each chain. For other model +fitting methods there should be just one sublist. The sublists should have +named elements corresponding to the parameters for which you are specifying +initial values. See Examples.

    • +
    • A function that returns a single list with names corresponding to the +parameters for which you are specifying initial values. The function can +take no arguments or a single argument chain_id. For MCMC, if the function +has argument chain_id it will be supplied with the chain id (from 1 to +number of chains) when called to generate the initial values. See +Examples.

    • +
    + + +
    save_latent_dynamics
    +

    (logical) Should auxiliary diagnostic information +about the latent dynamics be written to temporary diagnostic CSV files? +This argument replaces CmdStan's diagnostic_file argument and the content +written to CSV is controlled by the user's CmdStan installation and not +CmdStanR (for some algorithms no content may be written). The default +is FALSE, which is appropriate for almost every use case. To save the +temporary files created when save_latent_dynamics=TRUE see the +$save_latent_dynamics_files() +method.

    + + +
    output_dir
    +

    (string) A path to a directory where CmdStan should write +its output CSV files. For interactive use this can typically be left at +NULL (temporary directory) since CmdStanR makes the CmdStan output +(posterior draws and diagnostics) available in R via methods of the fitted +model objects. The behavior of output_dir is as follows:

    • If NULL (the default), then the CSV files are written to a temporary +directory and only saved permanently if the user calls one of the $save_* +methods of the fitted model object (e.g., +$save_output_files()). These temporary +files are removed when the fitted model object is +garbage collected (manually or automatically).

    • +
    • If a path, then the files are created in output_dir with names +corresponding to the defaults used by $save_output_files().

    • +
    + + +
    output_basename
    +

    (string) A string to use as a prefix for the names of +the output CSV files of CmdStan. If NULL (the default), the basename of +the output CSV files will be comprised from the model name, timestamp, and +5 random characters.

    + + +
    sig_figs
    +

    (positive integer) The number of significant figures used +when storing the output values. By default, CmdStan represent the output +values with 6 significant figures. The upper limit for sig_figs is 18. +Increasing this value will result in larger output CSV files and thus an +increased usage of disk space.

    + + +
    opencl_ids
    +

    (integer vector of length 2) The platform and +device IDs of the OpenCL device to use for fitting. The model must +be compiled with cpp_options = list(stan_opencl = TRUE) for this +argument to have an effect.

    + + +
    num_threads
    +

    (positive integer) If the model was +compiled with threading support, the number of +threads to use in parallelized sections (e.g., for multi-path pathfinder +as well as reduce_sum).

    + + +
    init_alpha
    +

    (positive real) The initial step size parameter.

    + + +
    tol_obj
    +

    (positive real) Convergence tolerance on changes in objective function value.

    + + +
    tol_rel_obj
    +

    (positive real) Convergence tolerance on relative changes in objective function value.

    + + +
    tol_grad
    +

    (positive real) Convergence tolerance on the norm of the gradient.

    + + +
    tol_rel_grad
    +

    (positive real) Convergence tolerance on the relative norm of the gradient.

    + + +
    tol_param
    +

    (positive real) Convergence tolerance on changes in parameter value.

    + + +
    history_size
    +

    (positive integer) The size of the history used when +approximating the Hessian.

    + + +
    single_path_draws
    +

    (positive integer) Number of draws a single +pathfinder should return. The number of draws PSIS sampling samples from +will be equal to single_path_draws * num_paths.

    + + +
    draws
    +

    (positive integer) Number of draws to return after performing +pareto smooted importance sampling (PSIS). This should be smaller than +single_path_draws * num_paths (future versions of CmdStan will throw a +warning).

    + + +
    num_paths
    +

    (positive integer) Number of single pathfinders to run.

    + + +
    max_lbfgs_iters
    +

    (positive integer) The maximum number of iterations +for LBFGS.

    + + +
    num_elbo_draws
    +

    (positive integer) Number of draws to make when +calculating the ELBO of the approximation at each iteration of LBFGS.

    + + +
    save_single_paths
    +

    (logical) Whether to save the results of single +pathfinder runs in multi-pathfinder.

    + +
    +
    +

    Value

    + + +

    A CmdStanPathfinder object.

    +
    + + +
    +

    Examples

    +
    # \dontrun{
    +library(cmdstanr)
    +library(posterior)
    +library(bayesplot)
    +color_scheme_set("brightblue")
    +
    +# Set path to CmdStan
    +# (Note: if you installed CmdStan via install_cmdstan() with default settings
    +# then setting the path is unnecessary but the default below should still work.
    +# Otherwise use the `path` argument to specify the location of your
    +# CmdStan installation.)
    +set_cmdstan_path(path = NULL)
    +#> CmdStan path set to: /Users/jgabry/.cmdstan/cmdstan-2.33.1
    +
    +# Create a CmdStanModel object from a Stan program,
    +# here using the example model that comes with CmdStan
    +file <- file.path(cmdstan_path(), "examples/bernoulli/bernoulli.stan")
    +mod <- cmdstan_model(file)
    +mod$print()
    +#> data {
    +#>   int<lower=0> N;
    +#>   array[N] int<lower=0,upper=1> y;
    +#> }
    +#> parameters {
    +#>   real<lower=0,upper=1> theta;
    +#> }
    +#> model {
    +#>   theta ~ beta(1,1);  // uniform prior on interval 0,1
    +#>   y ~ bernoulli(theta);
    +#> }
    +
    +# Data as a named list (like RStan)
    +stan_data <- list(N = 10, y = c(0,1,0,0,0,0,0,0,0,1))
    +
    +# Run MCMC using the 'sample' method
    +fit_mcmc <- mod$sample(
    +  data = stan_data,
    +  seed = 123,
    +  chains = 2,
    +  parallel_chains = 2
    +)
    +#> Running MCMC with 2 parallel chains...
    +#> 
    +#> Chain 1 Iteration:    1 / 2000 [  0%]  (Warmup) 
    +#> Chain 1 Iteration:  100 / 2000 [  5%]  (Warmup) 
    +#> Chain 1 Iteration:  200 / 2000 [ 10%]  (Warmup) 
    +#> Chain 1 Iteration:  300 / 2000 [ 15%]  (Warmup) 
    +#> Chain 1 Iteration:  400 / 2000 [ 20%]  (Warmup) 
    +#> Chain 1 Iteration:  500 / 2000 [ 25%]  (Warmup) 
    +#> Chain 1 Iteration:  600 / 2000 [ 30%]  (Warmup) 
    +#> Chain 1 Iteration:  700 / 2000 [ 35%]  (Warmup) 
    +#> Chain 1 Iteration:  800 / 2000 [ 40%]  (Warmup) 
    +#> Chain 1 Iteration:  900 / 2000 [ 45%]  (Warmup) 
    +#> Chain 1 Iteration: 1000 / 2000 [ 50%]  (Warmup) 
    +#> Chain 1 Iteration: 1001 / 2000 [ 50%]  (Sampling) 
    +#> Chain 1 Iteration: 1100 / 2000 [ 55%]  (Sampling) 
    +#> Chain 1 Iteration: 1200 / 2000 [ 60%]  (Sampling) 
    +#> Chain 1 Iteration: 1300 / 2000 [ 65%]  (Sampling) 
    +#> Chain 1 Iteration: 1400 / 2000 [ 70%]  (Sampling) 
    +#> Chain 1 Iteration: 1500 / 2000 [ 75%]  (Sampling) 
    +#> Chain 1 Iteration: 1600 / 2000 [ 80%]  (Sampling) 
    +#> Chain 1 Iteration: 1700 / 2000 [ 85%]  (Sampling) 
    +#> Chain 1 Iteration: 1800 / 2000 [ 90%]  (Sampling) 
    +#> Chain 1 Iteration: 1900 / 2000 [ 95%]  (Sampling) 
    +#> Chain 1 Iteration: 2000 / 2000 [100%]  (Sampling) 
    +#> Chain 2 Iteration:    1 / 2000 [  0%]  (Warmup) 
    +#> Chain 2 Iteration:  100 / 2000 [  5%]  (Warmup) 
    +#> Chain 2 Iteration:  200 / 2000 [ 10%]  (Warmup) 
    +#> Chain 2 Iteration:  300 / 2000 [ 15%]  (Warmup) 
    +#> Chain 2 Iteration:  400 / 2000 [ 20%]  (Warmup) 
    +#> Chain 2 Iteration:  500 / 2000 [ 25%]  (Warmup) 
    +#> Chain 2 Iteration:  600 / 2000 [ 30%]  (Warmup) 
    +#> Chain 2 Iteration:  700 / 2000 [ 35%]  (Warmup) 
    +#> Chain 2 Iteration:  800 / 2000 [ 40%]  (Warmup) 
    +#> Chain 2 Iteration:  900 / 2000 [ 45%]  (Warmup) 
    +#> Chain 2 Iteration: 1000 / 2000 [ 50%]  (Warmup) 
    +#> Chain 2 Iteration: 1001 / 2000 [ 50%]  (Sampling) 
    +#> Chain 2 Iteration: 1100 / 2000 [ 55%]  (Sampling) 
    +#> Chain 2 Iteration: 1200 / 2000 [ 60%]  (Sampling) 
    +#> Chain 2 Iteration: 1300 / 2000 [ 65%]  (Sampling) 
    +#> Chain 2 Iteration: 1400 / 2000 [ 70%]  (Sampling) 
    +#> Chain 2 Iteration: 1500 / 2000 [ 75%]  (Sampling) 
    +#> Chain 2 Iteration: 1600 / 2000 [ 80%]  (Sampling) 
    +#> Chain 2 Iteration: 1700 / 2000 [ 85%]  (Sampling) 
    +#> Chain 2 Iteration: 1800 / 2000 [ 90%]  (Sampling) 
    +#> Chain 2 Iteration: 1900 / 2000 [ 95%]  (Sampling) 
    +#> Chain 2 Iteration: 2000 / 2000 [100%]  (Sampling) 
    +#> Chain 1 finished in 0.0 seconds.
    +#> Chain 2 finished in 0.0 seconds.
    +#> 
    +#> Both chains finished successfully.
    +#> Mean chain execution time: 0.0 seconds.
    +#> Total execution time: 0.2 seconds.
    +#> 
    +
    +# Use 'posterior' package for summaries
    +fit_mcmc$summary()
    +#> # A tibble: 2 × 10
    +#>   variable   mean median    sd   mad      q5    q95  rhat ess_bulk ess_tail
    +#>   <chr>     <dbl>  <dbl> <dbl> <dbl>   <dbl>  <dbl> <dbl>    <dbl>    <dbl>
    +#> 1 lp__     -7.30  -7.03  0.721 0.380 -8.82   -6.75   1.00     902.    1006.
    +#> 2 theta     0.247  0.233 0.122 0.129  0.0786  0.470  1.00     762.     712.
    +
    +# Check sampling diagnostics
    +fit_mcmc$diagnostic_summary()
    +#> $num_divergent
    +#> [1] 0 0
    +#> 
    +#> $num_max_treedepth
    +#> [1] 0 0
    +#> 
    +#> $ebfmi
    +#> [1] 1.017555 1.250490
    +#> 
    +
    +# Get posterior draws
    +draws <- fit_mcmc$draws()
    +print(draws)
    +#> # A draws_array: 1000 iterations, 2 chains, and 2 variables
    +#> , , variable = lp__
    +#> 
    +#>          chain
    +#> iteration    1    2
    +#>         1 -6.8 -6.8
    +#>         2 -6.9 -6.8
    +#>         3 -7.0 -7.0
    +#>         4 -6.9 -7.1
    +#>         5 -6.7 -7.0
    +#> 
    +#> , , variable = theta
    +#> 
    +#>          chain
    +#> iteration    1    2
    +#>         1 0.28 0.21
    +#>         2 0.19 0.20
    +#>         3 0.16 0.17
    +#>         4 0.20 0.36
    +#>         5 0.25 0.34
    +#> 
    +#> # ... with 995 more iterations
    +
    +# Convert to data frame using posterior::as_draws_df
    +as_draws_df(draws)
    +#> # A draws_df: 1000 iterations, 2 chains, and 2 variables
    +#>    lp__ theta
    +#> 1  -6.8  0.28
    +#> 2  -6.9  0.19
    +#> 3  -7.0  0.16
    +#> 4  -6.9  0.20
    +#> 5  -6.7  0.25
    +#> 6  -7.1  0.36
    +#> 7  -9.0  0.55
    +#> 8  -7.2  0.15
    +#> 9  -6.8  0.23
    +#> 10 -7.5  0.42
    +#> # ... with 1990 more draws
    +#> # ... hidden reserved variables {'.chain', '.iteration', '.draw'}
    +
    +# Plot posterior using bayesplot (ggplot2)
    +mcmc_hist(fit_mcmc$draws("theta"))
    +#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
    +
    +
    +# For models fit using MCMC, if you like working with RStan's stanfit objects
    +# then you can create one with rstan::read_stan_csv()
    +# stanfit <- rstan::read_stan_csv(fit_mcmc$output_files())
    +
    +
    +# Run 'optimize' method to get a point estimate (default is Stan's LBFGS algorithm)
    +# and also demonstrate specifying data as a path to a file instead of a list
    +my_data_file <- file.path(cmdstan_path(), "examples/bernoulli/bernoulli.data.json")
    +fit_optim <- mod$optimize(data = my_data_file, seed = 123)
    +#> Initial log joint probability = -9.51104 
    +#>     Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes  
    +#>        6      -5.00402   0.000103557   2.55661e-07           1           1        9    
    +#> Optimization terminated normally:  
    +#>   Convergence detected: relative gradient magnitude is below tolerance 
    +#> Finished in  0.1 seconds.
    +fit_optim$summary()
    +#> # A tibble: 2 × 2
    +#>   variable estimate
    +#>   <chr>       <dbl>
    +#> 1 lp__        -5.00
    +#> 2 theta        0.2 
    +
    +# Run 'optimize' again with 'jacobian=TRUE' and then draw from Laplace approximation
    +# to the posterior
    +fit_optim <- mod$optimize(data = my_data_file, jacobian = TRUE)
    +#> Initial log joint probability = -11.5709 
    +#>     Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes  
    +#>        5      -6.74802    0.00107647   1.72219e-05           1           1        8    
    +#> Optimization terminated normally:  
    +#>   Convergence detected: relative gradient magnitude is below tolerance 
    +#> Finished in  0.1 seconds.
    +fit_laplace <- mod$laplace(data = my_data_file, mode = fit_optim, draws = 2000)
    +#> Calculating Hessian 
    +#> Calculating inverse of Cholesky factor 
    +#> Generating draws 
    +#> iteration: 0 
    +#> iteration: 100 
    +#> iteration: 200 
    +#> iteration: 300 
    +#> iteration: 400 
    +#> iteration: 500 
    +#> iteration: 600 
    +#> iteration: 700 
    +#> iteration: 800 
    +#> iteration: 900 
    +#> iteration: 1000 
    +#> iteration: 1100 
    +#> iteration: 1200 
    +#> iteration: 1300 
    +#> iteration: 1400 
    +#> iteration: 1500 
    +#> iteration: 1600 
    +#> iteration: 1700 
    +#> iteration: 1800 
    +#> iteration: 1900 
    +#> Finished in  0.1 seconds.
    +fit_laplace$summary()
    +#> # A tibble: 3 × 7
    +#>   variable      mean median    sd   mad     q5      q95
    +#>   <chr>        <dbl>  <dbl> <dbl> <dbl>  <dbl>    <dbl>
    +#> 1 lp__        -7.23  -6.99  0.637 0.323 -8.54  -6.75   
    +#> 2 lp_approx__ -0.486 -0.238 0.644 0.323 -1.80  -0.00166
    +#> 3 theta        0.268  0.252 0.121 0.126  0.104  0.492  
    +
    +# Run 'variational' method to use ADVI to approximate posterior
    +fit_vb <- mod$variational(data = stan_data, seed = 123)
    +#> ------------------------------------------------------------ 
    +#> EXPERIMENTAL ALGORITHM: 
    +#>   This procedure has not been thoroughly tested and may be unstable 
    +#>   or buggy. The interface is subject to change. 
    +#> ------------------------------------------------------------ 
    +#> Gradient evaluation took 6e-06 seconds 
    +#> 1000 transitions using 10 leapfrog steps per transition would take 0.06 seconds. 
    +#> Adjust your expectations accordingly! 
    +#> Begin eta adaptation. 
    +#> Iteration:   1 / 250 [  0%]  (Adaptation) 
    +#> Iteration:  50 / 250 [ 20%]  (Adaptation) 
    +#> Iteration: 100 / 250 [ 40%]  (Adaptation) 
    +#> Iteration: 150 / 250 [ 60%]  (Adaptation) 
    +#> Iteration: 200 / 250 [ 80%]  (Adaptation) 
    +#> Success! Found best value [eta = 1] earlier than expected. 
    +#> Begin stochastic gradient ascent. 
    +#>   iter             ELBO   delta_ELBO_mean   delta_ELBO_med   notes  
    +#>    100           -6.262             1.000            1.000 
    +#>    200           -6.263             0.500            1.000 
    +#>    300           -6.307             0.336            0.007   MEDIAN ELBO CONVERGED 
    +#> Drawing a sample of size 1000 from the approximate posterior...  
    +#> COMPLETED. 
    +#> Finished in  0.1 seconds.
    +fit_vb$summary()
    +#> # A tibble: 3 × 7
    +#>   variable      mean median    sd   mad     q5      q95
    +#>   <chr>        <dbl>  <dbl> <dbl> <dbl>  <dbl>    <dbl>
    +#> 1 lp__        -7.18  -6.94  0.588 0.259 -8.36  -6.75   
    +#> 2 lp_approx__ -0.515 -0.221 0.692 0.303 -2.06  -0.00257
    +#> 3 theta        0.263  0.246 0.115 0.113  0.106  0.481  
    +mcmc_hist(fit_vb$draws("theta"))
    +#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
    +
    +
    +# Run 'pathfinder' method, a new alternative to the variational method
    +fit_pf <- mod$pathfinder(data = stan_data, seed = 123)
    +#> Path [1] :Initial log joint density = -11.008832 
    +#> Path [1] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
    +#>               5      -6.748e+00      9.383e-04   1.391e-05    1.000e+00  1.000e+00       126 -6.264e+00 -6.264e+00                   
    +#> Path [1] :Best Iter: [3] ELBO (-6.195408) evaluations: (126) 
    +#> Path [2] :Initial log joint density = -7.318450 
    +#> Path [2] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
    +#>               4      -6.748e+00      5.414e-03   1.618e-04    1.000e+00  1.000e+00       101 -6.251e+00 -6.251e+00                   
    +#> Path [2] :Best Iter: [3] ELBO (-6.229174) evaluations: (101) 
    +#> Path [3] :Initial log joint density = -12.374612 
    +#> Path [3] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
    +#>               5      -6.748e+00      1.419e-03   2.837e-05    1.000e+00  1.000e+00       126 -6.199e+00 -6.199e+00                   
    +#> Path [3] :Best Iter: [5] ELBO (-6.199185) evaluations: (126) 
    +#> Path [4] :Initial log joint density = -13.009824 
    +#> Path [4] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
    +#>               5      -6.748e+00      1.677e-03   3.885e-05    1.000e+00  1.000e+00       126 -6.173e+00 -6.173e+00                   
    +#> Path [4] :Best Iter: [5] ELBO (-6.172860) evaluations: (126) 
    +#> Total log probability function evaluations:4379 
    +#> Finished in  0.1 seconds.
    +fit_pf$summary()
    +#> # A tibble: 3 × 7
    +#>   variable      mean median    sd   mad      q5    q95
    +#>   <chr>        <dbl>  <dbl> <dbl> <dbl>   <dbl>  <dbl>
    +#> 1 lp_approx__ -1.08  -0.728 0.886 0.304 -2.71   -0.511
    +#> 2 lp__        -7.26  -6.96  0.738 0.297 -8.72   -6.75 
    +#> 3 theta        0.249  0.230 0.120 0.121  0.0854  0.471
    +mcmc_hist(fit_pf$draws("theta"))
    +#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
    +
    +
    +# Run 'pathfinder' again with more paths, fewer draws per path,
    +# better covariance approximation, and fewer LBFGSs iterations
    +fit_pf <- mod$pathfinder(data = stan_data, num_paths=10, single_path_draws=40,
    +                         history_size=50, max_lbfgs_iters=100)
    +#> Path [1] :Initial log joint density = -14.470660 
    +#> Path [1] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
    +#>               5      -6.748e+00      2.026e-03   5.797e-05    1.000e+00  1.000e+00       126 -6.259e+00 -6.259e+00                   
    +#> Path [1] :Best Iter: [4] ELBO (-6.211388) evaluations: (126) 
    +#> Path [2] :Initial log joint density = -9.064681 
    +#> Path [2] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
    +#>               5      -6.748e+00      3.457e-04   2.927e-06    1.000e+00  1.000e+00       126 -6.253e+00 -6.253e+00                   
    +#> Path [2] :Best Iter: [3] ELBO (-6.227371) evaluations: (126) 
    +#> Path [3] :Initial log joint density = -15.152638 
    +#> Path [3] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
    +#>               5      -6.748e+00      2.028e-03   6.063e-05    1.000e+00  1.000e+00       126 -6.230e+00 -6.230e+00                   
    +#> Path [3] :Best Iter: [4] ELBO (-6.211247) evaluations: (126) 
    +#> Path [4] :Initial log joint density = -10.220491 
    +#> Path [4] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
    +#>               5      -6.748e+00      7.043e-04   8.900e-06    1.000e+00  1.000e+00       126 -6.194e+00 -6.194e+00                   
    +#> Path [4] :Best Iter: [5] ELBO (-6.193620) evaluations: (126) 
    +#> Path [5] :Initial log joint density = -6.766009 
    +#> Path [5] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
    +#>               3      -6.748e+00      2.724e-03   1.458e-04    1.000e+00  1.000e+00        76 -6.232e+00 -6.232e+00                   
    +#> Path [5] :Best Iter: [3] ELBO (-6.232284) evaluations: (76) 
    +#> Path [6] :Initial log joint density = -7.304442 
    +#> Path [6] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
    +#>               5      -6.748e+00      4.014e-04   5.879e-07    1.000e+00  1.000e+00       126 -6.247e+00 -6.247e+00                   
    +#> Path [6] :Best Iter: [4] ELBO (-6.212233) evaluations: (126) 
    +#> Path [7] :Initial log joint density = -7.233791 
    +#> Path [7] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
    +#>               5      -6.748e+00      2.804e-04   3.115e-07    1.000e+00  1.000e+00       126 -6.275e+00 -6.275e+00                   
    +#> Path [7] :Best Iter: [4] ELBO (-6.122234) evaluations: (126) 
    +#> Path [8] :Initial log joint density = -11.942008 
    +#> Path [8] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
    +#>               5      -6.748e+00      1.223e-03   2.161e-05    1.000e+00  1.000e+00       126 -6.232e+00 -6.232e+00                   
    +#> Path [8] :Best Iter: [3] ELBO (-6.217220) evaluations: (126) 
    +#> Path [9] :Initial log joint density = -7.438064 
    +#> Path [9] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
    +#>               5      -6.748e+00      9.817e-05   3.665e-07    1.000e+00  1.000e+00       126 -6.268e+00 -6.268e+00                   
    +#> Path [9] :Best Iter: [3] ELBO (-6.213533) evaluations: (126) 
    +#> Path [10] :Initial log joint density = -8.992806 
    +#> Path [10] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
    +#>               5      -6.748e+00      3.448e-04   2.904e-06    1.000e+00  1.000e+00       126 -6.247e+00 -6.247e+00                   
    +#> Path [10] :Best Iter: [2] ELBO (-6.141839) evaluations: (126) 
    +#> Total log probability function evaluations:1360 
    +#> Finished in  0.1 seconds.
    +
    +# Specifying initial values as a function
    +fit_mcmc_w_init_fun <- mod$sample(
    +  data = stan_data,
    +  seed = 123,
    +  chains = 2,
    +  refresh = 0,
    +  init = function() list(theta = runif(1))
    +)
    +#> Running MCMC with 2 sequential chains...
    +#> 
    +#> Chain 1 finished in 0.0 seconds.
    +#> Chain 2 finished in 0.0 seconds.
    +#> 
    +#> Both chains finished successfully.
    +#> Mean chain execution time: 0.0 seconds.
    +#> Total execution time: 0.3 seconds.
    +#> 
    +fit_mcmc_w_init_fun_2 <- mod$sample(
    +  data = stan_data,
    +  seed = 123,
    +  chains = 2,
    +  refresh = 0,
    +  init = function(chain_id) {
    +    # silly but demonstrates optional use of chain_id
    +    list(theta = 1 / (chain_id + 1))
    +  }
    +)
    +#> Running MCMC with 2 sequential chains...
    +#> 
    +#> Chain 1 finished in 0.0 seconds.
    +#> Chain 2 finished in 0.0 seconds.
    +#> 
    +#> Both chains finished successfully.
    +#> Mean chain execution time: 0.0 seconds.
    +#> Total execution time: 0.3 seconds.
    +#> 
    +fit_mcmc_w_init_fun_2$init()
    +#> [[1]]
    +#> [[1]]$theta
    +#> [1] 0.5
    +#> 
    +#> 
    +#> [[2]]
    +#> [[2]]$theta
    +#> [1] 0.3333333
    +#> 
    +#> 
    +
    +# Specifying initial values as a list of lists
    +fit_mcmc_w_init_list <- mod$sample(
    +  data = stan_data,
    +  seed = 123,
    +  chains = 2,
    +  refresh = 0,
    +  init = list(
    +    list(theta = 0.75), # chain 1
    +    list(theta = 0.25)  # chain 2
    +  )
    +)
    +#> Running MCMC with 2 sequential chains...
    +#> 
    +#> Chain 1 finished in 0.0 seconds.
    +#> Chain 2 finished in 0.0 seconds.
    +#> 
    +#> Both chains finished successfully.
    +#> Mean chain execution time: 0.0 seconds.
    +#> Total execution time: 0.3 seconds.
    +#> 
    +fit_optim_w_init_list <- mod$optimize(
    +  data = stan_data,
    +  seed = 123,
    +  init = list(
    +    list(theta = 0.75)
    +  )
    +)
    +#> Initial log joint probability = -11.6657 
    +#>     Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes  
    +#>        6      -5.00402   0.000237915   9.55309e-07           1           1        9    
    +#> Optimization terminated normally:  
    +#>   Convergence detected: relative gradient magnitude is below tolerance 
    +#> Finished in  0.1 seconds.
    +fit_optim_w_init_list$init()
    +#> [[1]]
    +#> [[1]]$theta
    +#> [1] 0.75
    +#> 
    +#> 
    +# }
    +
    +
    +
    +
    + +
    + + +
    + +
    +

    Site built with pkgdown 2.0.7.

    +
    + +
    + + + + + + + + diff --git a/docs/reference/model-method-sample-1.png b/docs/reference/model-method-sample-1.png index 27fd88217..0aa41c0e9 100644 Binary files a/docs/reference/model-method-sample-1.png and b/docs/reference/model-method-sample-1.png differ diff --git a/docs/reference/model-method-sample-2.png b/docs/reference/model-method-sample-2.png index 4ed9fe5c5..835a8a667 100644 Binary files a/docs/reference/model-method-sample-2.png and b/docs/reference/model-method-sample-2.png differ diff --git a/docs/reference/model-method-sample-3.png b/docs/reference/model-method-sample-3.png index e985d6949..8d58780fd 100644 Binary files a/docs/reference/model-method-sample-3.png and b/docs/reference/model-method-sample-3.png differ diff --git a/docs/reference/model-method-sample.html b/docs/reference/model-method-sample.html index f656c23a7..7925e4b45 100644 --- a/docs/reference/model-method-sample.html +++ b/docs/reference/model-method-sample.html @@ -24,7 +24,7 @@ cmdstanr - 0.6.0 + 0.7.0
    @@ -210,10 +210,10 @@

    Arguments

    write_stan_json() to write R objects to JSON files compatible with CmdStan.

  • A list of lists containing initial values for all or some parameters. For -MCMC the list should contain a sublist for each chain. For optimization and -variational inference there should be just one sublist. The sublists should -have named elements corresponding to the parameters for which you are -specifying initial values. See Examples.

  • +MCMC the list should contain a sublist for each chain. For other model +fitting methods there should be just one sublist. The sublists should have +named elements corresponding to the parameters for which you are specifying +initial values. See Examples.

  • A function that returns a single list with names corresponding to the parameters for which you are specifying initial values. The function can take no arguments or a single argument chain_id. For MCMC, if the function @@ -464,7 +464,9 @@

    See also

    model-method-expose_functions, model-method-format, model-method-generate-quantities, +model-method-laplace, model-method-optimize, +model-method-pathfinder, model-method-sample_mpi, model-method-variables, model-method-variational

    @@ -484,7 +486,7 @@

    Examples

    # Otherwise use the `path` argument to specify the location of your # CmdStan installation.) set_cmdstan_path(path = NULL) -#> CmdStan path set to: /Users/jgabry/.cmdstan/cmdstan-2.32.2 +#> CmdStan path set to: /Users/jgabry/.cmdstan/cmdstan-2.33.1 # Create a CmdStanModel object from a Stan program, # here using the example model that comes with CmdStan @@ -571,10 +573,22 @@

    Examples

    fit_mcmc$summary() #> # A tibble: 2 × 10 #> variable mean median sd mad q5 q95 rhat ess_bulk ess_tail -#> <chr> <num> <num> <num> <num> <num> <num> <num> <num> <num> +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 lp__ -7.30 -7.03 0.721 0.380 -8.82 -6.75 1.00 902. 1006. #> 2 theta 0.247 0.233 0.122 0.129 0.0786 0.470 1.00 762. 712. +# Check sampling diagnostics +fit_mcmc$diagnostic_summary() +#> $num_divergent +#> [1] 0 0 +#> +#> $num_max_treedepth +#> [1] 0 0 +#> +#> $ebfmi +#> [1] 1.017555 1.250490 +#> + # Get posterior draws draws <- fit_mcmc$draws() print(draws) @@ -623,51 +637,8 @@

    Examples

    #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. -# Call CmdStan's diagnose and stansummary utilities -fit_mcmc$cmdstan_diagnose() -#> Processing csv files: /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/bernoulli-202307251438-1-239737.csv, /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/bernoulli-202307251438-2-239737.csv -#> -#> Checking sampler transitions treedepth. -#> Treedepth satisfactory for all transitions. -#> -#> Checking sampler transitions for divergences. -#> No divergent transitions found. -#> -#> Checking E-BFMI - sampler transitions HMC potential energy. -#> E-BFMI satisfactory. -#> -#> Effective sample size satisfactory. -#> -#> Split R-hat values satisfactory all parameters. -#> -#> Processing complete, no problems detected. -fit_mcmc$cmdstan_summary() -#> Inference for Stan model: bernoulli_model -#> 2 chains: each with iter=(1000,1000); warmup=(0,0); thin=(1,1); 2000 iterations saved. -#> -#> Warmup took (0.0040, 0.0040) seconds, 0.0080 seconds total -#> Sampling took (0.011, 0.011) seconds, 0.022 seconds total -#> -#> Mean MCSE StdDev 5% 50% 95% N_Eff N_Eff/s R_hat -#> -#> lp__ -7.3 2.6e-02 0.72 -8.8 -7.0 -6.8 781 35502 1.0 -#> accept_stat__ 0.92 8.3e-03 0.13 0.64 0.97 1.0 235 10662 1.0e+00 -#> stepsize__ 0.95 7.9e-02 0.079 0.87 1.0 1.0 1.0 46 2.0e+13 -#> treedepth__ 1.4 1.1e-02 0.48 1.0 1.0 2.0 1874 85179 1.0e+00 -#> n_leapfrog__ 2.5 1.4e-01 1.3 1.0 3.0 3.0 89 4050 1.0e+00 -#> divergent__ 0.00 nan 0.00 0.00 0.00 0.00 nan nan nan -#> energy__ 7.8 3.6e-02 1.00 6.8 7.5 9.6 775 35215 1.0e+00 -#> -#> theta 0.25 4.3e-03 0.12 0.079 0.23 0.47 796 36197 1.0 -#> -#> Samples were drawn using hmc with nuts. -#> For each parameter, N_Eff is a crude measure of effective sample size, -#> and R_hat is the potential scale reduction factor on split chains (at -#> convergence, R_hat=1). - # For models fit using MCMC, if you like working with RStan's stanfit objects # then you can create one with rstan::read_stan_csv() - # stanfit <- rstan::read_stan_csv(fit_mcmc$output_files()) @@ -681,24 +652,64 @@

    Examples

    #> Optimization terminated normally: #> Convergence detected: relative gradient magnitude is below tolerance #> Finished in 0.1 seconds. - fit_optim$summary() #> # A tibble: 2 × 2 #> variable estimate -#> <chr> <num> +#> <chr> <dbl> #> 1 lp__ -5.00 #> 2 theta 0.2 +# Run 'optimize' again with 'jacobian=TRUE' and then draw from Laplace approximation +# to the posterior +fit_optim <- mod$optimize(data = my_data_file, jacobian = TRUE) +#> Initial log joint probability = -11.0695 +#> Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes +#> 5 -6.74802 0.000954615 1.42918e-05 1 1 8 +#> Optimization terminated normally: +#> Convergence detected: relative gradient magnitude is below tolerance +#> Finished in 0.1 seconds. +fit_laplace <- mod$laplace(data = my_data_file, mode = fit_optim, draws = 2000) +#> Calculating Hessian +#> Calculating inverse of Cholesky factor +#> Generating draws +#> iteration: 0 +#> iteration: 100 +#> iteration: 200 +#> iteration: 300 +#> iteration: 400 +#> iteration: 500 +#> iteration: 600 +#> iteration: 700 +#> iteration: 800 +#> iteration: 900 +#> iteration: 1000 +#> iteration: 1100 +#> iteration: 1200 +#> iteration: 1300 +#> iteration: 1400 +#> iteration: 1500 +#> iteration: 1600 +#> iteration: 1700 +#> iteration: 1800 +#> iteration: 1900 +#> Finished in 0.1 seconds. +fit_laplace$summary() +#> # A tibble: 3 × 7 +#> variable mean median sd mad q5 q95 +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> +#> 1 lp__ -7.23 -6.97 0.710 0.297 -8.57 -6.75 +#> 2 lp_approx__ -0.490 -0.220 0.696 0.300 -1.90 -0.00171 +#> 3 theta 0.270 0.252 0.123 0.120 0.102 0.495 -# Run 'variational' method to approximate the posterior (default is meanfield ADVI) +# Run 'variational' method to use ADVI to approximate posterior fit_vb <- mod$variational(data = stan_data, seed = 123) #> ------------------------------------------------------------ #> EXPERIMENTAL ALGORITHM: #> This procedure has not been thoroughly tested and may be unstable #> or buggy. The interface is subject to change. #> ------------------------------------------------------------ -#> Gradient evaluation took 9e-06 seconds -#> 1000 transitions using 10 leapfrog steps per transition would take 0.09 seconds. +#> Gradient evaluation took 6e-06 seconds +#> 1000 transitions using 10 leapfrog steps per transition would take 0.06 seconds. #> Adjust your expectations accordingly! #> Begin eta adaptation. #> Iteration: 1 / 250 [ 0%] (Adaptation) @@ -715,20 +726,94 @@

    Examples

    #> Drawing a sample of size 1000 from the approximate posterior... #> COMPLETED. #> Finished in 0.1 seconds. - fit_vb$summary() #> # A tibble: 3 × 7 #> variable mean median sd mad q5 q95 -#> <chr> <num> <num> <num> <num> <num> <num> +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 lp__ -7.18 -6.94 0.588 0.259 -8.36 -6.75 #> 2 lp_approx__ -0.515 -0.221 0.692 0.303 -2.06 -0.00257 #> 3 theta 0.263 0.246 0.115 0.113 0.106 0.481 - -# Plot approximate posterior using bayesplot mcmc_hist(fit_vb$draws("theta")) #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. +# Run 'pathfinder' method, a new alternative to the variational method +fit_pf <- mod$pathfinder(data = stan_data, seed = 123) +#> Path [1] :Initial log joint density = -11.008832 +#> Path [1] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 9.383e-04 1.391e-05 1.000e+00 1.000e+00 126 -6.264e+00 -6.264e+00 +#> Path [1] :Best Iter: [3] ELBO (-6.195408) evaluations: (126) +#> Path [2] :Initial log joint density = -7.318450 +#> Path [2] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 4 -6.748e+00 5.414e-03 1.618e-04 1.000e+00 1.000e+00 101 -6.251e+00 -6.251e+00 +#> Path [2] :Best Iter: [3] ELBO (-6.229174) evaluations: (101) +#> Path [3] :Initial log joint density = -12.374612 +#> Path [3] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.419e-03 2.837e-05 1.000e+00 1.000e+00 126 -6.199e+00 -6.199e+00 +#> Path [3] :Best Iter: [5] ELBO (-6.199185) evaluations: (126) +#> Path [4] :Initial log joint density = -13.009824 +#> Path [4] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.677e-03 3.885e-05 1.000e+00 1.000e+00 126 -6.173e+00 -6.173e+00 +#> Path [4] :Best Iter: [5] ELBO (-6.172860) evaluations: (126) +#> Total log probability function evaluations:4379 +#> Finished in 0.1 seconds. +fit_pf$summary() +#> # A tibble: 3 × 7 +#> variable mean median sd mad q5 q95 +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> +#> 1 lp_approx__ -1.08 -0.728 0.886 0.304 -2.71 -0.511 +#> 2 lp__ -7.26 -6.96 0.738 0.297 -8.72 -6.75 +#> 3 theta 0.249 0.230 0.120 0.121 0.0854 0.471 +mcmc_hist(fit_pf$draws("theta")) +#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. + + +# Run 'pathfinder' again with more paths, fewer draws per path, +# better covariance approximation, and fewer LBFGSs iterations +fit_pf <- mod$pathfinder(data = stan_data, num_paths=10, single_path_draws=40, + history_size=50, max_lbfgs_iters=100) +#> Path [1] :Initial log joint density = -10.398597 +#> Path [1] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 7.601e-04 1.002e-05 1.000e+00 1.000e+00 126 -6.246e+00 -6.246e+00 +#> Path [1] :Best Iter: [4] ELBO (-6.213236) evaluations: (126) +#> Path [2] :Initial log joint density = -7.343771 +#> Path [2] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 4 -6.748e+00 5.658e-03 1.740e-04 1.000e+00 1.000e+00 101 -6.270e+00 -6.270e+00 +#> Path [2] :Best Iter: [3] ELBO (-6.236555) evaluations: (101) +#> Path [3] :Initial log joint density = -7.253954 +#> Path [3] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 4 -6.748e+00 4.780e-03 1.316e-04 1.000e+00 1.000e+00 101 -6.293e+00 -6.293e+00 +#> Path [3] :Best Iter: [3] ELBO (-6.210231) evaluations: (101) +#> Path [4] :Initial log joint density = -6.884985 +#> Path [4] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 4 -6.748e+00 1.025e-03 1.015e-05 1.000e+00 1.000e+00 101 -6.254e+00 -6.254e+00 +#> Path [4] :Best Iter: [4] ELBO (-6.254382) evaluations: (101) +#> Path [5] :Initial log joint density = -10.549385 +#> Path [5] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 8.063e-04 1.099e-05 1.000e+00 1.000e+00 126 -6.155e+00 -6.155e+00 +#> Path [5] :Best Iter: [5] ELBO (-6.154812) evaluations: (126) +#> Path [6] :Initial log joint density = -11.021697 +#> Path [6] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 9.418e-04 1.400e-05 1.000e+00 1.000e+00 126 -6.261e+00 -6.261e+00 +#> Path [6] :Best Iter: [4] ELBO (-6.204645) evaluations: (126) +#> Path [7] :Initial log joint density = -10.721189 +#> Path [7] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 8.573e-04 1.209e-05 1.000e+00 1.000e+00 126 -6.195e+00 -6.195e+00 +#> Path [7] :Best Iter: [5] ELBO (-6.194637) evaluations: (126) +#> Path [8] :Initial log joint density = -11.729882 +#> Path [8] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.125e-03 1.854e-05 1.000e+00 1.000e+00 126 -6.195e+00 -6.195e+00 +#> Path [8] :Best Iter: [4] ELBO (-6.193964) evaluations: (126) +#> Path [9] :Initial log joint density = -8.375820 +#> Path [9] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 2.924e-04 2.169e-06 1.000e+00 1.000e+00 126 -6.198e+00 -6.198e+00 +#> Path [9] :Best Iter: [2] ELBO (-6.188312) evaluations: (126) +#> Path [10] :Initial log joint density = -7.041152 +#> Path [10] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 4 -6.748e+00 2.604e-03 4.795e-05 1.000e+00 1.000e+00 101 -6.233e+00 -6.233e+00 +#> Path [10] :Best Iter: [2] ELBO (-6.214834) evaluations: (101) +#> Total log probability function evaluations:1310 +#> Finished in 0.1 seconds. # Specifying initial values as a function fit_mcmc_w_init_fun <- mod$sample( diff --git a/docs/reference/model-method-sample_mpi.html b/docs/reference/model-method-sample_mpi.html index 53d2eb1a3..6247b07ac 100644 --- a/docs/reference/model-method-sample_mpi.html +++ b/docs/reference/model-method-sample_mpi.html @@ -1,7 +1,7 @@ Run Stan's MCMC algorithms with MPI — model-method-sample_mpi • cmdstanr

    The $sample_mpi() method of a CmdStanModel object is identical to the $sample() method but with support for -MPI. The target audience for MPI are +MPI (message passing interface). The target audience for MPI are those with large computer clusters. For other users, the $sample() method provides both parallelization of chains and threading support for within-chain parallelization.

    @@ -247,10 +247,10 @@

    Arguments

    write_stan_json() to write R objects to JSON files compatible with CmdStan.

  • A list of lists containing initial values for all or some parameters. For -MCMC the list should contain a sublist for each chain. For optimization and -variational inference there should be just one sublist. The sublists should -have named elements corresponding to the parameters for which you are -specifying initial values. See Examples.

  • +MCMC the list should contain a sublist for each chain. For other model +fitting methods there should be just one sublist. The sublists should have +named elements corresponding to the parameters for which you are specifying +initial values. See Examples.

  • A function that returns a single list with names corresponding to the parameters for which you are specifying initial values. The function can take no arguments or a single argument chain_id. For MCMC, if the function @@ -467,8 +467,8 @@

    See also

    documentation and tutorials.

    The Stan and CmdStan documentation:

    The Stan Math Library's MPI documentation -(mc-stan.org/math/mpi) for more +

    The Stan Math Library's documentation +(mc-stan.org/math) for more details on MPI support in Stan.

    Other CmdStanModel methods: model-method-check_syntax, @@ -477,7 +477,9 @@

    See also

    model-method-expose_functions, model-method-format, model-method-generate-quantities, +model-method-laplace, model-method-optimize, +model-method-pathfinder, model-method-sample, model-method-variables, model-method-variational

    diff --git a/docs/reference/model-method-variables.html b/docs/reference/model-method-variables.html index f26828ae3..6df7de09b 100644 --- a/docs/reference/model-method-variables.html +++ b/docs/reference/model-method-variables.html @@ -24,7 +24,7 @@ cmdstanr - 0.6.0 + 0.7.0 @@ -141,7 +141,9 @@

    See also

    model-method-expose_functions, model-method-format, model-method-generate-quantities, +model-method-laplace, model-method-optimize, +model-method-pathfinder, model-method-sample_mpi, model-method-sample, model-method-variational

    diff --git a/docs/reference/model-method-variational-1.png b/docs/reference/model-method-variational-1.png index 27fd88217..0aa41c0e9 100644 Binary files a/docs/reference/model-method-variational-1.png and b/docs/reference/model-method-variational-1.png differ diff --git a/docs/reference/model-method-variational-2.png b/docs/reference/model-method-variational-2.png index 4ed9fe5c5..835a8a667 100644 Binary files a/docs/reference/model-method-variational-2.png and b/docs/reference/model-method-variational-2.png differ diff --git a/docs/reference/model-method-variational-3.png b/docs/reference/model-method-variational-3.png index e985d6949..8d58780fd 100644 Binary files a/docs/reference/model-method-variational-3.png and b/docs/reference/model-method-variational-3.png differ diff --git a/docs/reference/model-method-variational.html b/docs/reference/model-method-variational.html index e5fdeb91e..ed9fcbc43 100644 --- a/docs/reference/model-method-variational.html +++ b/docs/reference/model-method-variational.html @@ -1,10 +1,15 @@ Run Stan's variational approximation algorithms — model-method-variational • cmdstanr @@ -22,7 +27,7 @@ cmdstanr - 0.6.0 + 0.7.0 @@ -110,11 +115,16 @@

    Run Stan's variational approximation algorithms

    The $variational() method of a CmdStanModel object runs -Stan's variational Bayes (ADVI) algorithms.

    -

    Any argument left as NULL will default to the default value used by the -installed version of CmdStan. See the +Stan's Automatic Differentiation Variational Inference (ADVI) algorithms. +The approximation is a Gaussian in the unconstrained variable space. Stan +implements two ADVI algorithms: the algorithm="meanfield" option uses a +fully factorized Gaussian for the approximation; the algorithm="fullrank" +option uses a Gaussian with a full-rank covariance matrix for the +approximation. See the CmdStan User’s Guide for more details.

    +

    Any argument left as NULL will default to the default value used by the +installed version of CmdStan.

    @@ -138,7 +148,8 @@

    Run Stan's variational approximation algorithms

    adapt_iter = NULL, tol_rel_obj = NULL, eval_elbo = NULL, - output_samples = NULL + output_samples = NULL, + draws = NULL )
    @@ -186,10 +197,10 @@

    Arguments

    write_stan_json() to write R objects to JSON files compatible with CmdStan.

  • A list of lists containing initial values for all or some parameters. For -MCMC the list should contain a sublist for each chain. For optimization and -variational inference there should be just one sublist. The sublists should -have named elements corresponding to the parameters for which you are -specifying initial values. See Examples.

  • +MCMC the list should contain a sublist for each chain. For other model +fitting methods there should be just one sublist. The sublists should have +named elements corresponding to the parameters for which you are specifying +initial values. See Examples.

  • A function that returns a single list with names corresponding to the parameters for which you are specifying initial values. The function can take no arguments or a single argument chain_id. For MCMC, if the function @@ -299,6 +310,11 @@

    Arguments

    output_samples
    +

    (positive integer) Use draws argument instead. +output_samples will be deprecated in the future.

    + + +
    draws

    (positive integer) Number of approximate posterior samples to draw and save.

    @@ -308,16 +324,6 @@

    Value

    A CmdStanVB object.

    - -
    -

    Details

    -

    CmdStan can fit a variational approximation to the posterior. The -approximation is a Gaussian in the unconstrained variable space. Stan -implements two variational algorithms. The algorithm="meanfield" option -uses a fully factorized Gaussian for the approximation. The -algorithm="fullrank" option uses a Gaussian with a full-rank covariance -matrix for the approximation.

    -

    -- CmdStan Interface User's Guide

    @@ -353,7 +361,7 @@

    Examples

    # Otherwise use the `path` argument to specify the location of your # CmdStan installation.) set_cmdstan_path(path = NULL) -#> CmdStan path set to: /Users/jgabry/.cmdstan/cmdstan-2.32.2 +#> CmdStan path set to: /Users/jgabry/.cmdstan/cmdstan-2.33.1 # Create a CmdStanModel object from a Stan program, # here using the example model that comes with CmdStan @@ -440,10 +448,22 @@

    Examples

    fit_mcmc$summary() #> # A tibble: 2 × 10 #> variable mean median sd mad q5 q95 rhat ess_bulk ess_tail -#> <chr> <num> <num> <num> <num> <num> <num> <num> <num> <num> +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 lp__ -7.30 -7.03 0.721 0.380 -8.82 -6.75 1.00 902. 1006. #> 2 theta 0.247 0.233 0.122 0.129 0.0786 0.470 1.00 762. 712. +# Check sampling diagnostics +fit_mcmc$diagnostic_summary() +#> $num_divergent +#> [1] 0 0 +#> +#> $num_max_treedepth +#> [1] 0 0 +#> +#> $ebfmi +#> [1] 1.017555 1.250490 +#> + # Get posterior draws draws <- fit_mcmc$draws() print(draws) @@ -492,51 +512,8 @@

    Examples

    #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. -# Call CmdStan's diagnose and stansummary utilities -fit_mcmc$cmdstan_diagnose() -#> Processing csv files: /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/bernoulli-202307251438-1-4ea737.csv, /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/bernoulli-202307251438-2-4ea737.csv -#> -#> Checking sampler transitions treedepth. -#> Treedepth satisfactory for all transitions. -#> -#> Checking sampler transitions for divergences. -#> No divergent transitions found. -#> -#> Checking E-BFMI - sampler transitions HMC potential energy. -#> E-BFMI satisfactory. -#> -#> Effective sample size satisfactory. -#> -#> Split R-hat values satisfactory all parameters. -#> -#> Processing complete, no problems detected. -fit_mcmc$cmdstan_summary() -#> Inference for Stan model: bernoulli_model -#> 2 chains: each with iter=(1000,1000); warmup=(0,0); thin=(1,1); 2000 iterations saved. -#> -#> Warmup took (0.0040, 0.0040) seconds, 0.0080 seconds total -#> Sampling took (0.011, 0.011) seconds, 0.022 seconds total -#> -#> Mean MCSE StdDev 5% 50% 95% N_Eff N_Eff/s R_hat -#> -#> lp__ -7.3 2.6e-02 0.72 -8.8 -7.0 -6.8 781 35502 1.0 -#> accept_stat__ 0.92 8.3e-03 0.13 0.64 0.97 1.0 235 10662 1.0e+00 -#> stepsize__ 0.95 7.9e-02 0.079 0.87 1.0 1.0 1.0 46 2.0e+13 -#> treedepth__ 1.4 1.1e-02 0.48 1.0 1.0 2.0 1874 85179 1.0e+00 -#> n_leapfrog__ 2.5 1.4e-01 1.3 1.0 3.0 3.0 89 4050 1.0e+00 -#> divergent__ 0.00 nan 0.00 0.00 0.00 0.00 nan nan nan -#> energy__ 7.8 3.6e-02 1.00 6.8 7.5 9.6 775 35215 1.0e+00 -#> -#> theta 0.25 4.3e-03 0.12 0.079 0.23 0.47 796 36197 1.0 -#> -#> Samples were drawn using hmc with nuts. -#> For each parameter, N_Eff is a crude measure of effective sample size, -#> and R_hat is the potential scale reduction factor on split chains (at -#> convergence, R_hat=1). - # For models fit using MCMC, if you like working with RStan's stanfit objects # then you can create one with rstan::read_stan_csv() - # stanfit <- rstan::read_stan_csv(fit_mcmc$output_files()) @@ -550,24 +527,64 @@

    Examples

    #> Optimization terminated normally: #> Convergence detected: relative gradient magnitude is below tolerance #> Finished in 0.1 seconds. - fit_optim$summary() #> # A tibble: 2 × 2 #> variable estimate -#> <chr> <num> +#> <chr> <dbl> #> 1 lp__ -5.00 #> 2 theta 0.2 +# Run 'optimize' again with 'jacobian=TRUE' and then draw from Laplace approximation +# to the posterior +fit_optim <- mod$optimize(data = my_data_file, jacobian = TRUE) +#> Initial log joint probability = -11.7793 +#> Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes +#> 5 -6.74802 0.00114772 1.92382e-05 1 1 8 +#> Optimization terminated normally: +#> Convergence detected: relative gradient magnitude is below tolerance +#> Finished in 0.1 seconds. +fit_laplace <- mod$laplace(data = my_data_file, mode = fit_optim, draws = 2000) +#> Calculating Hessian +#> Calculating inverse of Cholesky factor +#> Generating draws +#> iteration: 0 +#> iteration: 100 +#> iteration: 200 +#> iteration: 300 +#> iteration: 400 +#> iteration: 500 +#> iteration: 600 +#> iteration: 700 +#> iteration: 800 +#> iteration: 900 +#> iteration: 1000 +#> iteration: 1100 +#> iteration: 1200 +#> iteration: 1300 +#> iteration: 1400 +#> iteration: 1500 +#> iteration: 1600 +#> iteration: 1700 +#> iteration: 1800 +#> iteration: 1900 +#> Finished in 0.1 seconds. +fit_laplace$summary() +#> # A tibble: 3 × 7 +#> variable mean median sd mad q5 q95 +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> +#> 1 lp__ -7.22 -6.96 0.669 0.294 -8.51 -6.75 +#> 2 lp_approx__ -0.470 -0.213 0.645 0.295 -1.81 -0.00204 +#> 3 theta 0.267 0.247 0.121 0.118 0.104 0.493 -# Run 'variational' method to approximate the posterior (default is meanfield ADVI) +# Run 'variational' method to use ADVI to approximate posterior fit_vb <- mod$variational(data = stan_data, seed = 123) #> ------------------------------------------------------------ #> EXPERIMENTAL ALGORITHM: #> This procedure has not been thoroughly tested and may be unstable #> or buggy. The interface is subject to change. #> ------------------------------------------------------------ -#> Gradient evaluation took 9e-06 seconds -#> 1000 transitions using 10 leapfrog steps per transition would take 0.09 seconds. +#> Gradient evaluation took 5e-06 seconds +#> 1000 transitions using 10 leapfrog steps per transition would take 0.05 seconds. #> Adjust your expectations accordingly! #> Begin eta adaptation. #> Iteration: 1 / 250 [ 0%] (Adaptation) @@ -584,20 +601,95 @@

    Examples

    #> Drawing a sample of size 1000 from the approximate posterior... #> COMPLETED. #> Finished in 0.1 seconds. - fit_vb$summary() #> # A tibble: 3 × 7 #> variable mean median sd mad q5 q95 -#> <chr> <num> <num> <num> <num> <num> <num> +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 lp__ -7.18 -6.94 0.588 0.259 -8.36 -6.75 #> 2 lp_approx__ -0.515 -0.221 0.692 0.303 -2.06 -0.00257 #> 3 theta 0.263 0.246 0.115 0.113 0.106 0.481 - -# Plot approximate posterior using bayesplot mcmc_hist(fit_vb$draws("theta")) #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. +# Run 'pathfinder' method, a new alternative to the variational method +fit_pf <- mod$pathfinder(data = stan_data, seed = 123) +#> Path [1] :Initial log joint density = -11.008832 +#> Path [1] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 9.383e-04 1.391e-05 1.000e+00 1.000e+00 126 -6.264e+00 -6.264e+00 +#> Path [1] :Best Iter: [3] ELBO (-6.195408) evaluations: (126) +#> Path [2] :Initial log joint density = -7.318450 +#> Path [2] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 4 -6.748e+00 5.414e-03 1.618e-04 1.000e+00 1.000e+00 101 -6.251e+00 -6.251e+00 +#> Path [2] :Best Iter: [3] ELBO (-6.229174) evaluations: (101) +#> Path [3] :Initial log joint density = -12.374612 +#> Path [3] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.419e-03 2.837e-05 1.000e+00 1.000e+00 126 -6.199e+00 -6.199e+00 +#> Path [3] :Best Iter: [5] ELBO (-6.199185) evaluations: (126) +#> Path [4] :Initial log joint density = -13.009824 +#> Path [4] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.677e-03 3.885e-05 1.000e+00 1.000e+00 126 -6.173e+00 -6.173e+00 +#> Path [4] :Best Iter: [5] ELBO (-6.172860) evaluations: (126) +#> Total log probability function evaluations:4379 +#> Finished in 0.1 seconds. +fit_pf$summary() +#> # A tibble: 3 × 7 +#> variable mean median sd mad q5 q95 +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> +#> 1 lp_approx__ -1.08 -0.728 0.886 0.304 -2.71 -0.511 +#> 2 lp__ -7.26 -6.96 0.738 0.297 -8.72 -6.75 +#> 3 theta 0.249 0.230 0.120 0.121 0.0854 0.471 +mcmc_hist(fit_pf$draws("theta")) +#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. + + +# Run 'pathfinder' again with more paths, fewer draws per path, +# better covariance approximation, and fewer LBFGSs iterations +fit_pf <- mod$pathfinder(data = stan_data, num_paths=10, single_path_draws=40, + history_size=50, max_lbfgs_iters=100) +#> Path [1] :Initial log joint density = -7.711026 +#> Path [1] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.614e-04 8.188e-07 1.000e+00 1.000e+00 126 -6.206e+00 -6.206e+00 +#> Path [1] :Best Iter: [5] ELBO (-6.205511) evaluations: (126) +#> Path [2] :Initial log joint density = -12.450513 +#> Path [2] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.452e-03 2.960e-05 1.000e+00 1.000e+00 126 -6.251e+00 -6.251e+00 +#> Path [2] :Best Iter: [4] ELBO (-6.229238) evaluations: (126) +#> Path [3] :Initial log joint density = -7.647405 +#> Path [3] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.466e-04 7.009e-07 1.000e+00 1.000e+00 126 -6.193e+00 -6.193e+00 +#> Path [3] :Best Iter: [3] ELBO (-6.164986) evaluations: (126) +#> Path [4] :Initial log joint density = -15.036045 +#> Path [4] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 2.036e-03 6.056e-05 1.000e+00 1.000e+00 126 -6.235e+00 -6.235e+00 +#> Path [4] :Best Iter: [2] ELBO (-6.225650) evaluations: (126) +#> Path [5] :Initial log joint density = -6.760365 +#> Path [5] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 3 -6.748e+00 1.911e-03 8.518e-05 1.000e+00 1.000e+00 76 -6.247e+00 -6.247e+00 +#> Path [5] :Best Iter: [3] ELBO (-6.246721) evaluations: (76) +#> Path [6] :Initial log joint density = -6.826898 +#> Path [6] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 3 -6.748e+00 5.423e-03 1.282e-04 9.360e-01 9.360e-01 76 -6.169e+00 -6.169e+00 +#> Path [6] :Best Iter: [3] ELBO (-6.168890) evaluations: (76) +#> Path [7] :Initial log joint density = -8.134235 +#> Path [7] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 2.517e-04 1.691e-06 1.000e+00 1.000e+00 126 -6.243e+00 -6.243e+00 +#> Path [7] :Best Iter: [3] ELBO (-6.175493) evaluations: (126) +#> Path [8] :Initial log joint density = -7.511494 +#> Path [8] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 9.446e-04 2.687e-06 1.000e+00 1.000e+00 126 -6.214e+00 -6.214e+00 +#> Path [8] :Best Iter: [2] ELBO (-6.197279) evaluations: (126) +#> Path [9] :Initial log joint density = -11.516990 +#> Path [9] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.065e-03 1.693e-05 1.000e+00 1.000e+00 126 -6.244e+00 -6.244e+00 +#> Path [9] :Best Iter: [3] ELBO (-6.194623) evaluations: (126) +#> Path [10] :Initial log joint density = -17.778190 +#> Path [10] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes +#> 5 -6.748e+00 1.013e-03 2.414e-05 1.000e+00 1.000e+00 126 -6.262e+00 -6.262e+00 +#> Path [10] :Best Iter: [4] ELBO (-6.170410) evaluations: (126) +#> Total log probability function evaluations:1310 +#> Pareto k value (0.820024) is greater than 0.7. Importance resampling was not able to improve the approximation, which may indicate that the approximation itself is poor. +#> Finished in 0.1 seconds. # Specifying initial values as a function fit_mcmc_w_init_fun <- mod$sample( diff --git a/docs/reference/read_cmdstan_csv.html b/docs/reference/read_cmdstan_csv.html index 2106577ef..d6b8fb744 100644 --- a/docs/reference/read_cmdstan_csv.html +++ b/docs/reference/read_cmdstan_csv.html @@ -22,7 +22,7 @@ cmdstanr - 0.6.0 + 0.7.0 @@ -173,7 +173,7 @@

    Arguments

    Value

    -

    as_cmdstan_fit() returns a CmdStanMCMC, CmdStanMLE, or +

    as_cmdstan_fit() returns a CmdStanMCMC, CmdStanMLE, CmdStanLaplace or CmdStanVB object. Some methods typically defined for those objects will not work (e.g. save_data_file()) but the important methods like $summary(), $draws(), $sampler_diagnostics() and others will work fine.

    @@ -206,7 +206,8 @@

    Value

    specified) of post-warmup draws of the sampler diagnostic variables.

  • For optimization the returned list also includes the following components:

    • point_estimates: Point estimates for the model parameters.

    • -

    For variational inference the returned list also +

    For laplace and +variational inference the returned list also includes the following components:

    • draws: A draws_matrix (or different format if format is specified) of draws from the approximate posterior distribution.

    • @@ -222,10 +223,10 @@

      Examples

      fit1 <- cmdstanr_example("logistic", method = "sample", save_warmup = TRUE) csv_files <- fit1$output_files() print(csv_files) -#> [1] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/logistic-202307251438-1-0afc76.csv" -#> [2] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/logistic-202307251438-2-0afc76.csv" -#> [3] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/logistic-202307251438-3-0afc76.csv" -#> [4] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/logistic-202307251438-4-0afc76.csv" +#> [1] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/logistic-202312131008-1-642e8e.csv" +#> [2] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/logistic-202312131008-2-642e8e.csv" +#> [3] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/logistic-202312131008-3-642e8e.csv" +#> [4] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/logistic-202312131008-4-642e8e.csv" # Creating fitting model objects @@ -233,9 +234,9 @@

      Examples

      fit2 <- as_cmdstan_fit(csv_files) fit2$print("beta") #> variable mean median sd mad q5 q95 rhat ess_bulk ess_tail -#> beta[1] -0.67 -0.66 0.25 0.25 -1.09 -0.26 1.00 4467 2955 -#> beta[2] -0.27 -0.27 0.22 0.22 -0.63 0.09 1.00 4244 3209 -#> beta[3] 0.68 0.68 0.26 0.26 0.25 1.12 1.00 4340 3315 +#> beta[1] -0.67 -0.66 0.25 0.25 -1.09 -0.26 1.00 4234 3123 +#> beta[2] -0.28 -0.27 0.23 0.23 -0.65 0.09 1.00 4067 3185 +#> beta[3] 0.69 0.67 0.27 0.27 0.26 1.14 1.00 4291 3208 # Using read_cmdstan_csv # @@ -245,9 +246,9 @@

      Examples

      #> List of 8 #> $ metadata :List of 40 #> ..$ stan_version_major : num 2 -#> ..$ stan_version_minor : num 32 -#> ..$ stan_version_patch : num 2 -#> ..$ start_datetime : chr "2023-07-25 20:38:31 UTC" +#> ..$ stan_version_minor : num 33 +#> ..$ stan_version_patch : num 0 +#> ..$ start_datetime : chr "2023-12-13 17:08:27 UTC" #> ..$ method : chr "sample" #> ..$ save_warmup : num 1 #> ..$ thin : num 1 @@ -264,14 +265,14 @@

      Examples

      #> ..$ num_chains : num 1 #> ..$ id : num [1:4] 1 2 3 4 #> ..$ init : num [1:4] 2 2 2 2 -#> ..$ seed : num 1.15e+09 +#> ..$ seed : num 1.1e+09 #> ..$ refresh : num 100 #> ..$ sig_figs : num -1 -#> ..$ profile_file : chr "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/logistic-profile-202307251438-1-5569d9.csv" -#> ..$ stanc_version : chr "stanc3 v2.32.2" +#> ..$ profile_file : chr "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/logistic-profile-202312131008-1-0e2cb1.csv" +#> ..$ stanc_version : chr "stanc3 v2.33.1" #> ..$ sampler_diagnostics : chr [1:6] "accept_stat__" "stepsize__" "treedepth__" "n_leapfrog__" ... #> ..$ variables : chr [1:105] "lp__" "alpha" "beta[1]" "beta[2]" ... -#> ..$ step_size_adaptation: num [1:4] 0.666 0.702 0.658 0.727 +#> ..$ step_size_adaptation: num [1:4] 0.761 0.758 0.859 0.765 #> ..$ model_name : chr "logistic_model" #> ..$ adapt_engaged : num 1 #> ..$ adapt_delta : num 0.8 @@ -282,9 +283,9 @@

      Examples

      #> ..$ threads_per_chain : num 1 #> ..$ time :'data.frame': 4 obs. of 4 variables: #> .. ..$ chain_id: num [1:4] 1 2 3 4 -#> .. ..$ warmup : num [1:4] 0.07 0.081 0.077 0.071 -#> .. ..$ sampling: num [1:4] 0.068 0.075 0.065 0.062 -#> .. ..$ total : num [1:4] 0.138 0.156 0.142 0.133 +#> .. ..$ warmup : num [1:4] 0.071 0.073 0.072 0.069 +#> .. ..$ sampling: num [1:4] 0.068 0.068 0.065 0.068 +#> .. ..$ total : num [1:4] 0.139 0.141 0.137 0.137 #> ..$ stan_variable_sizes :List of 4 #> .. ..$ lp__ : num 1 #> .. ..$ alpha : num 1 @@ -296,35 +297,35 @@

      Examples

      #> ..$ total : int NA #> ..$ chains:'data.frame': 4 obs. of 4 variables: #> .. ..$ chain_id: num [1:4] 1 2 3 4 -#> .. ..$ warmup : num [1:4] 0.07 0.081 0.077 0.071 -#> .. ..$ sampling: num [1:4] 0.068 0.075 0.065 0.062 -#> .. ..$ total : num [1:4] 0.138 0.156 0.142 0.133 +#> .. ..$ warmup : num [1:4] 0.071 0.073 0.072 0.069 +#> .. ..$ sampling: num [1:4] 0.068 0.068 0.065 0.068 +#> .. ..$ total : num [1:4] 0.139 0.141 0.137 0.137 #> $ inv_metric :List of 4 -#> ..$ 1: num [1:4] 0.0426 0.0631 0.0525 0.0739 -#> ..$ 2: num [1:4] 0.0483 0.0658 0.0426 0.065 -#> ..$ 3: num [1:4] 0.0473 0.0591 0.0532 0.0814 -#> ..$ 4: num [1:4] 0.0485 0.0583 0.05 0.0707 +#> ..$ 1: num [1:4] 0.0469 0.0641 0.0523 0.0688 +#> ..$ 2: num [1:4] 0.0438 0.0555 0.0497 0.0685 +#> ..$ 3: num [1:4] 0.0457 0.0612 0.0451 0.0706 +#> ..$ 4: num [1:4] 0.0436 0.0627 0.0506 0.0832 #> $ step_size :List of 4 -#> ..$ 1: num 0.666 -#> ..$ 2: num 0.702 -#> ..$ 3: num 0.658 -#> ..$ 4: num 0.727 -#> $ warmup_draws : 'draws_array' num [1:1000, 1:4, 1:105] -105.5 -105.5 -105.5 -70.7 -66.2 ... +#> ..$ 1: num 0.761 +#> ..$ 2: num 0.758 +#> ..$ 3: num 0.859 +#> ..$ 4: num 0.765 +#> $ warmup_draws : 'draws_array' num [1:1000, 1:4, 1:105] -72 -72 -72 -71.3 -68.2 ... #> ..- attr(*, "dimnames")=List of 3 #> .. ..$ iteration: chr [1:1000] "1" "2" "3" "4" ... #> .. ..$ chain : chr [1:4] "1" "2" "3" "4" #> .. ..$ variable : chr [1:105] "lp__" "alpha" "beta[1]" "beta[2]" ... -#> $ post_warmup_draws : 'draws_array' num [1:1000, 1:4, 1:105] -65.2 -64.4 -65.4 -64.4 -70.3 ... +#> $ post_warmup_draws : 'draws_array' num [1:1000, 1:4, 1:105] -64.3 -64.3 -65.7 -67.2 -66 ... #> ..- attr(*, "dimnames")=List of 3 #> .. ..$ iteration: chr [1:1000] "1" "2" "3" "4" ... #> .. ..$ chain : chr [1:4] "1" "2" "3" "4" #> .. ..$ variable : chr [1:105] "lp__" "alpha" "beta[1]" "beta[2]" ... -#> $ warmup_sampler_diagnostics : 'draws_array' num [1:1000, 1:4, 1:6] 6.68e-01 0.00 1.27e-204 9.99e-01 9.89e-01 ... +#> $ warmup_sampler_diagnostics : 'draws_array' num [1:1000, 1:4, 1:6] 1 0 0 0.597 1 ... #> ..- attr(*, "dimnames")=List of 3 #> .. ..$ iteration: chr [1:1000] "1" "2" "3" "4" ... #> .. ..$ chain : chr [1:4] "1" "2" "3" "4" #> .. ..$ variable : chr [1:6] "accept_stat__" "stepsize__" "treedepth__" "n_leapfrog__" ... -#> $ post_warmup_sampler_diagnostics: 'draws_array' num [1:1000, 1:4, 1:6] 0.997 0.935 0.852 0.98 0.644 ... +#> $ post_warmup_sampler_diagnostics: 'draws_array' num [1:1000, 1:4, 1:6] 0.971 0.97 0.742 0.882 0.87 ... #> ..- attr(*, "dimnames")=List of 3 #> .. ..$ iteration: chr [1:1000] "1" "2" "3" "4" ... #> .. ..$ chain : chr [1:4] "1" "2" "3" "4" diff --git a/docs/reference/read_sample_csv.html b/docs/reference/read_sample_csv.html index af34b60aa..3e9b12d56 100644 --- a/docs/reference/read_sample_csv.html +++ b/docs/reference/read_sample_csv.html @@ -17,7 +17,7 @@ cmdstanr - 0.6.0 + 0.7.0 diff --git a/docs/reference/register_knitr_engine.html b/docs/reference/register_knitr_engine.html index 1e4517702..5a66dd01d 100644 --- a/docs/reference/register_knitr_engine.html +++ b/docs/reference/register_knitr_engine.html @@ -20,7 +20,7 @@ cmdstanr - 0.6.0 + 0.7.0 diff --git a/docs/reference/set_cmdstan_path.html b/docs/reference/set_cmdstan_path.html index a91b01e82..d467d271b 100644 --- a/docs/reference/set_cmdstan_path.html +++ b/docs/reference/set_cmdstan_path.html @@ -21,7 +21,7 @@ cmdstanr - 0.6.0 + 0.7.0 diff --git a/docs/reference/stan_threads.html b/docs/reference/stan_threads.html index eeeb82fae..bf8c1ef32 100644 --- a/docs/reference/stan_threads.html +++ b/docs/reference/stan_threads.html @@ -17,7 +17,7 @@ cmdstanr - 0.6.0 + 0.7.0 diff --git a/docs/reference/write_stan_file.html b/docs/reference/write_stan_file.html index 9b772e61c..e14591323 100644 --- a/docs/reference/write_stan_file.html +++ b/docs/reference/write_stan_file.html @@ -24,7 +24,7 @@ cmdstanr - 0.6.0 + 0.7.0 @@ -176,7 +176,7 @@

      Examples

      stan_program <- " data { int<lower=0> N; - int<lower=0,upper=1> y[N]; + array[N] int<lower=0,upper=1> y; } parameters { real<lower=0,upper=1> theta; @@ -188,21 +188,21 @@

      Examples

      f <- write_stan_file(stan_program) print(f) -#> [1] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/model_08f9e456ca04f3d3244db00f16ea5748.stan" +#> [1] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/model_7f12fc190dd23b0e462f7d73040dd97e.stan" lines <- readLines(f) print(lines) -#> [1] "" "data {" -#> [3] " int<lower=0> N;" " int<lower=0,upper=1> y[N];" -#> [5] "}" "parameters {" -#> [7] " real<lower=0,upper=1> theta;" "}" -#> [9] "model {" " y ~ bernoulli(theta);" -#> [11] "}" "" +#> [1] "" "data {" +#> [3] " int<lower=0> N;" " array[N] int<lower=0,upper=1> y;" +#> [5] "}" "parameters {" +#> [7] " real<lower=0,upper=1> theta;" "}" +#> [9] "model {" " y ~ bernoulli(theta);" +#> [11] "}" "" cat(lines, sep = "\n") #> #> data { #> int<lower=0> N; -#> int<lower=0,upper=1> y[N]; +#> array[N] int<lower=0,upper=1> y; #> } #> parameters { #> real<lower=0,upper=1> theta; diff --git a/docs/reference/write_stan_json.html b/docs/reference/write_stan_json.html index a241a6d9f..b7b128ef1 100644 --- a/docs/reference/write_stan_json.html +++ b/docs/reference/write_stan_json.html @@ -17,7 +17,7 @@ cmdstanr - 0.6.0 + 0.7.0 @@ -167,13 +167,13 @@

      Examples

      #> "N": 5, #> "K": 2, #> "x": [ -#> [1.39067866394794, 0.962441316104017], -#> [-0.473566508639385, -0.892862153643971], -#> [-2.10865772647623, 0.757598190570521], -#> [-0.366087824635297, -0.120402278774405], -#> [1.44942517823716, -0.444483690720062] +#> [0.281077678546936, -1.18908686575029], +#> [0.858726751146771, 0.46461805175525], +#> [-0.700205759803986, -0.115709696625946], +#> [1.45853659080418, 0.995745564145492], +#> [-0.671485199795285, -0.298752493529616] #> ], -#> "y": [5, 11, 1, 9, 11], +#> "y": [12, 6, 11, 9, 11], #> "z": [1, 0] #> } diff --git a/docs/reference/write_stan_tempfile.html b/docs/reference/write_stan_tempfile.html index 4e9c4ac10..107251424 100644 --- a/docs/reference/write_stan_tempfile.html +++ b/docs/reference/write_stan_tempfile.html @@ -17,7 +17,7 @@ cmdstanr - 0.6.0 + 0.7.0 diff --git a/docs/sitemap.xml b/docs/sitemap.xml index a9c13dbbf..74c961502 100644 --- a/docs/sitemap.xml +++ b/docs/sitemap.xml @@ -1,23 +1,98 @@ + + https://mc-stan.org/cmdstanr/404.html + + + https://mc-stan.org/cmdstanr/CONTRIBUTING.html + + + https://mc-stan.org/cmdstanr/LICENSE-text.html + + + https://mc-stan.org/cmdstanr/LICENSE.html + + + https://mc-stan.org/cmdstanr/articles/articles-online-only/opencl.html + + + https://mc-stan.org/cmdstanr/articles/children/settings-knitr.html + + + https://mc-stan.org/cmdstanr/articles/cmdstanr-internals.html + + + https://mc-stan.org/cmdstanr/articles/cmdstanr.html + + + https://mc-stan.org/cmdstanr/articles/deprecations.html + + + https://mc-stan.org/cmdstanr/articles/index.html + + + https://mc-stan.org/cmdstanr/articles/opencl.html + + + https://mc-stan.org/cmdstanr/articles/posterior.html + + + https://mc-stan.org/cmdstanr/articles/profiling.html + + + https://mc-stan.org/cmdstanr/articles/r-markdown.html + + + https://mc-stan.org/cmdstanr/articles/r_markdown.html + + + https://mc-stan.org/cmdstanr/authors.html + https://mc-stan.org/cmdstanr/index.html + + https://mc-stan.org/cmdstanr/news/index.html + + + https://mc-stan.org/cmdstanr/pull_request_template.html + https://mc-stan.org/cmdstanr/reference/CmdStanDiagnose.html + + https://mc-stan.org/cmdstanr/reference/CmdStanFit-method-save_output_files.html + https://mc-stan.org/cmdstanr/reference/CmdStanGQ.html + + https://mc-stan.org/cmdstanr/reference/CmdStanLaplace.html + https://mc-stan.org/cmdstanr/reference/CmdStanMCMC.html https://mc-stan.org/cmdstanr/reference/CmdStanMLE.html + + https://mc-stan.org/cmdstanr/reference/CmdStanModel-method-compile.html + + + https://mc-stan.org/cmdstanr/reference/CmdStanModel-method-optimize.html + + + https://mc-stan.org/cmdstanr/reference/CmdStanModel-method-sample.html + + + https://mc-stan.org/cmdstanr/reference/CmdStanModel-method-variational.html + https://mc-stan.org/cmdstanr/reference/CmdStanModel.html + + https://mc-stan.org/cmdstanr/reference/CmdStanPathfinder.html + https://mc-stan.org/cmdstanr/reference/CmdStanVB.html @@ -36,6 +111,9 @@ https://mc-stan.org/cmdstanr/reference/cmdstan_model.html + + https://mc-stan.org/cmdstanr/reference/cmdstan_path.html + https://mc-stan.org/cmdstanr/reference/cmdstanr-package.html @@ -54,21 +132,39 @@ https://mc-stan.org/cmdstanr/reference/fit-method-code.html + + https://mc-stan.org/cmdstanr/reference/fit-method-constrain_variables.html + https://mc-stan.org/cmdstanr/reference/fit-method-diagnostic_summary.html https://mc-stan.org/cmdstanr/reference/fit-method-draws.html + + https://mc-stan.org/cmdstanr/reference/fit-method-grad_log_prob.html + https://mc-stan.org/cmdstanr/reference/fit-method-gradients.html + + https://mc-stan.org/cmdstanr/reference/fit-method-hessian.html + https://mc-stan.org/cmdstanr/reference/fit-method-init.html + + https://mc-stan.org/cmdstanr/reference/fit-method-init_model_methods.html + https://mc-stan.org/cmdstanr/reference/fit-method-inv_metric.html + + https://mc-stan.org/cmdstanr/reference/fit-method-inverse_metric.html + + + https://mc-stan.org/cmdstanr/reference/fit-method-log_prob.html + https://mc-stan.org/cmdstanr/reference/fit-method-loo.html @@ -108,6 +204,18 @@ https://mc-stan.org/cmdstanr/reference/fit-method-time.html + + https://mc-stan.org/cmdstanr/reference/fit-method-unconstrain_draws.html + + + https://mc-stan.org/cmdstanr/reference/fit-method-unconstrain_variables.html + + + https://mc-stan.org/cmdstanr/reference/fit-method-variable_skeleton.html + + + https://mc-stan.org/cmdstanr/reference/index.html + https://mc-stan.org/cmdstanr/reference/install_cmdstan.html @@ -120,15 +228,24 @@ https://mc-stan.org/cmdstanr/reference/model-method-diagnose.html + + https://mc-stan.org/cmdstanr/reference/model-method-expose_functions.html + https://mc-stan.org/cmdstanr/reference/model-method-format.html https://mc-stan.org/cmdstanr/reference/model-method-generate-quantities.html + + https://mc-stan.org/cmdstanr/reference/model-method-laplace.html + https://mc-stan.org/cmdstanr/reference/model-method-optimize.html + + https://mc-stan.org/cmdstanr/reference/model-method-pathfinder.html + https://mc-stan.org/cmdstanr/reference/model-method-sample.html @@ -165,22 +282,4 @@ https://mc-stan.org/cmdstanr/reference/write_stan_tempfile.html - - https://mc-stan.org/cmdstanr/articles/articles-online-only/opencl.html - - - https://mc-stan.org/cmdstanr/articles/children/settings-knitr.html - - - https://mc-stan.org/cmdstanr/articles/cmdstanr-internals.html - - - https://mc-stan.org/cmdstanr/articles/cmdstanr.html - - - https://mc-stan.org/cmdstanr/articles/profiling.html - - - https://mc-stan.org/cmdstanr/articles/r-markdown.html - diff --git a/man-roxygen/model-common-args.R b/man-roxygen/model-common-args.R index 850138216..7cdcbc49e 100644 --- a/man-roxygen/model-common-args.R +++ b/man-roxygen/model-common-args.R @@ -34,10 +34,10 @@ #' [write_stan_json()] to write \R objects to JSON files compatible with #' CmdStan. #' * A list of lists containing initial values for all or some parameters. For -#' MCMC the list should contain a sublist for each chain. For optimization and -#' variational inference there should be just one sublist. The sublists should -#' have named elements corresponding to the parameters for which you are -#' specifying initial values. See **Examples**. +#' MCMC the list should contain a sublist for each chain. For other model +#' fitting methods there should be just one sublist. The sublists should have +#' named elements corresponding to the parameters for which you are specifying +#' initial values. See **Examples**. #' * A function that returns a single list with names corresponding to the #' parameters for which you are specifying initial values. The function can #' take no arguments or a single argument `chain_id`. For MCMC, if the function diff --git a/man/CmdStanDiagnose.Rd b/man/CmdStanDiagnose.Rd index 70930db17..8c786e88f 100644 --- a/man/CmdStanDiagnose.Rd +++ b/man/CmdStanDiagnose.Rd @@ -45,6 +45,7 @@ Other fitted model objects: \code{\link{CmdStanLaplace}}, \code{\link{CmdStanMCMC}}, \code{\link{CmdStanMLE}}, +\code{\link{CmdStanPathfinder}}, \code{\link{CmdStanVB}} } \concept{fitted model objects} diff --git a/man/CmdStanGQ.Rd b/man/CmdStanGQ.Rd index 6ebe2d9c6..73fab133a 100644 --- a/man/CmdStanGQ.Rd +++ b/man/CmdStanGQ.Rd @@ -106,6 +106,7 @@ Other fitted model objects: \code{\link{CmdStanLaplace}}, \code{\link{CmdStanMCMC}}, \code{\link{CmdStanMLE}}, +\code{\link{CmdStanPathfinder}}, \code{\link{CmdStanVB}} } \concept{fitted model objects} diff --git a/man/CmdStanLaplace.Rd b/man/CmdStanLaplace.Rd index 50d782a63..4d16723e0 100644 --- a/man/CmdStanLaplace.Rd +++ b/man/CmdStanLaplace.Rd @@ -67,6 +67,7 @@ Other fitted model objects: \code{\link{CmdStanGQ}}, \code{\link{CmdStanMCMC}}, \code{\link{CmdStanMLE}}, +\code{\link{CmdStanPathfinder}}, \code{\link{CmdStanVB}} } \concept{fitted model objects} diff --git a/man/CmdStanMCMC.Rd b/man/CmdStanMCMC.Rd index c74ff41f2..2148b7a68 100644 --- a/man/CmdStanMCMC.Rd +++ b/man/CmdStanMCMC.Rd @@ -89,6 +89,7 @@ Other fitted model objects: \code{\link{CmdStanGQ}}, \code{\link{CmdStanLaplace}}, \code{\link{CmdStanMLE}}, +\code{\link{CmdStanPathfinder}}, \code{\link{CmdStanVB}} } \concept{fitted model objects} diff --git a/man/CmdStanMLE.Rd b/man/CmdStanMLE.Rd index 141e45aa6..82c119106 100644 --- a/man/CmdStanMLE.Rd +++ b/man/CmdStanMLE.Rd @@ -79,6 +79,7 @@ Other fitted model objects: \code{\link{CmdStanGQ}}, \code{\link{CmdStanLaplace}}, \code{\link{CmdStanMCMC}}, +\code{\link{CmdStanPathfinder}}, \code{\link{CmdStanVB}} } \concept{fitted model objects} diff --git a/man/CmdStanModel.Rd b/man/CmdStanModel.Rd index 2644784e2..552003572 100644 --- a/man/CmdStanModel.Rd +++ b/man/CmdStanModel.Rd @@ -47,6 +47,7 @@ methods, many of which have their own (linked) documentation pages: \code{\link[=model-method-sample_mpi]{$sample_mpi()}} \tab Run CmdStan's \code{"sample"} method with \href{https://mc-stan.org/math/mpi.html}{MPI}, return \code{\link{CmdStanMCMC}} object. \cr \code{\link[=model-method-optimize]{$optimize()}} \tab Run CmdStan's \code{"optimize"} method, return \code{\link{CmdStanMLE}} object. \cr \code{\link[=model-method-variational]{$variational()}} \tab Run CmdStan's \code{"variational"} method, return \code{\link{CmdStanVB}} object. \cr + \code{\link[=model-method-pathfinder]{$pathfinder()}} \tab Run CmdStan's \code{"pathfinder"} method, return \code{\link{CmdStanPathfinder}} object. \cr \code{\link[=model-method-generate-quantities]{$generate_quantities()}} \tab Run CmdStan's \code{"generate quantities"} method, return \code{\link{CmdStanGQ}} object. \cr } @@ -111,19 +112,26 @@ my_data_file <- file.path(cmdstan_path(), "examples/bernoulli/bernoulli.data.jso fit_optim <- mod$optimize(data = my_data_file, seed = 123) fit_optim$summary() -# Run 'optimize' again with 'jacobian=TRUE' and then draw from laplace approximation +# Run 'optimize' again with 'jacobian=TRUE' and then draw from Laplace approximation # to the posterior fit_optim <- mod$optimize(data = my_data_file, jacobian = TRUE) fit_laplace <- mod$laplace(data = my_data_file, mode = fit_optim, draws = 2000) fit_laplace$summary() -# Run 'variational' method to approximate the posterior (default is meanfield ADVI) +# Run 'variational' method to use ADVI to approximate posterior fit_vb <- mod$variational(data = stan_data, seed = 123) fit_vb$summary() - -# Plot approximate posterior using bayesplot mcmc_hist(fit_vb$draws("theta")) +# Run 'pathfinder' method, a new alternative to the variational method +fit_pf <- mod$pathfinder(data = stan_data, seed = 123) +fit_pf$summary() +mcmc_hist(fit_pf$draws("theta")) + +# Run 'pathfinder' again with more paths, fewer draws per path, +# better covariance approximation, and fewer LBFGSs iterations +fit_pf <- mod$pathfinder(data = stan_data, num_paths=10, single_path_draws=40, + history_size=50, max_lbfgs_iters=100) # Specifying initial values as a function fit_mcmc_w_init_fun <- mod$sample( diff --git a/man/CmdStanPathfinder.Rd b/man/CmdStanPathfinder.Rd new file mode 100644 index 000000000..970174dcf --- /dev/null +++ b/man/CmdStanPathfinder.Rd @@ -0,0 +1,73 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/fit.R +\name{CmdStanPathfinder} +\alias{CmdStanPathfinder} +\title{CmdStanPathfinder objects} +\description{ +A \code{CmdStanPathfinder} object is the fitted model object returned by the +\code{\link[=model-method-pathfinder]{$pathfinder()}} method of a +\code{\link{CmdStanModel}} object. +} +\section{Methods}{ + \code{CmdStanPathfinder} objects have the following associated methods, +all of which have their own (linked) documentation pages. +\subsection{Extract contents of fitted model object}{\tabular{ll}{ + \strong{Method} \tab \strong{Description} \cr + \code{\link[=fit-method-draws]{$draws()}} \tab Return approximate posterior draws as a \code{\link[posterior:draws_matrix]{draws_matrix}}. \cr + \code{\link[=fit-method-lp]{$lp()}} \tab Return the total log probability density (\code{target}) computed in the model block of the Stan program. \cr + \code{\link[=fit-method-lp]{$lp_approx()}} \tab Return the log density of the approximation to the posterior. \cr + \code{\link[=fit-method-init]{$init()}} \tab Return user-specified initial values. \cr + \code{\link[=fit-method-metadata]{$metadata()}} \tab Return a list of metadata gathered from the CmdStan CSV files. \cr + \code{\link[=fit-method-code]{$code()}} \tab Return Stan code as a character vector. \cr +} + +} + +\subsection{Summarize inferences}{\tabular{ll}{ + \strong{Method} \tab \strong{Description} \cr + \code{\link[=fit-method-summary]{$summary()}} \tab Run \code{\link[posterior:draws_summary]{posterior::summarise_draws()}}. \cr + \code{\link[=fit-method-cmdstan_summary]{$cmdstan_summary()}} \tab Run and print CmdStan's \code{bin/stansummary}. \cr +} + +} + +\subsection{Save fitted model object and temporary files}{\tabular{ll}{ + \strong{Method} \tab \strong{Description} \cr + \code{\link[=fit-method-save_object]{$save_object()}} \tab Save fitted model object to a file. \cr + \code{\link[=fit-method-save_output_files]{$save_output_files()}} \tab Save output CSV files to a specified location. \cr + \code{\link[=fit-method-save_data_file]{$save_data_file()}} \tab Save JSON data file to a specified location. \cr + \code{\link[=fit-method-save_latent_dynamics_files]{$save_latent_dynamics_files()}} \tab Save diagnostic CSV files to a specified location. \cr +} + +} + +\subsection{Report run times, console output, return codes}{\tabular{ll}{ + \strong{Method} \tab \strong{Description} \cr + \code{\link[=fit-method-time]{$time()}} \tab Report the total run time. \cr + \code{\link[=fit-method-output]{$output()}} \tab Pretty print the output that was printed to the console. \cr + \code{\link[=fit-method-return_codes]{$return_codes()}} \tab Return the return codes from the CmdStan runs. \cr +} + +} +} + +\seealso{ +The CmdStanR website +(\href{https://mc-stan.org/cmdstanr/}{mc-stan.org/cmdstanr}) for online +documentation and tutorials. + +The Stan and CmdStan documentation: +\itemize{ +\item Stan documentation: \href{https://mc-stan.org/users/documentation/}{mc-stan.org/users/documentation} +\item CmdStan User’s Guide: \href{https://mc-stan.org/docs/cmdstan-guide/}{mc-stan.org/docs/cmdstan-guide} +} + +Other fitted model objects: +\code{\link{CmdStanDiagnose}}, +\code{\link{CmdStanGQ}}, +\code{\link{CmdStanLaplace}}, +\code{\link{CmdStanMCMC}}, +\code{\link{CmdStanMLE}}, +\code{\link{CmdStanVB}} +} +\concept{fitted model objects} diff --git a/man/CmdStanVB.Rd b/man/CmdStanVB.Rd index 9c8cd80b2..323ec339b 100644 --- a/man/CmdStanVB.Rd +++ b/man/CmdStanVB.Rd @@ -82,6 +82,7 @@ Other fitted model objects: \code{\link{CmdStanGQ}}, \code{\link{CmdStanLaplace}}, \code{\link{CmdStanMCMC}}, -\code{\link{CmdStanMLE}} +\code{\link{CmdStanMLE}}, +\code{\link{CmdStanPathfinder}} } \concept{fitted model objects} diff --git a/man/as_draws.CmdStanMCMC.Rd b/man/as_draws.CmdStanMCMC.Rd index a63ae953f..f28d5f50c 100644 --- a/man/as_draws.CmdStanMCMC.Rd +++ b/man/as_draws.CmdStanMCMC.Rd @@ -7,6 +7,7 @@ \alias{as_draws.CmdStanLaplace} \alias{as_draws.CmdStanVB} \alias{as_draws.CmdStanGQ} +\alias{as_draws.CmdStanPathfinder} \title{Create a \code{draws} object from a CmdStanR fitted model object} \usage{ \method{as_draws}{CmdStanMCMC}(x, ...) @@ -18,6 +19,8 @@ \method{as_draws}{CmdStanVB}(x, ...) \method{as_draws}{CmdStanGQ}(x, ...) + +\method{as_draws}{CmdStanPathfinder}(x, ...) } \arguments{ \item{x}{A CmdStanR fitted model object.} diff --git a/man/cmdstan_model.Rd b/man/cmdstan_model.Rd index 6e9408dd1..b0432abc2 100644 --- a/man/cmdstan_model.Rd +++ b/man/cmdstan_model.Rd @@ -31,7 +31,7 @@ more. See \code{\link[=model-method-compile]{$compile()}} for details.} A \code{\link{CmdStanModel}} object. } \description{ -\if{html}{\figure{logo.png}{options: width="25px"}} +\if{html}{\figure{logo.png}{options: width=25}} Create a new \code{\link{CmdStanModel}} object from a file containing a Stan program or from an existing Stan executable. The \code{\link{CmdStanModel}} object stores the path to a Stan program and compiled executable (once created), and provides @@ -98,19 +98,26 @@ my_data_file <- file.path(cmdstan_path(), "examples/bernoulli/bernoulli.data.jso fit_optim <- mod$optimize(data = my_data_file, seed = 123) fit_optim$summary() -# Run 'optimize' again with 'jacobian=TRUE' and then draw from laplace approximation +# Run 'optimize' again with 'jacobian=TRUE' and then draw from Laplace approximation # to the posterior fit_optim <- mod$optimize(data = my_data_file, jacobian = TRUE) fit_laplace <- mod$laplace(data = my_data_file, mode = fit_optim, draws = 2000) fit_laplace$summary() -# Run 'variational' method to approximate the posterior (default is meanfield ADVI) +# Run 'variational' method to use ADVI to approximate posterior fit_vb <- mod$variational(data = stan_data, seed = 123) fit_vb$summary() - -# Plot approximate posterior using bayesplot mcmc_hist(fit_vb$draws("theta")) +# Run 'pathfinder' method, a new alternative to the variational method +fit_pf <- mod$pathfinder(data = stan_data, seed = 123) +fit_pf$summary() +mcmc_hist(fit_pf$draws("theta")) + +# Run 'pathfinder' again with more paths, fewer draws per path, +# better covariance approximation, and fewer LBFGSs iterations +fit_pf <- mod$pathfinder(data = stan_data, num_paths=10, single_path_draws=40, + history_size=50, max_lbfgs_iters=100) # Specifying initial values as a function fit_mcmc_w_init_fun <- mod$sample( diff --git a/man/cmdstanr-package.Rd b/man/cmdstanr-package.Rd index 9f2feb180..930105ee3 100644 --- a/man/cmdstanr-package.Rd +++ b/man/cmdstanr-package.Rd @@ -34,9 +34,11 @@ algorithms, and writing results to output files. \subsection{Advantages of RStan}{ \itemize{ \item Allows other developers to distribute R packages with \emph{pre-compiled} -Stan programs (like \strong{rstanarm}) on CRAN. +Stan programs (like \strong{rstanarm}) on CRAN. (Note: As of 2023, this can +mostly be achieved with CmdStanR as well. See \href{https://mc-stan.org/cmdstanr/articles/cmdstanr-internals.html#developing-using-cmdstanr}{Developing using CmdStanR}.) \item Avoids use of R6 classes, which may result in more familiar syntax for many R users. +\item CRAN binaries available for Mac and Windows. } } @@ -123,19 +125,26 @@ my_data_file <- file.path(cmdstan_path(), "examples/bernoulli/bernoulli.data.jso fit_optim <- mod$optimize(data = my_data_file, seed = 123) fit_optim$summary() -# Run 'optimize' again with 'jacobian=TRUE' and then draw from laplace approximation +# Run 'optimize' again with 'jacobian=TRUE' and then draw from Laplace approximation # to the posterior fit_optim <- mod$optimize(data = my_data_file, jacobian = TRUE) fit_laplace <- mod$laplace(data = my_data_file, mode = fit_optim, draws = 2000) fit_laplace$summary() -# Run 'variational' method to approximate the posterior (default is meanfield ADVI) +# Run 'variational' method to use ADVI to approximate posterior fit_vb <- mod$variational(data = stan_data, seed = 123) fit_vb$summary() - -# Plot approximate posterior using bayesplot mcmc_hist(fit_vb$draws("theta")) +# Run 'pathfinder' method, a new alternative to the variational method +fit_pf <- mod$pathfinder(data = stan_data, seed = 123) +fit_pf$summary() +mcmc_hist(fit_pf$draws("theta")) + +# Run 'pathfinder' again with more paths, fewer draws per path, +# better covariance approximation, and fewer LBFGSs iterations +fit_pf <- mod$pathfinder(data = stan_data, num_paths=10, single_path_draws=40, + history_size=50, max_lbfgs_iters=100) # Specifying initial values as a function fit_mcmc_w_init_fun <- mod$sample( diff --git a/man/cmdstanr_example.Rd b/man/cmdstanr_example.Rd index 3474811b9..5efa2a42b 100644 --- a/man/cmdstanr_example.Rd +++ b/man/cmdstanr_example.Rd @@ -7,7 +7,7 @@ \usage{ cmdstanr_example( example = c("logistic", "schools", "schools_ncp"), - method = c("sample", "optimize", "laplace", "variational", "diagnose"), + method = c("sample", "optimize", "laplace", "variational", "pathfinder", "diagnose"), ..., quiet = TRUE, force_recompile = getOption("cmdstanr_force_recompile", default = FALSE) diff --git a/man/fit-method-grad_log_prob.Rd b/man/fit-method-grad_log_prob.Rd index 42b610467..b74f19bae 100644 --- a/man/fit-method-grad_log_prob.Rd +++ b/man/fit-method-grad_log_prob.Rd @@ -6,14 +6,19 @@ \title{Calculate the log-probability and the gradient w.r.t. each input for a given vector of unconstrained parameters} \usage{ -grad_log_prob(unconstrained_variables, jacobian_adjustment = TRUE) +grad_log_prob( + unconstrained_variables, + jacobian = TRUE, + jacobian_adjustment = NULL +) } \arguments{ -\item{unconstrained_variables}{(numeric) A vector of unconstrained parameters -to be passed to \code{grad_log_prob}.} +\item{unconstrained_variables}{(numeric) A vector of unconstrained parameters.} -\item{jacobian_adjustment}{(logical) Whether to include the log-density -adjustments from un/constraining variables.} +\item{jacobian}{(logical) Whether to include the log-density adjustments from +un/constraining variables.} + +\item{jacobian_adjustment}{Deprecated. Please use \code{jacobian} instead.} } \description{ The \verb{$grad_log_prob()} method provides access to the Stan diff --git a/man/fit-method-hessian.Rd b/man/fit-method-hessian.Rd index 8e1d5f308..8076a8898 100644 --- a/man/fit-method-hessian.Rd +++ b/man/fit-method-hessian.Rd @@ -6,14 +6,15 @@ \title{Calculate the log-probability , the gradient w.r.t. each input, and the hessian for a given vector of unconstrained parameters} \usage{ -hessian(unconstrained_variables, jacobian_adjustment = TRUE) +hessian(unconstrained_variables, jacobian = TRUE, jacobian_adjustment = NULL) } \arguments{ -\item{unconstrained_variables}{(numeric) A vector of unconstrained parameters -to be passed to \code{hessian}.} +\item{unconstrained_variables}{(numeric) A vector of unconstrained parameters.} -\item{jacobian_adjustment}{(logical) Whether to include the log-density -adjustments from un/constraining variables.} +\item{jacobian}{(logical) Whether to include the log-density adjustments from +un/constraining variables.} + +\item{jacobian_adjustment}{Deprecated. Please use \code{jacobian} instead.} } \description{ The \verb{$hessian()} method provides access to the Stan model's diff --git a/man/fit-method-log_prob.Rd b/man/fit-method-log_prob.Rd index 46eb4fb60..41edbc3c5 100644 --- a/man/fit-method-log_prob.Rd +++ b/man/fit-method-log_prob.Rd @@ -5,17 +5,19 @@ \alias{log_prob} \title{Calculate the log-probability given a provided vector of unconstrained parameters.} \usage{ -log_prob(unconstrained_variables, jacobian_adjustment = TRUE) +log_prob(unconstrained_variables, jacobian = TRUE, jacobian_adjustment = NULL) } \arguments{ -\item{unconstrained_variables}{(numeric) A vector of unconstrained parameters -to be passed to \code{log_prob}.} +\item{unconstrained_variables}{(numeric) A vector of unconstrained parameters.} -\item{jacobian_adjustment}{(logical) Whether to include the log-density -adjustments from un/constraining variables.} +\item{jacobian}{(logical) Whether to include the log-density adjustments from +un/constraining variables.} + +\item{jacobian_adjustment}{Deprecated. Please use \code{jacobian} instead.} } \description{ -The \verb{$log_prob()} method provides access to the Stan model's \code{log_prob} function +The \verb{$log_prob()} method provides access to the Stan model's +\code{log_prob} function. } \examples{ \dontrun{ diff --git a/man/fit-method-lp.Rd b/man/fit-method-lp.Rd index acda71b7c..686c55d2b 100644 --- a/man/fit-method-lp.Rd +++ b/man/fit-method-lp.Rd @@ -8,6 +8,8 @@ \usage{ lp() +lp_approx() + lp_approx() } \value{ diff --git a/man/install_cmdstan.Rd b/man/install_cmdstan.Rd index 84b0f2964..44450d8fd 100644 --- a/man/install_cmdstan.Rd +++ b/man/install_cmdstan.Rd @@ -15,6 +15,7 @@ install_cmdstan( timeout = 1200, version = NULL, release_url = NULL, + release_file = NULL, cpp_options = list(), check_toolchain = TRUE, wsl = FALSE @@ -66,6 +67,11 @@ The URL should point to the tarball (\code{.tar.gz.} file) itself, e.g., \code{release_url="https://github.com/stan-dev/cmdstan/releases/download/v2.25.0/cmdstan-2.25.0.tar.gz"}. If both \code{version} and \code{release_url} are specified then \code{version} will be used.} +\item{release_file}{(string) A file path to a CmdStan release tar.gz file +downloaded from the releases page: \url{https://github.com/stan-dev/cmdstan/releases}. +For example: \verb{release_file=""./cmdstan-2.33.1.tar.gz"}. If \code{release_file} is +specified then both \code{release_url} and \code{version} will be ignored.} + \item{cpp_options}{(list) Any makefile flags/variables to be written to the \code{make/local} file. For example, \code{list("CXX" = "clang++")} will force the use of clang for compilation.} diff --git a/man/model-method-check_syntax.Rd b/man/model-method-check_syntax.Rd index 64e93c02d..a646a5e16 100644 --- a/man/model-method-check_syntax.Rd +++ b/man/model-method-check_syntax.Rd @@ -15,7 +15,7 @@ check_syntax( \arguments{ \item{pedantic}{(logical) Should pedantic mode be turned on? The default is \code{FALSE}. Pedantic mode attempts to warn you about potential issues in your -Stan program beyond syntax errors. For details see the \href{https://mc-stan.org/docs/reference-manual/pedantic-mode.html}{\emph{Pedantic mode} chapter} in +Stan program beyond syntax errors. For details see the \href{https://mc-stan.org/docs/stan-users-guide/pedantic-mode.html}{\emph{Pedantic mode} chapter} in the Stan Reference Manual.} \item{include_paths}{(character vector) Paths to directories where Stan @@ -85,6 +85,7 @@ Other CmdStanModel methods: \code{\link{model-method-generate-quantities}}, \code{\link{model-method-laplace}}, \code{\link{model-method-optimize}}, +\code{\link{model-method-pathfinder}}, \code{\link{model-method-sample_mpi}}, \code{\link{model-method-sample}}, \code{\link{model-method-variables}}, diff --git a/man/model-method-compile.Rd b/man/model-method-compile.Rd index 7e1805747..d295eedcc 100644 --- a/man/model-method-compile.Rd +++ b/man/model-method-compile.Rd @@ -33,7 +33,7 @@ same location as the Stan program.} \item{pedantic}{(logical) Should pedantic mode be turned on? The default is \code{FALSE}. Pedantic mode attempts to warn you about potential issues in your -Stan program beyond syntax errors. For details see the \href{https://mc-stan.org/docs/reference-manual/pedantic-mode.html}{\emph{Pedantic mode} chapter} in +Stan program beyond syntax errors. For details see the \href{https://mc-stan.org/docs/stan-users-guide/pedantic-mode.html}{\emph{Pedantic mode} chapter} in the Stan Reference Manual. \strong{Note:} to do a pedantic check for a model without compiling it or for a model that is already compiled the \code{\link[=model-method-check_syntax]{$check_syntax()}} method can be used instead.} @@ -156,6 +156,7 @@ Other CmdStanModel methods: \code{\link{model-method-generate-quantities}}, \code{\link{model-method-laplace}}, \code{\link{model-method-optimize}}, +\code{\link{model-method-pathfinder}}, \code{\link{model-method-sample_mpi}}, \code{\link{model-method-sample}}, \code{\link{model-method-variables}}, diff --git a/man/model-method-diagnose.Rd b/man/model-method-diagnose.Rd index 15e7e21c4..dff6359cc 100644 --- a/man/model-method-diagnose.Rd +++ b/man/model-method-diagnose.Rd @@ -51,10 +51,10 @@ containing initial values for all or some parameters. See \code{\link[=write_stan_json]{write_stan_json()}} to write \R objects to JSON files compatible with CmdStan. \item A list of lists containing initial values for all or some parameters. For -MCMC the list should contain a sublist for each chain. For optimization and -variational inference there should be just one sublist. The sublists should -have named elements corresponding to the parameters for which you are -specifying initial values. See \strong{Examples}. +MCMC the list should contain a sublist for each chain. For other model +fitting methods there should be just one sublist. The sublists should have +named elements corresponding to the parameters for which you are specifying +initial values. See \strong{Examples}. \item A function that returns a single list with names corresponding to the parameters for which you are specifying initial values. The function can take no arguments or a single argument \code{chain_id}. For MCMC, if the function @@ -127,6 +127,7 @@ Other CmdStanModel methods: \code{\link{model-method-generate-quantities}}, \code{\link{model-method-laplace}}, \code{\link{model-method-optimize}}, +\code{\link{model-method-pathfinder}}, \code{\link{model-method-sample_mpi}}, \code{\link{model-method-sample}}, \code{\link{model-method-variables}}, diff --git a/man/model-method-expose_functions.Rd b/man/model-method-expose_functions.Rd index 066c7bad3..a62f7bb86 100644 --- a/man/model-method-expose_functions.Rd +++ b/man/model-method-expose_functions.Rd @@ -76,6 +76,7 @@ Other CmdStanModel methods: \code{\link{model-method-generate-quantities}}, \code{\link{model-method-laplace}}, \code{\link{model-method-optimize}}, +\code{\link{model-method-pathfinder}}, \code{\link{model-method-sample_mpi}}, \code{\link{model-method-sample}}, \code{\link{model-method-variables}}, diff --git a/man/model-method-format.Rd b/man/model-method-format.Rd index 294ad8cce..2aa34f186 100644 --- a/man/model-method-format.Rd +++ b/man/model-method-format.Rd @@ -105,6 +105,7 @@ Other CmdStanModel methods: \code{\link{model-method-generate-quantities}}, \code{\link{model-method-laplace}}, \code{\link{model-method-optimize}}, +\code{\link{model-method-pathfinder}}, \code{\link{model-method-sample_mpi}}, \code{\link{model-method-sample}}, \code{\link{model-method-variables}}, diff --git a/man/model-method-generate-quantities.Rd b/man/model-method-generate-quantities.Rd index adf2fd146..c9173c30e 100644 --- a/man/model-method-generate-quantities.Rd +++ b/man/model-method-generate-quantities.Rd @@ -176,6 +176,7 @@ Other CmdStanModel methods: \code{\link{model-method-format}}, \code{\link{model-method-laplace}}, \code{\link{model-method-optimize}}, +\code{\link{model-method-pathfinder}}, \code{\link{model-method-sample_mpi}}, \code{\link{model-method-sample}}, \code{\link{model-method-variables}}, diff --git a/man/model-method-laplace.Rd b/man/model-method-laplace.Rd index 21e0e1b68..64852f886 100644 --- a/man/model-method-laplace.Rd +++ b/man/model-method-laplace.Rd @@ -3,7 +3,7 @@ \name{model-method-laplace} \alias{model-method-laplace} \alias{laplace} -\title{Run Stan's laplace algorithm} +\title{Run Stan's Laplace algorithm} \usage{ laplace( data = NULL, @@ -62,10 +62,10 @@ containing initial values for all or some parameters. See \code{\link[=write_stan_json]{write_stan_json()}} to write \R objects to JSON files compatible with CmdStan. \item A list of lists containing initial values for all or some parameters. For -MCMC the list should contain a sublist for each chain. For optimization and -variational inference there should be just one sublist. The sublists should -have named elements corresponding to the parameters for which you are -specifying initial values. See \strong{Examples}. +MCMC the list should contain a sublist for each chain. For other model +fitting methods there should be just one sublist. The sublists should have +named elements corresponding to the parameters for which you are specifying +initial values. See \strong{Examples}. \item A function that returns a single list with names corresponding to the parameters for which you are specifying initial values. The function can take no arguments or a single argument \code{chain_id}. For MCMC, if the function @@ -151,13 +151,11 @@ deviation of the posterior distribution. If the mode is a maximum likelihood estimate (MLE), the sample provides an estimate of the standard error of the likelihood. Whether the mode is the MAP or MLE depends on the value of the \code{jacobian} argument when running optimization. See the -\href{https://mc-stan.org/docs/cmdstan-guide/laplace-sampling.html}{Laplace Sampling} -section of the CmdStan User's Guide for more details. +\href{https://mc-stan.org/docs/cmdstan-guide/}{CmdStan User’s Guide} +for more details. Any argument left as \code{NULL} will default to the default value used by the -installed version of CmdStan. See the -\href{https://mc-stan.org/docs/cmdstan-guide/}{CmdStan User’s Guide} -for more details on the default arguments. +installed version of CmdStan. } \examples{ \dontrun{ @@ -199,6 +197,7 @@ Other CmdStanModel methods: \code{\link{model-method-format}}, \code{\link{model-method-generate-quantities}}, \code{\link{model-method-optimize}}, +\code{\link{model-method-pathfinder}}, \code{\link{model-method-sample_mpi}}, \code{\link{model-method-sample}}, \code{\link{model-method-variables}}, diff --git a/man/model-method-optimize.Rd b/man/model-method-optimize.Rd index 06535f141..88d402792 100644 --- a/man/model-method-optimize.Rd +++ b/man/model-method-optimize.Rd @@ -68,10 +68,10 @@ containing initial values for all or some parameters. See \code{\link[=write_stan_json]{write_stan_json()}} to write \R objects to JSON files compatible with CmdStan. \item A list of lists containing initial values for all or some parameters. For -MCMC the list should contain a sublist for each chain. For optimization and -variational inference there should be just one sublist. The sublists should -have named elements corresponding to the parameters for which you are -specifying initial values. See \strong{Examples}. +MCMC the list should contain a sublist for each chain. For other model +fitting methods there should be just one sublist. The sublists should have +named elements corresponding to the parameters for which you are specifying +initial values. See \strong{Examples}. \item A function that returns a single list with names corresponding to the parameters for which you are specifying initial values. The function can take no arguments or a single argument \code{chain_id}. For MCMC, if the function @@ -166,8 +166,8 @@ A \code{\link{CmdStanMLE}} object. The \verb{$optimize()} method of a \code{\link{CmdStanModel}} object runs Stan's optimizer to obtain a (penalized) maximum likelihood estimate or a maximum a posteriori estimate (if \code{jacobian=TRUE}). See the -\href{https://mc-stan.org/docs/cmdstan-guide/maximum-likelihood-estimation.html}{Maximum Likelihood Estimation} -section of the CmdStan User's Guide for more details. +\href{https://mc-stan.org/docs/cmdstan-guide/index.html}{CmdStan User's Guide} +for more details. Any argument left as \code{NULL} will default to the default value used by the installed version of CmdStan. See the \href{https://mc-stan.org/docs/cmdstan-guide/}{CmdStan User’s Guide} for more details on the @@ -233,19 +233,26 @@ my_data_file <- file.path(cmdstan_path(), "examples/bernoulli/bernoulli.data.jso fit_optim <- mod$optimize(data = my_data_file, seed = 123) fit_optim$summary() -# Run 'optimize' again with 'jacobian=TRUE' and then draw from laplace approximation +# Run 'optimize' again with 'jacobian=TRUE' and then draw from Laplace approximation # to the posterior fit_optim <- mod$optimize(data = my_data_file, jacobian = TRUE) fit_laplace <- mod$laplace(data = my_data_file, mode = fit_optim, draws = 2000) fit_laplace$summary() -# Run 'variational' method to approximate the posterior (default is meanfield ADVI) +# Run 'variational' method to use ADVI to approximate posterior fit_vb <- mod$variational(data = stan_data, seed = 123) fit_vb$summary() - -# Plot approximate posterior using bayesplot mcmc_hist(fit_vb$draws("theta")) +# Run 'pathfinder' method, a new alternative to the variational method +fit_pf <- mod$pathfinder(data = stan_data, seed = 123) +fit_pf$summary() +mcmc_hist(fit_pf$draws("theta")) + +# Run 'pathfinder' again with more paths, fewer draws per path, +# better covariance approximation, and fewer LBFGSs iterations +fit_pf <- mod$pathfinder(data = stan_data, num_paths=10, single_path_draws=40, + history_size=50, max_lbfgs_iters=100) # Specifying initial values as a function fit_mcmc_w_init_fun <- mod$sample( @@ -308,6 +315,7 @@ Other CmdStanModel methods: \code{\link{model-method-format}}, \code{\link{model-method-generate-quantities}}, \code{\link{model-method-laplace}}, +\code{\link{model-method-pathfinder}}, \code{\link{model-method-sample_mpi}}, \code{\link{model-method-sample}}, \code{\link{model-method-variables}}, diff --git a/man/model-method-pathfinder.Rd b/man/model-method-pathfinder.Rd new file mode 100644 index 000000000..1642ec593 --- /dev/null +++ b/man/model-method-pathfinder.Rd @@ -0,0 +1,336 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/model.R +\name{model-method-pathfinder} +\alias{model-method-pathfinder} +\alias{pathfinder} +\title{Run Stan's Pathfinder Variational Inference Algorithm} +\usage{ +pathfinder( + data = NULL, + seed = NULL, + refresh = NULL, + init = NULL, + save_latent_dynamics = FALSE, + output_dir = NULL, + output_basename = NULL, + sig_figs = NULL, + opencl_ids = NULL, + num_threads = NULL, + init_alpha = NULL, + tol_obj = NULL, + tol_rel_obj = NULL, + tol_grad = NULL, + tol_rel_grad = NULL, + tol_param = NULL, + history_size = NULL, + single_path_draws = NULL, + draws = NULL, + num_paths = NULL, + max_lbfgs_iters = NULL, + num_elbo_draws = NULL, + save_single_paths = NULL +) +} +\arguments{ +\item{data}{(multiple options) The data to use for the variables specified in +the data block of the Stan program. One of the following: +\itemize{ +\item A named list of \R objects with the names corresponding to variables +declared in the data block of the Stan program. Internally this list is then +written to JSON for CmdStan using \code{\link[=write_stan_json]{write_stan_json()}}. See +\code{\link[=write_stan_json]{write_stan_json()}} for details on the conversions performed on \R objects +before they are passed to Stan. +\item A path to a data file compatible with CmdStan (JSON or \R dump). See the +appendices in the CmdStan guide for details on using these formats. +\item \code{NULL} or an empty list if the Stan program has no data block. +}} + +\item{seed}{(positive integer(s)) A seed for the (P)RNG to pass to CmdStan. +In the case of multi-chain sampling the single \code{seed} will automatically be +augmented by the the run (chain) ID so that each chain uses a different +seed. The exception is the transformed data block, which defaults to +using same seed for all chains so that the same data is generated for all +chains if RNG functions are used. The only time \code{seed} should be specified +as a vector (one element per chain) is if RNG functions are used in +transformed data and the goal is to generate \emph{different} data for each +chain.} + +\item{refresh}{(non-negative integer) The number of iterations between +printed screen updates. If \code{refresh = 0}, only error messages will be +printed.} + +\item{init}{(multiple options) The initialization method to use for the +variables declared in the parameters block of the Stan program. One of +the following: +\itemize{ +\item A real number \code{x>0}. This initializes \emph{all} parameters randomly between +\verb{[-x,x]} on the \emph{unconstrained} parameter space.; +\item The number \code{0}. This initializes \emph{all} parameters to \code{0}; +\item A character vector of paths (one per chain) to JSON or Rdump files +containing initial values for all or some parameters. See +\code{\link[=write_stan_json]{write_stan_json()}} to write \R objects to JSON files compatible with +CmdStan. +\item A list of lists containing initial values for all or some parameters. For +MCMC the list should contain a sublist for each chain. For other model +fitting methods there should be just one sublist. The sublists should have +named elements corresponding to the parameters for which you are specifying +initial values. See \strong{Examples}. +\item A function that returns a single list with names corresponding to the +parameters for which you are specifying initial values. The function can +take no arguments or a single argument \code{chain_id}. For MCMC, if the function +has argument \code{chain_id} it will be supplied with the chain id (from 1 to +number of chains) when called to generate the initial values. See +\strong{Examples}. +}} + +\item{save_latent_dynamics}{(logical) Should auxiliary diagnostic information +about the latent dynamics be written to temporary diagnostic CSV files? +This argument replaces CmdStan's \code{diagnostic_file} argument and the content +written to CSV is controlled by the user's CmdStan installation and not +CmdStanR (for some algorithms no content may be written). The default +is \code{FALSE}, which is appropriate for almost every use case. To save the +temporary files created when \code{save_latent_dynamics=TRUE} see the +\code{\link[=fit-method-save_latent_dynamics_files]{$save_latent_dynamics_files()}} +method.} + +\item{output_dir}{(string) A path to a directory where CmdStan should write +its output CSV files. For interactive use this can typically be left at +\code{NULL} (temporary directory) since CmdStanR makes the CmdStan output +(posterior draws and diagnostics) available in \R via methods of the fitted +model objects. The behavior of \code{output_dir} is as follows: +\itemize{ +\item If \code{NULL} (the default), then the CSV files are written to a temporary +directory and only saved permanently if the user calls one of the \verb{$save_*} +methods of the fitted model object (e.g., +\code{\link[=fit-method-save_output_files]{$save_output_files()}}). These temporary +files are removed when the fitted model object is +\link[base:gc]{garbage collected} (manually or automatically). +\item If a path, then the files are created in \code{output_dir} with names +corresponding to the defaults used by \verb{$save_output_files()}. +}} + +\item{output_basename}{(string) A string to use as a prefix for the names of +the output CSV files of CmdStan. If \code{NULL} (the default), the basename of +the output CSV files will be comprised from the model name, timestamp, and +5 random characters.} + +\item{sig_figs}{(positive integer) The number of significant figures used +when storing the output values. By default, CmdStan represent the output +values with 6 significant figures. The upper limit for \code{sig_figs} is 18. +Increasing this value will result in larger output CSV files and thus an +increased usage of disk space.} + +\item{opencl_ids}{(integer vector of length 2) The platform and +device IDs of the OpenCL device to use for fitting. The model must +be compiled with \code{cpp_options = list(stan_opencl = TRUE)} for this +argument to have an effect.} + +\item{num_threads}{(positive integer) If the model was +\link[=model-method-compile]{compiled} with threading support, the number of +threads to use in parallelized sections (e.g., for multi-path pathfinder +as well as \code{reduce_sum}).} + +\item{init_alpha}{(positive real) The initial step size parameter.} + +\item{tol_obj}{(positive real) Convergence tolerance on changes in objective function value.} + +\item{tol_rel_obj}{(positive real) Convergence tolerance on relative changes in objective function value.} + +\item{tol_grad}{(positive real) Convergence tolerance on the norm of the gradient.} + +\item{tol_rel_grad}{(positive real) Convergence tolerance on the relative norm of the gradient.} + +\item{tol_param}{(positive real) Convergence tolerance on changes in parameter value.} + +\item{history_size}{(positive integer) The size of the history used when +approximating the Hessian.} + +\item{single_path_draws}{(positive integer) Number of draws a single +pathfinder should return. The number of draws PSIS sampling samples from +will be equal to \code{single_path_draws * num_paths}.} + +\item{draws}{(positive integer) Number of draws to return after performing +pareto smooted importance sampling (PSIS). This should be smaller than +\code{single_path_draws * num_paths} (future versions of CmdStan will throw a +warning).} + +\item{num_paths}{(positive integer) Number of single pathfinders to run.} + +\item{max_lbfgs_iters}{(positive integer) The maximum number of iterations +for LBFGS.} + +\item{num_elbo_draws}{(positive integer) Number of draws to make when +calculating the ELBO of the approximation at each iteration of LBFGS.} + +\item{save_single_paths}{(logical) Whether to save the results of single +pathfinder runs in multi-pathfinder.} +} +\value{ +A \code{\link{CmdStanPathfinder}} object. +} +\description{ +The \verb{$pathfinder()} method of a \code{\link{CmdStanModel}} object runs +Stan's Pathfinder algorithms. Pathfinder is a variational method for +approximately sampling from differentiable log densities. Starting from a +random initialization, Pathfinder locates normal approximations +to the target density along a quasi-Newton optimization path in +the unconstrained space, with local covariance estimated using +the negative inverse Hessian estimates produced by the LBFGS +optimizer. Pathfinder selects the normal approximation with the +lowest estimated Kullback-Leibler (KL) divergence to the true +posterior. Finally Pathfinder draws from that normal +approximation and returns the draws transformed to the +constrained scale. See the +\href{https://mc-stan.org/docs/cmdstan-guide/}{CmdStan User’s Guide} +for more details. + +Any argument left as \code{NULL} will default to the default value used by the +installed version of CmdStan +} +\examples{ +\dontrun{ +library(cmdstanr) +library(posterior) +library(bayesplot) +color_scheme_set("brightblue") + +# Set path to CmdStan +# (Note: if you installed CmdStan via install_cmdstan() with default settings +# then setting the path is unnecessary but the default below should still work. +# Otherwise use the `path` argument to specify the location of your +# CmdStan installation.) +set_cmdstan_path(path = NULL) + +# Create a CmdStanModel object from a Stan program, +# here using the example model that comes with CmdStan +file <- file.path(cmdstan_path(), "examples/bernoulli/bernoulli.stan") +mod <- cmdstan_model(file) +mod$print() + +# Data as a named list (like RStan) +stan_data <- list(N = 10, y = c(0,1,0,0,0,0,0,0,0,1)) + +# Run MCMC using the 'sample' method +fit_mcmc <- mod$sample( + data = stan_data, + seed = 123, + chains = 2, + parallel_chains = 2 +) + +# Use 'posterior' package for summaries +fit_mcmc$summary() + +# Check sampling diagnostics +fit_mcmc$diagnostic_summary() + +# Get posterior draws +draws <- fit_mcmc$draws() +print(draws) + +# Convert to data frame using posterior::as_draws_df +as_draws_df(draws) + +# Plot posterior using bayesplot (ggplot2) +mcmc_hist(fit_mcmc$draws("theta")) + +# For models fit using MCMC, if you like working with RStan's stanfit objects +# then you can create one with rstan::read_stan_csv() +# stanfit <- rstan::read_stan_csv(fit_mcmc$output_files()) + + +# Run 'optimize' method to get a point estimate (default is Stan's LBFGS algorithm) +# and also demonstrate specifying data as a path to a file instead of a list +my_data_file <- file.path(cmdstan_path(), "examples/bernoulli/bernoulli.data.json") +fit_optim <- mod$optimize(data = my_data_file, seed = 123) +fit_optim$summary() + +# Run 'optimize' again with 'jacobian=TRUE' and then draw from Laplace approximation +# to the posterior +fit_optim <- mod$optimize(data = my_data_file, jacobian = TRUE) +fit_laplace <- mod$laplace(data = my_data_file, mode = fit_optim, draws = 2000) +fit_laplace$summary() + +# Run 'variational' method to use ADVI to approximate posterior +fit_vb <- mod$variational(data = stan_data, seed = 123) +fit_vb$summary() +mcmc_hist(fit_vb$draws("theta")) + +# Run 'pathfinder' method, a new alternative to the variational method +fit_pf <- mod$pathfinder(data = stan_data, seed = 123) +fit_pf$summary() +mcmc_hist(fit_pf$draws("theta")) + +# Run 'pathfinder' again with more paths, fewer draws per path, +# better covariance approximation, and fewer LBFGSs iterations +fit_pf <- mod$pathfinder(data = stan_data, num_paths=10, single_path_draws=40, + history_size=50, max_lbfgs_iters=100) + +# Specifying initial values as a function +fit_mcmc_w_init_fun <- mod$sample( + data = stan_data, + seed = 123, + chains = 2, + refresh = 0, + init = function() list(theta = runif(1)) +) +fit_mcmc_w_init_fun_2 <- mod$sample( + data = stan_data, + seed = 123, + chains = 2, + refresh = 0, + init = function(chain_id) { + # silly but demonstrates optional use of chain_id + list(theta = 1 / (chain_id + 1)) + } +) +fit_mcmc_w_init_fun_2$init() + +# Specifying initial values as a list of lists +fit_mcmc_w_init_list <- mod$sample( + data = stan_data, + seed = 123, + chains = 2, + refresh = 0, + init = list( + list(theta = 0.75), # chain 1 + list(theta = 0.25) # chain 2 + ) +) +fit_optim_w_init_list <- mod$optimize( + data = stan_data, + seed = 123, + init = list( + list(theta = 0.75) + ) +) +fit_optim_w_init_list$init() +} + +} +\seealso{ +The CmdStanR website +(\href{https://mc-stan.org/cmdstanr/}{mc-stan.org/cmdstanr}) for online +documentation and tutorials. + +The Stan and CmdStan documentation: +\itemize{ +\item Stan documentation: \href{https://mc-stan.org/users/documentation/}{mc-stan.org/users/documentation} +\item CmdStan User’s Guide: \href{https://mc-stan.org/docs/cmdstan-guide/}{mc-stan.org/docs/cmdstan-guide} +} + +Other CmdStanModel methods: +\code{\link{model-method-check_syntax}}, +\code{\link{model-method-compile}}, +\code{\link{model-method-diagnose}}, +\code{\link{model-method-expose_functions}}, +\code{\link{model-method-format}}, +\code{\link{model-method-generate-quantities}}, +\code{\link{model-method-laplace}}, +\code{\link{model-method-optimize}}, +\code{\link{model-method-sample_mpi}}, +\code{\link{model-method-sample}}, +\code{\link{model-method-variables}}, +\code{\link{model-method-variational}} +} +\concept{CmdStanModel methods} diff --git a/man/model-method-sample.Rd b/man/model-method-sample.Rd index 1c415f6a6..c3f2c1e60 100644 --- a/man/model-method-sample.Rd +++ b/man/model-method-sample.Rd @@ -88,10 +88,10 @@ containing initial values for all or some parameters. See \code{\link[=write_stan_json]{write_stan_json()}} to write \R objects to JSON files compatible with CmdStan. \item A list of lists containing initial values for all or some parameters. For -MCMC the list should contain a sublist for each chain. For optimization and -variational inference there should be just one sublist. The sublists should -have named elements corresponding to the parameters for which you are -specifying initial values. See \strong{Examples}. +MCMC the list should contain a sublist for each chain. For other model +fitting methods there should be just one sublist. The sublists should have +named elements corresponding to the parameters for which you are specifying +initial values. See \strong{Examples}. \item A function that returns a single list with names corresponding to the parameters for which you are specifying initial values. The function can take no arguments or a single argument \code{chain_id}. For MCMC, if the function @@ -342,19 +342,26 @@ my_data_file <- file.path(cmdstan_path(), "examples/bernoulli/bernoulli.data.jso fit_optim <- mod$optimize(data = my_data_file, seed = 123) fit_optim$summary() -# Run 'optimize' again with 'jacobian=TRUE' and then draw from laplace approximation +# Run 'optimize' again with 'jacobian=TRUE' and then draw from Laplace approximation # to the posterior fit_optim <- mod$optimize(data = my_data_file, jacobian = TRUE) fit_laplace <- mod$laplace(data = my_data_file, mode = fit_optim, draws = 2000) fit_laplace$summary() -# Run 'variational' method to approximate the posterior (default is meanfield ADVI) +# Run 'variational' method to use ADVI to approximate posterior fit_vb <- mod$variational(data = stan_data, seed = 123) fit_vb$summary() - -# Plot approximate posterior using bayesplot mcmc_hist(fit_vb$draws("theta")) +# Run 'pathfinder' method, a new alternative to the variational method +fit_pf <- mod$pathfinder(data = stan_data, seed = 123) +fit_pf$summary() +mcmc_hist(fit_pf$draws("theta")) + +# Run 'pathfinder' again with more paths, fewer draws per path, +# better covariance approximation, and fewer LBFGSs iterations +fit_pf <- mod$pathfinder(data = stan_data, num_paths=10, single_path_draws=40, + history_size=50, max_lbfgs_iters=100) # Specifying initial values as a function fit_mcmc_w_init_fun <- mod$sample( @@ -418,6 +425,7 @@ Other CmdStanModel methods: \code{\link{model-method-generate-quantities}}, \code{\link{model-method-laplace}}, \code{\link{model-method-optimize}}, +\code{\link{model-method-pathfinder}}, \code{\link{model-method-sample_mpi}}, \code{\link{model-method-variables}}, \code{\link{model-method-variational}} diff --git a/man/model-method-sample_mpi.Rd b/man/model-method-sample_mpi.Rd index 8c17a4dee..8d961e62a 100644 --- a/man/model-method-sample_mpi.Rd +++ b/man/model-method-sample_mpi.Rd @@ -87,10 +87,10 @@ containing initial values for all or some parameters. See \code{\link[=write_stan_json]{write_stan_json()}} to write \R objects to JSON files compatible with CmdStan. \item A list of lists containing initial values for all or some parameters. For -MCMC the list should contain a sublist for each chain. For optimization and -variational inference there should be just one sublist. The sublists should -have named elements corresponding to the parameters for which you are -specifying initial values. See \strong{Examples}. +MCMC the list should contain a sublist for each chain. For other model +fitting methods there should be just one sublist. The sublists should have +named elements corresponding to the parameters for which you are specifying +initial values. See \strong{Examples}. \item A function that returns a single list with names corresponding to the parameters for which you are specifying initial values. The function can take no arguments or a single argument \code{chain_id}. For MCMC, if the function @@ -253,7 +253,7 @@ A \code{\link{CmdStanMCMC}} object. \description{ The \verb{$sample_mpi()} method of a \code{\link{CmdStanModel}} object is identical to the \verb{$sample()} method but with support for -\href{https://mc-stan.org/math/mpi.html}{MPI}. The target audience for MPI are +MPI (message passing interface). The target audience for MPI are those with large computer clusters. For other users, the \code{\link[=model-method-sample]{$sample()}} method provides both parallelization of chains and threading support for within-chain parallelization. @@ -303,8 +303,8 @@ The Stan and CmdStan documentation: \item CmdStan User’s Guide: \href{https://mc-stan.org/docs/cmdstan-guide/}{mc-stan.org/docs/cmdstan-guide} } -The Stan Math Library's MPI documentation -(\href{https://mc-stan.org/math/mpi.html}{mc-stan.org/math/mpi}) for more +The Stan Math Library's documentation +(\href{https://mc-stan.org/math/}{mc-stan.org/math}) for more details on MPI support in Stan. Other CmdStanModel methods: @@ -316,6 +316,7 @@ Other CmdStanModel methods: \code{\link{model-method-generate-quantities}}, \code{\link{model-method-laplace}}, \code{\link{model-method-optimize}}, +\code{\link{model-method-pathfinder}}, \code{\link{model-method-sample}}, \code{\link{model-method-variables}}, \code{\link{model-method-variational}} diff --git a/man/model-method-variables.Rd b/man/model-method-variables.Rd index b5b85f1c2..dc80ed9a6 100644 --- a/man/model-method-variables.Rd +++ b/man/model-method-variables.Rd @@ -45,6 +45,7 @@ Other CmdStanModel methods: \code{\link{model-method-generate-quantities}}, \code{\link{model-method-laplace}}, \code{\link{model-method-optimize}}, +\code{\link{model-method-pathfinder}}, \code{\link{model-method-sample_mpi}}, \code{\link{model-method-sample}}, \code{\link{model-method-variational}} diff --git a/man/model-method-variational.Rd b/man/model-method-variational.Rd index b04f8b7c1..e7d9fa1e8 100644 --- a/man/model-method-variational.Rd +++ b/man/model-method-variational.Rd @@ -69,10 +69,10 @@ containing initial values for all or some parameters. See \code{\link[=write_stan_json]{write_stan_json()}} to write \R objects to JSON files compatible with CmdStan. \item A list of lists containing initial values for all or some parameters. For -MCMC the list should contain a sublist for each chain. For optimization and -variational inference there should be just one sublist. The sublists should -have named elements corresponding to the parameters for which you are -specifying initial values. See \strong{Examples}. +MCMC the list should contain a sublist for each chain. For other model +fitting methods there should be just one sublist. The sublists should have +named elements corresponding to the parameters for which you are specifying +initial values. See \strong{Examples}. \item A function that returns a single list with names corresponding to the parameters for which you are specifying initial values. The function can take no arguments or a single argument \code{chain_id}. For MCMC, if the function @@ -163,22 +163,17 @@ A \code{\link{CmdStanVB}} object. } \description{ The \verb{$variational()} method of a \code{\link{CmdStanModel}} object runs -Stan's variational Bayes (ADVI) algorithms. - -Any argument left as \code{NULL} will default to the default value used by the -installed version of CmdStan. See the +Stan's Automatic Differentiation Variational Inference (ADVI) algorithms. +The approximation is a Gaussian in the unconstrained variable space. Stan +implements two ADVI algorithms: the \code{algorithm="meanfield"} option uses a +fully factorized Gaussian for the approximation; the \code{algorithm="fullrank"} +option uses a Gaussian with a full-rank covariance matrix for the +approximation. See the \href{https://mc-stan.org/docs/cmdstan-guide/}{CmdStan User’s Guide} for more details. -} -\details{ -CmdStan can fit a variational approximation to the posterior. The -approximation is a Gaussian in the unconstrained variable space. Stan -implements two variational algorithms. The \code{algorithm="meanfield"} option -uses a fully factorized Gaussian for the approximation. The -\code{algorithm="fullrank"} option uses a Gaussian with a full-rank covariance -matrix for the approximation. - --- \href{https://github.com/stan-dev/cmdstan/releases/latest}{\emph{CmdStan Interface User's Guide}} + +Any argument left as \code{NULL} will default to the default value used by the +installed version of CmdStan. } \examples{ \dontrun{ @@ -238,19 +233,26 @@ my_data_file <- file.path(cmdstan_path(), "examples/bernoulli/bernoulli.data.jso fit_optim <- mod$optimize(data = my_data_file, seed = 123) fit_optim$summary() -# Run 'optimize' again with 'jacobian=TRUE' and then draw from laplace approximation +# Run 'optimize' again with 'jacobian=TRUE' and then draw from Laplace approximation # to the posterior fit_optim <- mod$optimize(data = my_data_file, jacobian = TRUE) fit_laplace <- mod$laplace(data = my_data_file, mode = fit_optim, draws = 2000) fit_laplace$summary() -# Run 'variational' method to approximate the posterior (default is meanfield ADVI) +# Run 'variational' method to use ADVI to approximate posterior fit_vb <- mod$variational(data = stan_data, seed = 123) fit_vb$summary() - -# Plot approximate posterior using bayesplot mcmc_hist(fit_vb$draws("theta")) +# Run 'pathfinder' method, a new alternative to the variational method +fit_pf <- mod$pathfinder(data = stan_data, seed = 123) +fit_pf$summary() +mcmc_hist(fit_pf$draws("theta")) + +# Run 'pathfinder' again with more paths, fewer draws per path, +# better covariance approximation, and fewer LBFGSs iterations +fit_pf <- mod$pathfinder(data = stan_data, num_paths=10, single_path_draws=40, + history_size=50, max_lbfgs_iters=100) # Specifying initial values as a function fit_mcmc_w_init_fun <- mod$sample( @@ -314,6 +316,7 @@ Other CmdStanModel methods: \code{\link{model-method-generate-quantities}}, \code{\link{model-method-laplace}}, \code{\link{model-method-optimize}}, +\code{\link{model-method-pathfinder}}, \code{\link{model-method-sample_mpi}}, \code{\link{model-method-sample}}, \code{\link{model-method-variables}} diff --git a/tests/testthat/test-install.R b/tests/testthat/test-install.R index 9f53f86f1..488e47317 100644 --- a/tests/testthat/test-install.R +++ b/tests/testthat/test-install.R @@ -247,3 +247,28 @@ test_that("Download failures return error message", { "GitHub download of release list failed with error: cannot open URL 'https://api.github.com/repos/stan-dev/cmdstan/releases/latest'") }) +test_that("Install from release file works", { + if (getRversion() < '3.5.0') { + dir <- tempdir() + } else { + dir <- tempdir(check = TRUE) + } + + destfile = file.path(dir, "cmdstan-2.33.1.tar.gz") + + download_with_retries( + "https://github.com/stan-dev/cmdstan/releases/download/v2.33.1/cmdstan-2.33.1.tar.gz", + destfile) + + expect_message( + expect_output( + install_cmdstan(dir = dir, cores = 2, quiet = FALSE, overwrite = TRUE, + release_file = destfile, + wsl = os_is_wsl()), + "Compiling, linking C++ code", + fixed = TRUE + ), + "CmdStan path set", + fixed = TRUE + ) +}) diff --git a/tests/testthat/test-model-methods.R b/tests/testthat/test-model-methods.R index 1289cb063..c3607f7b7 100644 --- a/tests/testthat/test-model-methods.R +++ b/tests/testthat/test-model-methods.R @@ -3,7 +3,7 @@ context("model-methods") set_cmdstan_path() mod <- cmdstan_model(testing_stan_file("bernoulli_log_lik"), force_recompile = TRUE) data_list <- testing_data("bernoulli") -fit <- mod$sample(data = data_list, chains = 1) +fit <- mod$sample(data = data_list, chains = 1, refresh = 0) test_that("Methods error if not compiled", { skip_if(os_is_wsl()) @@ -65,7 +65,7 @@ test_that("Methods return correct values", { hessian = as.matrix(-2.4937604019289194568, nrow=1, ncol=1) ) - expect_equal(fit$hessian(unconstrained_variables=c(0.1), jacobian_adjustment = FALSE), + expect_equal(fit$hessian(unconstrained_variables=c(0.1), jacobian = FALSE), hessian_noadj) cpars <- fit$constrain_variables(c(0.1)) diff --git a/tests/testthat/test-model-pathfinder.R b/tests/testthat/test-model-pathfinder.R new file mode 100644 index 000000000..2f23cc9bd --- /dev/null +++ b/tests/testthat/test-model-pathfinder.R @@ -0,0 +1,144 @@ +context("model-pathfinder") + +set_cmdstan_path() +stan_program <- testing_stan_file("bernoulli") +mod <- testing_model("bernoulli") +stan_program_fp <- testing_stan_file("bernoulli_fp") +mod_fp <- testing_model("bernoulli_fp") + +# valid ways to supply data +data_list <- testing_data("bernoulli") +data_file_r <- test_path("resources", "data", "bernoulli.data.R") +data_file_json <- test_path("resources", "data", "bernoulli.data.json") + +# these are all valid for sample() +ok_arg_values <- list( + data = data_list, + output_dir = tempdir(), + refresh = 5, + init = 1.5, + seed = 12345, + init_alpha = 1, + tol_obj = 1e-12, + tol_rel_obj = 1e-12, + tol_grad = 1e-12, + tol_rel_grad = 1e-12, + tol_param = 1e-12, + history_size = 5, + num_elbo_draws = 10, + single_path_draws = 10, + draws = 100, + num_paths = 4, + max_lbfgs_iters = 100, + save_single_paths = FALSE) + +# using any one of these should cause sample() to error +bad_arg_values <- list( + data = "NOT_A_FILE", + output_dir = "NOT_A_DIRECTORY", + refresh = -1, + init = "maybe :P", + seed = -80, + init_alpha = "cat.jpeg", + init_alpha = -3, + tol_obj = -1, + tol_rel_obj = -4, + tol_grad = -5, + tol_rel_grad = -9, + tol_param = -2, + history_size = -6, + num_elbo_draws = -8, + draws = "no thanks", + single_path_draws = "Just one plz", + num_paths = -1, + max_lbfgs_iters = "idk :/" +) + +bad_arg_values_2 <- list( + data = "NOT_A_FILE", + output_dir = "NOT_A_DIRECTORY", + refresh = -1, + init = "maybe :P", + seed = -80, + init_alpha = -3, + tol_obj = -1, + tol_rel_obj = -4, + tol_grad = -5, + tol_rel_grad = -9, + tol_param = -2, + history_size = -6, + num_elbo_draws = -8, + draws = "no thanks", + single_path_draws = "nope", + num_paths = -1, + max_lbfgs_iters = "idk :/", + save_single_paths = "Mby" +) + +bad_arg_values_3 <- list( + data = matrix(1:10), + output_dir = 8, + init = "maybe :P", + seed = -80, + init_alpha = -3, + tol_obj = -1, + tol_rel_obj = -4, + tol_grad = -5, + tol_rel_grad = -9, + tol_param = -2, + history_size = -6, + num_elbo_draws = -8, + draws = "no thanks", + single_path_draws = " ", + num_paths = "NO!", + max_lbfgs_iters = "idk :/" +) +expect_pathfinder_output <- function(object, num_chains = NULL) { + expect_output(object, regexp = "Finished in (.*) seconds.") +} + + +test_that("Pathfinder Runs", { + expect_pathfinder_output(fit <- mod$pathfinder(data=data_list, seed=1234, refresh = 0)) + expect_is(fit, "CmdStanPathfinder") +}) + +test_that("pathfinder() method works with data files", { + expect_pathfinder_output(fit_r <- mod$pathfinder(data = data_file_r)) + expect_is(fit_r, "CmdStanPathfinder") + + expect_pathfinder_output(fit_json <- mod$pathfinder(data = data_file_json)) + expect_is(fit_json, "CmdStanPathfinder") +}) + +test_that("pathfinder() method works with init file", { + init_list <- list(theta = 0.5) + init_file <- tempfile( + tmpdir = cmdstanr:::cmdstan_tempdir(), + pattern = "testing-inits-", + fileext = ".json" + ) + write_stan_json(init_list, file = init_file) + expect_pathfinder_output(mod$pathfinder(data = data_file_r, init = init_file)) +}) + +test_that("pathfinder() method runs when all arguments specified", { + expect_pathfinder_output(fit <- do.call(mod$pathfinder, ok_arg_values)) + expect_is(fit, "CmdStanPathfinder") +}) + +test_that("pathfinder() method runs when the stan file is removed", { + stan_file_tmp <- tempfile(pattern = "tmp", fileext = ".stan") + file.copy(stan_program, stan_file_tmp) + mod_tmp <- cmdstan_model(stan_file_tmp) + file.remove(stan_file_tmp) + expect_pathfinder_output( + mod_tmp$pathfinder(data = data_list) + ) +}) + +test_that("no error when checking estimates after failure", { + fit <- cmdstanr_example("schools", method = "pathfinder", seed = 123) # optim always fails for this + expect_silent(fit$summary()) # no error +}) + diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 147b1e143..180f7dedd 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -248,3 +248,13 @@ test_that("as_mcmc.list() works", { "Currently only CmdStanMCMC objects can be converted to mcmc.list" ) }) + +test_that("get_cmdstan_flags() can be used recursively in `make`", { + mkfile <- normalizePath(test_path("testdata", "Makefile")) + nonrecursive_flags <- get_cmdstan_flags("STANCFLAGS") + stdo <- processx::run( + command = "make", args = sprintf("--file=%s", mkfile) + )$stdout + recursive_flags <- readLines(textConnection(stdo)) + expect_equal(nonrecursive_flags, recursive_flags) +}) diff --git a/tests/testthat/testdata/Makefile b/tests/testthat/testdata/Makefile new file mode 100644 index 000000000..4a58d6544 --- /dev/null +++ b/tests/testthat/testdata/Makefile @@ -0,0 +1,3 @@ + +test: + @$(R_HOME)/bin/Rscript --vanilla -e 'cat(cmdstanr:::get_cmdstan_flags("STANCFLAGS"))' diff --git a/vignettes/children/comparison-with-rstan.md b/vignettes/children/comparison-with-rstan.md index ed1628f1b..8f8e61f78 100644 --- a/vignettes/children/comparison-with-rstan.md +++ b/vignettes/children/comparison-with-rstan.md @@ -10,10 +10,14 @@ results to output files. ### Advantages of RStan * Allows other developers to distribute R packages with -_pre-compiled_ Stan programs (like **rstanarm**) on CRAN. +_pre-compiled_ Stan programs (like **rstanarm**) on CRAN. (Note: As of 2023, +this can mostly be achieved with CmdStanR as well. See +[Developing using CmdStanR](https://mc-stan.org/cmdstanr/articles/cmdstanr-internals.html#developing-using-cmdstanr).) * Avoids use of R6 classes, which may result in more familiar syntax for many R users. +* CRAN binaries available for Mac and Windows. + ### Advantages of CmdStanR * Compatible with latest versions of Stan. Keeping up with Stan releases is diff --git a/vignettes/cmdstanr-internals.Rmd b/vignettes/cmdstanr-internals.Rmd index cefed0b0f..96ef06ea0 100644 --- a/vignettes/cmdstanr-internals.Rmd +++ b/vignettes/cmdstanr-internals.Rmd @@ -114,7 +114,7 @@ value. bounds. For the latest information on the checks performed in pedantic mode see the -[Pedantic mode chapter](https://mc-stan.org/docs/reference-manual/pedantic-mode.html) +[Pedantic mode chapter](https://mc-stan.org/docs/stan-users-guide/pedantic-mode.html) in the Stan Reference Manual. Pedantic mode is available when compiling the model or when using the separate diff --git a/vignettes/cmdstanr.Rmd b/vignettes/cmdstanr.Rmd index 0addfedfb..b9b2e3414 100644 --- a/vignettes/cmdstanr.Rmd +++ b/vignettes/cmdstanr.Rmd @@ -1,6 +1,6 @@ --- title: "Getting started with CmdStanR" -author: "Jonah Gabry and Rok Češnovar" +author: "Jonah Gabry, Rok Češnovar, and Andrew Johnson" output: rmarkdown::html_vignette: toc: true @@ -341,8 +341,8 @@ stanfit <- rstan::read_stan_csv(fit$output_files()) CmdStanR also supports running Stan's optimization algorithms and its algorithms for variational approximation of full Bayesian inference. These are run via the -`$optimize()`, `$laplace()`, and `$variational()` methods, which are called in a -similar way to the `$sample()` method demonstrated above. +`$optimize()`, `$laplace()`, `$variational()`, and `$pathfinder()` methods, which +are called in a similar way to the `$sample()` method demonstrated above. ### Optimization @@ -380,7 +380,7 @@ fit_map <- mod$optimize( ### Laplace Approximation The [`$laplace()`](https://mc-stan.org/cmdstanr/reference/model-method-laplace.html) -produces a sample from a normal approximation centered at the mode of a +method produces a sample from a normal approximation centered at the mode of a distribution in the unconstrained space. If the mode is a MAP estimate, the samples provide an estimate of the mean and standard deviation of the posterior distribution. If the mode is the MLE, the sample provides an estimate of the @@ -401,15 +401,17 @@ fit_laplace <- mod$laplace( seed = 123, refresh = 1000 ) -fit_laplace$summary("theta") +fit_laplace$print("theta") mcmc_hist(fit_laplace$draws("theta"), binwidth = 0.025) ``` -### Variational Bayes +### Variational (ADVI) + +We can run Stan's experimental Automatic Differentiation Variational Inference +(ADVI) using the [`$variational()`](https://mc-stan.org/cmdstanr/reference/model-method-variational.html) +method. For details on the ADVI algorithm see the +[CmdStan User's Guide](https://mc-stan.org/docs/cmdstan-guide/variational-inference-algorithm-advi.html). -We can run Stan's experimental variational Bayes algorithm (ADVI) using the -[`$variational()`](https://mc-stan.org/cmdstanr/reference/model-method-variational.html) -method. ```{r variational} fit_vb <- mod$variational( @@ -418,12 +420,38 @@ fit_vb <- mod$variational( draws = 4000 ) fit_vb$print("theta") +mcmc_hist(fit_vb$draws("theta"), binwidth = 0.025) +``` + +### Variational (Pathfinder) + +Stan version 2.33 introduced a new variational method called Pathfinder, +which is intended to be faster and more stable than ADVI. For details on how +Pathfinder works see the section in the +[CmdStan User's Guide](https://mc-stan.org/docs/cmdstan-guide/pathfinder-intro.html#pathfinder-intro). +Pathfinder is run using the [`$pathfinder()`](https://mc-stan.org/cmdstanr/reference/model-method-pathfinder.html) +method. + +```{r pathfinder} +fit_pf <- mod$pathfinder( + data = data_list, + seed = 123, + draws = 4000 +) +fit_pf$print("theta") ``` -Let's extract the draws, make the same plot we made after MCMC and Laplace -approximation, and compare them all. In this simple example the distributions -are quite similar, but this will not always be the case. +Let's extract the draws, make the same plot we made after running the other +algorithms, and compare them all. approximation, and compare them all. In this +simple example the distributions are quite similar, but this will not always be +the case for more challenging problems. + +```{r plot-compare-pf, message = FALSE} +mcmc_hist(fit_pf$draws("theta"), binwidth = 0.025) + + ggplot2::labs(subtitle = "Approximate posterior from pathfinder") + + ggplot2::xlim(0, 1) +``` ```{r plot-compare-vb, message = FALSE} mcmc_hist(fit_vb$draws("theta"), binwidth = 0.025) + ggplot2::labs(subtitle = "Approximate posterior from variational") + @@ -440,12 +468,13 @@ mcmc_hist(fit$draws("theta"), binwidth = 0.025) + ggplot2::xlim(0, 1) ``` -For more details on the `$optimize()`, `$laplace()` and `$variational()` -methods, follow these links to their documentation pages. +For more details on the `$optimize()`, `$laplace()`, `$variational()`, and +`pathfinder()` methods, follow these links to their documentation pages. * [`$optimize()`](https://mc-stan.org/cmdstanr/reference/model-method-optimize.html) * [`$laplace()`](https://mc-stan.org/cmdstanr/reference/model-method-laplace.html) * [`$variational()`](https://mc-stan.org/cmdstanr/reference/model-method-variational.html) +* [`$pathfinder()`](https://mc-stan.org/cmdstanr/reference/model-method-pathfinder.html) ## Saving fitted model objects diff --git a/vignettes/posterior.Rmd b/vignettes/posterior.Rmd index 526178346..15da8e681 100644 --- a/vignettes/posterior.Rmd +++ b/vignettes/posterior.Rmd @@ -15,6 +15,14 @@ vignette: > ```{r child="children/_settings-knitr.Rmd"} ``` +```{r include=FALSE} +# Needed temporarily to avoiding weird rendering of posterior's tibbles +# in pkgdown sites +print.tbl_df <- function(x, ...) { + print.data.frame(x) +} +``` + ## Summary statistics We can easily customize the summary statistics reported by `$summary()` and `$print()`. @@ -45,7 +53,10 @@ fit$metadata()$model_params fit$summary(variables = NULL, "mean", "median") ``` -Summary functions can be specified by character string, function, or using a formula (or anything else supported by [rlang::as_function]). If these arguments are named, those names will be used in the tibble output. If the summary results are named they will take precedence. +Summary functions can be specified by character string, function, or using a +formula (or anything else supported by `rlang::as_function()`). If these +arguments are named, those names will be used in the tibble output. If the +summary results are named they will take precedence. ```{r} my_sd <- function(x) c(My_SD = sd(x)) fit$summary(