Struct bridgestan::Model
source · pub struct Model<T: Borrow<StanLibrary>> { /* private fields */ }Expand description
A Stan model instance with data
Implementations§
source§impl<T: Borrow<StanLibrary>> Model<T>
impl<T: Borrow<StanLibrary>> Model<T>
sourcepub fn new<D: AsRef<CStr>>(
lib: T,
data: Option<D>,
seed: u32
) -> Result<Self, BridgeStanError>
pub fn new<D: AsRef<CStr>>( lib: T, data: Option<D>, seed: u32 ) -> Result<Self, BridgeStanError>
Create a new instance of the compiled Stan model.
Data is specified as a JSON file at the given path, a JSON string literal,
or empty for no data. The seed is used if the model has RNG functions in
the transformed data section.
sourcepub fn ref_library(&self) -> &StanLibrary
pub fn ref_library(&self) -> &StanLibrary
Return a reference to the underlying Stan library
sourcepub fn new_rng(&self, seed: u32) -> Result<Rng<&StanLibrary>, BridgeStanError>
pub fn new_rng(&self, seed: u32) -> Result<Rng<&StanLibrary>, BridgeStanError>
Create a new Rng random number generator from the library underlying this model.
This can be used in param_constrain when values from the generated quantities
block are desired.
This instance can only be used with models from the same Stan library. Invalid usage will otherwise result in a panic.
sourcepub fn name(&self) -> Result<&str, BridgeStanError>
pub fn name(&self) -> Result<&str, BridgeStanError>
Return the name of the model or error if UTF decode fails
sourcepub fn param_names(&self, include_tp: bool, include_gq: bool) -> &str
pub fn param_names(&self, include_tp: bool, include_gq: bool) -> &str
Return a comma-separated sequence of indexed parameter names, including the transformed parameters and/or generated quantities as specified.
The parameters are returned in the order they are declared.
Multivariate parameters are return in column-major (more
generally last-index major) order. Parameter indices are separated
with periods (.). For example, a[3] is written a.3 and b[2, 3]
as b.2.3. The numbering follows Stan and is indexed from 1.
If include_tp is set the names will also include the transformed
parameters of the Stan model after the parameters. If include_gq is
set, we also include the names of the generated quantities at
the very end.
sourcepub fn param_unc_names(&mut self) -> &str
pub fn param_unc_names(&mut self) -> &str
Return a comma-separated sequence of unconstrained parameters. Only parameters are unconstrained, so there are no unconstrained transformed parameters or generated quantities.
The parameters are returned in the order they are declared.
Multivariate parameters are return in column-major (more
generally last-index major) order. Parameter indices are separated with
periods (.). For example, a[3] is written a.3 and b[2, 3] as b.2.3. The numbering follows Stan and is indexed from 1.
sourcepub fn param_num(&self, include_tp: bool, include_gq: bool) -> usize
pub fn param_num(&self, include_tp: bool, include_gq: bool) -> usize
Number of parameters in the model on the constrained scale.
Will also count transformed parameters (include_tp) and generated
quantities (include_gq) if requested.
sourcepub fn param_unc_num(&self) -> usize
pub fn param_unc_num(&self) -> usize
Return the number of parameters on the unconstrained scale.
In particular, this is the size of the slice required by the log_density functions.
sourcepub fn log_density(
&self,
theta_unc: &[f64],
propto: bool,
jacobian: bool
) -> Result<f64, BridgeStanError>
pub fn log_density( &self, theta_unc: &[f64], propto: bool, jacobian: bool ) -> Result<f64, BridgeStanError>
Compute the log of the prior times likelihood density
Drop jacobian determinant terms of the transformation from unconstrained
to the constrained space if jacobian == false and drop terms
of the density that do not depend on the parameters if propto == true.
sourcepub fn log_density_gradient(
&self,
theta_unc: &[f64],
propto: bool,
jacobian: bool,
grad: &mut [f64]
) -> Result<f64, BridgeStanError>
pub fn log_density_gradient( &self, theta_unc: &[f64], propto: bool, jacobian: bool, grad: &mut [f64] ) -> Result<f64, BridgeStanError>
Compute the log of the prior times likelihood density and its gradient
Drop jacobian determinant terms of the transformation from unconstrained
to the constrained space if jacobian == false and drop terms
of the density that do not depend on the parameters if propto == true.
The gradient of the log density will be stored in grad.
Panics if the provided buffer has incorrect shape. The gradient buffer grad
must have length self.param_unc_num().
sourcepub fn log_density_hessian(
&self,
theta_unc: &[f64],
propto: bool,
jacobian: bool,
grad: &mut [f64],
hessian: &mut [f64]
) -> Result<f64, BridgeStanError>
pub fn log_density_hessian( &self, theta_unc: &[f64], propto: bool, jacobian: bool, grad: &mut [f64], hessian: &mut [f64] ) -> Result<f64, BridgeStanError>
Compute the log of the prior times likelihood density and its gradient and hessian.
Drop jacobian determinant terms of the transformation from unconstrained
to the constrained space if jacobian == false and drop terms
of the density that do not depend on the parameters if propto == true.
The gradient of the log density will be stored in grad, the
hessian is stored in hessian.
Panics if the provided buffers have incorrect shapes. The gradient buffer grad
must have length self.param_unc_num() and the hessian buffer must
have length self.param_unc_num() * self.param_unc_num().
sourcepub fn log_density_hessian_vector_product(
&self,
theta_unc: &[f64],
v: &[f64],
propto: bool,
jacobian: bool,
hvp: &mut [f64]
) -> Result<f64, BridgeStanError>
pub fn log_density_hessian_vector_product( &self, theta_unc: &[f64], v: &[f64], propto: bool, jacobian: bool, hvp: &mut [f64] ) -> Result<f64, BridgeStanError>
Compute the log of the prior times likelihood density the product of the Hessian and specified vector.
Drop jacobian determinant terms of the transformation from unconstrained
to the constrained space if jacobian == false and drop terms
of the density that do not depend on the parameters if propto == true.
The product of the Hessian of the log density and the provided vector
will be stored in hvp.
Panics if the provided buffer has incorrect shape. The buffer hvp
must have length self.param_unc_num().
sourcepub fn param_constrain<R: Borrow<StanLibrary>>(
&self,
theta_unc: &[f64],
include_tp: bool,
include_gq: bool,
out: &mut [f64],
rng: Option<&mut Rng<R>>
) -> Result<(), BridgeStanError>
pub fn param_constrain<R: Borrow<StanLibrary>>( &self, theta_unc: &[f64], include_tp: bool, include_gq: bool, out: &mut [f64], rng: Option<&mut Rng<R>> ) -> Result<(), BridgeStanError>
Map a point in unconstrained parameter space to the constrained space.
theta_unc must contain the point in the unconstrained parameter space.
If include_tp is set the output will also include the transformed
parameters of the Stan model after the parameters. If include_gq is
set, we also include the generated quantities at the very end.
Panics if the provided buffer has incorrect shape. The length of the out buffer
self.param_num(include_tp, include_gq).
Panics if include_gq is set but no random number generator is provided.
sourcepub fn param_unconstrain(
&self,
theta: &[f64],
theta_unc: &mut [f64]
) -> Result<(), BridgeStanError>
pub fn param_unconstrain( &self, theta: &[f64], theta_unc: &mut [f64] ) -> Result<(), BridgeStanError>
Map a point in constrained parameter space to the unconstrained space.
sourcepub fn param_unconstrain_json<S: AsRef<CStr>>(
&self,
json: S,
theta_unc: &mut [f64]
) -> Result<(), BridgeStanError>
pub fn param_unconstrain_json<S: AsRef<CStr>>( &self, json: S, theta_unc: &mut [f64] ) -> Result<(), BridgeStanError>
Map a constrained point in json format to the unconstrained space.
The JSON schema assumed is fully defined in the CmdStan Reference Manual. A value for each parameter in the Stan program should be provided, with dimensions and size corresponding to the Stan program declarations.
source§impl<T: Borrow<StanLibrary> + Clone> Model<T>
impl<T: Borrow<StanLibrary> + Clone> Model<T>
sourcepub fn clone_library_ref(&self) -> T
pub fn clone_library_ref(&self) -> T
Return a clone of the underlying Stan library