Internal API Reference#

The following documents the internal API of CmdStanPy. No guarantees are made about backwards compatibility between minor versions and refactors are expected. If you find yourself needing something exposed here, please open an issue requesting it be added to the public API.

Classes#

InferenceMetadata#

class cmdstanpy.stanfit.InferenceMetadata(header)[source]#

Names and structure of the output variables of a CmdStan run, parsed from the Stan CSV file’s column header. Assumes valid CSV files.

Parameters:

header (str)

property method_vars: dict[str, Variable]#

Method variable names always end in __, e.g. lp__.

property stan_vars: dict[str, Variable]#

These are the user-defined variables in the Stan program.

RunSet#

class cmdstanpy.stanfit.RunSet(args, chains=1, *, chain_ids=None, time_fmt='%Y%m%d%H%M%S', one_process_per_chain=True)[source]#

Encapsulates the configuration and results of a call to any CmdStan inference method. Records the method return code and locations of all console, error, and output files.

RunSet objects are instantiated by the CmdStanModel class inference methods which validate all inputs, therefore “__init__” method skips input checks.

Parameters:
cmd(idx)[source]#

Assemble CmdStan invocation. When running parallel chains from single process (2.28 and up), specify CmdStan arg num_chains and leave chain idx off CSV files.

Parameters:

idx (int)

Return type:

list[str]

gen_file_name(suffix, *, extra='', id=None)[source]#

Generate a standard file name according to CmdStan output pattern

Parameters:
Return type:

str

get_err_msgs()[source]#

Checks console messages for each CmdStan run.

Return type:

str

property chain_ids: list[int]#

Chain ids.

property chains: int#

Number of chains.

property config_files: list[str]#

List of paths to CmdStan config json files.

property csv_files: list[str]#

List of paths to CmdStan output files.

property diagnostic_files: list[str]#

List of paths to CmdStan hamiltonian diagnostic files.

property method: Method#

CmdStan method used to generate this fit.

property metric_files: list[str]#

List of paths to CmdStan NUTS-HMC sampler metric files.

property model: str#

Stan model name.

property num_procs: int#

Number of processes run.

property one_process_per_chain: bool#

When True, for each chain, call CmdStan in its own subprocess. When False, use CmdStan’s num_chains arg to run parallel chains, which requires a model compiled with STAN_THREADS. Determined by the sample method.

property profile_files: list[str]#

List of paths to CmdStan profiler files.

property stdout_files: list[str]#

List of paths to transcript of CmdStan messages sent to the console. Transcripts include config information, progress, and error messages.

CompilerOptions#

class cmdstanpy.compilation.CompilerOptions(*, stanc_options=None, cpp_options=None, user_header=None)[source]#

User-specified flags for stanc and C++ compiler.

Parameters:
stanc_options - stanc compiler flags, options
cpp_options - makefile options
Type:

NAME=value

user_header - path to a user .hpp file to include during compilation
compose(filename_in_msg=None)[source]#

Format makefile options as list of strings.

Parameters:

filename_in_msg (str, optional) – filename to be displayed in stanc3 error messages (if different from actual filename on disk), by default None

Return type:

list[str]

validate()[source]#

Check compiler args. Raise ValueError if invalid options are found.

Return type:

None

validate_cpp_opts()[source]#

Check cpp compiler args. Raise ValueError if bad config is found.

Return type:

None

validate_stanc_opts()[source]#

Check stanc compiler args and consistency between stanc and C++ options. Raise ValueError if bad config is found.

Return type:

None

validate_user_header()[source]#

User header exists. Raise ValueError if bad config is found.

Return type:

None

property cpp_options: dict[str, bool | int]#

C++ compiler options.

property stanc_options: dict[str, bool | int | str | Iterable[str]]#

Stanc compiler options.

property user_header: str#

user header.

CmdStanArgs#

class cmdstanpy.cmdstan_args.CmdStanArgs(model_name, model_exe, chain_ids, method_args, data=None, seed=None, inits=None, output_dir=None, sig_figs=None, save_diagnostics=False, save_profile=False, refresh=None)[source]#

Container for CmdStan command line arguments. Consists of arguments common to all methods and and an object which contains the method-specific arguments.

Parameters:
compose_command(idx, csv_file, *, diagnostic_file=None, profile_file=None)[source]#

Compose CmdStan command for non-default arguments.

Parameters:
  • idx (int)

  • csv_file (str)

  • diagnostic_file (str | None)

  • profile_file (str | None)

Return type:

list[str]

validate()[source]#

Check arguments correctness and consistency.

  • input files must exist

  • output files must be in a writeable directory

  • if no seed specified, set random seed.

  • length of per-chain lists equals specified # of chains

Return type:

None

SamplerArgs#

