How to get draws from cmdstanpy.stanfit.CmdStanMCMC?

In colab I do:

import cmdstanpy


INFO:cmdstanpy:Install directory: /root/.cmdstanpy
INFO:cmdstanpy:Downloading CmdStan version 2.26.1
INFO:cmdstanpy:Download successful, file: /tmp/tmplpsqqk0x
INFO:cmdstanpy:Unpacked download as cmdstan-2.26.1

I then have no problem fitting my model, and I get a fit object:


I used to run fit.draws_pd(['epsilon_steps']) but this and everything else I’ve tried from the documentation (e.g. fit.draws() and fit.draws_as_dataframe()) don’t work.

thanks so much !!

1 Like

I don’t know exactly how cmdstanpy has evolved with each version, but you have 0.9.5 installed based on the output you posted and are using the docs for 0.9.65 based on the link you shared. I would recommend updating cmdstanpy. I believe you can do that in colab with !pip install -U cmdstanpy

Alternatively, using arviz.from_cmdstanpy may still work with 0.9.5 (I know it works on latest cmdstanpy, not sure about older versions) so you can get an InferenceData that can afterwards be converted to an array, dataframe or used as is.

1 Like

Thanks ! !pip install -U cmdstanpy worked and now I have version 0.9.75, which is what is in the latest documentation: cmdstanpy – Python interface to CmdStan — CmdStanPy 0.9.75 documentation

I still can’t get draws, does the documentation say how ? fit.draws() seems to work but not sure how to specify particular parameters…? I tried guessing fit.draws('epsilon_steps') but get TypeError: draws() takes 1 positional argument but 2 were given.

1 Like

Try draws_pd. See cmdstanpy/ at develop · stan-dev/cmdstanpy · GitHub



There are couple of ways accessing parameters

raw_draws = fit.draws()
dataframe_draws = fit.draws_pd()
all_parameters_nd = fit.stan_variables()
one_parameter_nd = fit.stan_variable("theta")

import arviz as az
inference_data = az.from_cmdstanpy(fit)

You can see all supported methods for fit by



just repeating what Ari said above, because it’s very clear and we will put this front and center in the CmdStanPy docs.

this is a valuable lesson for me, w/r/t the futility of trying to find good names for things.

the problem is really dealing with structured data in a 2-D, i.e., tabular/dataframe/tidy/database/flatland world

when you run a bunch of chains, the per-chain structure is needed to assess convergence and effective sample size; but if you have good convergence and proper ESS, then you just want the draws.

the draws method returns either draws by chains or flattened draws.

if you have a structured variable, i.e., vector, matrix, array of scalars, vectors, or matrices, we need convenience methods which return that variable with its structure, hence stan_variable and stan_variables. the intent is to make it easier to manipulate the variable, instead of having to deal with dataframe column names. but maybe this isn’t that useful.

1 Like