Title: | BridgeStan, Accessing Stan Model Functions in R |
---|---|
Description: | BridgeStan provides efficient in-memory access to the methods of a Stan model, including log densities, gradients, Hessians, and constraining and unconstraining transforms. |
Authors: | Brian Ward [aut, cre] |
Maintainer: | Brian Ward <[email protected]> |
License: | BSD_3_clause |
Version: | 2.6.1 |
Built: | 2025-01-18 18:26:35 UTC |
Source: | https://github.com/roualdes/bridgestan |
compile_model()
Compiles a Stan model.
compile_model(stan_file, stanc_args = NULL, make_args = NULL)
compile_model(stan_file, stanc_args = NULL, make_args = NULL)
stan_file |
A path to a Stan model file. |
make_args |
A vector of additional arguments to pass to Make.
For example, |
stanc_arg |
A vector of arguments to pass to stanc3.
For example, |
Run BridgeStan's Makefile on a .stan
file, creating
the .so
used by the StanModel class.
This function checks that the path to BridgeStan is valid
and will error if not. This can be set with set_bridgestan_path
.
Path to the compiled model.
By default this is set to the value of the environment
variable BRIDGESTAN
.
get_bridgestan_path(download = TRUE)
get_bridgestan_path(download = TRUE)
If there is no path set and the argument download
is TRUE,
this function will download a copy of the BridgeStan source code
for the currently installed version under a folder called
.bridgestan
in the user's home directory if one is not already
present.
set_bridgestan_path()
Set the path to BridgeStan.
set_bridgestan_path(path)
set_bridgestan_path(path)
This should point to the top-level folder of the repository.
R6 Class representing a compiled BridgeStan model.
This model exposes log density, gradient, and Hessian information as well as constraining and unconstraining transforms.
new()
Create a Stan Model instance.
StanModel$new( lib, data, seed, stanc_args = NULL, make_args = NULL, warn = TRUE )
lib
A path to a compiled BridgeStan Shared Object file or a .stan file (will be compiled).
data
Either a JSON string literal, a path to a data file in JSON format ending in ".json", or the empty string.
seed
Seed for the RNG used in constructing the model.
stanc_args
A list of arguments to pass to stanc3 if the model is not already compiled.
make_args
A list of additional arguments to pass to Make if the model is not already compiled.
warn
If false, the warning about re-loading the same shared object is suppressed.
A new StanModel.
name()
Get the name of this StanModel.
StanModel$name()
A character vector of the name.
model_info()
Get compile information about this Stan model.
StanModel$model_info()
A character vector of the Stan version and important flags.
model_version()
Get the version of BridgeStan used in the compiled model.
StanModel$model_version()
param_names()
Return the indexed names of the (constrained) parameters. For containers, indexes are separated by periods (.).
For example, the scalar a
has indexed name "a", the vector entry a[1]
has
indexed name "a.1" and the matrix entry a[2, 3]
has indexed name "a.2.3". Parameter
order of the output is column major and more generally last-index major for containers.
StanModel$param_names(include_tp = FALSE, include_gq = FALSE)
include_tp
Whether to include variables from transformed parameters.
include_gq
Whether to include variables from generated quantities.
A list of character vectors of the names.
param_unc_names()
Return the indexed names of the unconstrained parameters. For containers, indexes are separated by periods (.).
For example, the scalar a
has indexed name "a", the vector entry a[1]
has
indexed name "a.1" and the matrix entry a[2, 3]
has indexed name "a.2.3". Parameter
order of the output is column major and more generally last-index major for containers.
StanModel$param_unc_names()
A list of character vectors of the names.
param_num()
Return the number of (constrained) parameters in the model.
StanModel$param_num(include_tp = FALSE, include_gq = FALSE)
include_tp
Whether to include variables from transformed parameters.
include_gq
Whether to include variables from generated quantities.
The number of parameters in the model.
param_unc_num()
Return the number of unconstrained parameters in the model.
This function is mainly different from param_num
when variables are declared with constraints.
For example, simplex[5]
has a constrained size of 5, but an unconstrained size of 4.
StanModel$param_unc_num()
The number of parameters in the model.
param_constrain()
Returns a vector of constrained parameters given the unconstrained parameters.
See also StanModel$param_unconstrain()
, the inverse of this function.
StanModel$param_constrain( theta_unc, include_tp = FALSE, include_gq = FALSE, rng )
theta_unc
The vector of unconstrained parameters.
include_tp
Whether to also output the transformed parameters of the model.
include_gq
Whether to also output the generated quantities of the model.
rng
The random number generator to use if include_gq
is TRUE
. See StanModel$new_rng()
.
The constrained parameters of the model.
new_rng()
Create a new persistent PRNG object for use in param_constrain()
.
StanModel$new_rng(seed)
seed
The seed for the PRNG.
A StanRNG
object.
param_unconstrain()
Returns a vector of unconstrained parameters give the constrained parameters.
It is assumed that these will be in the same order as internally represented by
the model (e.g., in the same order as StanModel$param_names()
).
If structured input is needed, use StanModel$param_unconstrain_json()
.
See also StanModel$param_constrain()
, the inverse of this function.
StanModel$param_unconstrain(theta)
theta
The vector of constrained parameters.
The unconstrained parameters of the model.
param_unconstrain_json()
This accepts a JSON string of constrained parameters and returns the unconstrained parameters.
The JSON is expected to be in the JSON Format for CmdStan.
StanModel$param_unconstrain_json(json)
json
Character vector containing a string representation of JSON data.
The unconstrained parameters of the model.
log_density()
Return the log density of the specified unconstrained parameters.
StanModel$log_density(theta_unc, propto = TRUE, jacobian = TRUE)
theta_unc
The vector of unconstrained parameters.
propto
If TRUE
, drop terms which do not depend on the parameters.
jacobian
If TRUE
, include change of variables terms for constrained parameters.
The log density.
log_density_gradient()
Return the log density and gradient of the specified unconstrained parameters.
StanModel$log_density_gradient(theta_unc, propto = TRUE, jacobian = TRUE)
theta_unc
The vector of unconstrained parameters.
propto
If TRUE
, drop terms which do not depend on the parameters.
jacobian
If TRUE
, include change of variables terms for constrained parameters.
List containing entries val
(the log density) and gradient
(the gradient).
log_density_hessian()
Return the log density, gradient, and Hessian of the specified unconstrained parameters.
StanModel$log_density_hessian(theta_unc, propto = TRUE, jacobian = TRUE)
theta_unc
The vector of unconstrained parameters.
propto
If TRUE
, drop terms which do not depend on the parameters.
jacobian
If TRUE
, include change of variables terms for constrained parameters.
List containing entries val
(the log density), gradient
(the gradient), and hessian
(the Hessian).
log_density_hessian_vector_product()
Return the log density and the product of the Hessian with the specified vector.
StanModel$log_density_hessian_vector_product( theta_unc, v, propto = TRUE, jacobian = TRUE )
theta_unc
The vector of unconstrained parameters.
v
The vector to multiply the Hessian by.
propto
If TRUE
, drop terms which do not depend on the parameters.
jacobian
If TRUE
, include change of variables terms for constrained parameters.
List containing entries val
(the log density) and Hvp
(the hessian-vector product).