You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
40
38
41
39
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.
42
40
@@ -76,7 +74,7 @@ p(\alpha, \beta)
76
74
77
75
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.
78
76
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)$.
80
78
81
79
```{code-cell} ipython3
82
80
# rat data (BDA3, p. 102)
@@ -196,11 +194,11 @@ $$ \operatorname{E}(\beta \lvert y) \text{ is estimated by }
196
194
(df.beta * df.normed_exp_trans).sum().round(3)
197
195
```
198
196
199
-
## Computing the Posterior using PyMC3
197
+
## Computing the Posterior using PyMC
200
198
201
199
Computing the marginal posterior directly is a lot of work, and is not always possible for sufficiently complex models.
202
200
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$.
204
202
205
203
```{code-cell} ipython3
206
204
coords = {
@@ -212,7 +210,7 @@ coords = {
212
210
```{code-cell} ipython3
213
211
def logp_ab(value):
214
212
"""prior density"""
215
-
return tt.log(tt.pow(tt.sum(value), -5 / 2))
213
+
return at.log(at.pow(at.sum(value), -5 / 2))
216
214
217
215
218
216
with pm.Model(coords=coords) as model:
@@ -221,13 +219,13 @@ with pm.Model(coords=coords) as model:
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.
0 commit comments