BridgeStan from Rust
View the BridgeStan documentation on Github Pages.
This is a Rust wrapper for BridgeStan. It allows users to evaluate the log likelihood and related functions for Stan models natively from Rust.
Internally, it relies on bindgen
and
libloading
.
Compiling the model
The Rust wrapper does not currently have any functionality to compile Stan models.
Compiled shared libraries need to be built manually using make
or with the Julia
or Python bindings.
For safety reasons all Stan models need to be installed with STAN_THREADS=true
.
When compiling a model using make
, set the environment variable:
STAN_THREADS=true
When compiling a Stan model in python, this has to be specified in the make_args
argument:
=
If STAN_THREADS
was not specified while building the model, the Rust wrapper
will throw an error when loading the model.
Usage:
Run this example with cargo run --example=example
.
use CString;
use Path;
use ;
// The path to the compiled model.
// Get for instance from python `bridgestan.compile_model`
let path = new
.parent
.unwrap
.join;
let lib = open_library.expect;
// The dataset as json
let data = r#"{"N": 7}"#;
let data = new.unwrap;
// The seed is used in case the model contains a transformed data section
// that uses rng functions.
let seed = 42;
let model = match new ;
let n_dim = model.param_unc_num;
assert_eq!;
let point = vec!;
let mut gradient_out = vec!;
let logp = model.log_density_gradient
.expect;
// gradient_out contains the gradient of the logp density