class cmdstanpy.cmdstan_args.SamplerArgs(iter_warmup=None, iter_sampling=None, save_warmup=False, thin=None, max_treedepth=None, metric_type=None, metric_file=None, step_size=None, adapt_engaged=True, adapt_delta=None, adapt_init_phase=None, adapt_metric_window=None, adapt_step_size=None, fixed_param=False, num_chains=1)[source]#

Arguments for the NUTS adaptive sampler.

Parameters:
  • iter_warmup (int | None)

  • iter_sampling (int | None)

  • save_warmup (bool)

  • thin (int | None)

  • max_treedepth (int | None)

  • metric_type (str | None)

  • metric_file (str | list[str] | None)

  • step_size (float | list[float] | None)

  • adapt_engaged (bool)

  • adapt_delta (float | None)

  • adapt_init_phase (int | None)

  • adapt_metric_window (int | None)

  • adapt_step_size (int | None)

  • fixed_param (bool)

  • num_chains (int)

compose(idx, cmd)[source]#

Compose CmdStan command for method-specific non-default arguments.

Parameters:
Return type:

list[str]

validate(chains)[source]#

Check arguments correctness and consistency.

  • adaptation and warmup args are consistent

  • if file(s) for metric are supplied, check contents.

  • length of per-chain lists equals specified # of chains

Parameters:

chains (int | None)

Return type:

None

OptimizeArgs#

class cmdstanpy.cmdstan_args.OptimizeArgs(algorithm=None, init_alpha=None, iter=None, save_iterations=False, tol_obj=None, tol_rel_obj=None, tol_grad=None, tol_rel_grad=None, tol_param=None, history_size=None, jacobian=False)[source]#

Container for arguments for the optimizer.

Parameters:
  • algorithm (str | None)

  • init_alpha (float | None)

  • iter (int | None)

  • save_iterations (bool)

  • tol_obj (float | None)

  • tol_rel_obj (float | None)

  • tol_grad (float | None)

  • tol_rel_grad (float | None)

  • tol_param (float | None)

  • history_size (int | None)

  • jacobian (bool)

compose(_idx, cmd)[source]#

compose command string for CmdStan for non-default arg values.

Parameters:
Return type:

list[str]

validate(_chains=None)[source]#

Check arguments correctness and consistency.

Parameters:

_chains (int | None)

Return type:

None

LaplaceArgs#

class cmdstanpy.cmdstan_args.LaplaceArgs(mode, draws=None, jacobian=True)[source]#

Arguments needed for laplace method.

Parameters:
compose(_idx, cmd)[source]#

compose command string for CmdStan for non-default arg values.

Parameters:
Return type:

list[str]

validate(_chains=None)[source]#

Check arguments correctness and consistency.

Parameters:

_chains (int | None)

Return type:

None

PathfinderArgs#

class cmdstanpy.cmdstan_args.PathfinderArgs(init_alpha=None, tol_obj=None, tol_rel_obj=None, tol_grad=None, tol_rel_grad=None, tol_param=None, history_size=None, num_psis_draws=None, num_paths=None, max_lbfgs_iters=None, num_draws=None, num_elbo_draws=None, save_single_paths=False, psis_resample=True, calculate_lp=True)[source]#

Container for arguments for Pathfinder.

Parameters:
  • init_alpha (float | None)

  • tol_obj (float | None)

  • tol_rel_obj (float | None)

  • tol_grad (float | None)

  • tol_rel_grad (float | None)

  • tol_param (float | None)

  • history_size (int | None)

  • num_psis_draws (int | None)

  • num_paths (int | None)

  • max_lbfgs_iters (int | None)

  • num_draws (int | None)

  • num_elbo_draws (int | None)

  • save_single_paths (bool)

  • psis_resample (bool)

  • calculate_lp (bool)

compose(_idx, cmd)[source]#

compose command string for CmdStan for non-default arg values.

Parameters:
Return type:

list[str]

validate(_chains=None)[source]#

Check arguments correctness and consistency.

Parameters:

_chains (int | None)

Return type:

None

VariationalArgs#

class cmdstanpy.cmdstan_args.VariationalArgs(algorithm=None, iter=None, grad_samples=None, elbo_samples=None, eta=None, adapt_iter=None, adapt_engaged=True, tol_rel_obj=None, eval_elbo=None, output_samples=None)[source]#

Arguments needed for variational method.

Parameters:
  • algorithm (str | None)

  • iter (int | None)

  • grad_samples (int | None)

  • elbo_samples (int | None)

  • eta (float | None)

  • adapt_iter (int | None)

  • adapt_engaged (bool)

  • tol_rel_obj (float | None)

  • eval_elbo (int | None)

  • output_samples (int | None)

compose(idx, cmd)[source]#

Compose CmdStan command for method-specific non-default arguments.

Parameters:
Return type:

list[str]

validate(chains=None)[source]#

Check arguments correctness and consistency.

Parameters:

chains (int | None)

Return type:

None