arviz-devs / arviz

Exploratory analysis of Bayesian models with Python
https://python.arviz.org
Apache License 2.0
1.59k stars 393 forks source link

R hat not showing in plot_forest? #2039

Open cluhmann opened 2 years ago

cluhmann commented 2 years ago

Describe the bug When plot_forest is called with pymc-created InferenceData and r_hat=True, a subplot is created but the R hat values are not visible.

To Reproduce

import pymc as pm
import arviz as az
import matplotlib.pyplot as plt
with pm.Model() as model:
    a = pm.Normal("a")
    obs = pm.Normal("obs", mu=a, observed=[2])
    idata = pm.sample()
print(az.summary(idata))
az.plot_forest(idata, r_hat=True)
plt.show()

The summary looks ok:

    mean     sd  hdi_3%  hdi_97%  mcse_mean  mcse_sd  ess_bulk  ess_tail  r_hat
a  0.983  0.678  -0.296    2.241       0.02    0.014    1139.0    1436.0    1.0

But no R-hat is visible: Figure_1

Linux arviz : 0.12.1 pymc : 4.0.0b6

OriolAbril commented 2 years ago

I think it has to do with https://github.com/arviz-devs/arviz/blob/main/arviz/plots/backends/matplotlib/forestplot.py#L646 (and same/similar repeated line in bokeh which is broken too) and its interaction with the skip_dim/combine_dim changes. Will have to track it down better and see how to fix it

Alexander-Serov commented 2 years ago

I am having the same issue with pymc 3.11.4, unexpected indeed because I had the rhat values plotted before for more or less the same code.

I was wondering whether someone has been able to find a hotfix? Do you think downgrading to a previous version could work for the moment?

Also, just curious, @cluhmann what did you use to print the summary?

cluhmann commented 2 years ago

what did you use to print the summary?

It's in the example: print(az.summary(idata))

aloctavodia commented 1 year ago

@ndmlny-qs This is the issue I mentioned today. Essentially we should have as many "point-intervals" as chains, but only one ESS and R-hat per parameter. The issue is that the value variable that is passed to _ess and _rhat is a 1d vector https://github.com/arviz-devs/arviz/blob/main/arviz/plots/backends/matplotlib/forestplot.py#L635-L648 instead i.e. the chains have been split.

If you do az.plot_forest(..., ess=true, r_hat=True, combined=True) then you will see that the ess and r_hat are correctly computed and plotted.