A Julia interface to Stan's cmdstan executable
Stan.jl
Stan is a system for statistical modeling, data analysis, and prediction. It is extensively used in social, biological, and physical sciences, engineering, and business. The Stan program language and interfaces are documented here.
cmdstan is the shell/command line interface to run Stan language programs.
Stan.jl wraps cmdstan and captures the samples for further processing.
StanJulia overview
Stan.jl is part of the StanJulia Github organization set of packages. CmdStan.jl is one of two options in StanJulia to capture draws from a Stan language program. The other option is under development and is illustrated in Stan.jl and StatisticalRethinking.jl.
These are not the only options to sample using Stan from Julia. Valid other options are PyCall.jl/PyStan and StanRun.jl.
On a very high level, a typical workflow for using CmdStan.jl looks like:
using CmdStan
# Define a Stan language program.
bernoulli = "..."
# Prepare for calling cmdstan.
sm = SampleModel(...)
# Compile and run Stan program, collect draws.
rc = stan_sample(...)
if rc == 0
# Cmdstan summary of result
sdf = read_summary(sm)
# Display the summary as a DataFrame
sdf |> display
# Show the draws
samples = read_samples(sm, output_format=:array)
end
This workflow creates an array of draws, the default value for the output_format
argument in read_samples().
If at this point a vector of DataFrames (a DataFrame for each chain) is preferred:
df = read_samples(sm; output_format=:dataframes)
Other options are :dataframe, :dataframes
, :mcmcchains
and :particles
. See
?read_samples
for more details.
Version 5 of Stan.jl used :mcmcchains
by default but the dependencies of MCMCChains.jl, including access to plotting features, lead to long compile times. In version 6 the default is :array again. In order to use the other options glue code is needed which is handled by Requires.jl.
References
There is no shortage of good books on Bayesian statistics. A few of my favorites are:
and a great read (and implementation in DynamicHMC.jl):