NOTE: Stan Model Server is no longer under active development. For a more efficient interface to a Stan model's methods from multiple languages, check out bridgestan.
A lightweight server interface to Stan model methods.
For installation instructions, see
- Installing Stan Model Server Install Documentation
For full documentation on the REPL, see:
- Stan Model Server REPL Documentation
The package is distributed with a a Stan program in stan/bernoulli/bernoulli.hpp
and matching data in stan/bernoulli/bernoulli.data.json
.
To compile the server executable,
$ cd stan-model-server
$ make CMDSTAN=/Users/carp/github/stan-dev/cmdstan/ stan/bernoulli/bernoulli
Then we run from the command line with data and an RNG seed.
$ stan/bernoulli/bernoulli -s 1234 -d stan/bernoulli/bernoulli.data.json
The lines marked with <
indicate input from the user and the unmarked lines are responses from the server.
< name
bernoulli_model
< param_num 1 1
3
< param_unc_num
1
< param_names 1 1
theta,logit_theta,y_sim
< param_unc_names
theta
< param_constrain 1 1 -2.3
0.0911229610148561,-2.3,0
< param_unconstrain {"theta":0.09112}
-2.30003575312272
< log_density 1 1 1 1 -1.5
-6.91695933579303,0.810893714323724,-1.78975742485563
We want to be able to access Stan model methods from within R or Python in order to do algorithm development. The first system that did this is HTTPStan, an official Stan project:
- GitHub stan-dev: httpstan
The direct inspiration for this project came from the simple I/O structure of ReddingStan:
- Dan Muck and Daniel Lee. 2022. Smuggling log probability and gradients out of Stan programs — ReddingStan. The Stan Blog.
- Github: dmuck/redding-stan
The Python client is feature complete. It can be invoked this way after the Bernoulli example is compiled.
> import StanModelClient as smc
> sc = smc.StanClient("./stan/bernoulli/bernoulli",
data = "stan/bernoulli/bernoulli.data.json",
seed = 1234)
Here are some example calls.
> sc.name()
> sc.param_names()
> sc.log_density_gradient([-2.3])
> sc.param_constrain([-2.3])
Documentation is available as docstrings in the source code.
The plan is to build samplers out in Python using this interface. The samplers are even more a work-in-progress than the client interface. For now, there is a worked example of Metropolis with a Stan model.
The R Client is feature complete. It can be invoked this way after the Bernoulli example is compiled.
# Load source file into environment
source('StanModelClient.R')
# Create the Stan Client
sc <- create_stan_client(exe_file = "./stan/bernoulli/bernoulli",
data_file = "stan/bernoulli/bernoulli.data.json",
seed = 1234)
Here are some example calls.
sc$name()
sc$param_names()
sc$log_density_gradient(-2.3)
sc$param_constrain(-2.3)
For more information on the R Client, see the source code and the documentation for a complete list of commands and their usage.
In time, the samplers available in Python will also be available through the R interface. For now, however, see the worked example of gradient descent on the Bernoulli Stan model.
- Code released under the BSD-3 license.
- Documentation released under the CC BY 4.0 license.