Skip to content

Commit 41d03df

Browse files
author
Benjamin T. Vincent
authored
update GLM Poisson example to v4 (#335)
1 parent c9aa429 commit 41d03df

File tree

2 files changed

+224
-305
lines changed

2 files changed

+224
-305
lines changed

examples/generalized_linear_models/GLM-poisson-regression.ipynb

+199-289
Large diffs are not rendered by default.

myst_nbs/generalized_linear_models/GLM-poisson-regression.myst.md

+25-16
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ jupytext:
66
format_version: 0.13
77
jupytext_version: 1.13.7
88
kernelspec:
9-
display_name: Python 3
9+
display_name: pymc_env
1010
language: python
11-
name: python3
11+
name: pymc_env
1212
---
1313

1414
+++ {"papermill": {"duration": 0.043172, "end_time": "2021-02-23T11:26:55.064791", "exception": false, "start_time": "2021-02-23T11:26:55.021619", "status": "completed"}, "tags": []}
@@ -30,14 +30,10 @@ import bambi as bmb
3030
import matplotlib.pyplot as plt
3131
import numpy as np
3232
import pandas as pd
33-
import pymc3 as pm
33+
import pymc as pm
3434
import seaborn as sns
35-
import theano
36-
import xarray as xr
3735
3836
from formulae import design_matrices
39-
40-
print(f"Running on PyMC3 v{pm.__version__}")
4137
```
4238

4339
```{code-cell} ipython3
@@ -301,10 +297,15 @@ with pm.Model() as mdl_fish:
301297
b3 = pm.Normal("alcohol:nomeds", mu=0, sigma=10)
302298
303299
# define linear model and exp link function
304-
theta = b0 + b1 * mx_ex["alcohol"] + b2 * mx_ex["nomeds"] + b3 * mx_ex["alcohol:nomeds"]
300+
theta = (
301+
b0
302+
+ b1 * mx_ex["alcohol"].values
303+
+ b2 * mx_ex["nomeds"].values
304+
+ b3 * mx_ex["alcohol:nomeds"].values
305+
)
305306
306307
## Define Poisson likelihood
307-
y = pm.Poisson("y", mu=np.exp(theta), observed=mx_en["nsneeze"].values)
308+
y = pm.Poisson("y", mu=pm.math.exp(theta), observed=mx_en["nsneeze"].values)
308309
```
309310

310311
+++ {"papermill": {"duration": 0.049445, "end_time": "2021-02-23T11:27:35.720870", "exception": false, "start_time": "2021-02-23T11:27:35.671425", "status": "completed"}, "tags": []}
@@ -322,7 +323,8 @@ papermill:
322323
tags: []
323324
---
324325
with mdl_fish:
325-
inf_fish = pm.sample(1000, tune=1000, cores=4, return_inferencedata=True)
326+
inf_fish = pm.sample()
327+
# inf_fish.extend(pm.sample_posterior_predictive(inf_fish))
326328
```
327329

328330
+++ {"papermill": {"duration": 0.118023, "end_time": "2021-02-23T11:29:24.142987", "exception": false, "start_time": "2021-02-23T11:29:24.024964", "status": "completed"}, "tags": []}
@@ -443,7 +445,7 @@ papermill:
443445
status: completed
444446
tags: []
445447
---
446-
inf_fish_alt = model.fit(draws=2000, tune=2000)
448+
inf_fish_alt = model.fit()
447449
```
448450

449451
+++ {"papermill": {"duration": 0.075564, "end_time": "2021-02-23T11:31:30.375433", "exception": false, "start_time": "2021-02-23T11:31:30.299869", "status": "completed"}, "tags": []}
@@ -480,21 +482,28 @@ az.summary(np.exp(inf_fish_alt.posterior), kind="stats")
480482
+ Note that the posterior predictive samples have an extreme skew
481483

482484
```{code-cell} ipython3
483-
model.posterior_predictive(inf_fish_alt, draws=None)
485+
:tags: []
486+
487+
posterior_predictive = model.predict(inf_fish_alt, kind="pps")
484488
```
485489

486490
We can use `az.plot_ppc()` to check that the posterior predictive samples are similar to the observed data.
487491

488492
For more information on posterior predictive checks, we can refer to https://docs.pymc.io/notebooks/posterior_predictive.html.
489493

490494
```{code-cell} ipython3
491-
az.plot_ppc(inf_fish_alt, num_pp_samples=200);
495+
az.plot_ppc(inf_fish_alt);
492496
```
493497

494498
+++ {"papermill": {"duration": 0.106366, "end_time": "2021-02-23T11:31:34.956844", "exception": false, "start_time": "2021-02-23T11:31:34.850478", "status": "completed"}, "tags": []}
495499

496-
---
497-
Example originally contributed by Jonathan Sedar 2016-05-15 [github.com/jonsedar](https://github.com/jonsedar)
500+
## Authors
501+
- Example originally contributed by Jonathan Sedar 2016-05-15 [github.com/jonsedar](https://github.com/jonsedar)
502+
- Updated to PyMC v4 by [Benjamin Vincent](https://github.com/drbenvincent) May 2022.
503+
504+
+++
505+
506+
## Watermark
498507

499508
```{code-cell} ipython3
500509
---
@@ -507,5 +516,5 @@ papermill:
507516
tags: []
508517
---
509518
%load_ext watermark
510-
%watermark -n -u -v -iv -w
519+
%watermark -n -u -v -iv -w -p aesara,aeppl
511520
```

0 commit comments

Comments
 (0)