API

The page documents the API of PyCmdStan, mostly by pulling docstrings from the sources.

Reading and writing files

I/O functions for working with CmdStan executables.

pycmdstan.io.merge_csv_data(*csvs, skip=0)[source]

Merge multiple CSV dicts into a single dict.

pycmdstan.io.parse_csv(fname, merge=True)[source]

Parse samples from a Stan output CSV file.

pycmdstan.io.parse_summary_csv(fname)[source]

Parse CSV output of the stansummary program.

pycmdstan.io.rdump(fname, data)[source]

Dump a dict of data to a R dump format file.

pycmdstan.io.rload(fname)[source]

Load a dict of data from an R dump format file.

Working with Stan models

exception pycmdstan.model.CmdStanNotFound[source]
class pycmdstan.model.Model(code: str = None, fname: os.PathLike = None, opt_lvl=3)[source]

Stan model.

compile(path=None)[source]

Compile the model.

model_id

SHA256 checksum of model code.

optimize(**kwargs)[source]

Run the model with the optimize (L-BFGS) method.

run(method, chains=1, wait=False, seed=0, **kwargs)[source]

Run chains instances of the model with the given method. Extra keyword arguments are passed to Run.__init__.

sample(**kwargs)[source]

Run the model with the sample (NUTS/HMC) method.

variational(**kwargs)[source]

Run the model with the variational (ADVI) method.

class pycmdstan.model.Run(model: pycmdstan.model.Model, method: str, data: dict = None, id: int = None, log_lik: str = 'log_lik', start: bool = True, wait: bool = False, tmp_dir: str = '', **method_args)[source]
cmd

Generate the command line for this run.

csv

Return output samples.

start(wait=True)[source]

Start the run; invokes executable in subprocess.

wait()[source]

Wait for run to complete.

class pycmdstan.model.RunSet(*runs)[source]

A set of runs.

N_Eff

Retrieve vector of all N_eff values.

N_Eff_per_iter

Retrieve vector of N_eff / niter.

R_hats

Retrive vector of all R_hat values.

csv

Return all output samples for Runs in this Runset.

flat_field(field, only_params=True)[source]

Collect all values from summary for a given column, e.g. R_hat.

summary

Retrive summary results for this RunSet.

pycmdstan.model.compile_model(stan_fname, opt_lvl=3)[source]

Compile the given Stan model file to an executable.

pycmdstan.model.model_path() → str[source]

Returns the directory where compiled models should be cached.

pycmdstan.model.preprocess_model(stan_fname, hpp_fname=None, overwrite=False)[source]

Invoke stanc on the given Stan model file.

pycmdstan.model.stansummary_csv(csv_in)[source]

Invoke the stansummary command on a list of CSV files, and return the resulting summary.

Plotting results

pycmdstan.viz.hist_key(csv, *keys)[source]

Create a histogram for keys (parameter names) in sample set.

pycmdstan.viz.pairs(csv, keys, skip=0)[source]

Create a pairs plot for keys in the given dataset.

pycmdstan.viz.parallel_coordinates(csv, keys, marker='ko-')[source]

Create a parallel coordinates plot for keys in the given dataset.

pycmdstan.viz.plot_key(csv, *keys)[source]

Create a trace plot for keys (parameter names) in sample set.

pycmdstan.viz.trace_nuts(csv, extras='', skip=0, n_col=4)[source]

Trace plots of NUTS state, along with other model parameters.