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>

source

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.

source

pub fn ref_library(&self) -> &StanLibrary

Return a reference to the underlying Stan library

source

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.

source

pub fn name(&self) -> Result<&str, BridgeStanError>

Return the name of the model or error if UTF decode fails

source

pub fn info(&self) -> &CStr

Return information about the compiled model

source

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.

source

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.

source

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.

source

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.

source

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.

source

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().

source

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().

source

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().

source

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.

source

pub fn param_unconstrain( &self, theta: &[f64], theta_unc: &mut [f64] ) -> Result<(), BridgeStanError>

Map a point in constrained parameter space to the unconstrained space.

source

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>

source

pub fn clone_library_ref(&self) -> T

Return a clone of the underlying Stan library

Trait Implementations§

source§

impl<T: Borrow<StanLibrary>> Drop for Model<T>

source§

fn drop(&mut self)

Free the memory allocated in C++.

source§

impl<T: Send + Borrow<StanLibrary>> Send for Model<T>

source§

impl<T: Sync + Borrow<StanLibrary>> Sync for Model<T>

Auto Trait Implementations§

§

impl<T> Freeze for Model<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Model<T>
where T: RefUnwindSafe,

§

impl<T> Unpin for Model<T>
where T: Unpin,

§

impl<T> UnwindSafe for Model<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.