├── Chapter 1
├── 01_Thinking_Probabilistically_a_Bayesian_Inference_Primer (3).ipynb
├── hpd (1).py
├── mauna_loa_CO2 (1).csv
└── plot_post (1).py
├── Chapter 2
└── 02_Programming_probabilistically_a_PyMC3_primer (1).ipynb
├── Chapter 3
└── 03_Juggling with multiparametric and Hierarchical models.ipynb
├── Chapter 4
└── 04_Understanding_and_predicting_data_with_linear_regression_models (2).ipynb
├── Chapter 5
└── 05_Classifying_outcomes_with_logistic_regression (2).ipynb
├── Chapter 6
└── 06_Model_comparison.ipynb
├── Chapter 7
└── 07_Mixture_Models (1).ipynb
├── Chapter 8
└── 08_Gaussian_processes.ipynb
├── LICENSE
└── README.md
/Chapter 1/hpd (1).py:
--------------------------------------------------------------------------------
1 | from __future__ import division
2 | import numpy as np
3 | import scipy.stats.kde as kde
4 |
5 | def hpd_grid(sample, alpha=0.05, roundto=2):
6 | """Calculate highest posterior density (HPD) of array for given alpha.
7 | The HPD is the minimum width Bayesian credible interval (BCI).
8 | The function works for multimodal distributions, returning more than one mode
9 |
10 | Parameters
11 | ----------
12 |
13 | sample : Numpy array or python list
14 | An array containing MCMC samples
15 | alpha : float
16 | Desired probability of type I error (defaults to 0.05)
17 | roundto: integer
18 | Number of digits after the decimal point for the results
19 |
20 | Returns
21 | ----------
22 | hpd: array with the lower
23 |
24 | """
25 | sample = np.asarray(sample)
26 | sample = sample[~np.isnan(sample)]
27 | # get upper and lower bounds
28 | l = np.min(sample)
29 | u = np.max(sample)
30 | density = kde.gaussian_kde(sample)
31 | x = np.linspace(l, u, 2000)
32 | y = density.evaluate(x)
33 | #y = density.evaluate(x, l, u) waitting for PR to be accepted
34 | xy_zipped = zip(x, y/np.sum(y))
35 | xy = sorted(xy_zipped, key=lambda x: x[1], reverse=True)
36 | xy_cum_sum = 0
37 | hdv = []
38 | for val in xy:
39 | xy_cum_sum += val[1]
40 | hdv.append(val[0])
41 | if xy_cum_sum >= (1-alpha):
42 | break
43 | hdv.sort()
44 | diff = (u-l)/20 # differences of 5%
45 | hpd = []
46 | hpd.append(round(min(hdv), roundto))
47 | for i in range(1, len(hdv)):
48 | if hdv[i]-hdv[i-1] >= diff:
49 | hpd.append(round(hdv[i-1], roundto))
50 | hpd.append(round(hdv[i], roundto))
51 | hpd.append(round(max(hdv), roundto))
52 | ite = iter(hpd)
53 | hpd = list(zip(ite, ite))
54 | modes = []
55 | for value in hpd:
56 | x_hpd = x[(x > value[0]) & (x < value[1])]
57 | y_hpd = y[(x > value[0]) & (x < value[1])]
58 | modes.append(round(x_hpd[np.argmax(y_hpd)], roundto))
59 | return hpd, x, y, modes
60 |
--------------------------------------------------------------------------------
/Chapter 1/mauna_loa_CO2 (1).csv:
--------------------------------------------------------------------------------
1 | 1.959000000000000000e+03,3.154200000000000159e+02
2 | 1.959083000000000084e+03,3.163100000000000023e+02
3 | 1.959166999999999916e+03,3.165000000000000000e+02
4 | 1.959250000000000000e+03,3.175600000000000023e+02
5 | 1.959333000000000084e+03,3.181299999999999955e+02
6 | 1.959416999999999916e+03,3.180000000000000000e+02
7 | 1.959500000000000000e+03,3.163899999999999864e+02
8 | 1.959583000000000084e+03,3.146499999999999773e+02
9 | 1.959666999999999916e+03,3.136800000000000068e+02
10 | 1.959750000000000000e+03,3.131800000000000068e+02
11 | 1.959833000000000084e+03,3.146600000000000250e+02
12 | 1.959916999999999916e+03,3.154300000000000068e+02
13 | 1.960000000000000000e+03,3.162699999999999818e+02
14 | 1.960083000000000084e+03,3.168100000000000023e+02
15 | 1.960166999999999916e+03,3.174200000000000159e+02
16 | 1.960250000000000000e+03,3.188700000000000045e+02
17 | 1.960333000000000084e+03,3.198700000000000045e+02
18 | 1.960416999999999916e+03,3.194300000000000068e+02
19 | 1.960500000000000000e+03,3.180099999999999909e+02
20 | 1.960583000000000084e+03,3.157400000000000091e+02
21 | 1.960666999999999916e+03,3.140000000000000000e+02
22 | 1.960750000000000000e+03,3.136800000000000068e+02
23 | 1.960833000000000084e+03,3.148399999999999750e+02
24 | 1.960916999999999916e+03,3.160299999999999727e+02
25 | 1.961000000000000000e+03,3.167300000000000182e+02
26 | 1.961083000000000084e+03,3.175400000000000205e+02
27 | 1.961166999999999916e+03,3.183799999999999955e+02
28 | 1.961250000000000000e+03,3.193100000000000023e+02
29 | 1.961333000000000084e+03,3.204200000000000159e+02
30 | 1.961416999999999916e+03,3.196100000000000136e+02
31 | 1.961500000000000000e+03,3.184200000000000159e+02
32 | 1.961583000000000084e+03,3.166299999999999955e+02
33 | 1.961666999999999916e+03,3.148299999999999841e+02
34 | 1.961750000000000000e+03,3.151600000000000250e+02
35 | 1.961833000000000084e+03,3.159399999999999977e+02
36 | 1.961916999999999916e+03,3.168500000000000227e+02
37 | 1.962000000000000000e+03,3.177799999999999727e+02
38 | 1.962083000000000084e+03,3.183999999999999773e+02
39 | 1.962166999999999916e+03,3.195299999999999727e+02
40 | 1.962250000000000000e+03,3.204200000000000159e+02
41 | 1.962333000000000084e+03,3.208500000000000227e+02
42 | 1.962416999999999916e+03,3.204499999999999886e+02
43 | 1.962500000000000000e+03,3.194499999999999886e+02
44 | 1.962583000000000084e+03,3.172500000000000000e+02
45 | 1.962666999999999916e+03,3.161100000000000136e+02
46 | 1.962750000000000000e+03,3.152699999999999818e+02
47 | 1.962833000000000084e+03,3.165299999999999727e+02
48 | 1.962916999999999916e+03,3.175299999999999727e+02
49 | 1.963000000000000000e+03,3.185799999999999841e+02
50 | 1.963083000000000084e+03,3.189200000000000159e+02
51 | 1.963166999999999916e+03,3.196999999999999886e+02
52 | 1.963250000000000000e+03,3.212200000000000273e+02
53 | 1.963333000000000084e+03,3.220799999999999841e+02
54 | 1.963416999999999916e+03,3.213100000000000023e+02
55 | 1.963500000000000000e+03,3.195799999999999841e+02
56 | 1.963583000000000084e+03,3.176100000000000136e+02
57 | 1.963666999999999916e+03,3.160500000000000114e+02
58 | 1.963750000000000000e+03,3.158299999999999841e+02
59 | 1.963833000000000084e+03,3.169100000000000250e+02
60 | 1.963916999999999916e+03,3.181999999999999886e+02
61 | 1.964000000000000000e+03,3.194100000000000250e+02
62 | 1.964083000000000084e+03,3.200699999999999932e+02
63 | 1.964166999999999916e+03,3.207400000000000091e+02
64 | 1.964250000000000000e+03,3.213999999999999773e+02
65 | 1.964333000000000084e+03,3.220600000000000023e+02
66 | 1.964416999999999916e+03,3.217300000000000182e+02
67 | 1.964500000000000000e+03,3.202699999999999818e+02
68 | 1.964583000000000084e+03,3.185400000000000205e+02
69 | 1.964666999999999916e+03,3.165400000000000205e+02
70 | 1.964750000000000000e+03,3.167099999999999795e+02
71 | 1.964833000000000084e+03,3.175299999999999727e+02
72 | 1.964916999999999916e+03,3.185500000000000114e+02
73 | 1.965000000000000000e+03,3.192699999999999818e+02
74 | 1.965083000000000084e+03,3.202799999999999727e+02
75 | 1.965166999999999916e+03,3.207300000000000182e+02
76 | 1.965250000000000000e+03,3.219700000000000273e+02
77 | 1.965333000000000084e+03,3.220000000000000000e+02
78 | 1.965416999999999916e+03,3.217099999999999795e+02
79 | 1.965500000000000000e+03,3.210500000000000114e+02
80 | 1.965583000000000084e+03,3.187099999999999795e+02
81 | 1.965666999999999916e+03,3.176600000000000250e+02
82 | 1.965750000000000000e+03,3.171399999999999864e+02
83 | 1.965833000000000084e+03,3.186999999999999886e+02
84 | 1.965916999999999916e+03,3.192500000000000000e+02
85 | 1.966000000000000000e+03,3.204599999999999795e+02
86 | 1.966083000000000084e+03,3.214300000000000068e+02
87 | 1.966166999999999916e+03,3.222300000000000182e+02
88 | 1.966250000000000000e+03,3.235400000000000205e+02
89 | 1.966333000000000084e+03,3.239100000000000250e+02
90 | 1.966416999999999916e+03,3.235899999999999750e+02
91 | 1.966500000000000000e+03,3.222400000000000091e+02
92 | 1.966583000000000084e+03,3.201999999999999886e+02
93 | 1.966666999999999916e+03,3.184800000000000182e+02
94 | 1.966750000000000000e+03,3.179399999999999977e+02
95 | 1.966833000000000084e+03,3.196299999999999955e+02
96 | 1.966916999999999916e+03,3.208700000000000045e+02
97 | 1.967000000000000000e+03,3.221700000000000159e+02
98 | 1.967083000000000084e+03,3.223399999999999750e+02
99 | 1.967166999999999916e+03,3.228799999999999955e+02
100 | 1.967250000000000000e+03,3.242500000000000000e+02
101 | 1.967333000000000084e+03,3.248299999999999841e+02
102 | 1.967416999999999916e+03,3.239300000000000068e+02
103 | 1.967500000000000000e+03,3.223799999999999955e+02
104 | 1.967583000000000084e+03,3.207599999999999909e+02
105 | 1.967666999999999916e+03,3.191000000000000227e+02
106 | 1.967750000000000000e+03,3.192400000000000091e+02
107 | 1.967833000000000084e+03,3.205600000000000023e+02
108 | 1.967916999999999916e+03,3.218000000000000114e+02
109 | 1.968000000000000000e+03,3.223999999999999773e+02
110 | 1.968083000000000084e+03,3.229900000000000091e+02
111 | 1.968166999999999916e+03,3.237300000000000182e+02
112 | 1.968250000000000000e+03,3.248600000000000136e+02
113 | 1.968333000000000084e+03,3.253999999999999773e+02
114 | 1.968416999999999916e+03,3.251999999999999886e+02
115 | 1.968500000000000000e+03,3.239800000000000182e+02
116 | 1.968583000000000084e+03,3.219499999999999886e+02
117 | 1.968666999999999916e+03,3.201800000000000068e+02
118 | 1.968750000000000000e+03,3.200899999999999750e+02
119 | 1.968833000000000084e+03,3.211600000000000250e+02
120 | 1.968916999999999916e+03,3.227400000000000091e+02
121 | 1.969000000000000000e+03,3.238299999999999841e+02
122 | 1.969083000000000084e+03,3.242599999999999909e+02
123 | 1.969166999999999916e+03,3.254700000000000273e+02
124 | 1.969250000000000000e+03,3.265000000000000000e+02
125 | 1.969333000000000084e+03,3.272099999999999795e+02
126 | 1.969416999999999916e+03,3.265400000000000205e+02
127 | 1.969500000000000000e+03,3.257200000000000273e+02
128 | 1.969583000000000084e+03,3.235000000000000000e+02
129 | 1.969666999999999916e+03,3.222200000000000273e+02
130 | 1.969750000000000000e+03,3.216200000000000045e+02
131 | 1.969833000000000084e+03,3.226899999999999977e+02
132 | 1.969916999999999916e+03,3.239499999999999886e+02
133 | 1.970000000000000000e+03,3.248899999999999864e+02
134 | 1.970083000000000084e+03,3.258199999999999932e+02
135 | 1.970166999999999916e+03,3.267699999999999818e+02
136 | 1.970250000000000000e+03,3.279700000000000273e+02
137 | 1.970333000000000084e+03,3.279100000000000250e+02
138 | 1.970416999999999916e+03,3.275000000000000000e+02
139 | 1.970500000000000000e+03,3.261800000000000068e+02
140 | 1.970583000000000084e+03,3.245299999999999727e+02
141 | 1.970666999999999916e+03,3.229300000000000068e+02
142 | 1.970750000000000000e+03,3.228999999999999773e+02
143 | 1.970833000000000084e+03,3.238500000000000227e+02
144 | 1.970916999999999916e+03,3.249599999999999795e+02
145 | 1.971000000000000000e+03,3.260099999999999909e+02
146 | 1.971083000000000084e+03,3.265099999999999909e+02
147 | 1.971166999999999916e+03,3.270099999999999909e+02
148 | 1.971250000000000000e+03,3.276200000000000045e+02
149 | 1.971333000000000084e+03,3.287599999999999909e+02
150 | 1.971416999999999916e+03,3.283999999999999773e+02
151 | 1.971500000000000000e+03,3.271999999999999886e+02
152 | 1.971583000000000084e+03,3.252699999999999818e+02
153 | 1.971666999999999916e+03,3.231999999999999886e+02
154 | 1.971750000000000000e+03,3.233999999999999773e+02
155 | 1.971833000000000084e+03,3.246299999999999955e+02
156 | 1.971916999999999916e+03,3.258500000000000227e+02
157 | 1.972000000000000000e+03,3.266000000000000227e+02
158 | 1.972083000000000084e+03,3.274700000000000273e+02
159 | 1.972166999999999916e+03,3.275799999999999841e+02
160 | 1.972250000000000000e+03,3.295600000000000023e+02
161 | 1.972333000000000084e+03,3.298999999999999773e+02
162 | 1.972416999999999916e+03,3.289200000000000159e+02
163 | 1.972500000000000000e+03,3.278799999999999955e+02
164 | 1.972583000000000084e+03,3.261600000000000250e+02
165 | 1.972666999999999916e+03,3.246800000000000068e+02
166 | 1.972750000000000000e+03,3.250400000000000205e+02
167 | 1.972833000000000084e+03,3.263399999999999750e+02
168 | 1.972916999999999916e+03,3.273899999999999864e+02
169 | 1.973000000000000000e+03,3.283700000000000045e+02
170 | 1.973083000000000084e+03,3.293999999999999773e+02
171 | 1.973166999999999916e+03,3.301399999999999864e+02
172 | 1.973250000000000000e+03,3.313299999999999841e+02
173 | 1.973333000000000084e+03,3.323100000000000023e+02
174 | 1.973416999999999916e+03,3.318999999999999773e+02
175 | 1.973500000000000000e+03,3.306999999999999886e+02
176 | 1.973583000000000084e+03,3.291499999999999773e+02
177 | 1.973666999999999916e+03,3.273500000000000227e+02
178 | 1.973750000000000000e+03,3.270199999999999818e+02
179 | 1.973833000000000084e+03,3.279900000000000091e+02
180 | 1.973916999999999916e+03,3.284800000000000182e+02
181 | 1.974000000000000000e+03,3.291800000000000068e+02
182 | 1.974083000000000084e+03,3.305500000000000114e+02
183 | 1.974166999999999916e+03,3.313199999999999932e+02
184 | 1.974250000000000000e+03,3.324800000000000182e+02
185 | 1.974333000000000084e+03,3.329200000000000159e+02
186 | 1.974416999999999916e+03,3.320799999999999841e+02
187 | 1.974500000000000000e+03,3.310099999999999909e+02
188 | 1.974583000000000084e+03,3.292300000000000182e+02
189 | 1.974666999999999916e+03,3.272699999999999818e+02
190 | 1.974750000000000000e+03,3.272099999999999795e+02
191 | 1.974833000000000084e+03,3.282900000000000205e+02
192 | 1.974916999999999916e+03,3.294100000000000250e+02
193 | 1.975000000000000000e+03,3.302300000000000182e+02
194 | 1.975083000000000084e+03,3.312500000000000000e+02
195 | 1.975166999999999916e+03,3.318700000000000045e+02
196 | 1.975250000000000000e+03,3.331399999999999864e+02
197 | 1.975333000000000084e+03,3.338000000000000114e+02
198 | 1.975416999999999916e+03,3.334300000000000068e+02
199 | 1.975500000000000000e+03,3.317300000000000182e+02
200 | 1.975583000000000084e+03,3.298999999999999773e+02
201 | 1.975666999999999916e+03,3.283999999999999773e+02
202 | 1.975750000000000000e+03,3.281700000000000159e+02
203 | 1.975833000000000084e+03,3.293199999999999932e+02
204 | 1.975916999999999916e+03,3.305899999999999750e+02
205 | 1.976000000000000000e+03,3.315799999999999841e+02
206 | 1.976083000000000084e+03,3.323899999999999864e+02
207 | 1.976166999999999916e+03,3.333299999999999841e+02
208 | 1.976250000000000000e+03,3.344100000000000250e+02
209 | 1.976333000000000084e+03,3.347099999999999795e+02
210 | 1.976416999999999916e+03,3.341700000000000159e+02
211 | 1.976500000000000000e+03,3.328899999999999864e+02
212 | 1.976583000000000084e+03,3.307699999999999818e+02
213 | 1.976666999999999916e+03,3.291399999999999864e+02
214 | 1.976750000000000000e+03,3.287799999999999727e+02
215 | 1.976833000000000084e+03,3.301399999999999864e+02
216 | 1.976916999999999916e+03,3.315199999999999818e+02
217 | 1.977000000000000000e+03,3.327500000000000000e+02
218 | 1.977083000000000084e+03,3.332400000000000091e+02
219 | 1.977166999999999916e+03,3.345299999999999727e+02
220 | 1.977250000000000000e+03,3.358999999999999773e+02
221 | 1.977333000000000084e+03,3.365699999999999932e+02
222 | 1.977416999999999916e+03,3.361000000000000227e+02
223 | 1.977500000000000000e+03,3.347599999999999909e+02
224 | 1.977583000000000084e+03,3.325899999999999750e+02
225 | 1.977666999999999916e+03,3.314200000000000159e+02
226 | 1.977750000000000000e+03,3.309800000000000182e+02
227 | 1.977833000000000084e+03,3.322400000000000091e+02
228 | 1.977916999999999916e+03,3.336800000000000068e+02
229 | 1.978000000000000000e+03,3.348000000000000114e+02
230 | 1.978083000000000084e+03,3.352200000000000273e+02
231 | 1.978166999999999916e+03,3.364700000000000273e+02
232 | 1.978250000000000000e+03,3.375899999999999750e+02
233 | 1.978333000000000084e+03,3.378399999999999750e+02
234 | 1.978416999999999916e+03,3.377200000000000273e+02
235 | 1.978500000000000000e+03,3.363700000000000045e+02
236 | 1.978583000000000084e+03,3.345099999999999909e+02
237 | 1.978666999999999916e+03,3.326000000000000227e+02
238 | 1.978750000000000000e+03,3.323799999999999955e+02
239 | 1.978833000000000084e+03,3.337500000000000000e+02
240 | 1.978916999999999916e+03,3.347799999999999727e+02
241 | 1.979000000000000000e+03,3.360500000000000114e+02
242 | 1.979083000000000084e+03,3.365899999999999750e+02
243 | 1.979166999999999916e+03,3.377900000000000205e+02
244 | 1.979250000000000000e+03,3.387099999999999795e+02
245 | 1.979333000000000084e+03,3.393000000000000114e+02
246 | 1.979416999999999916e+03,3.391200000000000045e+02
247 | 1.979500000000000000e+03,3.375600000000000023e+02
248 | 1.979583000000000084e+03,3.359200000000000159e+02
249 | 1.979666999999999916e+03,3.337500000000000000e+02
250 | 1.979750000000000000e+03,3.336999999999999886e+02
251 | 1.979833000000000084e+03,3.351200000000000045e+02
252 | 1.979916999999999916e+03,3.365600000000000023e+02
253 | 1.980000000000000000e+03,3.378399999999999750e+02
254 | 1.980083000000000084e+03,3.381899999999999977e+02
255 | 1.980166999999999916e+03,3.399100000000000250e+02
256 | 1.980250000000000000e+03,3.406000000000000227e+02
257 | 1.980333000000000084e+03,3.412900000000000205e+02
258 | 1.980416999999999916e+03,3.410000000000000000e+02
259 | 1.980500000000000000e+03,3.393899999999999864e+02
260 | 1.980583000000000084e+03,3.374300000000000068e+02
261 | 1.980666999999999916e+03,3.357200000000000273e+02
262 | 1.980750000000000000e+03,3.358399999999999750e+02
263 | 1.980833000000000084e+03,3.369300000000000068e+02
264 | 1.980916999999999916e+03,3.380400000000000205e+02
265 | 1.981000000000000000e+03,3.390600000000000023e+02
266 | 1.981083000000000084e+03,3.403000000000000114e+02
267 | 1.981166999999999916e+03,3.412099999999999795e+02
268 | 1.981250000000000000e+03,3.423299999999999841e+02
269 | 1.981333000000000084e+03,3.427400000000000091e+02
270 | 1.981416999999999916e+03,3.420799999999999841e+02
271 | 1.981500000000000000e+03,3.403199999999999932e+02
272 | 1.981583000000000084e+03,3.382599999999999909e+02
273 | 1.981666999999999916e+03,3.365199999999999818e+02
274 | 1.981750000000000000e+03,3.366800000000000068e+02
275 | 1.981833000000000084e+03,3.381899999999999977e+02
276 | 1.981916999999999916e+03,3.394399999999999977e+02
277 | 1.982000000000000000e+03,3.405699999999999932e+02
278 | 1.982083000000000084e+03,3.414399999999999977e+02
279 | 1.982166999999999916e+03,3.425299999999999727e+02
280 | 1.982250000000000000e+03,3.433899999999999864e+02
281 | 1.982333000000000084e+03,3.439599999999999795e+02
282 | 1.982416999999999916e+03,3.431800000000000068e+02
283 | 1.982500000000000000e+03,3.418799999999999955e+02
284 | 1.982583000000000084e+03,3.396499999999999773e+02
285 | 1.982666999999999916e+03,3.378100000000000023e+02
286 | 1.982750000000000000e+03,3.376899999999999977e+02
287 | 1.982833000000000084e+03,3.390899999999999750e+02
288 | 1.982916999999999916e+03,3.403199999999999932e+02
289 | 1.983000000000000000e+03,3.411999999999999886e+02
290 | 1.983083000000000084e+03,3.423500000000000227e+02
291 | 1.983166999999999916e+03,3.429300000000000068e+02
292 | 1.983250000000000000e+03,3.447699999999999818e+02
293 | 1.983333000000000084e+03,3.455799999999999841e+02
294 | 1.983416999999999916e+03,3.451399999999999864e+02
295 | 1.983500000000000000e+03,3.438100000000000023e+02
296 | 1.983583000000000084e+03,3.422099999999999795e+02
297 | 1.983666999999999916e+03,3.396899999999999977e+02
298 | 1.983750000000000000e+03,3.398199999999999932e+02
299 | 1.983833000000000084e+03,3.409800000000000182e+02
300 | 1.983916999999999916e+03,3.428199999999999932e+02
301 | 1.984000000000000000e+03,3.435199999999999818e+02
302 | 1.984083000000000084e+03,3.443299999999999841e+02
303 | 1.984166999999999916e+03,3.451100000000000136e+02
304 | 1.984250000000000000e+03,3.468799999999999955e+02
305 | 1.984333000000000084e+03,3.472500000000000000e+02
306 | 1.984416999999999916e+03,3.466200000000000045e+02
307 | 1.984500000000000000e+03,3.452200000000000273e+02
308 | 1.984583000000000084e+03,3.431100000000000136e+02
309 | 1.984666999999999916e+03,3.408999999999999773e+02
310 | 1.984750000000000000e+03,3.411800000000000068e+02
311 | 1.984833000000000084e+03,3.428000000000000114e+02
312 | 1.984916999999999916e+03,3.440400000000000205e+02
313 | 1.985000000000000000e+03,3.447900000000000205e+02
314 | 1.985083000000000084e+03,3.458199999999999932e+02
315 | 1.985166999999999916e+03,3.472500000000000000e+02
316 | 1.985250000000000000e+03,3.481700000000000159e+02
317 | 1.985333000000000084e+03,3.487400000000000091e+02
318 | 1.985416999999999916e+03,3.480699999999999932e+02
319 | 1.985500000000000000e+03,3.463799999999999955e+02
320 | 1.985583000000000084e+03,3.445099999999999909e+02
321 | 1.985666999999999916e+03,3.429200000000000159e+02
322 | 1.985750000000000000e+03,3.426200000000000045e+02
323 | 1.985833000000000084e+03,3.440600000000000023e+02
324 | 1.985916999999999916e+03,3.453799999999999955e+02
325 | 1.986000000000000000e+03,3.461100000000000136e+02
326 | 1.986083000000000084e+03,3.467799999999999727e+02
327 | 1.986166999999999916e+03,3.476800000000000068e+02
328 | 1.986250000000000000e+03,3.493700000000000045e+02
329 | 1.986333000000000084e+03,3.500299999999999727e+02
330 | 1.986416999999999916e+03,3.493700000000000045e+02
331 | 1.986500000000000000e+03,3.477599999999999909e+02
332 | 1.986583000000000084e+03,3.457300000000000182e+02
333 | 1.986666999999999916e+03,3.446800000000000068e+02
334 | 1.986750000000000000e+03,3.439900000000000091e+02
335 | 1.986833000000000084e+03,3.454800000000000182e+02
336 | 1.986916999999999916e+03,3.467200000000000273e+02
337 | 1.987000000000000000e+03,3.478399999999999750e+02
338 | 1.987083000000000084e+03,3.482900000000000205e+02
339 | 1.987166999999999916e+03,3.492300000000000182e+02
340 | 1.987250000000000000e+03,3.508000000000000114e+02
341 | 1.987333000000000084e+03,3.516600000000000250e+02
342 | 1.987416999999999916e+03,3.510699999999999932e+02
343 | 1.987500000000000000e+03,3.493299999999999841e+02
344 | 1.987583000000000084e+03,3.479200000000000159e+02
345 | 1.987666999999999916e+03,3.462699999999999818e+02
346 | 1.987750000000000000e+03,3.461800000000000068e+02
347 | 1.987833000000000084e+03,3.476399999999999864e+02
348 | 1.987916999999999916e+03,3.487799999999999727e+02
349 | 1.988000000000000000e+03,3.502500000000000000e+02
350 | 1.988083000000000084e+03,3.515400000000000205e+02
351 | 1.988166999999999916e+03,3.520500000000000114e+02
352 | 1.988250000000000000e+03,3.534100000000000250e+02
353 | 1.988333000000000084e+03,3.540400000000000205e+02
354 | 1.988416999999999916e+03,3.536200000000000045e+02
355 | 1.988500000000000000e+03,3.522200000000000273e+02
356 | 1.988583000000000084e+03,3.502699999999999818e+02
357 | 1.988666999999999916e+03,3.485500000000000114e+02
358 | 1.988750000000000000e+03,3.487200000000000273e+02
359 | 1.988833000000000084e+03,3.499100000000000250e+02
360 | 1.988916999999999916e+03,3.511800000000000068e+02
361 | 1.989000000000000000e+03,3.526000000000000227e+02
362 | 1.989083000000000084e+03,3.529200000000000159e+02
363 | 1.989166999999999916e+03,3.535299999999999727e+02
364 | 1.989250000000000000e+03,3.552599999999999909e+02
365 | 1.989333000000000084e+03,3.555199999999999818e+02
366 | 1.989416999999999916e+03,3.549700000000000273e+02
367 | 1.989500000000000000e+03,3.537500000000000000e+02
368 | 1.989583000000000084e+03,3.515199999999999818e+02
369 | 1.989666999999999916e+03,3.496399999999999864e+02
370 | 1.989750000000000000e+03,3.498299999999999841e+02
371 | 1.989833000000000084e+03,3.511399999999999864e+02
372 | 1.989916999999999916e+03,3.523700000000000045e+02
373 | 1.990000000000000000e+03,3.535000000000000000e+02
374 | 1.990083000000000084e+03,3.545500000000000114e+02
375 | 1.990166999999999916e+03,3.552300000000000182e+02
376 | 1.990250000000000000e+03,3.560400000000000205e+02
377 | 1.990333000000000084e+03,3.570000000000000000e+02
378 | 1.990416999999999916e+03,3.560699999999999932e+02
379 | 1.990500000000000000e+03,3.546700000000000159e+02
380 | 1.990583000000000084e+03,3.527599999999999909e+02
381 | 1.990666999999999916e+03,3.508199999999999932e+02
382 | 1.990750000000000000e+03,3.510400000000000205e+02
383 | 1.990833000000000084e+03,3.526899999999999977e+02
384 | 1.990916999999999916e+03,3.540699999999999932e+02
385 | 1.991000000000000000e+03,3.545899999999999750e+02
386 | 1.991083000000000084e+03,3.556299999999999955e+02
387 | 1.991166999999999916e+03,3.570299999999999727e+02
388 | 1.991250000000000000e+03,3.584800000000000182e+02
389 | 1.991333000000000084e+03,3.592200000000000273e+02
390 | 1.991416999999999916e+03,3.581200000000000045e+02
391 | 1.991500000000000000e+03,3.560600000000000023e+02
392 | 1.991583000000000084e+03,3.539200000000000159e+02
393 | 1.991666999999999916e+03,3.520500000000000114e+02
394 | 1.991750000000000000e+03,3.521100000000000136e+02
395 | 1.991833000000000084e+03,3.536399999999999864e+02
396 | 1.991916999999999916e+03,3.548899999999999864e+02
397 | 1.992000000000000000e+03,3.558799999999999955e+02
398 | 1.992083000000000084e+03,3.566299999999999955e+02
399 | 1.992166999999999916e+03,3.577200000000000273e+02
400 | 1.992250000000000000e+03,3.590699999999999932e+02
401 | 1.992333000000000084e+03,3.595799999999999841e+02
402 | 1.992416999999999916e+03,3.591700000000000159e+02
403 | 1.992500000000000000e+03,3.569399999999999977e+02
404 | 1.992583000000000084e+03,3.549200000000000159e+02
405 | 1.992666999999999916e+03,3.529399999999999977e+02
406 | 1.992750000000000000e+03,3.532300000000000182e+02
407 | 1.992833000000000084e+03,3.540899999999999750e+02
408 | 1.992916999999999916e+03,3.553299999999999841e+02
409 | 1.993000000000000000e+03,3.566299999999999955e+02
410 | 1.993083000000000084e+03,3.571000000000000227e+02
411 | 1.993166999999999916e+03,3.583199999999999932e+02
412 | 1.993250000000000000e+03,3.594100000000000250e+02
413 | 1.993333000000000084e+03,3.602300000000000182e+02
414 | 1.993416999999999916e+03,3.595500000000000114e+02
415 | 1.993500000000000000e+03,3.575299999999999727e+02
416 | 1.993583000000000084e+03,3.554800000000000182e+02
417 | 1.993666999999999916e+03,3.536700000000000159e+02
418 | 1.993750000000000000e+03,3.539499999999999886e+02
419 | 1.993833000000000084e+03,3.553000000000000114e+02
420 | 1.993916999999999916e+03,3.567799999999999727e+02
421 | 1.994000000000000000e+03,3.583399999999999750e+02
422 | 1.994083000000000084e+03,3.588899999999999864e+02
423 | 1.994166999999999916e+03,3.599499999999999886e+02
424 | 1.994250000000000000e+03,3.612500000000000000e+02
425 | 1.994333000000000084e+03,3.616700000000000159e+02
426 | 1.994416999999999916e+03,3.609399999999999977e+02
427 | 1.994500000000000000e+03,3.595500000000000114e+02
428 | 1.994583000000000084e+03,3.574900000000000091e+02
429 | 1.994666999999999916e+03,3.558399999999999750e+02
430 | 1.994750000000000000e+03,3.560000000000000000e+02
431 | 1.994833000000000084e+03,3.575899999999999750e+02
432 | 1.994916999999999916e+03,3.590500000000000114e+02
433 | 1.995000000000000000e+03,3.599800000000000182e+02
434 | 1.995083000000000084e+03,3.610299999999999727e+02
435 | 1.995166999999999916e+03,3.616600000000000250e+02
436 | 1.995250000000000000e+03,3.634800000000000182e+02
437 | 1.995333000000000084e+03,3.638199999999999932e+02
438 | 1.995416999999999916e+03,3.633000000000000114e+02
439 | 1.995500000000000000e+03,3.619399999999999977e+02
440 | 1.995583000000000084e+03,3.595000000000000000e+02
441 | 1.995666999999999916e+03,3.581100000000000136e+02
442 | 1.995750000000000000e+03,3.578000000000000114e+02
443 | 1.995833000000000084e+03,3.596100000000000136e+02
444 | 1.995916999999999916e+03,3.607400000000000091e+02
445 | 1.996000000000000000e+03,3.620899999999999750e+02
446 | 1.996083000000000084e+03,3.632900000000000205e+02
447 | 1.996166999999999916e+03,3.640600000000000023e+02
448 | 1.996250000000000000e+03,3.647599999999999909e+02
449 | 1.996333000000000084e+03,3.654499999999999886e+02
450 | 1.996416999999999916e+03,3.650099999999999909e+02
451 | 1.996500000000000000e+03,3.636999999999999886e+02
452 | 1.996583000000000084e+03,3.615400000000000205e+02
453 | 1.996666999999999916e+03,3.595099999999999909e+02
454 | 1.996750000000000000e+03,3.596499999999999773e+02
455 | 1.996833000000000084e+03,3.608000000000000114e+02
456 | 1.996916999999999916e+03,3.623799999999999955e+02
457 | 1.997000000000000000e+03,3.632300000000000182e+02
458 | 1.997083000000000084e+03,3.640600000000000023e+02
459 | 1.997166999999999916e+03,3.646100000000000136e+02
460 | 1.997250000000000000e+03,3.663999999999999773e+02
461 | 1.997333000000000084e+03,3.668399999999999750e+02
462 | 1.997416999999999916e+03,3.656800000000000068e+02
463 | 1.997500000000000000e+03,3.645199999999999818e+02
464 | 1.997583000000000084e+03,3.625699999999999932e+02
465 | 1.997666999999999916e+03,3.602400000000000091e+02
466 | 1.997750000000000000e+03,3.608299999999999841e+02
467 | 1.997833000000000084e+03,3.624900000000000091e+02
468 | 1.997916999999999916e+03,3.643399999999999750e+02
469 |
--------------------------------------------------------------------------------
/Chapter 1/plot_post (1).py:
--------------------------------------------------------------------------------
1 | from __future__ import division
2 | import numpy as np
3 | from scipy import stats
4 | import matplotlib.pyplot as plt
5 | from hpd import hpd_grid
6 |
7 |
8 | def plot_post(sample, alpha=0.05, show_mode=True, kde_plot=True, bins=50,
9 | ROPE=None, comp_val=None, roundto=2):
10 | """Plot posterior and HPD
11 |
12 | Parameters
13 | ----------
14 |
15 | sample : Numpy array or python list
16 | An array containing MCMC samples
17 | alpha : float
18 | Desired probability of type I error (defaults to 0.05)
19 | show_mode: Bool
20 | If True the legend will show the mode(s) value(s), if false the mean(s)
21 | will be displayed
22 | kde_plot: Bool
23 | If True the posterior will be displayed using a Kernel Density Estimation
24 | otherwise an histogram will be used
25 | bins: integer
26 | Number of bins used for the histogram, only works when kde_plot is False
27 | ROPE: list or numpy array
28 | Lower and upper values of the Region Of Practical Equivalence
29 | comp_val: float
30 | Comparison value
31 |
32 |
33 | Returns
34 | -------
35 |
36 | post_summary : dictionary
37 | Containing values with several summary statistics
38 |
39 | """
40 |
41 | post_summary = {'mean':0,'median':0,'mode':0, 'alpha':0,'hpd_low':0,
42 | 'hpd_high':0, 'comp_val':0, 'pc_gt_comp_val':0, 'ROPE_low':0,
43 | 'ROPE_high':0, 'pc_in_ROPE':0}
44 |
45 | post_summary['mean'] = round(np.mean(sample), roundto)
46 | post_summary['median'] = round(np.median(sample), roundto)
47 | post_summary['alpha'] = alpha
48 |
49 | # Compute the hpd, KDE and mode for the posterior
50 | hpd, x, y, modes = hpd_grid(sample, alpha, roundto)
51 | post_summary['hpd'] = hpd
52 | post_summary['mode'] = modes
53 |
54 | ## Plot KDE.
55 | if kde_plot:
56 | plt.plot(x, y, color='k', lw=2)
57 | ## Plot histogram.
58 | else:
59 | plt.hist(sample, normed=True, bins=bins, facecolor='b',
60 | edgecolor='w')
61 |
62 | ## Display mode or mean:
63 | if show_mode:
64 | string = '{:g} ' * len(post_summary['mode'])
65 | plt.plot(0, label='mode =' + string.format(*post_summary['mode']), alpha=0)
66 | else:
67 | plt.plot(0, label='mean = {:g}'.format(post_summary['mean']), alpha=0)
68 |
69 | ## Display the hpd.
70 | hpd_label = ''
71 | for value in hpd:
72 | plt.plot(value, [0, 0], linewidth=10, color='b')
73 | hpd_label = hpd_label + '{:g} {:g}\n'.format(round(value[0], roundto), round(value[1], roundto))
74 | plt.plot(0, 0, linewidth=4, color='b', label='hpd {:g}%\n{}'.format((1-alpha)*100, hpd_label))
75 | ## Display the ROPE.
76 | if ROPE is not None:
77 | pc_in_ROPE = round(np.sum((sample > ROPE[0]) & (sample < ROPE[1]))/len(sample)*100, roundto)
78 | plt.plot(ROPE, [0, 0], linewidth=20, color='r', alpha=0.75)
79 | plt.plot(0, 0, linewidth=4, color='r', label='{:g}% in ROPE'.format(pc_in_ROPE))
80 | post_summary['ROPE_low'] = ROPE[0]
81 | post_summary['ROPE_high'] = ROPE[1]
82 | post_summary['pc_in_ROPE'] = pc_in_ROPE
83 | ## Display the comparison value.
84 | if comp_val is not None:
85 | pc_gt_comp_val = round(100 * np.sum(sample > comp_val)/len(sample), roundto)
86 | pc_lt_comp_val = round(100 - pc_gt_comp_val, roundto)
87 | plt.axvline(comp_val, ymax=.75, color='g', linewidth=4, alpha=0.75,
88 | label='{:g}% < {:g} < {:g}%'.format(pc_lt_comp_val,
89 | comp_val, pc_gt_comp_val))
90 | post_summary['comp_val'] = comp_val
91 | post_summary['pc_gt_comp_val'] = pc_gt_comp_val
92 |
93 | plt.legend(loc=0, framealpha=1)
94 | frame = plt.gca()
95 | frame.axes.get_yaxis().set_ticks([])
96 | return post_summary
97 |
98 |
99 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2016 Packt
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | # Bayesian Analysis with Python
5 |
6 | This is the code repository for [Bayesian Analysis with Python](https://www.packtpub.com/big-data-and-business-intelligence/bayesian-analysis-python?utm_source=github&utm_medium=repository&utm_campaign=9781785883804), published by Packt. It contains all the supporting project files necessary to work through the book from start to finish.
7 |
8 | **Please follow this [link](https://github.com/aloctavodia/BAP) for an updated version of the code that have been tested to run with the last version of PyMC3.**
9 |
10 |
11 | ## Instructions and Navigations
12 |
13 | All of the code is organized into folders. Each folder starts with a number followed by the chapter name.
14 |
15 | This book is written for Python version >= 3.5, and it is recommended that you use
16 | the most recent version of Python 3 that is currently available, although most of the
17 | code examples may also run for older versions of Python, including Python 2.7 with
18 | minor adjustments.
19 |
20 | Maybe the easiest way to install Python and Python libraries is using Anaconda,
21 | a scientific computing distribution. You can read more about Anaconda and
22 | download it [here](https://www.continuum.io/downloads). Once Anaconda is in
23 | our system, we can install new Python packages with this command:
24 |
25 | ```conda install NamePackage```
26 |
27 | We will use the following python packages:
28 |
29 |
30 | * Ipython 5.0
31 | * NumPy 1.11.1
32 | * SciPy 0.18.1
33 | * Pandas 0.18.1
34 | * Matplotlib 1.5.3
35 | * Seaborn 0.7.1
36 | * PyMC3 3.0
37 |
38 | ## Errata
39 |
40 | If you find an error in the book please fill an issue or send a PR [here](https://github.com/aloctavodia/BAP)
41 | ### Download a free PDF
42 |
43 | If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF.
44 |