Model averaging with pymc instead of pymc3

Hi guys again.
I am checking out the model averaging example but it is written with pymc3.
I have adjusted the code after each sample of each of the example three models with e.g.

trace_0.extend(pm.sample_prior_predictive(samples=2000, random_seed=58))
pm.sample_posterior_predictive(trace_0, extend_inferencedata=True, random_seed=58)
pm.compute_log_likelihood(trace_0)

used the traces with arviz compare to obtain the weight list and instead of pm.sample_posterior_predictive_w I am using

ppc_w = az.weight_predictions(
    [trace_0,trace_1,trace_2],
    weights=list(comp.weight.values)
)

as it is suggested in Ch.5 of Bayesian Analysis with Python (3rd Edition). Is this a proper adjustment?
But i got stuck in adjusting

mean_w = ppc_w["kcal"].mean() 
hpd_w = az.hdi(ppc_w["kcal"].flatten())

How can i adjust above lines of code to my pymc version (5.16.2)?
Is there an example of model averaging with newer versions of pymc instead of pymc3?
Thanks in advance.

Hi @v_p

I will update the example in the pymc docs. In the meantime notice that ppc_w is a idata object not an array, so you will need to do something like:

mean_w = ppc_w.posterior_predictive["kcal"].mean()
hpd_w = az.hdi(ppc_w.posterior_predictive["kcal"].values.flatten()

I have updated the example, still not available on the webpage but you can take a look here update model averaging example to pymc 5.x by aloctavodia · Pull Request #687 · pymc-devs/pymc-examples · GitHub

Thanks a lot. Help is always welcome!

I will check out the example. Thanks again.