Hi everyone.
In pymc3 it is easy to iterate over the MultiTrace object using:
for cnt, sample in enumerate(self.trace[burn_in:]):
Is there a similar way to perform that operation in pymc5 ?
I am currently getting an
/miniconda3/envs/pymc_env5/lib/python3.11/site-packages/pymc/backends/base.py", line 366, in __iter__
raise NotImplementedError
NotImplementedError
I checked and in both pymc versions the MultiTrace objects have the same structure.
Thanks in advance!
Found an answer that is helpful here:
opened 11:52PM - 28 Jun 21 UTC
closed 05:52AM - 13 Sep 21 UTC
bug
beginner friendly
In the code below, I run a simple example model (the details of the model don't … seem relevant to the problem). There should be 2000 draws, so why does the array `xvars` have only 1000? In contrast, tracetab.trace_to_dataframe results in a dataframe with 2000 rows, as I would expect. My system details are:
Numpy 1.16.3
PyMC3 3.11.0
Theano 1.1.0
sys.version_info(major=3, minor=7, micro=3, releaselevel='final', serial=0)
```
import sys
import numpy as np
import pymc3
from pymc3.distributions.discrete import Poisson, Categorical
from pymc3.sampling import sample
from pymc3 import Model
from pymc3.backends import tracetab
import theano
def run_MCMC(n, obs, draws):
obs = np.array(obs)
with Model() as model:
pn = np.ones(n) / n
x = Categorical('x', pn, testval=0)
sfs_obs = Poisson('sfs_obs', mu=x + 1, observed=obs)
with model:
trace = sample(draws, tune=0, return_inferencedata=False)
return trace
print('Numpy ', np.__version__)
print('PyMC3 ', pymc3.__version__)
print('Theano ', theano.__version__)
print(sys.version_info)
obs = 3
draws = 1000
n = 10
trace = run_MCMC(n, obs, draws)
xvars = [t['x'] for t in trace]
print(len(xvars))
print(xvars[:10])
trace_df = tracetab.trace_to_dataframe(trace)
print(trace_df.shape)
```
Output is:
Numpy 1.16.3
PyMC3 3.11.0
Theano 1.1.0
sys.version_info(major=3, minor=7, micro=3, releaselevel='final', serial=0)
Multiprocess sampling (2 chains in 2 jobs)
CategoricalGibbsMetropolis: [x]
100.00% [2000/2000 00:02<00:00 Sampling 2 chains, 0 divergences]
Sampling 2 chains for 0 tune and 1_000 draw iterations (0 + 2_000 draws total) took 11 seconds.
1000
[3, 6, 7, 0, 7, 4, 1, 1, 1, 1]
(2000, 1)
Keep in mind that with more recent versions of PyMC you are dealing with ArviZ InferenceData
objects instead of raw MultiTrace
objects. So you would be doing something like this:
for val in trace.posterior['m'].sel(chain=0)[:10]:
...
Details about InferenceData
objects are here .