“Hello, World!”#

Fitting a Stan model using the NUTS-HMC sampler#

In order to verify the installation and also to demonstrate the CmdStanPy workflow, we use CmdStanPy to fit the the example Stan model bernoulli.stan to the dataset bernoulli.data.json. The bernoulli.stan is a Hello, World! program which illustrates the basic syntax of the Stan language. It allows the user to verify that CmdStanPy, CmdStan, the StanC compiler, and the C++ toolchain have all been properly installed.

For substantive example models and guidance on coding statistical models in Stan, see the Stan User’s Guide.

The Stan model#

The model bernoulli.stan is a trivial model: given a set of N observations of i.i.d. binary data y[1] … y[N], it calculates the Bernoulli chance-of-success theta.

data {
  int<lower=0> N;
  int<lower=0,upper=1> y[N];
parameters {
  real<lower=0,upper=1> theta;
model {
  theta ~ beta(1,1);  // uniform prior on interval 0,1
  y ~ bernoulli(theta);

The CmdStanModel class manages the Stan program and its corresponding compiled executable. It provides properties and functions to inspect the model code and filepaths. A CmdStanModel can be instantiated from a Stan file or its corresponding compiled executable file.

# import packages
In [1]: import os

In [2]: from cmdstanpy import CmdStanModel

# specify Stan program file
In [3]: stan_file = os.path.join('examples', 'bernoulli.stan')

# instantiate the model object
In [4]: model = CmdStanModel(stan_file=stan_file)
INFO:cmdstanpy:compiling stan file /home/docs/checkouts/readthedocs.org/user_builds/cmdstanpy/checkouts/v1.0.3/docsrc/examples/bernoulli.stan to exe file /home/docs/checkouts/readthedocs.org/user_builds/cmdstanpy/checkouts/v1.0.3/docsrc/examples/bernoulli
INFO:cmdstanpy:compiled model executable: /home/docs/checkouts/readthedocs.org/user_builds/cmdstanpy/checkouts/v1.0.3/docsrc/examples/bernoulli

# inspect model object
In [5]: print(model)
CmdStanModel: name=bernoulli
	 compiler_options=stanc_options={}, cpp_options={}

# inspect compiled model
In [6]: print(model.exe_info())
{'stan_version_major': '2', 'stan_version_minor': '29', 'stan_version_patch': '2', 'STAN_THREADS': 'false', 'STAN_MPI': 'false', 'STAN_OPENCL': 'false', 'STAN_NO_RANGE_CHECKS': 'false', 'STAN_CPP_OPTIMS': 'false'}

Data inputs#

CmdStanPy accepts input data either as a Python dictionary which maps data variable names to values, or as the corresponding JSON file.

The bernoulli model requires two inputs: the number of observations N, and an N-length vector y of binary outcomes. The data file bernoulli.data.json contains the following inputs:

 "N" : 10,
 "y" : [0,1,0,0,0,0,0,0,0,1]

Fitting the model#

The sample() method is used to do Bayesian inference over the model conditioned on data using using Hamiltonian Monte Carlo (HMC) sampling. It runs Stan’s HMC-NUTS sampler on the model and data and returns a CmdStanMCMC object. The data can be specified either as a filepath or a Python dictionary; in this example, we use the example datafile bernoulli.data.json: By default, the sample method runs 4 sampler chains.

# specify data file
In [7]: data_file = os.path.join('examples', 'bernoulli.data.json')

# fit the model
In [8]: fit = model.sample(data=data_file)
INFO:cmdstanpy:CmdStan start processing
INFO:cmdstanpy:CmdStan done processing.

Underlyingly, the CmdStan outputs are a set of per-chain Stan CSV files. The filenames follow the template ‘<model_name>-<YYYYMMDDHHMMSS>-<chain_id>’ plus the file suffix ‘.csv’. CmdStanPy also captures the per-chain console and error messages. The output_dir argument is an optional argument which specifies the path to the output directory used by CmdStan. If this argument is omitted, the output files are written to a temporary directory which is deleted when the current Python session is terminated.

# printing the object reports sampler commands, output files
In [9]: print(fit)
CmdStanMCMC: model=bernoulli chains=4['method=sample', 'algorithm=hmc', 'adapt', 'engaged=1']

Accessing the results#

The sample method returns a CmdStanMCMC object, which provides access to the information from the Stan CSV files. The CSV header and data rows contain the outputs from each iteration of the sampler. CSV comment blocks are used to report the inference engine configuration and timing information. The NUTS-HMC adaptive sampler algorithm also outputs the per-chain HMC tuning parameters step_size and metric.

The CmdStanMCMC object parses the set of Stan CSV files into separate in-memory data structures for the set of sampler iterations, the metadata, and the step_size and metric and provides accessor methods for each. The primary object of interest are the draws from all iterations of the sampler, i.e., the CSV data rows. The CmdStanMCMC methods allow the user to extract the sample in whatever data format is needed for their analysis. The sample can be extracted in tabular format, either as

In [10]: print(fit.draws().shape)
(1000, 4, 8)

In [11]: print(fit.draws(concat_chains=True).shape)
(4000, 8)

In [12]: fit.draws_pd()
         lp__  accept_stat__  stepsize__  ...  divergent__  energy__     theta
0    -6.80473       0.999962     0.87796  ...          0.0   6.81962  0.293582
1    -6.78109       0.984032     0.87796  ...          0.0   6.91542  0.283025
2    -6.84957       0.963323     0.87796  ...          0.0   7.01175  0.308936
3    -6.84957       0.426410     0.87796  ...          0.0  10.26620  0.308936
4    -6.97294       0.965294     0.87796  ...          0.0   6.98341  0.339348
...       ...            ...         ...  ...          ...       ...       ...
3995 -7.41351       0.921764     1.10296  ...          0.0   7.70970  0.126968
3996 -7.63183       0.968631     1.10296  ...          0.0   7.69472  0.112300
3997 -7.48124       1.000000     1.10296  ...          0.0   7.69891  0.122065
3998 -6.75003       1.000000     1.10296  ...          0.0   7.38286  0.242136
3999 -6.75709       0.997943     1.10296  ...          0.0   6.76554  0.233421

[4000 rows x 8 columns]

The sample can be treated as a collection of named, structured variables. CmdStanPy makes a distinction between the per-iteration model outputs and the per-iteration algorithm outputs: the former are ‘stan_variables’ and the information reported by the sampler are ‘method_variables’. Accessor functions extract these as:

  • a structured numpy.ndarray: stan_variable() which contains the set of all draws in the sample for the named Stan program variable. The draws from all chains are flattened, i.e., the first ndarray dimension is the number of draws X number of chains. The remaining ndarray dimensions correspond to the Stan program variable dimension.

  • an xarray.Dataset: draws_xr()

  • a Python dict mapping Stan variable names to numpy.ndarray objects, where the chains are flattened, as above: stan_variables().

  • a Python dict mapping the algorithm outputs to numpy.ndarray objects. Because these outputs are used for within-chain and cross-chain diagnostics, they are not flattened. stan_variables().

In [13]: print(fit.stan_variable('theta'))
[0.293582 0.283025 0.308936 ... 0.122065 0.242136 0.233421]

In [14]: print(fit.draws_xr('theta'))
Dimensions:  (draw: 1000, chain: 4)
  * chain    (chain) int64 1 2 3 4
  * draw     (draw) int64 0 1 2 3 4 5 6 7 8 ... 992 993 994 995 996 997 998 999
Data variables:
    theta    (chain, draw) float64 0.2936 0.283 0.3089 ... 0.1221 0.2421 0.2334
    stan_version:        2.29.2
    model:               bernoulli_model
    num_draws_sampling:  1000

In [15]: for k, v in fit.stan_variables().items():
   ....:     print(f'{k}\t{v.shape}')
theta	(4000,)

In [16]: for k, v in fit.method_variables().items():
   ....:     print(f'{k}\t{v.shape}')
lp__	(1000, 4)
accept_stat__	(1000, 4)
stepsize__	(1000, 4)
treedepth__	(1000, 4)
n_leapfrog__	(1000, 4)
divergent__	(1000, 4)
energy__	(1000, 4)

In addition to the MCMC sample itself, the CmdStanMCMC object provides access to the the per-chain HMC tuning parameters from the NUTS-HMC adaptive sampler, (if present).

In [17]: print(fit.metric_type)

In [18]: print(fit.metric)

In [19]: print(fit.step_size)
[0.87796  1.14716  0.901842 1.10296 ]

The CmdStanMCMC object also provides access to metadata about the model and the sampler run.

In [20]: print(fit.metadata.cmdstan_config['model'])

In [21]: print(fit.metadata.cmdstan_config['seed'])

In [22]: print(fit.metadata.stan_vars_cols.keys())

In [23]: print(fit.metadata.method_vars_cols.keys())
dict_keys(['lp__', 'accept_stat__', 'stepsize__', 'treedepth__', 'n_leapfrog__', 'divergent__', 'energy__'])

CmdStan utilities: stansummary, diagnose#

CmdStan is distributed with a posterior analysis utility stansummary that reads the outputs of all chains and computes summary statistics for all sampler and model parameters and quantities of interest. The CmdStanMCMC method summary() runs this utility and returns summaries of the total joint log-probability density lp__ plus all model parameters and quantities of interest in a pandas.DataFrame:

In [24]: fit.summary()
          Mean      MCSE    StdDev  ...    N_Eff  N_Eff/s     R_hat
name                                ...                            
lp__  -7.26763  0.017239  0.713950  ...  1715.19  21711.2  1.001120
theta  0.25883  0.003078  0.121248  ...  1551.73  19642.2  0.999983

[2 rows x 9 columns]

CmdStan is distributed with a second posterior analysis utility diagnose which analyzes the per-draw sampler parameters across all chains looking for potential problems which indicate that the sample isn’t a representative sample from the posterior. The diagnose() method runs this utility and prints the output to the console.

In [25]: print(fit.diagnose())
Processing csv files: /tmp/tmpv33sodhs/bernoulli71r9iw3r/bernoulli-20220629202849_1.csv, /tmp/tmpv33sodhs/bernoulli71r9iw3r/bernoulli-20220629202849_2.csv, /tmp/tmpv33sodhs/bernoulli71r9iw3r/bernoulli-20220629202849_3.csv, /tmp/tmpv33sodhs/bernoulli71r9iw3r/bernoulli-20220629202849_4.csv

Checking sampler transitions treedepth.
Treedepth satisfactory for all transitions.

Checking sampler transitions for divergences.
No divergent transitions found.

Checking E-BFMI - sampler transitions HMC potential energy.
E-BFMI satisfactory.

Effective sample size satisfactory.

Split R-hat values satisfactory all parameters.

Processing complete, no problems detected.

Managing Stan CSV files#

The CmdStanMCMC object keeps track of all output files produced by the sampler run. The save_csvfiles() function moves the CSV files to a specified directory.

In [26]: fit.save_csvfiles(dir='some/path')