The following Python code assumes the usage of pystan 2. However, when running it with pystan 3, it raises an error stating that algorithm='Fixed_param' is unknown. In the case of pystan 3, what modifications are required apart from changing import pystan to import stan and pystan.StanModel to stan.build?
import stan
with open('ben_files.stan', 'r') as f:
ben = f.read()
data_list = {'n': 100, 'p': M, 'c': N, 'k': r}
posterior = stan.build(model_code=ben)
fit = posterior.sample(data=data_list)
What other modifications are necessary for using pystan 3 instead of pystan 2, apart from the ones mentioned above (import statements and replacing pystan.StanModel with stan.build)?
The PyStan docs provide a useful "Upgrading to v3" document here. The "Notable changes" section is particularly helpful, which I duplicate here for convenience (with some minor rewording for clarity):
Use
import staninstead ofimport pystan.dataandrandom_seedmust now be passed at compile time, to the.build()method. Previously these values were passed at.sampling()time.Use
num_samplesto indicate number of desired draws, instead ofiter.Use
fit["param"]instead offit.extract()["param"]. The shape of the array returned will be different.Draws are returned in a shape which reflects their shape in the Stan model. Number of draws is the trailing index.
The "stansummary" display is no longer supported. Support for displaying effective sample size is planned. In the meanwhile, you may wish to look into the
arvizpackage, in particular, notearviz.summary(), which still reports ESS (bulk and tail) and Rhat.The
check_hmc_diagnostics()function is removed. Support for plugins has been added to allow for the development of a replacement. The function was removed from PyStan because it is not part of the Stan C++ library.Microsoft Windows is not supported in PyStan3. It was (partially) supported in PyStan 2.
The default, recommended HMC sampler is fully supported. Variational inference, maximization algorithms, and other sampling algorithms are not supported. Users who need these features should consider using different software (e.g., CmdStan, CmdStanPy, jax, PyTorch).
I also found there were a number of changes I needed to make when migrating from PyStan2 to PyStan3, which weren't indicated in the "Notable changes" section, namely:
The
.sampling()method has been replaced by.sample()(you've already made this change in your example code)The
.StanModel()function has been replaced by.build()(again, you've already noted this one in your example).Values in the
datadictionary passed to.build()can no longer bepandas.Seriesobjects (use.to_numpy()method in any data preparation steps)Models can no longer be passed as filepaths at the compile step, and instead have to be loaded separately and passed as a string (ie. the
fileargument ofStanModel()has not been ported to.build())Introduction of
asyncioevents breaks PyStan in Jupyter Notebooks. If you wish to use PyStan3 in Jupyter notebooks you should use nest-asyncio. This is documented in this FAQ.