Kernel: Python 3
In [1]:
Code 10.1
In [2]:
Code 10.2
In [3]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:02<00:00, 933.20it/s]
In [4]:
mean | sd | mc_error | hpd_5.5 | hpd_94.5 | n_eff | Rhat | |
---|---|---|---|---|---|---|---|
a | 0.32 | 0.09 | 0.00 | 0.17 | 0.47 | 1317.0 | 1.0 |
bp | 0.52 | 10.32 | 0.24 | -17.18 | 15.96 | 1669.0 | 1.0 |
Code 10.3
In [5]:
hpd_5.5 | hpd_94.5 | |
---|---|---|
a | 0.54299 | 0.61441 |
bp | 0.00000 | 1.00000 |
Code 10.4
In [6]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:03<00:00, 602.80it/s]
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:05<00:00, 371.93it/s]
Code 10.5
In [7]:
WAIC | pWAIC | dWAIC | weight | SE | dSE | warning | |
---|---|---|---|---|---|---|---|
model | |||||||
m10.2 | 680.39 | 1.95 | 0 | 0.73 | 9.35 | 0 | 0 |
m10.3 | 682.51 | 3.08 | 2.12 | 0.25 | 9.49 | 0.83 | 0 |
m10.1 | 688.13 | 1.09 | 7.74 | 0.02 | 7.09 | 6.21 | 0 |
In [8]:
Code 10.6
In [9]:
mean | sd | mc_error | hpd_5.5 | hpd_94.5 | n_eff | Rhat | |
---|---|---|---|---|---|---|---|
a | 0.05 | 0.13 | 0.00 | -0.15 | 0.26 | 1193.0 | 1.0 |
bp | 0.62 | 0.23 | 0.01 | 0.26 | 0.97 | 1177.0 | 1.0 |
bpC | -0.11 | 0.27 | 0.01 | -0.48 | 0.36 | 1388.0 | 1.0 |
Code 10.7
In [10]:
1.8404313987816374
Code 10.8
In [11]:
0.98201379003790845
Code 10.9
In [12]:
0.99014624447676869
Code 10.10 and 10.11
In [13]:
100%|██████████| 1000/1000 [00:00<00:00, 1527.58it/s]
In [14]:
Code 10.12 & 10.13
This is the same as 10.6, but in the book using MCMC rather than quadratic approximation.
Code 10.14
In [15]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
99%|█████████▉| 1984/2000 [00:08<00:00, 239.82it/s]/Users/jlao/Documents/Github/pymc3/pymc3/step_methods/hmc/nuts.py:468: UserWarning: Chain 0 contains 8 diverging samples after tuning. If increasing `target_accept` does not help try to reparameterize.
% (self._chain_id, n_diverging))
100%|██████████| 2000/2000 [00:08<00:00, 225.05it/s]
/Users/jlao/Documents/Github/pymc3/pymc3/step_methods/hmc/nuts.py:468: UserWarning: Chain 1 contains 18 diverging samples after tuning. If increasing `target_accept` does not help try to reparameterize.
% (self._chain_id, n_diverging))
Code 10.15
In [16]:
array([0, 1, 2, 3, 4, 5, 6])
Code 10.16
In [17]:
mean | sd | mc_error | hpd_5.5 | hpd_94.5 | n_eff | Rhat | |
---|---|---|---|---|---|---|---|
alpha__0 | -0.75 | 0.27 | 0.01 | -1.21 | -0.34 | 1163.0 | 1.0 |
alpha__1 | 10.95 | 5.44 | 0.18 | 3.37 | 18.39 | 937.0 | 1.0 |
alpha__2 | -1.05 | 0.28 | 0.01 | -1.50 | -0.63 | 1333.0 | 1.0 |
alpha__3 | -1.05 | 0.28 | 0.01 | -1.53 | -0.64 | 890.0 | 1.0 |
alpha__4 | -0.76 | 0.27 | 0.01 | -1.19 | -0.34 | 1422.0 | 1.0 |
alpha__5 | 0.21 | 0.28 | 0.01 | -0.24 | 0.65 | 985.0 | 1.0 |
alpha__6 | 1.80 | 0.40 | 0.01 | 1.18 | 2.44 | 1393.0 | 1.0 |
bp | 0.85 | 0.26 | 0.01 | 0.44 | 1.27 | 938.0 | 1.0 |
bpC | -0.14 | 0.31 | 0.01 | -0.62 | 0.37 | 1479.0 | 1.0 |
Code 10.17
In [18]:
alpha__0 | alpha__1 | alpha__2 | alpha__3 | alpha__4 | alpha__5 | alpha__6 | bpC | bp | |
---|---|---|---|---|---|---|---|---|---|
0 | -1.115434 | 12.110896 | -0.625134 | -0.597404 | -0.895642 | 0.261634 | 2.209256 | 0.174950 | 0.575461 |
1 | -1.234031 | 7.743170 | -0.551588 | -0.866132 | -0.727092 | 0.695167 | 2.152252 | 0.309446 | 0.452707 |
2 | -1.298370 | 19.117585 | -0.337676 | -0.503491 | -0.865684 | 0.625298 | 2.350492 | 0.077861 | 0.647056 |
3 | -0.394510 | 10.449205 | -1.390085 | -1.707261 | -0.826964 | 0.581814 | 1.785261 | -0.306445 | 0.938187 |
4 | -0.459913 | 6.716079 | -1.341561 | -1.802549 | -0.850939 | 0.621452 | 1.886712 | -0.099751 | 1.029564 |
Code 10.18
In [19]:
Code 10.19
In [20]:
100%|██████████| 1000/1000 [00:00<00:00, 1757.16it/s]
In [21]:
Code 10.20
In [22]:
actor | condition | prosoc_left | pulled_left | |
---|---|---|---|---|
0 | 0 | 0 | 0 | 6 |
1 | 0 | 0 | 1 | 9 |
2 | 0 | 1 | 0 | 5 |
3 | 0 | 1 | 1 | 10 |
4 | 1 | 0 | 0 | 18 |
5 | 1 | 0 | 1 | 18 |
6 | 1 | 1 | 0 | 18 |
7 | 1 | 1 | 1 | 18 |
Code 10.21
In [23]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:02<00:00, 758.10it/s]
In [24]:
mean | sd | mc_error | hpd_5.5 | hpd_94.5 | n_eff | Rhat | |
---|---|---|---|---|---|---|---|
alpha | 0.05 | 0.13 | 0.00 | -0.14 | 0.26 | 1273.0 | 1.0 |
bp | 0.61 | 0.23 | 0.01 | 0.25 | 0.96 | 1172.0 | 1.0 |
bpC | -0.11 | 0.27 | 0.01 | -0.57 | 0.28 | 1284.0 | 1.0 |
In [25]:
array([[ True, True, True, False, False, False, True],
[ True, True, True, False, False, False, True],
[ True, True, True, False, False, False, True]], dtype=bool)
Code 10.22
In [26]:
dept | applicant.gender | admit | reject | applications | |
---|---|---|---|---|---|
1 | A | male | 512 | 313 | 825 |
2 | A | female | 89 | 19 | 108 |
3 | B | male | 353 | 207 | 560 |
4 | B | female | 17 | 8 | 25 |
5 | C | male | 120 | 205 | 325 |
6 | C | female | 202 | 391 | 593 |
7 | D | male | 138 | 279 | 417 |
8 | D | female | 131 | 244 | 375 |
Code 10.23
In [27]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:01<00:00, 1007.47it/s]
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:01<00:00, 1869.83it/s]
Code 10.24
In [28]:
WAIC | pWAIC | dWAIC | weight | SE | dSE | warning | |
---|---|---|---|---|---|---|---|
model | |||||||
m10.6 | 992.74 | 110.11 | 0 | 0.39 | 312.94 | 0 | 1 |
m10.7 | 1041.85 | 80.2 | 49.12 | 0.61 | 312.13 | 156.01 | 1 |
Code 10.25
In [29]:
mean | sd | mc_error | hpd_5.5 | hpd_94.5 | n_eff | Rhat | |
---|---|---|---|---|---|---|---|
a | -0.83 | 0.05 | 0.0 | -0.91 | -0.74 | 582.0 | 1.0 |
bm | 0.61 | 0.07 | 0.0 | 0.52 | 0.72 | 594.0 | 1.0 |
Code 10.26
In [30]:
2.5% 0.112591
50% 0.141882
97.5% 0.169246
dtype: float64
Code 10.27
In [31]:
Code 10.28
In [32]:
In [33]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:01<00:00, 1023.44it/s]
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:02<00:00, 679.73it/s]
Code 10.29
In [34]:
WAIC | pWAIC | dWAIC | weight | SE | dSE | warning | |
---|---|---|---|---|---|---|---|
model | |||||||
m10.8 | 105.21 | 6.59 | 0 | 0.89 | 17.12 | 0 | 1 |
m10.9 | 109.32 | 9.9 | 4.11 | 0.11 | 15.59 | 3.49 | 1 |
m10.6 | 992.74 | 110.11 | 887.53 | 0 | 312.94 | 309.95 | 1 |
m10.7 | 1041.85 | 80.2 | 936.65 | 0 | 312.13 | 309.79 | 1 |
Code 10.30
In [35]:
mean | sd | mc_error | hpd_5.5 | hpd_94.5 | n_eff | Rhat | |
---|---|---|---|---|---|---|---|
a__0 | 0.69 | 0.10 | 0.0 | 0.54 | 0.86 | 1499.0 | 1.0 |
a__1 | 0.64 | 0.12 | 0.0 | 0.46 | 0.82 | 1799.0 | 1.0 |
a__2 | -0.58 | 0.07 | 0.0 | -0.69 | -0.46 | 2000.0 | 1.0 |
a__3 | -0.61 | 0.09 | 0.0 | -0.76 | -0.48 | 1817.0 | 1.0 |
a__4 | -1.06 | 0.10 | 0.0 | -1.21 | -0.90 | 2000.0 | 1.0 |
a__5 | -2.63 | 0.16 | 0.0 | -2.88 | -2.37 | 2000.0 | 1.0 |
bm | -0.10 | 0.08 | 0.0 | -0.24 | 0.03 | 1335.0 | 1.0 |
Code 10.31
Replicated model above but with MCMC in book.
Code 10.32
In [36]:
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/statsmodels-0.8.0-py3.5-macosx-10.6-intel.egg/statsmodels/compat/pandas.py:56: FutureWarning: The pandas.core.datetools module is deprecated and will be removed in a future version. Please use the pandas.tseries module instead.
from pandas.core import datetools
Code 10.33
In [37]:
Code 10.34
In [38]:
Code 10.35
In [39]:
Dep. Variable: | y | No. Observations: | 20 |
---|---|---|---|
Model: | GLM | Df Residuals: | 18 |
Model Family: | Binomial | Df Model: | 1 |
Link Function: | logit | Scale: | 1.0 |
Method: | IRLS | Log-Likelihood: | -3.3510 |
Date: | Mon, 06 Nov 2017 | Deviance: | 6.7020 |
Time: | 08:35:17 | Pearson chi2: | 11.0 |
No. Iterations: | 21 |
coef | std err | z | P>|z| | [0.025 | 0.975] | |
---|---|---|---|---|---|---|
Intercept | -10.1317 | 8032.690 | -0.001 | 0.999 | -1.58e+04 | 1.57e+04 |
x | 12.4343 | 8032.690 | 0.002 | 0.999 | -1.57e+04 | 1.58e+04 |
Code 10.36
In [40]:
logp = -9.9185, ||grad|| = 7.2889e-05: 100%|██████████| 13/13 [00:00<00:00, 3777.87it/s]
{'ab': array([-1.72704484, 4.01710522])}
Code 10.37
In [41]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:02<00:00, 686.07it/s]
/Users/jlao/Documents/Github/pymc3/pymc3/step_methods/hmc/nuts.py:452: UserWarning: The acceptance probability in chain 1 does not match the target. It is 0.929478370478, but should be close to 0.8. Try to increase the number of tuning steps.
% (self._chain_id, mean_accept, target_accept))
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/axes/_axes.py:545: UserWarning: No labelled objects found. Use label='...' kwarg on individual plots.
warnings.warn("No labelled objects found. "
Code 10.38
In [42]:
(1.0034700000000001, 0.99533795909999989)
Code 10.39
In [43]:
culture | population | contact | total_tools | mean_TU | |
---|---|---|---|---|---|
0 | Malekula | 1100 | low | 13 | 3.2 |
1 | Tikopia | 1500 | low | 22 | 4.7 |
2 | Santa Cruz | 3600 | low | 24 | 4.0 |
3 | Yap | 4791 | high | 43 | 5.0 |
4 | Lau Fiji | 7400 | high | 33 | 5.0 |
5 | Trobriand | 8000 | high | 19 | 4.0 |
6 | Chuuk | 9200 | high | 40 | 3.8 |
7 | Manus | 13000 | low | 28 | 6.6 |
8 | Tonga | 17500 | high | 55 | 5.4 |
9 | Hawaii | 275000 | low | 71 | 6.6 |
Code 10.40
In [44]:
In [45]:
Code 10.41
In [46]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|█████████▉| 1997/2000 [00:27<00:00, 78.93it/s] /Users/jlao/Documents/Github/pymc3/pymc3/step_methods/hmc/nuts.py:452: UserWarning: The acceptance probability in chain 0 does not match the target. It is 0.887261631246, but should be close to 0.8. Try to increase the number of tuning steps.
% (self._chain_id, mean_accept, target_accept))
100%|██████████| 2000/2000 [00:27<00:00, 71.84it/s]
Code 10.42
In [47]:
mean | sd | hpd_5.5 | hpd_94.5 | a | b__0 | b__1 | b__2 | |
---|---|---|---|---|---|---|---|---|
a | 0.96 | 0.37 | 0.38 | 1.55 | 1.00 | -0.97 | -0.13 | 0.07 |
b__0 | 0.26 | 0.04 | 0.21 | 0.32 | -0.97 | 1.00 | 0.13 | -0.09 |
b__1 | -0.13 | 0.83 | -1.41 | 1.21 | -0.13 | 0.13 | 1.00 | -0.99 |
b__2 | 0.05 | 0.09 | -0.09 | 0.20 | 0.07 | -0.09 | -0.99 | 1.00 |
In [48]:
Code 10.43
In [49]:
Code 10.44
In [50]:
0.94999999999999996
Code 10.45
In [51]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:12<00:00, 154.69it/s]
Code 10.46
In [52]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:05<00:00, 342.47it/s]
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:02<00:00, 762.48it/s]
Code 10.47
In [53]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:01<00:00, 1589.73it/s]
In [54]:
WAIC | pWAIC | dWAIC | weight | SE | dSE | warning | |
---|---|---|---|---|---|---|---|
model | |||||||
m10.11 | 78.91 | 4.18 | 0 | 0.64 | 11.07 | 0 | 1 |
m10.10 | 80.29 | 4.94 | 1.38 | 0.32 | 11.14 | 1.34 | 1 |
m10.12 | 84.68 | 3.87 | 5.76 | 0.04 | 8.97 | 7.94 | 1 |
m10.14 | 141.6 | 8.21 | 62.69 | 0 | 31.88 | 32.99 | 1 |
m10.13 | 151.18 | 17.3 | 72.27 | 0 | 45.1 | 44.83 | 1 |
In [55]:
Code 10.48
In [57]:
100%|██████████| 10000/10000 [00:03<00:00, 3042.71it/s]
In [58]:
Code 10.49
This is the same as 10.41, but in the book using MCMC rather than MAP.
In [59]:
mean | sd | mc_error | hpd_5.5 | hpd_94.5 | n_eff | Rhat | |
---|---|---|---|---|---|---|---|
a | 0.96 | 0.37 | 0.01 | 0.38 | 1.55 | 671.0 | 1.0 |
b__0 | 0.26 | 0.04 | 0.00 | 0.21 | 0.32 | 678.0 | 1.0 |
b__1 | -0.13 | 0.83 | 0.03 | -1.41 | 1.21 | 626.0 | 1.0 |
b__2 | 0.05 | 0.09 | 0.00 | -0.09 | 0.20 | 627.0 | 1.0 |
Code 10.50
In [60]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:05<00:00, 399.60it/s]
In [61]:
mean | sd | mc_error | hpd_5.5 | hpd_94.5 | n_eff | Rhat | |
---|---|---|---|---|---|---|---|
a | 3.31 | 0.09 | 0.0 | 3.19 | 3.46 | 1229.0 | 1.0 |
b__0 | 0.26 | 0.03 | 0.0 | 0.20 | 0.31 | 1569.0 | 1.0 |
b__1 | 0.29 | 0.12 | 0.0 | 0.09 | 0.46 | 1270.0 | 1.0 |
b__2 | 0.06 | 0.17 | 0.0 | -0.20 | 0.34 | 1749.0 | 1.0 |
In [62]:
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/axes/_axes.py:545: UserWarning: No labelled objects found. Use label='...' kwarg on individual plots.
warnings.warn("No labelled objects found. "
Code 10.51
In [63]:
Code 10.52
In [64]:
Code 10.53
In [65]:
Code 10.54
In [66]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:01<00:00, 1180.07it/s]
Code 10.55
In [67]:
mean | sd | mc_error | hpd_5.5 | hpd_94.5 | n_eff | Rhat | |
---|---|---|---|---|---|---|---|
lambda_old | 1.56 | 0.22 | 0.01 | 1.22 | 1.93 | 1131.0 | 1.0 |
lambda_new | 0.60 | 0.14 | 0.00 | 0.39 | 0.82 | 1595.0 | 1.0 |
Code 10.56
In [68]:
Code 10.57
In [69]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:01<00:00, 1029.05it/s]
mean | sd | mc_error | hpd_5.5 | hpd_94.5 | n_eff | Rhat | |
---|---|---|---|---|---|---|---|
b | 0.44 | 0.06 | 0.0 | 0.35 | 0.53 | 859.0 | 1.01 |
Code 10.58
In [70]:
In [71]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:07<00:00, 256.75it/s]
mean | sd | mc_error | hpd_5.5 | hpd_94.5 | n_eff | Rhat | |
---|---|---|---|---|---|---|---|
a23__0 | 0.36 | 0.68 | 0.03 | -0.68 | 1.49 | 590.0 | 1.0 |
a23__1 | 0.51 | 0.62 | 0.03 | -0.41 | 1.56 | 616.0 | 1.0 |
b23__0 | 2.01 | 1.51 | 0.07 | -0.44 | 4.32 | 482.0 | 1.0 |
b23__1 | 3.44 | 1.42 | 0.06 | 1.32 | 5.94 | 477.0 | 1.0 |
Code 10.59
In [72]:
Code 10.60
In [73]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:01<00:00, 1813.50it/s]
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:01<00:00, 1361.79it/s]
Code 10.61
In [74]:
a 0.386986
Name: mean, dtype: float64
Code 10.62
In [75]:
0.38936076605077796
Code 10.63
In [78]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:02<00:00, 807.60it/s]
mean | sd | mc_error | hpd_5.5 | hpd_94.5 | n_eff | Rhat | |
---|---|---|---|---|---|---|---|
a | -1.03 | 0.22 | 0.01 | -1.38 | -0.68 | 648.0 | 1.0 |
b | 1.88 | 0.43 | 0.02 | 1.16 | 2.53 | 611.0 | 1.0 |
In [77]:
This notebook was createad on a computer x86_64 running and using:
Python 3.5.1
IPython 6.2.1
PyMC3 3.2
NumPy 1.12.0
Pandas 0.20.2
SciPy 0.19.1
Matplotlib 2.0.2