Skip to content

Commit 5706f59

Browse files
percevalveSeverin Hatt
and
Severin Hatt
authored
Re-running GLM-hierarchical-binomial-model.ipynb (pymc-devs#355)
* Re-running GLM-hierarchical-binomial-model.ipynb * Adding figure * Pre-commit Md update * Remove tags in header * Pre-commit Md update * Adding back tags Co-authored-by: Severin Hatt <[email protected]>
1 parent d8baf4a commit 5706f59

File tree

2 files changed

+95
-90
lines changed

2 files changed

+95
-90
lines changed

examples/generalized_linear_models/GLM-hierarchical-binomial-model.ipynb

+82-75
Large diffs are not rendered by default.

myst_nbs/generalized_linear_models/GLM-hierarchical-binomial-model.myst.md

+13-15
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,35 @@ jupytext:
66
format_version: 0.13
77
jupytext_version: 1.13.7
88
kernelspec:
9-
display_name: Python PyMC3 (Dev)
9+
display_name: Python 3 (ipykernel)
1010
language: python
1111
name: python3
1212
---
1313

1414
# Hierarchical Binomial Model: Rat Tumor Example
1515
:::{post} Nov 11, 2021
16-
:tags: generalized linear model, hierarchical model, pymc3.Beta, pymc3.Binomial, pymc3.Data, pymc3.Deterministic, pymc3.HalfNormal, pymc3.Model, pymc3.Potential
16+
:tags: generalized linear model, hierarchical model
1717
:category: intermediate
1818
:author: Demetri Pananos, Junpeng Lao, Raúl Maldonado, Farhan Reynaldo
1919
:::
2020

2121
```{code-cell} ipython3
22+
import aesara.tensor as at
2223
import arviz as az
2324
import matplotlib.pyplot as plt
2425
import numpy as np
2526
import pandas as pd
26-
import pymc3 as pm
27-
import theano.tensor as tt
27+
import pymc as pm
2828
2929
from scipy.special import gammaln
30-
31-
print(f"Running on PyMC3 v{pm.__version__}")
3230
```
3331

3432
```{code-cell} ipython3
3533
%config InlineBackend.figure_format = 'retina'
3634
az.style.use("arviz-darkgrid")
3735
```
3836

39-
This short tutorial demonstrates how to use PyMC3 to do inference for the rat tumour example found in chapter 5 of *Bayesian Data Analysis 3rd Edition* {cite:p}`gelman2013bayesian`. Readers should already be familiar with the PyMC3 API.
37+
This short tutorial demonstrates how to use PyMC to do inference for the rat tumour example found in chapter 5 of *Bayesian Data Analysis 3rd Edition* {cite:p}`gelman2013bayesian`. Readers should already be familiar with the PyMC API.
4038

4139
Suppose we are interested in the probability that a lab rat develops endometrial stromal polyps. We have data from 71 previously performed trials and would like to use this data to perform inference.
4240

@@ -76,7 +74,7 @@ p(\alpha, \beta)
7674

7775
See BDA3 pg. 110 for a more information on the deriving the marginal posterior distribution. With a little determination, we can plot the marginal posterior and estimate the means of $\alpha$ and $\beta$ without having to resort to MCMC. We will see, however, that this requires considerable effort.
7876

79-
The authors of BDA3 choose to plot the surface under the parameterization $(\log(\alpha/\beta), \log(\alpha+\beta))$. We do so as well. Through the remainder of the example let $x = \log(\alpha/\beta)$ and $z = \log(\alpha+\beta)$.
77+
The authors of BDA3 choose to plot the surface under the parameterization $(\log(\alpha/\beta), \log(\alpha+\beta))$. We do so as well. Through the remainder of the example let $x = \log(\alpha/\beta)$ and $z = \log(\alpha+\beta)$.
8078

8179
```{code-cell} ipython3
8280
# rat data (BDA3, p. 102)
@@ -196,11 +194,11 @@ $$ \operatorname{E}(\beta \lvert y) \text{ is estimated by }
196194
(df.beta * df.normed_exp_trans).sum().round(3)
197195
```
198196

199-
## Computing the Posterior using PyMC3
197+
## Computing the Posterior using PyMC
200198

201199
Computing the marginal posterior directly is a lot of work, and is not always possible for sufficiently complex models.
202200

203-
On the other hand, creating hierarchical models in PyMC3 is simple. We can use the samples obtained from the posterior to estimate the means of $\alpha$ and $\beta$.
201+
On the other hand, creating hierarchical models in PyMC is simple. We can use the samples obtained from the posterior to estimate the means of $\alpha$ and $\beta$.
204202

205203
```{code-cell} ipython3
206204
coords = {
@@ -212,7 +210,7 @@ coords = {
212210
```{code-cell} ipython3
213211
def logp_ab(value):
214212
"""prior density"""
215-
return tt.log(tt.pow(tt.sum(value), -5 / 2))
213+
return at.log(at.pow(at.sum(value), -5 / 2))
216214
217215
218216
with pm.Model(coords=coords) as model:
@@ -221,13 +219,13 @@ with pm.Model(coords=coords) as model:
221219
ab = pm.HalfNormal("ab", sigma=10, dims="param")
222220
pm.Potential("p(a, b)", logp_ab(ab))
223221
224-
X = pm.Deterministic("X", tt.log(ab[0] / ab[1]))
225-
Z = pm.Deterministic("Z", tt.log(tt.sum(ab)))
222+
X = pm.Deterministic("X", at.log(ab[0] / ab[1]))
223+
Z = pm.Deterministic("Z", at.log(at.sum(ab)))
226224
227225
theta = pm.Beta("theta", alpha=ab[0], beta=ab[1], dims="obs_id")
228226
229227
p = pm.Binomial("y", p=theta, observed=y, n=n_val)
230-
trace = pm.sample(draws=1000, tune=2000, target_accept=0.95, return_inferencedata=True)
228+
trace = pm.sample(draws=1000, tune=2000, target_accept=0.95)
231229
```
232230

233231
```{code-cell} ipython3
@@ -254,7 +252,7 @@ trace.posterior["ab"].mean(("chain", "draw"))
254252

255253
## Conclusion
256254

257-
Analytically calculating statistics for posterior distributions is difficult if not impossible for some models. PyMC3 provides an easy way drawing samples from your model's posterior with only a few lines of code. Here, we used PyMC3 to obtain estimates of the posterior mean for the rat tumor example in chapter 5 of BDA3. The estimates obtained from PyMC3 are encouragingly close to the estimates obtained from the analytical posterior density.
255+
Analytically calculating statistics for posterior distributions is difficult if not impossible for some models. PyMC provides an easy way drawing samples from your model's posterior with only a few lines of code. Here, we used PyMC to obtain estimates of the posterior mean for the rat tumor example in chapter 5 of BDA3. The estimates obtained from PyMC are encouragingly close to the estimates obtained from the analytical posterior density.
258256

259257
+++
260258

0 commit comments

Comments
 (0)