Kernel: Python 3
In [1]:
Code 12.1
In [2]:
(48, 5)
density | pred | size | surv | propsurv | |
---|---|---|---|---|---|
0 | 10 | no | big | 9 | 0.9 |
1 | 10 | no | big | 10 | 1.0 |
2 | 10 | no | big | 7 | 0.7 |
3 | 10 | no | big | 10 | 1.0 |
4 | 10 | no | small | 9 | 0.9 |
5 | 10 | no | small | 9 | 0.9 |
6 | 10 | no | small | 10 | 1.0 |
7 | 10 | no | small | 9 | 0.9 |
Code 12.2
In [3]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 4000/4000 [00:18<00:00, 213.57it/s]
Code 12.3
In [4]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 4000/4000 [00:17<00:00, 231.65it/s]
Code 12.4
In [5]:
WAIC | pWAIC | dWAIC | weight | SE | dSE | warning | |
---|---|---|---|---|---|---|---|
model | |||||||
m12.2 | 200.7 | 21.18 | 0 | 0.65 | 7.15 | 0 | 1 |
m12.1 | 201.91 | 22.83 | 1.21 | 0.35 | 9.31 | 4.44 | 1 |
Code 12.5
In [6]:
In [7]:
Code 12.6
In [8]:
Code 12.7
In [9]:
Code 12.8
In [10]:
Code 12.9
In [11]:
Code 12.10
Data types related. Python is dynamically-typed.
Code 12.11
In [12]:
Code 12.12
In [13]:
Code 12.13
In [14]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:06<00:00, 297.12it/s]
Code 12.14
In [15]:
mean | sd | mc_error | hpd_5.5 | hpd_94.5 | n_eff | Rhat | |
---|---|---|---|---|---|---|---|
a | 1.29 | 0.20 | 0.00 | 0.96 | 1.60 | 1827.0 | 1.0 |
a_pond__0 | 1.46 | 0.89 | 0.02 | 0.03 | 2.75 | 2000.0 | 1.0 |
a_pond__1 | 1.48 | 0.94 | 0.02 | 0.05 | 2.96 | 2000.0 | 1.0 |
a_pond__58 | 3.63 | 0.90 | 0.02 | 2.13 | 4.92 | 2000.0 | 1.0 |
a_pond__59 | 0.24 | 0.34 | 0.01 | -0.23 | 0.85 | 2000.0 | 1.0 |
sigma | 1.42 | 0.20 | 0.01 | 1.12 | 1.75 | 783.0 | 1.0 |
Code 12.15
In [16]:
Code 12.16
In [17]:
Code 12.17
In [18]:
Code 12.18
In [19]:
Code 12.19
This part is more Stan and rethinking related. To do the same in PyMC3 (i.e., avoide compiling the same model twice), you need to set up the input data with theano.shared
or use sampled, a functional decorator for PyMC3.
Code 12.20
In [20]:
Code 12.21
In [21]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 6000/6000 [02:18<00:00, 43.33it/s]
Code 12.22
In [22]:
array([-0.71, 4.62, -1.02, -1.02, -0.71, 0.23, 1.76])
Code 12.23
In [23]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
82%|████████▏ | 5741/7000 [03:10<00:36, 34.51it/s]/home/osvaldo/Documentos/Proyectos/01_PyMC3/pymc3/pymc3/step_methods/hmc/nuts.py:468: UserWarning: Chain 3 contains 3 diverging samples after tuning. If increasing `target_accept` does not help try to reparameterize.
% (self._chain_id, n_diverging))
94%|█████████▍| 6594/7000 [03:25<00:06, 60.57it/s]/home/osvaldo/Documentos/Proyectos/01_PyMC3/pymc3/pymc3/step_methods/hmc/nuts.py:468: UserWarning: Chain 2 contains 7 diverging samples after tuning. If increasing `target_accept` does not help try to reparameterize.
% (self._chain_id, n_diverging))
98%|█████████▊| 6863/7000 [03:30<00:02, 61.19it/s]/home/osvaldo/Documentos/Proyectos/01_PyMC3/pymc3/pymc3/step_methods/hmc/nuts.py:468: UserWarning: Chain 1 contains 26 diverging samples after tuning. If increasing `target_accept` does not help try to reparameterize.
% (self._chain_id, n_diverging))
100%|█████████▉| 6994/7000 [03:32<00:00, 62.62it/s]/home/osvaldo/Documentos/Proyectos/01_PyMC3/pymc3/pymc3/step_methods/hmc/nuts.py:468: UserWarning: Chain 0 contains 4 diverging samples after tuning. If increasing `target_accept` does not help try to reparameterize.
% (self._chain_id, n_diverging))
100%|██████████| 7000/7000 [03:32<00:00, 32.99it/s]
Code 12.24
In [24]:
mean | sd | mc_error | hpd_5.5 | hpd_94.5 | n_eff | Rhat | |
---|---|---|---|---|---|---|---|
bp | 0.83 | 0.26 | 0.00 | 0.41 | 1.25 | 15660.0 | 1.0 |
bpC | -0.14 | 0.30 | 0.00 | -0.63 | 0.33 | 15743.0 | 1.0 |
a | 0.43 | 0.95 | 0.02 | -1.07 | 1.82 | 3661.0 | 1.0 |
a_actor__0 | -1.15 | 0.96 | 0.02 | -2.60 | 0.30 | 3743.0 | 1.0 |
a_actor__1 | 4.19 | 1.64 | 0.02 | 1.78 | 6.38 | 6420.0 | 1.0 |
a_actor__2 | -1.45 | 0.97 | 0.02 | -2.93 | 0.03 | 3629.0 | 1.0 |
a_actor__3 | -1.45 | 0.96 | 0.02 | -2.93 | 0.01 | 3790.0 | 1.0 |
a_actor__4 | -1.15 | 0.96 | 0.02 | -2.63 | 0.30 | 3731.0 | 1.0 |
a_actor__5 | -0.20 | 0.96 | 0.02 | -1.60 | 1.32 | 3789.0 | 1.0 |
a_actor__6 | 1.34 | 0.98 | 0.02 | -0.19 | 2.81 | 4029.0 | 1.0 |
a_block__0 | -0.18 | 0.23 | 0.00 | -0.53 | 0.12 | 4962.0 | 1.0 |
a_block__1 | 0.04 | 0.19 | 0.00 | -0.24 | 0.35 | 11541.0 | 1.0 |
a_block__2 | 0.05 | 0.19 | 0.00 | -0.24 | 0.36 | 12229.0 | 1.0 |
a_block__3 | 0.00 | 0.19 | 0.00 | -0.31 | 0.29 | 14163.0 | 1.0 |
a_block__4 | -0.03 | 0.19 | 0.00 | -0.33 | 0.25 | 12378.0 | 1.0 |
a_block__5 | 0.12 | 0.20 | 0.00 | -0.16 | 0.45 | 7508.0 | 1.0 |
sigma_actor | 2.26 | 0.92 | 0.01 | 1.06 | 3.40 | 6639.0 | 1.0 |
sigma_block | 0.23 | 0.18 | 0.00 | 0.01 | 0.44 | 2776.0 | 1.0 |
In [25]:
Code 12.25
In [26]:
Code 12.26
In [27]:
WAIC | pWAIC | dWAIC | weight | SE | dSE | warning | |
---|---|---|---|---|---|---|---|
model | |||||||
m12.4 | 531.27 | 8.06 | 0 | 0.67 | 19.46 | 0 | 0 |
m12.5 | 532.68 | 10.42 | 1.41 | 0.33 | 19.66 | 1.78 | 0 |
Code 12.27
In [28]:
actor | recipient | condition | block | trial | prosoc_left | chose_prosoc | pulled_left | |
---|---|---|---|---|---|---|---|---|
73 | 1 | NaN | 0 | 0 | 3 | 0 | 0 | 1 |
72 | 1 | NaN | 0 | 0 | 1 | 1 | 1 | 1 |
108 | 1 | 7.0 | 1 | 0 | 2 | 0 | 0 | 1 |
116 | 1 | 5.0 | 1 | 1 | 16 | 1 | 1 | 1 |
In [29]:
100%|██████████| 1000/1000 [00:01<00:00, 941.01it/s]
In [30]:
Code 12.28
In [31]:
bp | bpC | a | a_actor__0 | a_actor__1 | a_actor__2 | a_actor__3 | a_actor__4 | a_actor__5 | a_actor__6 | sigma_actor | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1.144842 | -0.192101 | 1.336637 | -2.001404 | 3.133755 | -2.554750 | -2.685470 | -1.728456 | -0.844019 | 0.500316 | 2.390074 |
1 | 0.927530 | -0.210558 | 1.217429 | -2.172665 | 3.282467 | -2.475223 | -2.507842 | -1.873292 | -1.144004 | 0.414225 | 1.485859 |
2 | 0.895586 | -0.189188 | -0.522454 | -0.054271 | 4.134344 | -0.538216 | -0.309994 | -0.414063 | 0.816621 | 2.577835 | 2.078943 |
3 | 0.981783 | -0.235233 | 0.141105 | -0.704803 | 3.815423 | -1.235665 | -1.194887 | -1.007658 | 0.132304 | 1.557893 | 1.254438 |
4 | 0.979146 | -0.311983 | 1.357626 | -1.988020 | 4.218693 | -2.529535 | -2.441370 | -2.289553 | -1.060942 | 0.150617 | 2.299303 |
Code 12.29
In [32]:
Code 12.30
In [33]:
Code 12.31
In [34]:
Code 12.32
In [35]:
Code 12.33
In [36]:
Code 12.34
In [37]:
Code 12.35
In [38]:
Code 12.36
In [39]:
Code 12.37
In [40]:
Code 12.38
In [41]:
Code 12.39
In [42]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
82%|████████▏ | 4924/6000 [01:58<00:26, 40.75it/s]/home/osvaldo/Documentos/Proyectos/01_PyMC3/pymc3/pymc3/step_methods/hmc/nuts.py:468: UserWarning: Chain 2 contains 2 diverging samples after tuning. If increasing `target_accept` does not help try to reparameterize.
% (self._chain_id, n_diverging))
89%|████████▉ | 5357/6000 [02:04<00:08, 74.66it/s]/home/osvaldo/Documentos/Proyectos/01_PyMC3/pymc3/pymc3/step_methods/hmc/nuts.py:452: UserWarning: The acceptance probability in chain 3 does not match the target. It is 0.891742114153, but should be close to 0.8. Try to increase the number of tuning steps.
% (self._chain_id, mean_accept, target_accept))
100%|█████████▉| 5995/6000 [02:12<00:00, 74.15it/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.886014219993, but should be close to 0.8. Try to increase the number of tuning steps.
% (self._chain_id, mean_accept, target_accept))
100%|██████████| 6000/6000 [02:12<00:00, 45.20it/s]
Code 12.40
In [43]:
Code 12.41
In [44]:
In [45]:
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
Pandas 0.20.3
SciPy 0.19.1
Matplotlib 2.1.0