Skip to content

Commit 9b994f0

Browse files
authored
Merge bf0183a into bc60419
2 parents bc60419 + bf0183a commit 9b994f0

File tree

142 files changed

+4557
-1770
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

142 files changed

+4557
-1770
lines changed

.github/CONTRIBUTING.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Contributing to cmdstanr
2+
3+
This outlines how to propose a change to cmdstanr and is based on similar
4+
instructions for tidyverse packages, including the contributing guidelines
5+
generated by `usethis::use_tidy_contributing()`.
6+
7+
## Fixing typos
8+
9+
You can fix typos, spelling mistakes, or grammatical errors in the documentation
10+
directly using the GitHub web interface, as long as the changes are made in the
11+
_source_ file. This generally means you'll need to edit
12+
[roxygen2 comments](https://roxygen2.r-lib.org/articles/roxygen2.html) in an `.R`,
13+
not a `.Rd` file. You can find the `.R` file that generates the `.Rd` by reading
14+
the comment in the first line.
15+
16+
## Bigger changes
17+
18+
If you want to make a bigger change, it's a good idea to first file an issue and
19+
make sure someone from the team agrees that it’s needed. If you’ve found a bug,
20+
please file an issue that illustrates the bug with a minimal reproducible
21+
example (see e.g. the [tidyverse reprex instructions](https://www.tidyverse.org/help/#reprex)).
22+
The tidyverse guide on [how to create a great issue](https://code-review.tidyverse.org/issues/)
23+
has more advice.
24+
25+
### Pull request process
26+
27+
If you are new to creating pull requests here are some tips. Using the functions
28+
from the `usethis` package is not required but can be helpful if this process is
29+
new to you.
30+
31+
* 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)`.
32+
33+
* Install all development dependencies with `devtools::install_dev_deps()`, and then make sure the package passes R CMD check by running `devtools::check()`.
34+
If R CMD check doesn't pass cleanly, it's a good idea to ask for help before continuing.
35+
* Create a Git branch for your pull request (PR). We recommend using `usethis::pr_init("brief-description-of-change")`.
36+
37+
* Make your changes, commit to git, and then create a PR by running `usethis::pr_push()`, and following the prompts in your browser.
38+
The title of your PR should briefly describe the change.
39+
The body of your PR should contain `Fixes #issue-number`.
40+
41+
* 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`.
42+
43+
### Code style
44+
45+
* New code should attempt to follow the style used in the package. When in doubt follow the tidyverse [style guide](https://style.tidyverse.org).
46+
47+
* 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.
48+
49+
* We use [testthat](https://cran.r-project.org/package=testthat) for unit tests.
50+
Contributions with test cases included are easier to accept.
51+
52+
## Code of Conduct
53+
54+
Please note that the cmdstanr project follows the Stan project's
55+
[Code of Conduct](https://discourse.mc-stan.org/t/announcing-our-new-stan-code-of-conduct/23764).
56+
By contributing to this project you agree to abide by its terms.

DESCRIPTION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: cmdstanr
22
Title: R Interface to 'CmdStan'
3-
Version: 0.6.1.9000
4-
Date: 2023-08-25
3+
Version: 0.7.0
4+
Date: 2023-12-13
55
Authors@R:
66
c(person(given = "Jonah", family = "Gabry", role = c("aut", "cre"),
77
email = "[email protected]"),
@@ -45,6 +45,7 @@ Imports:
4545
rlang (>= 0.4.7)
4646
Suggests:
4747
bayesplot,
48+
ggplot2,
4849
knitr (>= 1.37),
4950
loo (>= 2.0.0),
5051
rmarkdown,

NEWS.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
1-
# cmdstanr 0.6.1.9000
1+
# cmdstanr 0.7.0
2+
3+
## Major new features
4+
5+
* New `laplace` method by @jgabry in #800
6+
* New `pathfinder` method by @SteveBronder in #848
7+
8+
## Other improvements and bug fixes
9+
10+
* Add missing link to diagnose method in CmdStanModel doc by @jgabry in #833
11+
* Improvements to compile tests by @martinmodrak in #836
12+
* Changed the delay behavior in wsl_installed by @martinmodrak in #839
13+
* Update array syntax in website vignette by @andrjohns in #841
14+
* Compatibility fixes for cmdstan 2.33+ by @jgabry in #843
15+
* Suggest format method after error due to old syntax by @jgabry in #852
16+
* Clarifications in R-markdown vignette by @jgcolman in #854
17+
* Update linux/wsl detection for install arch by @andrjohns in #856
18+
* Fix handling of single-length inits for containers by @andrjohns in #857
19+
* Add support/tests for exposing functions with tuples by @andrjohns in #860
20+
* Add support/tests for exporting functions with complex types by @andrjohns in #861
21+
* Add option for installing from release archive by @andrjohns in #866
222

323
# cmdstanr 0.6.1
424

R/fit.R

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,13 @@ CmdStanFit$set("public", name = "init_model_methods", value = init_model_methods
362362
#'
363363
#' @name fit-method-log_prob
364364
#' @aliases log_prob
365-
#' @description The `$log_prob()` method provides access to the Stan model's `log_prob` function
365+
#' @description The `$log_prob()` method provides access to the Stan model's
366+
#' `log_prob` function.
366367
#'
367-
#' @param unconstrained_variables (numeric) A vector of unconstrained parameters
368-
#' to be passed to `log_prob`.
369-
#' @param jacobian_adjustment (logical) Whether to include the log-density
370-
#' adjustments from un/constraining variables.
368+
#' @param unconstrained_variables (numeric) A vector of unconstrained parameters.
369+
#' @param jacobian (logical) Whether to include the log-density adjustments from
370+
#' un/constraining variables.
371+
#' @param jacobian_adjustment Deprecated. Please use `jacobian` instead.
371372
#'
372373
#' @examples
373374
#' \dontrun{
@@ -380,7 +381,11 @@ CmdStanFit$set("public", name = "init_model_methods", value = init_model_methods
380381
#' [unconstrain_variables()], [unconstrain_draws()], [variable_skeleton()],
381382
#' [hessian()]
382383
#'
383-
log_prob <- function(unconstrained_variables, jacobian_adjustment = TRUE) {
384+
log_prob <- function(unconstrained_variables, jacobian = TRUE, jacobian_adjustment = NULL) {
385+
if (!is.null(jacobian_adjustment)) {
386+
warning("'jacobian_adjustment' is deprecated. Please use 'jacobian' instead.", call. = FALSE)
387+
jacobian <- jacobian_adjustment
388+
}
384389
if (is.null(private$model_methods_env_$model_ptr)) {
385390
stop("The method has not been compiled, please call `init_model_methods()` first",
386391
call. = FALSE)
@@ -390,7 +395,7 @@ log_prob <- function(unconstrained_variables, jacobian_adjustment = TRUE) {
390395
length(unconstrained_variables), " were provided!", call. = FALSE)
391396
}
392397
private$model_methods_env_$log_prob(private$model_methods_env_$model_ptr_,
393-
unconstrained_variables, jacobian_adjustment)
398+
unconstrained_variables, jacobian)
394399
}
395400
CmdStanFit$set("public", name = "log_prob", value = log_prob)
396401

@@ -401,11 +406,7 @@ CmdStanFit$set("public", name = "log_prob", value = log_prob)
401406
#' @aliases grad_log_prob
402407
#' @description The `$grad_log_prob()` method provides access to the Stan
403408
#' model's `log_prob` function and its derivative.
404-
#'
405-
#' @param unconstrained_variables (numeric) A vector of unconstrained parameters
406-
#' to be passed to `grad_log_prob`.
407-
#' @param jacobian_adjustment (logical) Whether to include the log-density
408-
#' adjustments from un/constraining variables.
409+
#' @inheritParams fit-method-log_prob
409410
#'
410411
#' @examples
411412
#' \dontrun{
@@ -418,7 +419,11 @@ CmdStanFit$set("public", name = "log_prob", value = log_prob)
418419
#' [unconstrain_variables()], [unconstrain_draws()], [variable_skeleton()],
419420
#' [hessian()]
420421
#'
421-
grad_log_prob <- function(unconstrained_variables, jacobian_adjustment = TRUE) {
422+
grad_log_prob <- function(unconstrained_variables, jacobian = TRUE, jacobian_adjustment = NULL) {
423+
if (!is.null(jacobian_adjustment)) {
424+
warning("'jacobian_adjustment' is deprecated. Please use 'jacobian' instead.", call. = FALSE)
425+
jacobian <- jacobian_adjustment
426+
}
422427
if (is.null(private$model_methods_env_$model_ptr)) {
423428
stop("The method has not been compiled, please call `init_model_methods()` first",
424429
call. = FALSE)
@@ -428,7 +433,7 @@ grad_log_prob <- function(unconstrained_variables, jacobian_adjustment = TRUE) {
428433
length(unconstrained_variables), " were provided!", call. = FALSE)
429434
}
430435
private$model_methods_env_$grad_log_prob(private$model_methods_env_$model_ptr_,
431-
unconstrained_variables, jacobian_adjustment)
436+
unconstrained_variables, jacobian)
432437
}
433438
CmdStanFit$set("public", name = "grad_log_prob", value = grad_log_prob)
434439

@@ -439,11 +444,7 @@ CmdStanFit$set("public", name = "grad_log_prob", value = grad_log_prob)
439444
#' @aliases hessian
440445
#' @description The `$hessian()` method provides access to the Stan model's
441446
#' `log_prob`, its derivative, and its hessian.
442-
#'
443-
#' @param unconstrained_variables (numeric) A vector of unconstrained parameters
444-
#' to be passed to `hessian`.
445-
#' @param jacobian_adjustment (logical) Whether to include the log-density
446-
#' adjustments from un/constraining variables.
447+
#' @inheritParams fit-method-log_prob
447448
#'
448449
#' @examples
449450
#' \dontrun{
@@ -456,7 +457,11 @@ CmdStanFit$set("public", name = "grad_log_prob", value = grad_log_prob)
456457
#' [unconstrain_variables()], [unconstrain_draws()], [variable_skeleton()],
457458
#' [hessian()]
458459
#'
459-
hessian <- function(unconstrained_variables, jacobian_adjustment = TRUE) {
460+
hessian <- function(unconstrained_variables, jacobian = TRUE, jacobian_adjustment = NULL) {
461+
if (!is.null(jacobian_adjustment)) {
462+
warning("'jacobian_adjustment' is deprecated. Please use 'jacobian' instead.", call. = FALSE)
463+
jacobian <- jacobian_adjustment
464+
}
460465
if (is.null(private$model_methods_env_$model_ptr)) {
461466
stop("The method has not been compiled, please call `init_model_methods()` first",
462467
call. = FALSE)
@@ -466,7 +471,7 @@ hessian <- function(unconstrained_variables, jacobian_adjustment = TRUE) {
466471
length(unconstrained_variables), " were provided!", call. = FALSE)
467472
}
468473
private$model_methods_env_$hessian(private$model_methods_env_$model_ptr_,
469-
unconstrained_variables, jacobian_adjustment)
474+
unconstrained_variables, jacobian)
470475
}
471476
CmdStanFit$set("public", name = "hessian", value = hessian)
472477

R/model.R

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#' Create a new CmdStanModel object
22
#'
3-
#' @description \if{html}{\figure{logo.png}{options: width="25px"}}
3+
#' @description \if{html}{\figure{logo.png}{options: width=25}}
44
#' Create a new [`CmdStanModel`] object from a file containing a Stan program
55
#' or from an existing Stan executable. The [`CmdStanModel`] object stores the
66
#' path to a Stan program and compiled executable (once created), and provides
@@ -93,7 +93,7 @@
9393
#' fit_optim <- mod$optimize(data = my_data_file, seed = 123)
9494
#' fit_optim$summary()
9595
#'
96-
#' # Run 'optimize' again with 'jacobian=TRUE' and then draw from laplace approximation
96+
#' # Run 'optimize' again with 'jacobian=TRUE' and then draw from Laplace approximation
9797
#' # to the posterior
9898
#' fit_optim <- mod$optimize(data = my_data_file, jacobian = TRUE)
9999
#' fit_laplace <- mod$laplace(data = my_data_file, mode = fit_optim, draws = 2000)
@@ -109,6 +109,11 @@
109109
#' fit_pf$summary()
110110
#' mcmc_hist(fit_pf$draws("theta"))
111111
#'
112+
#' # Run 'pathfinder' again with more paths, fewer draws per path,
113+
#' # better covariance approximation, and fewer LBFGSs iterations
114+
#' fit_pf <- mod$pathfinder(data = stan_data, num_paths=10, single_path_draws=40,
115+
#' history_size=50, max_lbfgs_iters=100)
116+
#'
112117
#' # Specifying initial values as a function
113118
#' fit_mcmc_w_init_fun <- mod$sample(
114119
#' data = stan_data,
@@ -376,7 +381,7 @@ CmdStanModel <- R6::R6Class(
376381
#' @param pedantic (logical) Should pedantic mode be turned on? The default is
377382
#' `FALSE`. Pedantic mode attempts to warn you about potential issues in your
378383
#' Stan program beyond syntax errors. For details see the [*Pedantic mode*
379-
#' chapter](https://mc-stan.org/docs/reference-manual/pedantic-mode.html) in
384+
#' chapter](https://mc-stan.org/docs/stan-users-guide/pedantic-mode.html) in
380385
#' the Stan Reference Manual. **Note:** to do a pedantic check for a model
381386
#' without compiling it or for a model that is already compiled the
382387
#' [`$check_syntax()`][model-method-check_syntax] method can be used instead.
@@ -792,7 +797,7 @@ CmdStanModel$set("public", name = "variables", value = variables)
792797
#' @param pedantic (logical) Should pedantic mode be turned on? The default is
793798
#' `FALSE`. Pedantic mode attempts to warn you about potential issues in your
794799
#' Stan program beyond syntax errors. For details see the [*Pedantic mode*
795-
#' chapter](https://mc-stan.org/docs/reference-manual/pedantic-mode.html) in
800+
#' chapter](https://mc-stan.org/docs/stan-users-guide/pedantic-mode.html) in
796801
#' the Stan Reference Manual.
797802
#' @param include_paths (character vector) Paths to directories where Stan
798803
#' should look for files specified in `#include` directives in the Stan
@@ -1268,7 +1273,7 @@ CmdStanModel$set("public", name = "sample", value = sample)
12681273
#'
12691274
#' @description The `$sample_mpi()` method of a [`CmdStanModel`] object is
12701275
#' identical to the `$sample()` method but with support for
1271-
#' [MPI](https://mc-stan.org/math/mpi.html). The target audience for MPI are
1276+
#' MPI (message passing interface). The target audience for MPI are
12721277
#' those with large computer clusters. For other users, the
12731278
#' [`$sample()`][model-method-sample] method provides both parallelization of
12741279
#' chains and threading support for within-chain parallelization.
@@ -1308,8 +1313,8 @@ CmdStanModel$set("public", name = "sample", value = sample)
13081313
#' @return A [`CmdStanMCMC`] object.
13091314
#'
13101315
#' @template seealso-docs
1311-
#' @seealso The Stan Math Library's MPI documentation
1312-
#' ([mc-stan.org/math/mpi](https://mc-stan.org/math/mpi.html)) for more
1316+
#' @seealso The Stan Math Library's documentation
1317+
#' ([mc-stan.org/math](https://mc-stan.org/math/)) for more
13131318
#' details on MPI support in Stan.
13141319
#'
13151320
#' @examples
@@ -1539,7 +1544,7 @@ optimize <- function(data = NULL,
15391544
CmdStanModel$set("public", name = "optimize", value = optimize)
15401545

15411546

1542-
#' Run Stan's laplace algorithm
1547+
#' Run Stan's Laplace algorithm
15431548
#'
15441549
#' @name model-method-laplace
15451550
#' @aliases laplace
@@ -1828,12 +1833,15 @@ CmdStanModel$set("public", name = "variational", value = variational)
18281833
#' @description The `$pathfinder()` method of a [`CmdStanModel`] object runs
18291834
#' Stan's Pathfinder algorithms. Pathfinder is a variational method for
18301835
#' approximately sampling from differentiable log densities. Starting from a
1831-
#' random initialization, Pathfinder locates normal approximations to the
1832-
#' target density along a quasi-Newton optimization path, with local
1833-
#' covariance estimated using the negative inverse Hessian estimates produced
1834-
#' by the L-BFGS optimizer. Pathfinder returns draws from the Gaussian
1835-
#' approximation with the lowest estimated Kullback-Leibler (KL) divergence to
1836-
#' the true posterior. See the
1836+
#' random initialization, Pathfinder locates normal approximations
1837+
#' to the target density along a quasi-Newton optimization path in
1838+
#' the unconstrained space, with local covariance estimated using
1839+
#' the negative inverse Hessian estimates produced by the LBFGS
1840+
#' optimizer. Pathfinder selects the normal approximation with the
1841+
#' lowest estimated Kullback-Leibler (KL) divergence to the true
1842+
#' posterior. Finally Pathfinder draws from that normal
1843+
#' approximation and returns the draws transformed to the
1844+
#' constrained scale. See the
18371845
#' [CmdStan User’s Guide](https://mc-stan.org/docs/cmdstan-guide/)
18381846
#' for more details.
18391847
#'
@@ -1857,8 +1865,9 @@ CmdStanModel$set("public", name = "variational", value = variational)
18571865
#' pathfinder should return. The number of draws PSIS sampling samples from
18581866
#' will be equal to `single_path_draws * num_paths`.
18591867
#' @param draws (positive integer) Number of draws to return after performing
1860-
#' pareto smooted importance sampling (PSIS). This must be smaller than
1861-
#' `single_path_draws * num_paths`.
1868+
#' pareto smooted importance sampling (PSIS). This should be smaller than
1869+
#' `single_path_draws * num_paths` (future versions of CmdStan will throw a
1870+
#' warning).
18621871
#' @param num_paths (positive integer) Number of single pathfinders to run.
18631872
#' @param max_lbfgs_iters (positive integer) The maximum number of iterations
18641873
#' for LBFGS.
@@ -1904,23 +1913,26 @@ pathfinder <- function(data = NULL,
19041913
model_variables <- self$variables()
19051914
}
19061915
pathfinder_args <- PathfinderArgs$new(
1907-
init_alpha = init_alpha,
1908-
tol_obj = tol_obj,
1909-
tol_rel_obj = tol_rel_obj,
1910-
tol_grad = tol_grad,
1911-
tol_rel_grad = tol_rel_grad,
1912-
tol_param = tol_param,
1913-
history_size = history_size,
1914-
draws = draws,
1915-
single_path_draws = single_path_draws,
1916-
num_paths = num_paths,
1917-
max_lbfgs_iters = max_lbfgs_iters,
1918-
num_elbo_draws = num_elbo_draws,
1919-
save_single_paths = save_single_paths)
1916+
init_alpha = init_alpha,
1917+
tol_obj = tol_obj,
1918+
tol_rel_obj = tol_rel_obj,
1919+
tol_grad = tol_grad,
1920+
tol_rel_grad = tol_rel_grad,
1921+
tol_param = tol_param,
1922+
history_size = history_size,
1923+
draws = draws,
1924+
single_path_draws = single_path_draws,
1925+
num_paths = num_paths,
1926+
max_lbfgs_iters = max_lbfgs_iters,
1927+
num_elbo_draws = num_elbo_draws,
1928+
save_single_paths = save_single_paths
1929+
)
19201930
args <- CmdStanArgs$new(
19211931
method_args = pathfinder_args,
19221932
stan_file = self$stan_file(),
19231933
stan_code = suppressWarnings(self$code()),
1934+
model_methods_env = private$model_methods_env_,
1935+
standalone_env = self$functions,
19241936
model_name = self$model_name(),
19251937
exe_file = self$exe_file(),
19261938
proc_ids = 1,

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ releases.
2727
* Modularity: CmdStanR runs Stan's algorithms and lets downstream modules do the
2828
analysis.
2929

30-
* Flexible [BSD-3 license](https://opensource.org/licenses/BSD-3-Clause).
30+
* Flexible [BSD-3 license](https://opensource.org/license/bsd-3-clause/).
3131

3232

3333
### Installation
@@ -61,14 +61,14 @@ CmdStanR_](https://mc-stan.org/cmdstanr/articles/cmdstanr.html) vignette.
6161

6262
### Contributing
6363

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

6868
### License
6969

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

73-
- Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)
73+
- Code: BSD 3-clause (https://opensource.org/license/bsd-3-clause/)
7474
- Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)

_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ reference:
102102
- CmdStanMLE
103103
- CmdStanLaplace
104104
- CmdStanVB
105+
- CmdStanPathfinder
105106
- CmdStanGQ
106107
- CmdStanDiagnose
107108
- starts_with("fit-method")

0 commit comments

Comments
 (0)