Path: blob/master/notebooks/book2/03/hierarchical_binom_rats_pymc3.ipynb
1193 views
Kernel: Python 3 (ipykernel)
We fit a hierarchical beta-binomial model to some count data derived from rat survival. (In the book, we motivate this in terms of covid incidence rates.) Based on https://www.pymc.io/projects/examples/en/latest/generalized_linear_models/GLM-hierarchical-binomial-model.html
In [ ]:
In [ ]:
3.11.2
0.11.2
In [ ]:
In [ ]:
In [ ]:
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:22: FutureWarning: In v4.0, pm.sample will return an `arviz.InferenceData` object instead of a `MultiTrace` by default. You can pass return_inferencedata=True or return_inferencedata=False to be safe and silence this warning.
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
Sequential sampling (2 chains in 1 job)
NUTS: [theta, ab]
Sampling 2 chains for 500 tune and 1_000 draw iterations (1_000 + 2_000 draws total) took 17 seconds.
There were 2 divergences after tuning. Increase `target_accept` or reparameterize.
There were 39 divergences after tuning. Increase `target_accept` or reparameterize.
The number of effective samples is smaller than 25% for some parameters.
In [ ]:
/usr/local/lib/python3.7/dist-packages/arviz/data/io_pymc3.py:100: FutureWarning: Using `from_pymc3` without the model will be deprecated in a future release. Not using the model will return less accurate and less useful results. Make sure you use the model argument or call from_pymc3 within a model context.
FutureWarning,
/usr/local/lib/python3.7/dist-packages/arviz/data/io_pymc3.py:100: FutureWarning: Using `from_pymc3` without the model will be deprecated in a future release. Not using the model will return less accurate and less useful results. Make sure you use the model argument or call from_pymc3 within a model context.
FutureWarning,
mean sd hdi_3% hdi_97% ... mcse_sd ess_bulk ess_tail r_hat
ab[0] 2.285 0.705 1.155 3.693 ... 0.026 380.0 528.0 1.0
ab[1] 13.769 4.376 6.333 22.154 ... 0.159 397.0 506.0 1.0
alpha 2.285 0.705 1.155 3.693 ... 0.026 380.0 528.0 1.0
beta 13.769 4.376 6.333 22.154 ... 0.159 397.0 506.0 1.0
X -1.792 0.110 -2.000 -1.578 ... 0.002 1536.0 1441.0 1.0
... ... ... ... ... ... ... ... ... ...
theta[66] 0.270 0.053 0.176 0.371 ... 0.001 1924.0 1206.0 1.0
theta[67] 0.279 0.059 0.173 0.385 ... 0.001 1928.0 1023.0 1.0
theta[68] 0.275 0.056 0.180 0.383 ... 0.001 1409.0 1269.0 1.0
theta[69] 0.288 0.074 0.153 0.425 ... 0.001 1361.0 1149.0 1.0
theta[70] 0.209 0.077 0.072 0.355 ... 0.001 1704.0 822.0 1.0
[77 rows x 9 columns]
In [ ]:
hbayes_binom_rats_trace.png sample_data
In [ ]:
post mean
[0.06165285 0.06209179 0.06249498 0.06219465 0.06022995 0.06255777
0.06164838 0.06327575 0.06465686 0.06351028 0.06333434 0.0659995
0.06527241 0.06793269 0.09016638 0.09022209 0.08909435 0.08898105
0.09253411 0.09422614 0.0977361 0.09510004 0.10446713 0.10759323
0.11087827 0.11962881 0.11864461 0.11709105 0.11766299 0.11826686
0.11809571 0.12520729 0.11106424 0.12135935 0.11750879 0.12135598
0.1310315 0.14319236 0.14632288 0.14614197 0.14630019 0.14749157
0.17459389 0.18594023 0.17427627 0.17551521 0.17630513 0.1752244
0.17395244 0.17569121 0.17711694 0.19249357 0.18161606 0.18368988
0.17869469 0.19331308 0.21462783 0.22167791 0.20262534 0.2043273
0.21425832 0.20821414 0.21805169 0.23147269 0.23414802 0.23021472
0.26963507 0.27906669 0.27505595 0.28814994 0.20892171]
hyper mean
0.14235314360269444
pooled mle
0.1535365152386429
/usr/local/lib/python3.7/dist-packages/arviz/data/io_pymc3.py:100: FutureWarning: Using `from_pymc3` without the model will be deprecated in a future release. Not using the model will return less accurate and less useful results. Make sure you use the model argument or call from_pymc3 within a model context.
FutureWarning,