Skip to content

Commit

Permalink
Extras update (#752)
Browse files Browse the repository at this point in the history
* Draft update of BNN notebook

* Pre-commit fixes

* Address reviewer comments

* Additional edits

* Removed bivariate example; updated formatting

* Updated GEV

* Changed pymc_experimental to pymc_extras

---------

Co-authored-by: Chris Fonnesbeck <[email protected]>
  • Loading branch information
fonnesbeck and Chris Fonnesbeck authored Dec 14, 2024
1 parent a3f03a0 commit 0b5594e
Show file tree
Hide file tree
Showing 12 changed files with 6,213 additions and 671 deletions.
176 changes: 78 additions & 98 deletions examples/case_studies/GEV.ipynb

Large diffs are not rendered by default.

10 changes: 3 additions & 7 deletions examples/case_studies/GEV.myst.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ jupytext:
format_name: myst
format_version: 0.13
kernelspec:
display_name: pymc4-dev
display_name: default
language: python
name: pymc4-dev
name: python3
---

# Generalized Extreme Value Distribution
Expand Down Expand Up @@ -42,7 +42,7 @@ import arviz as az
import matplotlib.pyplot as plt
import numpy as np
import pymc as pm
import pymc_experimental.distributions as pmx
import pymc_extras.distributions as pmx
import pytensor.tensor as pt
from arviz.plots import plot_utils as azpu
Expand Down Expand Up @@ -230,7 +230,3 @@ az.plot_pair(idata, var_names=["μ", "σ", "ξ"], kind="kde", marginals=True, di
%load_ext watermark
%watermark -n -u -v -iv -w -p pytensor,arviz
```

```{code-cell} ipython3
```
565 changes: 236 additions & 329 deletions examples/howto/marginalizing-models.ipynb

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions examples/howto/marginalizing-models.myst.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ jupytext:
format_name: myst
format_version: 0.13
kernelspec:
display_name: pymc-dev
display_name: default
language: python
name: pymc-dev
name: python3
myst:
substitutions:
extra_dependencies: pymc-experimental
Expand Down Expand Up @@ -53,7 +53,7 @@ import pytensor.tensor as pt
:::

```{code-cell} ipython3
import pymc_experimental as pmx
import pymc_extras as pmx
```

```{code-cell} ipython3
Expand Down Expand Up @@ -84,7 +84,7 @@ with pmx.MarginalModel() as explicit_mixture:
plt.hist(pm.draw(y, draws=2000, random_seed=rng), bins=30, rwidth=0.9);
```

The other way is where we use the built-in {class}`NormalMixture <pymc.NormalMixture>` distribution. Here the mixture assignment is not an explicit variable in our model. There is nothing unique about the first model other than we initialize it with {class}`pmx.MarginalModel <pymc_experimental.MarginalModel>` instead of {class}`pm.Model <pymc.model.core.Model>`. This different class is what will allow us to marginalize out variables later.
The other way is where we use the built-in {class}`NormalMixture <pymc.NormalMixture>` distribution. Here the mixture assignment is not an explicit variable in our model. There is nothing unique about the first model other than we initialize it with {class}`pmx.MarginalModel <pymc_extras.MarginalModel>` instead of {class}`pm.Model <pymc.model.core.Model>`. This different class is what will allow us to marginalize out variables later.

```{code-cell} ipython3
with pm.Model() as prebuilt_mixture:
Expand Down Expand Up @@ -121,7 +121,7 @@ az.summary(idata)

As we can see, the `idx` variable is gone now. We also were able to use the NUTS sampler, and the ESS has improved.

But {class}`MarginalModel <pymc_experimental.MarginalModel>` has a distinct advantage. It still knows about the discrete variables that were marginalized out, and we can obtain estimates for the posterior of `idx` given the other variables. We do this using the {meth}`recover_marginals <pymc_experimental.MarginalModel.recover_marginals>` method.
But {class}`MarginalModel <pymc_extras.MarginalModel>` has a distinct advantage. It still knows about the discrete variables that were marginalized out, and we can obtain estimates for the posterior of `idx` given the other variables. We do this using the {meth}`recover_marginals <pymc_extras.MarginalModel.recover_marginals>` method.

```{code-cell} ipython3
explicit_mixture.recover_marginals(idata, random_seed=rng);
Expand Down
Loading

0 comments on commit 0b5594e

Please sign in to comment.