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

User warning when using single chain #2202

Closed hadjipantelis closed 1 year ago

hadjipantelis commented 1 year ago

Thank you for your work in ArviZ, it amazing helpful.

When sampling and using a single chain (chains=1), I get an UserWarning from arviz that I have more chains than draws. The warning does not existing if I use more chains (e.g. chains=2). (Oddly I don't load arviz explicitly so apologies if this this a pymc issue.)

(Taken from: https://nbviewer.org/github/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/blob/master/Chapter2_MorePyMC/Ch2_MorePyMC_PyMC3.ipynb)

import scipy.stats as stats
import arviz
import pymc as pm

true_p_A = 0.05
true_p_B = 0.04

N_A = 1500
N_B = 750

observations_A = stats.bernoulli.rvs(true_p_A, size=N_A)
observations_B = stats.bernoulli.rvs(true_p_B, size=N_B)

with pm.Model() as model_w:
    p_A = pm.Uniform("p_A", 0, 1)
    p_B = pm.Uniform("p_B", 0, 1)

    delta = pm.Deterministic("delta", p_A - p_B) 

    obs_A = pm.Bernoulli("obs_A", p_A, observed=observations_A)
    obs_B = pm.Bernoulli("obs_B", p_B, observed=observations_B)

    step = pm.Metropolis()
    trace = pm.sample(draws=20000, step=step, chains=1)

Gives out a message and warning like: Sampling 1 chain for 1_000 tune and 20_000 draw iterations (1_000 + 20_000 draws total) took 11 seconds. /home/pzh3000/anaconda3/envs/bmbgame/lib/python3.9/site-packages/arviz/data/base.py:221: UserWarning: More chains (20000) than draws (2). Passed array should have shape (chains, draws, *shape) If I set chains=2 (or 3 or 4) I get no warnings. (as expected)

Additional context Python implementation: CPython Python version : 3.9.16 IPython version : 8.5.0

pymc : 5.0.2 scipy : 1.9.3 sys : 3.9.16 (main, Jan 11 2023, 16:05:54) [GCC 11.2.0] arviz : 0.14.0 matplotlib: 3.5.2 pytensor : 2.9.1 numpy : 1.23.5

I can reproduce the issue on Ubuntu 20.04.5 LTS and on MacOS 12.6.1 so I doubt this is OS-dependant.

OriolAbril commented 1 year ago

Does this still happen with the development version of pymc? I think it was fixed recently but that change hasn't been released yet

hadjipantelis commented 1 year ago

I haven't checked it against dev versions, just stable in pypi. (I wouldn't open an issue for "dev" version behaviour) I will wait for the release after 5.0.2, no worries, thanks for the reply @OriolAbril.

ahartikainen commented 1 year ago

This is a bug in pymc not in ArviZ, we should raise an issue there if this has not been fixed already.

ArviZ is raising the warning as it should.