How to run generate quantities with PyStan

I am learning about posterior predictive inference from this page. The author uses CmdStan, but I am using pystan, and it is already confusing to see two interfaces, but I try to ignore this fact.

The author uses the method generate_quantities. To see the context:

model = csp.CmdStanModel(
     stan_file = '../stan/iris-posterior-predictive-sim.stan')
pps_sample = model.generate_quantities(data = data, seed = 123,
                                       previous_fit = sample,
                                       show_console = False)

How can I replicate the code with PyStan?

  • Ubuntu
  • Python 3.9
  • PyStan 3.7

You need to create a Stan model with data containing needed parts and generated quantities block and then call API Reference — pystan 3.7.0 documentation

have you considered using CmdStanPy instead of PyStan?
it’s pretty straightforward - Generating new quantities of interest. — CmdStanPy 1.1.0 documentation

I thought it’d be possible to separate the generated quantities from the model file, which allows, I think, to make predictions without recompiling the model.

Thank you!

Thank you for the suggestion. In fact, the first version conda downloaded was PyStan 2.x, which didn’t work. Then, I realized I had to install Stan with pip, and it installed PyStan 3.x, which has worked well so far.

The problem with having CmdStan and PyStan is not whether it is hard to use or not, but the uncertainty you are in. Why two interfaces? If I switch to CmdStan, will I regret some functionality I had in PyStan? and, what if one version misses something the other has, and the other way around? I recently started learning Bayesian statistics for my job, and I have many other things I must pay attention to, so this is an additional unwanted distraction. I’d like to know the difference, and a quick google search didn’t yield much.

Don’t take it bad, I thank the Stan community for releasing this amazing software I am enjoying for free; online interaction lacks the facial feedback we need to have healthy conversations.

I wrote up the differences a while ago, which I believe are mostly still the situation:

1 Like

Thank you, this is useful.

It is worth noticing that CmdStanPy works in VSCode interactive mode, while StanPy not.

1 Like