1

I am learning Bayesian inference by the book Bayesian Analysis with Python. However, when using plot_ppc, I got AttributeError and the warning

/usr/local/Caskroom/miniconda/base/envs/kaggle/lib/python3.9/site-packages/pymc3/sampling.py:1689: UserWarning: samples parameter is smaller than nchains times ndraws, some draws and/or chains may not be represented in the returned posterior predictive sample warnings.warn(

The model is

shift = pd.read_csv('../data/chemical_shifts.csv')

with pm.Model() as model_g:
    μ = pm.Uniform('μ', lower=40, upper=70)
    σ = pm.HalfNormal('σ', sd=10)
    y = pm.Normal('y', mu=μ, sd=σ, observed=shift)
    trace_g = pm.sample(1000, return_inferencedata=True)

If I used the following codes

with model_g:
    y_pred_g = pm.sample_posterior_predictive(trace_g, 100, random_seed=123)
    data_ppc = az.from_pymc3(trace_g.posterior, posterior_predictive=y_pred_g) # 'Dataset' object has no attribute 'report'

I got 'Dataset' object has no attribute 'report'.

If I used the following codes

with model_g:
    y_pred_g = pm.sample_posterior_predictive(trace_g, 100, random_seed=123)
    data_ppc = az.from_pymc3(trace_g, posterior_predictive=y_pred_g) # AttributeError: 'InferenceData' object has no attribute 'report'

I got AttributeError: 'InferenceData' object has no attribute 'report'.

ArviZ version: 0.11.2 PyMC3 Version: 3.11.2 Aesara/Theano Version: 1.1.2 Python Version: 3.9.6 Operating system: MacOS Big Sur How did you install PyMC3: conda

merv
  • 67,214
  • 13
  • 180
  • 245
ivaquero
  • 69
  • 6

1 Answers1

4

You are passing return_inferancedata=True to pm.sample(), which according to the PyMC3 documentation will return an InferenceData object rather than a MultiTrace object.

return_inferencedatabool, default=False

    Whether to return the trace as an arviz.InferenceData (True) object or a MultiTrace (False) Defaults to False, but we’ll switch to True in an upcoming release.

The from_pymc3 function, however, expects a MultiTrace object.

The good news is that from_pymc3 returns an InferenceData object, so you can solve this in one of two ways:

  1. The easiest solution is to simply remove the from_pymc3 calls, since it returns InferenceData, which you already have due to return_inferencedata=True.
  2. Set return_inferencedata=False (you can also remove that argument, but the documentation states that in the future it will default to True, so to be future proof it's best to explicitly set it to False). This will return a MultiTrace which can be passed to from_pymc3.
Nikolas Stevenson-Molnar
  • 4,235
  • 1
  • 22
  • 31