# Model averaging with pymc instead of pymc3

**URL:** <https://discourse.pymc.io/t/model-averaging-with-pymc-instead-of-pymc3/14866>\
**Category:** version agnostic\
**Created:** [July 31, 2024, 1:35pm UTC](https://discourse.pymc.io/t/model-averaging-with-pymc-instead-of-pymc3/14866 "2024-07-31T13:35:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![v\_p](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/v_p/32/5249_2.png) [@v\_p](https://discourse.pymc.io/u/v_p)\
**Post date:** [July 31, 2024, 1:35pm UTC](https://discourse.pymc.io/t/model-averaging-with-pymc-instead-of-pymc3/14866/1 "2024-07-31T13:35:32Z")

</div>

Hi guys again.  
I am checking out [the model averaging example](https://www.pymc.io/projects/examples/en/latest/diagnostics_and_criticism/model_averaging.html) but it is written with pymc3.  
I have adjusted the code after each sample of each of the example three models with e.g.

```python
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

```python
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

```python
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.

---

<div class="post-metadata">

**Author:** ![aloctavodia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aloctavodia/32/8642_2.png) [@aloctavodia](https://discourse.pymc.io/u/aloctavodia)\
**Post date:** [August 1, 2024, 7:51pm UTC](https://discourse.pymc.io/t/model-averaging-with-pymc-instead-of-pymc3/14866/2 "2024-08-01T19:51:57Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![aloctavodia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aloctavodia/32/8642_2.png) [@aloctavodia](https://discourse.pymc.io/u/aloctavodia)\
**Post date:** [August 1, 2024, 8:24pm UTC](https://discourse.pymc.io/t/model-averaging-with-pymc-instead-of-pymc3/14866/3 "2024-08-01T20:24:13Z")

</div>

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](https://github.com/pymc-devs/pymc-examples/pull/687)

---

<div class="post-metadata">

**Author:** ![v\_p](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/v_p/32/5249_2.png) [@v\_p](https://discourse.pymc.io/u/v_p)\
**Post date:** [August 3, 2024, 6:24am UTC](https://discourse.pymc.io/t/model-averaging-with-pymc-instead-of-pymc3/14866/4 "2024-08-03T06:24:00Z")

</div>

Thanks a lot. Help is always welcome!

---

<div class="post-metadata">

**Author:** ![v\_p](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/v_p/32/5249_2.png) [@v\_p](https://discourse.pymc.io/u/v_p)\
**Post date:** [August 3, 2024, 6:24am UTC](https://discourse.pymc.io/t/model-averaging-with-pymc-instead-of-pymc3/14866/5 "2024-08-03T06:24:44Z")

</div>

I will check out the example. Thanks again.
