Kernel: Python 3
In [1]:
Code 13.1
In [2]:
Code 13.2
In [3]:
Code 13.3
In [4]:
array([[ 1. , -0.35],
[-0.35, 0.25]])
Code 13.4
The code 13.4 and the related comment in the book is particular to R and not relevant for Python.
Code 13.5
In [5]:
matrix([[ 1. , -0.35],
[-0.35, 0.25]])
Code 13.6
In [6]:
Code 13.7
In [7]:
Code 13.8
In [8]:
Code 13.9
In [9]:
In [10]:
Code 13.10
In [11]:
Code 13.11
In [12]:
In [13]:
Code 13.12
In [14]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 7000/7000 [01:41<00:00, 77.07it/s]/home/osvaldo/Documentos/Proyectos/01_PyMC3/pymc3/pymc3/step_methods/hmc/nuts.py:468: UserWarning: Chain 0 contains 1 diverging samples after tuning. If increasing `target_accept` does not help try to reparameterize.
% (self._chain_id, n_diverging))
For complicate model it is always good to do more checks:
In [15]:
Code 13.13
In [16]:
In [17]:
Code 13.14
In [18]:
Code 13.15
In [19]:
Code 13.16
In [20]:
Code 13.17
In [21]:
Code 13.18
In [22]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 5000/5000 [00:15<00:00, 331.41it/s]
mean | sd | mc_error | hpd_5.5 | hpd_94.5 | n_eff | Rhat | |
---|---|---|---|---|---|---|---|
a | -0.59 | 0.64 | 0.01 | -1.61 | 0.38 | 6877.0 | 1.0 |
bm | -0.10 | 0.08 | 0.00 | -0.22 | 0.04 | 5429.0 | 1.0 |
a_dept__0 | 0.67 | 0.10 | 0.00 | 0.52 | 0.83 | 6476.0 | 1.0 |
a_dept__1 | 0.63 | 0.11 | 0.00 | 0.44 | 0.80 | 6950.0 | 1.0 |
a_dept__2 | -0.58 | 0.07 | 0.00 | -0.70 | -0.47 | 9000.0 | 1.0 |
a_dept__3 | -0.62 | 0.09 | 0.00 | -0.76 | -0.48 | 8912.0 | 1.0 |
a_dept__4 | -1.06 | 0.10 | 0.00 | -1.21 | -0.90 | 9000.0 | 1.0 |
a_dept__5 | -2.61 | 0.16 | 0.00 | -2.86 | -2.36 | 9000.0 | 1.0 |
sigma_dept | 1.48 | 0.58 | 0.01 | 0.72 | 2.20 | 6156.0 | 1.0 |
Code 13.19
In [23]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
91%|█████████ | 5453/6000 [02:51<00:12, 43.66it/s]/home/osvaldo/Documentos/Proyectos/01_PyMC3/pymc3/pymc3/step_methods/hmc/nuts.py:468: UserWarning: Chain 3 contains 11 diverging samples after tuning. If increasing `target_accept` does not help try to reparameterize.
% (self._chain_id, n_diverging))
91%|█████████ | 5473/6000 [02:52<00:16, 32.46it/s]/home/osvaldo/Documentos/Proyectos/01_PyMC3/pymc3/pymc3/step_methods/hmc/nuts.py:468: UserWarning: Chain 1 contains 86 diverging samples after tuning. If increasing `target_accept` does not help try to reparameterize.
% (self._chain_id, n_diverging))
94%|█████████▍| 5651/6000 [02:55<00:05, 59.88it/s]/home/osvaldo/Documentos/Proyectos/01_PyMC3/pymc3/pymc3/step_methods/hmc/nuts.py:468: UserWarning: Chain 2 contains 1 diverging samples after tuning. If increasing `target_accept` does not help try to reparameterize.
% (self._chain_id, n_diverging))
100%|█████████▉| 5997/6000 [03:01<00:00, 56.46it/s]/home/osvaldo/Documentos/Proyectos/01_PyMC3/pymc3/pymc3/step_methods/hmc/nuts.py:468: UserWarning: Chain 0 contains 64 diverging samples after tuning. If increasing `target_accept` does not help try to reparameterize.
% (self._chain_id, n_diverging))
100%|██████████| 6000/6000 [03:01<00:00, 33.06it/s]
Code 13.20
In [24]:
Code 13.21
In [25]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 5000/5000 [00:12<00:00, 415.96it/s]
WAIC | pWAIC | dWAIC | weight | SE | dSE | warning | |
---|---|---|---|---|---|---|---|
model | |||||||
m13.3 | 91.43 | 7.01 | 0 | 0.88 | 4.75 | 0 | 1 |
m13.4 | 105.47 | 6.68 | 14.04 | 0.12 | 17.33 | 14.01 | 1 |
m13.2 | 108.13 | 9.19 | 16.69 | 0 | 15.74 | 12.22 | 1 |
Code 13.22
In [26]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
51%|█████▏ | 3085/6000 [06:02<04:42, 10.31it/s]/home/osvaldo/Documentos/Proyectos/01_PyMC3/pymc3/pymc3/step_methods/hmc/nuts.py:452: UserWarning: The acceptance probability in chain 1 does not match the target. It is 0.434980363024, but should be close to 0.8. Try to increase the number of tuning steps.
% (self._chain_id, mean_accept, target_accept))
/home/osvaldo/Documentos/Proyectos/01_PyMC3/pymc3/pymc3/step_methods/hmc/nuts.py:468: UserWarning: Chain 1 contains 1597 diverging samples after tuning. If increasing `target_accept` does not help try to reparameterize.
% (self._chain_id, n_diverging))
100%|██████████| 6000/6000 [11:13<00:00, 7.16it/s]/home/osvaldo/Documentos/Proyectos/01_PyMC3/pymc3/pymc3/step_methods/hmc/nuts.py:468: UserWarning: Chain 0 contains 218 diverging samples after tuning. If increasing `target_accept` does not help try to reparameterize.
% (self._chain_id, n_diverging))
In [27]:
Code 13.23
In [28]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 6000/6000 [06:22<00:00, 13.32it/s]
Code 13.24
In [29]:
Code 13.25
In [30]:
mean | sd | mc_error | hpd_2.5 | hpd_97.5 | n_eff | Rhat | |
---|---|---|---|---|---|---|---|
Sigma_actor__0 | 2.34 | 0.90 | 0.02 | 0.98 | 4.07 | 2809.0 | 1.0 |
Sigma_actor__1 | 0.47 | 0.38 | 0.01 | 0.02 | 1.16 | 2083.0 | 1.0 |
Sigma_actor__2 | 0.54 | 0.49 | 0.01 | 0.01 | 1.47 | 1324.0 | 1.0 |
Sigma_block__0 | 0.23 | 0.20 | 0.00 | 0.00 | 0.60 | 3687.0 | 1.0 |
Sigma_block__1 | 0.57 | 0.40 | 0.01 | 0.01 | 1.28 | 1063.0 | 1.0 |
Sigma_block__2 | 0.50 | 0.40 | 0.01 | 0.02 | 1.26 | 1375.0 | 1.0 |
Code 13.26
R and Rethinking related, skip for now
Code 13.27
In [31]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|█████████▉| 6997/7000 [02:12<00:00, 66.38it/s]/home/osvaldo/Documentos/Proyectos/01_PyMC3/pymc3/pymc3/step_methods/hmc/nuts.py:468: UserWarning: Chain 0 contains 11 diverging samples after tuning. If increasing `target_accept` does not help try to reparameterize.
% (self._chain_id, n_diverging))
100%|██████████| 7000/7000 [02:12<00:00, 52.88it/s]
/home/osvaldo/Documentos/Proyectos/01_PyMC3/pymc3/pymc3/step_methods/hmc/nuts.py:468: UserWarning: Chain 1 contains 28 diverging samples after tuning. If increasing `target_accept` does not help try to reparameterize.
% (self._chain_id, n_diverging))
In [32]:
WAIC | pWAIC | dWAIC | weight | SE | dSE | warning | |
---|---|---|---|---|---|---|---|
model | |||||||
m12.5 | 532.73 | 10.44 | 0 | 0.73 | 19.65 | 0 | 0 |
m13.6NC | 534.71 | 18.43 | 1.97 | 0.27 | 19.89 | 4.13 | 0 |
Code 13.28
Actually in model m_13_6NC I am already using the Cholesky decomposition of the covariance matrix. If we want to strictly follow the parameterization of m13.6NC and m13.6nc1 as in the book, we will model the Rho using pm.LKJCorr and do multiplication it with pm.HalfCauchy.
Code 13.29
In [33]:
Ml | Ti | SC | Ya | Fi | Tr | Ch | Mn | To | Ha | |
---|---|---|---|---|---|---|---|---|---|---|
Malekula | 0.0 | 0.5 | 0.6 | 4.4 | 1.2 | 2.0 | 3.2 | 2.8 | 1.9 | 5.7 |
Tikopia | 0.5 | 0.0 | 0.3 | 4.2 | 1.2 | 2.0 | 2.9 | 2.7 | 2.0 | 5.3 |
Santa Cruz | 0.6 | 0.3 | 0.0 | 3.9 | 1.6 | 1.7 | 2.6 | 2.4 | 2.3 | 5.4 |
Yap | 4.4 | 4.2 | 3.9 | 0.0 | 5.4 | 2.5 | 1.6 | 1.6 | 6.1 | 7.2 |
Lau Fiji | 1.2 | 1.2 | 1.6 | 5.4 | 0.0 | 3.2 | 4.0 | 3.9 | 0.8 | 4.9 |
Trobriand | 2.0 | 2.0 | 1.7 | 2.5 | 3.2 | 0.0 | 1.8 | 0.8 | 3.9 | 6.7 |
Chuuk | 3.2 | 2.9 | 2.6 | 1.6 | 4.0 | 1.8 | 0.0 | 1.2 | 4.8 | 5.8 |
Manus | 2.8 | 2.7 | 2.4 | 1.6 | 3.9 | 0.8 | 1.2 | 0.0 | 4.6 | 6.7 |
Tonga | 1.9 | 2.0 | 2.3 | 6.1 | 0.8 | 3.9 | 4.8 | 4.6 | 0.0 | 5.0 |
Hawaii | 5.7 | 5.3 | 5.4 | 7.2 | 4.9 | 6.7 | 5.8 | 6.7 | 5.0 | 0.0 |
Code 13.30
In [34]:
Code 13.31
In [35]:
In [36]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [02:35<00:00, 13.02it/s]
In [37]:
A reparameterization of m13.7
In [38]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [02:14<00:00, 12.39it/s]/home/osvaldo/Documentos/Proyectos/01_PyMC3/pymc3/pymc3/step_methods/hmc/nuts.py:452: UserWarning: The acceptance probability in chain 0 does not match the target. It is 0.709777131845, but should be close to 0.8. Try to increase the number of tuning steps.
% (self._chain_id, mean_accept, target_accept))
/home/osvaldo/Documentos/Proyectos/01_PyMC3/pymc3/pymc3/step_methods/hmc/nuts.py:468: UserWarning: Chain 0 contains 17 diverging samples after tuning. If increasing `target_accept` does not help try to reparameterize.
% (self._chain_id, n_diverging))
/home/osvaldo/Documentos/Proyectos/01_PyMC3/pymc3/pymc3/step_methods/hmc/nuts.py:468: UserWarning: Chain 1 contains 1 diverging samples after tuning. If increasing `target_accept` does not help try to reparameterize.
% (self._chain_id, n_diverging))
In [39]:
Code 13.32
In [40]:
mean | sd | mc_error | hpd_2.5 | hpd_97.5 | n_eff | Rhat | |
---|---|---|---|---|---|---|---|
g__0 | -0.26 | 0.39 | 0.02 | -1.00 | 0.49 | 325.0 | 1.00 |
g__1 | -0.13 | 0.37 | 0.02 | -0.92 | 0.50 | 299.0 | 1.00 |
g__2 | -0.16 | 0.36 | 0.02 | -0.89 | 0.49 | 314.0 | 1.01 |
g__3 | 0.29 | 0.33 | 0.02 | -0.33 | 0.98 | 359.0 | 1.00 |
g__4 | 0.03 | 0.32 | 0.02 | -0.65 | 0.62 | 330.0 | 1.00 |
g__5 | -0.46 | 0.34 | 0.02 | -1.10 | 0.18 | 347.0 | 1.00 |
g__6 | 0.09 | 0.33 | 0.02 | -0.56 | 0.75 | 364.0 | 1.00 |
g__7 | -0.26 | 0.33 | 0.02 | -0.88 | 0.41 | 360.0 | 1.00 |
g__8 | 0.22 | 0.31 | 0.02 | -0.42 | 0.80 | 356.0 | 1.00 |
g__9 | -0.12 | 0.44 | 0.02 | -1.03 | 0.72 | 334.0 | 1.00 |
a | 1.30 | 1.03 | 0.06 | -0.86 | 3.40 | 216.0 | 1.01 |
bp | 0.25 | 0.11 | 0.01 | 0.03 | 0.46 | 255.0 | 1.00 |
etasq | 0.30 | 0.35 | 0.01 | 0.01 | 0.90 | 506.0 | 1.00 |
rhosq | 3.09 | 33.12 | 1.30 | 0.01 | 4.33 | 635.0 | 1.00 |
In [41]:
mean | sd | mc_error | hpd_2.5 | hpd_97.5 | n_eff | Rhat | |
---|---|---|---|---|---|---|---|
g__0 | -0.26 | 0.40 | 0.02 | -1.11 | 0.54 | 616.0 | 1.0 |
g__1 | -0.14 | 0.40 | 0.02 | -1.02 | 0.61 | 579.0 | 1.0 |
g__2 | -0.17 | 0.38 | 0.02 | -0.99 | 0.57 | 546.0 | 1.0 |
g__3 | 0.30 | 0.34 | 0.01 | -0.33 | 1.06 | 746.0 | 1.0 |
g__4 | 0.04 | 0.32 | 0.01 | -0.62 | 0.68 | 949.0 | 1.0 |
g__5 | -0.46 | 0.34 | 0.01 | -1.17 | 0.16 | 700.0 | 1.0 |
g__6 | 0.10 | 0.33 | 0.01 | -0.60 | 0.72 | 867.0 | 1.0 |
g__7 | -0.26 | 0.33 | 0.01 | -0.99 | 0.39 | 928.0 | 1.0 |
g__8 | 0.23 | 0.31 | 0.01 | -0.42 | 0.84 | 977.0 | 1.0 |
g__9 | -0.11 | 0.43 | 0.01 | -0.95 | 0.78 | 946.0 | 1.0 |
a | 1.33 | 1.13 | 0.04 | -1.04 | 3.63 | 621.0 | 1.0 |
bp | 0.24 | 0.11 | 0.00 | 0.03 | 0.49 | 667.0 | 1.0 |
etasq | 0.31 | 0.34 | 0.01 | 0.01 | 0.91 | 575.0 | 1.0 |
rhosq | 1.41 | 4.55 | 0.14 | 0.01 | 5.59 | 969.0 | 1.0 |
Code 13.33
In [42]:
Code 13.34
In [43]:
Code 13.35
In [44]:
Ml | Ti | SC | Ya | Fi | Tr | Ch | Mn | To | Ha | |
---|---|---|---|---|---|---|---|---|---|---|
Ml | 1.00 | 0.90 | 0.83 | 0.00 | 0.51 | 0.16 | 0.01 | 0.03 | 0.22 | 0.0 |
Ti | 0.90 | 1.00 | 0.95 | 0.00 | 0.50 | 0.17 | 0.03 | 0.04 | 0.18 | 0.0 |
SC | 0.83 | 0.95 | 1.00 | 0.00 | 0.34 | 0.27 | 0.05 | 0.09 | 0.10 | 0.0 |
Ya | 0.00 | 0.00 | 0.00 | 1.00 | 0.00 | 0.07 | 0.34 | 0.31 | 0.00 | 0.0 |
Fi | 0.51 | 0.50 | 0.34 | 0.00 | 1.00 | 0.01 | 0.00 | 0.00 | 0.77 | 0.0 |
Tr | 0.16 | 0.17 | 0.27 | 0.07 | 0.01 | 1.00 | 0.24 | 0.72 | 0.00 | 0.0 |
Ch | 0.01 | 0.03 | 0.05 | 0.34 | 0.00 | 0.24 | 1.00 | 0.52 | 0.00 | 0.0 |
Mn | 0.03 | 0.04 | 0.09 | 0.31 | 0.00 | 0.72 | 0.52 | 1.00 | 0.00 | 0.0 |
To | 0.22 | 0.18 | 0.10 | 0.00 | 0.77 | 0.00 | 0.00 | 0.00 | 1.00 | 0.0 |
Ha | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 1.0 |
Code 13.36
In [45]:
Code 13.37
In [46]:
Code 13.38
Practice session, skip for now.
In [47]:
This notebook was createad on a computer x86_64 running debian stretch/sid and using:
Python 3.6.2
IPython 6.1.0
PyMC3 3.2
NumPy 1.13.3
SciPy 0.19.1
Matplotlib 2.1.0