# Diagnostic\_file in PyStan

**URL:** <https://discourse.mc-stan.org/t/diagnostic-file-in-pystan/25936>\
**Category:** PyStan\
**Created:** [January 11, 2022, 9:43am UTC](https://discourse.mc-stan.org/t/diagnostic-file-in-pystan/25936 "2022-01-11T09:43:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Jenness](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/christopher_jenness/32/4741_2.png) [@Christopher\_Jenness](https://discourse.mc-stan.org/u/Christopher_Jenness)\
**Post date:** [January 11, 2022, 9:43am UTC](https://discourse.mc-stan.org/t/diagnostic-file-in-pystan/25936/1 "2022-01-11T09:43:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Christopher\_Jenness](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/christopher_jenness/32/4741_2.png) [@Christopher\_Jenness](https://discourse.mc-stan.org/u/Christopher_Jenness)\
**Post date:** [January 11, 2022, 9:55am UTC](https://discourse.mc-stan.org/t/diagnostic-file-in-pystan/25936/2 "2022-01-11T09:55:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [January 12, 2022, 10:35am UTC](https://discourse.mc-stan.org/t/diagnostic-file-in-pystan/25936/3 "2022-01-12T10:35:42Z")

</div>

In PyStan 3 you can calculate the unconstrained values for each draw manually.

```python
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 …

---

<div class="post-metadata">

**Author:** ![Christopher\_Jenness](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/christopher_jenness/32/4741_2.png) [@Christopher\_Jenness](https://discourse.mc-stan.org/u/Christopher_Jenness)\
**Post date:** [January 12, 2022, 11:06am UTC](https://discourse.mc-stan.org/t/diagnostic-file-in-pystan/25936/4 "2022-01-12T11:06:14Z")

</div>

Thanks! I’ve been trying to get better at maneuvering the docs. Could you point me to where this is mentioned?

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [January 12, 2022, 11:33am UTC](https://discourse.mc-stan.org/t/diagnostic-file-in-pystan/25936/5 "2022-01-12T11:33:31Z")

</div>

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](https://pystan.readthedocs.io/en/latest/reference.html#stan.model.Model.constrain_pars)

Tests are something that will help

> <https://github.com/stan-dev/pystan/blob/main/tests/test_unconstrain_pars.py>
