📚 The CoCalc Library - books, templates and other resources
License: OTHER
Kernel: Python 3
In [1]:
Code 10.1
In [2]:
Code 10.2
In [3]:
Out[3]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:02<00:00, 933.20it/s]
In [4]:
Out[4]:
Code 10.3
In [5]:
Out[5]:
Code 10.4
In [6]:
Out[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]:
Out[7]:
In [8]:
Out[8]:
Code 10.6
In [9]:
Out[9]:
Code 10.7
In [10]:
Out[10]:
1.8404313987816374
Code 10.8
In [11]:
Out[11]:
0.98201379003790845
Code 10.9
In [12]:
Out[12]:
0.99014624447676869
Code 10.10 and 10.11
In [13]:
Out[13]:
100%|██████████| 1000/1000 [00:00<00:00, 1527.58it/s]
In [14]:
Out[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]:
Out[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]:
Out[16]:
array([0, 1, 2, 3, 4, 5, 6])
Code 10.16
In [17]:
Out[17]:
Code 10.17
In [18]:
Out[18]:
Code 10.18
In [19]:
Out[19]:
Code 10.19
In [20]:
Out[20]:
100%|██████████| 1000/1000 [00:00<00:00, 1757.16it/s]
In [21]:
Out[21]:
Code 10.20
In [22]:
Out[22]:
Code 10.21
In [23]:
Out[23]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:02<00:00, 758.10it/s]
In [24]:
Out[24]:
In [25]:
Out[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]:
Out[26]:
Code 10.23
In [27]:
Out[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]:
Out[28]:
Code 10.25
In [29]:
Out[29]:
Code 10.26
In [30]:
Out[30]:
2.5% 0.112591
50% 0.141882
97.5% 0.169246
dtype: float64
Code 10.27
In [31]:
Out[31]:
Code 10.28
In [32]:
In [33]:
Out[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]:
Out[34]:
Code 10.30
In [35]:
Out[35]:
Code 10.31
Replicated model above but with MCMC in book.
Code 10.32
In [36]:
Out[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]:
Out[38]:
Code 10.35
In [39]:
Out[39]:
Code 10.36
In [40]:
Out[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]:
Out[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]:
Out[42]:
(1.0034700000000001, 0.99533795909999989)
Code 10.39
In [43]:
Out[43]:
Code 10.40
In [44]:
In [45]:
Code 10.41
In [46]:
Out[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]:
Out[47]:
In [48]:
Out[48]:
Code 10.43
In [49]:
Code 10.44
In [50]:
Out[50]:
0.94999999999999996
Code 10.45
In [51]:
Out[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]:
Out[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]:
Out[53]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:01<00:00, 1589.73it/s]
In [54]:
Out[54]:
In [55]:
Out[55]:
Code 10.48
In [57]:
Out[57]:
100%|██████████| 10000/10000 [00:03<00:00, 3042.71it/s]
In [58]:
Out[58]:
Code 10.49
This is the same as 10.41, but in the book using MCMC rather than MAP.
In [59]:
Out[59]:
Code 10.50
In [60]:
Out[60]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:05<00:00, 399.60it/s]
In [61]:
Out[61]:
In [62]:
Out[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]:
Out[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]:
Out[67]:
Code 10.56
In [68]:
Code 10.57
In [69]:
Out[69]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:01<00:00, 1029.05it/s]
Code 10.58
In [70]:
In [71]:
Out[71]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:07<00:00, 256.75it/s]
Code 10.59
In [72]:
Code 10.60
In [73]:
Out[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]:
Out[74]:
a 0.386986
Name: mean, dtype: float64
Code 10.62
In [75]:
Out[75]:
0.38936076605077796
Code 10.63
In [78]:
Out[78]:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
100%|██████████| 2000/2000 [00:02<00:00, 807.60it/s]
In [77]:
Out[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