As I understand how Stan works under the hood, constrained parameters are transformed into an unconstrained space during sampling and then transformed back into the original constrained space.
To access the parameters in the unconstrained space, you can look at Stan’s diagnostic_file
. I’ve been trying to understand how to generate and find this file when using PyStan? Or, is there another way to look at the unconstrained parameter iterations with PyStan?
I see RStan has a diagnostic_file
parameter, but I don’t see something similar for PyStan.
I have found an old forum post that points to how you can get diagnostic_file data from pystan2 (and also see it in the pystan2 docs), but cannot find the corresponding parameter in pystan3
In PyStan 3 you can calculate the unconstrained values for each draw manually.
import stan
import pandas as pd
posterior = stan.build(...)
fit = posterior.sample(...)
unconstrained_dicts = []
N = fit[fit.param_names[0]].shape[-1]
for n in range(N):
constrained_params = {param: fit[param][..., n].tolist() for param in posterior.param_names}
unconstrained_params = posterior.unconstrain_pars(constrained_params)
unconstrained_dict = dict(zip(posterior.constrained_param_names, unconstrained_params))
unconstrained_dicts.append(unconstrained_dict)
unconstrained_dataframe = pd.DataFrame(unconstrained_dicts)
Edit. Change : to …
1 Like
Thanks! I’ve been trying to get better at maneuvering the docs. Could you point me to where this is mentioned?
Probably not mentioned yet.
Here API doc, but one would still need to do more research
https://pystan.readthedocs.io/en/latest/reference.html#stan.model.Model.constrain_pars
Tests are something that will help