Traceplot

bayesplot

#1

Hi,
At present, I have the distribution for mu[1,1], mu[1,2], mu[2,1], and mu[2,2]. As the figure shows, the distributions overlapped, and the frequencies are different. So the figures are users unfriendly. I wonder how can I draw the four different distributions separately?
Figure_2
I found a module named ‘arviz’ but it can only be used on Python 3.0. My python is 2.7.13, I don’t want to update it. I am using ‘fit.plot(‘mu’)’ now, how can I change the code to make the figures more user friendly?


#2

Plot items individually?

Test this

fit.plot(pars=fit.sim['fnames_oi'])

Ps. Large amount of PyData libraries are dropping python 2 support next January, so it is recommended to use python 3.

PyStan 3 is also Python 3 only library.


#3

Do you mean write in that way?


fit = pystan.stan(model_code=model,data=dat,iter=5000,warmup=500, chains=1,init_r=0.5)
print(fit)
plt.figure()
fit.plot('mu')
fit.plot(pars=fit.sim['fnames_oi'])

It gives me errors:
Traceback (most recent call last):
File “C:\Users\Administrator\Desktop\python\tm-plt.py”, line 117, in
fit.plot(pars=fit.sim[‘fnames_oi’])
File “stanfit4anon_model_e9f21cac235c409a3438cda2932fdc69_804104811.pyx”, line 518, in stanfit4anon_model_e9f21cac235c409a3438cda2932fdc69_804104811.StanFit4Model.plot
File “C:\Users\Administrator\Miniconda2\lib\site-packages\pystan\plots.py”, line 25, in traceplot
values = fit.extract(dtypes=dtypes, pars=pars, permuted=False)
File “stanfit4anon_model_e9f21cac235c409a3438cda2932fdc69_804104811.pyx”, line 633, in stanfit4anon_model_e9f21cac235c409a3438cda2932fdc69_804104811.StanFit4Model.extract
ValueError: u’theta[1]’ is not in list


#4

Thanks for testing.

Before PyStan 2.18, the plot function was broken by plotting permuted samples and couldn’t plot multivariate samples.

In 2.18 apparently there is still a bug in extract that somehow can not extract subparameters. This is currently fixed in the develop branch on Github.

Current master branch can use the provided code.

To fix your problem, either update PyStan to current develop

pip install git+https://github.com/stan-dev/pystan

or for quick fix you can do

This will create multiple figures (not axes).

from pystan.external.pymc.plots import traceplot
par = 'mu'
df = fit.to_dataframe(pars=par)
subd_df = df[[col for col in df.columns if par in col]]
for col, values in subd_df.iteritems():
    traceplot({col : values}, vars=[col])

Edit.

This will create one figure

from pystan.external.pymc.plots import traceplot
par = 'mu'
df = fit.to_dataframe(pars=par)
values_dict = {}
subd_df = df[[col for col in df.columns if par in col]]
for col, values in subd_df.iteritems():
    values_dict[col] = values
fig = traceplot(values_dict, vars=list(values_dict.keys()))