Skip to content

Commit db9986e

Browse files
committed
run black formatter on Rethinking
1 parent faf03f1 commit db9986e

19 files changed

+3372
-2647
lines changed

Rethinking/Chp_02.ipynb

+31-24
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"outputs": [],
2121
"source": [
2222
"%config InlineBackend.figure_format = 'retina'\n",
23-
"az.style.use('arviz-darkgrid')"
23+
"az.style.use(\"arviz-darkgrid\")"
2424
]
2525
},
2626
{
@@ -101,15 +101,14 @@
101101
"outputs": [],
102102
"source": [
103103
"def posterior_grid_approx(grid_points=5, success=6, tosses=9):\n",
104-
" \"\"\"\n",
105-
" \"\"\"\n",
104+
" \"\"\"\"\"\"\n",
106105
" # define grid\n",
107106
" p_grid = np.linspace(0, 1, grid_points)\n",
108107
"\n",
109108
" # define prior\n",
110109
" prior = np.repeat(5, grid_points) # uniform\n",
111-
" #prior = (p_grid >= 0.5).astype(int) # truncated\n",
112-
" #prior = np.exp(- 5 * abs(p_grid - 0.5)) # double exp\n",
110+
" # prior = (p_grid >= 0.5).astype(int) # truncated\n",
111+
" # prior = np.exp(- 5 * abs(p_grid - 0.5)) # double exp\n",
113112
"\n",
114113
" # compute likelihood at each point in the grid\n",
115114
" likelihood = stats.binom.pmf(success, tosses, p_grid)\n",
@@ -154,10 +153,10 @@
154153
"points = 20\n",
155154
"w, n = 6, 9\n",
156155
"p_grid, posterior = posterior_grid_approx(points, w, n)\n",
157-
"plt.plot(p_grid, posterior, 'o-', label=f'success = {w}\\ntosses = {n}')\n",
158-
"plt.xlabel('probability of water', fontsize=14)\n",
159-
"plt.ylabel('posterior probability', fontsize=14)\n",
160-
"plt.title(f'{points} points')\n",
156+
"plt.plot(p_grid, posterior, \"o-\", label=f\"success = {w}\\ntosses = {n}\")\n",
157+
"plt.xlabel(\"probability of water\", fontsize=14)\n",
158+
"plt.ylabel(\"posterior probability\", fontsize=14)\n",
159+
"plt.title(f\"{points} points\")\n",
161160
"plt.legend(loc=0);"
162161
]
163162
},
@@ -198,11 +197,11 @@
198197
"source": [
199198
"data = np.repeat((0, 1), (3, 6))\n",
200199
"with pm.Model() as normal_aproximation:\n",
201-
" p = pm.Uniform('p', 0, 1)\n",
202-
" w = pm.Binomial('w', n=len(data), p=p, observed=data.sum())\n",
200+
" p = pm.Uniform(\"p\", 0, 1)\n",
201+
" w = pm.Binomial(\"w\", n=len(data), p=p, observed=data.sum())\n",
203202
" mean_q = pm.find_MAP()\n",
204-
" std_q = ((1/pm.find_hessian(mean_q, vars=[p]))**0.5)[0]\n",
205-
"mean_q['p'], std_q"
203+
" std_q = ((1 / pm.find_hessian(mean_q, vars=[p])) ** 0.5)[0]\n",
204+
"mean_q[\"p\"], std_q"
206205
]
207206
},
208207
{
@@ -223,9 +222,9 @@
223222
],
224223
"source": [
225224
"norm = stats.norm(mean_q, std_q)\n",
226-
"prob = .89\n",
227-
"z = stats.norm.ppf([(1-prob)/2, (1+prob)/2])\n",
228-
"pi = mean_q['p'] + std_q * z \n",
225+
"prob = 0.89\n",
226+
"z = stats.norm.ppf([(1 - prob) / 2, (1 + prob) / 2])\n",
227+
"pi = mean_q[\"p\"] + std_q * z\n",
229228
"pi"
230229
]
231230
},
@@ -261,17 +260,15 @@
261260
"# analytical calculation\n",
262261
"w, n = 6, 9\n",
263262
"x = np.linspace(0, 1, 100)\n",
264-
"plt.plot(x, stats.beta.pdf(x , w+1, n-w+1),\n",
265-
" label='True posterior')\n",
263+
"plt.plot(x, stats.beta.pdf(x, w + 1, n - w + 1), label=\"True posterior\")\n",
266264
"\n",
267265
"# quadratic approximation\n",
268-
"plt.plot(x, stats.norm.pdf(x, mean_q['p'], std_q),\n",
269-
" label='Quadratic approximation')\n",
266+
"plt.plot(x, stats.norm.pdf(x, mean_q[\"p\"], std_q), label=\"Quadratic approximation\")\n",
270267
"plt.legend(loc=0, fontsize=13)\n",
271268
"\n",
272-
"plt.title(f'n = {n}', fontsize=14)\n",
273-
"plt.xlabel('Proportion water', fontsize=14)\n",
274-
"plt.ylabel('Density', fontsize=14);"
269+
"plt.title(f\"n = {n}\", fontsize=14)\n",
270+
"plt.xlabel(\"Proportion water\", fontsize=14)\n",
271+
"plt.ylabel(\"Density\", fontsize=14);"
275272
]
276273
},
277274
{
@@ -303,7 +300,17 @@
303300
"import matplotlib\n",
304301
"import scipy\n",
305302
"\n",
306-
"print(\"\"\"This notebook was created using:\\nPython {}\\nIPython {}\\nPyMC3 {}\\nArviZ {}\\nNumPy {}\\nSciPy {}\\nMatplotlib {}\\n\"\"\".format(sys.version[:5], IPython.__version__, pm.__version__, az.__version__, np.__version__, scipy.__version__, matplotlib.__version__))"
303+
"print(\n",
304+
" \"\"\"This notebook was created using:\\nPython {}\\nIPython {}\\nPyMC3 {}\\nArviZ {}\\nNumPy {}\\nSciPy {}\\nMatplotlib {}\\n\"\"\".format(\n",
305+
" sys.version[:5],\n",
306+
" IPython.__version__,\n",
307+
" pm.__version__,\n",
308+
" az.__version__,\n",
309+
" np.__version__,\n",
310+
" scipy.__version__,\n",
311+
" matplotlib.__version__,\n",
312+
" )\n",
313+
")"
307314
]
308315
}
309316
],

Rethinking/Chp_03.ipynb

+66-55
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"outputs": [],
2121
"source": [
2222
"%config InlineBackend.figure_format = 'retina'\n",
23-
"az.style.use('arviz-darkgrid')"
23+
"az.style.use(\"arviz-darkgrid\")"
2424
]
2525
},
2626
{
@@ -75,15 +75,14 @@
7575
"outputs": [],
7676
"source": [
7777
"def posterior_grid_approx(grid_points=100, success=6, tosses=9):\n",
78-
" \"\"\"\n",
79-
" \"\"\"\n",
78+
" \"\"\"\"\"\"\n",
8079
" # define grid\n",
8180
" p_grid = np.linspace(0, 1, grid_points)\n",
8281
"\n",
8382
" # define prior\n",
8483
" prior = np.repeat(5, grid_points) # uniform\n",
85-
" #prior = (p_grid >= 0.5).astype(int) # truncated\n",
86-
" #prior = np.exp(- 5 * abs(p_grid - 0.5)) # double exp\n",
84+
" # prior = (p_grid >= 0.5).astype(int) # truncated\n",
85+
" # prior = np.exp(- 5 * abs(p_grid - 0.5)) # double exp\n",
8786
"\n",
8887
" # compute likelihood at each point in the grid\n",
8988
" likelihood = stats.binom.pmf(success, tosses, p_grid)\n",
@@ -128,13 +127,13 @@
128127
}
129128
],
130129
"source": [
131-
"_, (ax0, ax1) = plt.subplots(1,2, figsize=(12,6))\n",
132-
"ax0.plot(samples, 'o', alpha=0.2)\n",
133-
"ax0.set_xlabel('sample number', fontsize=14)\n",
134-
"ax0.set_ylabel('proportion water (p)', fontsize=14)\n",
130+
"_, (ax0, ax1) = plt.subplots(1, 2, figsize=(12, 6))\n",
131+
"ax0.plot(samples, \"o\", alpha=0.2)\n",
132+
"ax0.set_xlabel(\"sample number\", fontsize=14)\n",
133+
"ax0.set_ylabel(\"proportion water (p)\", fontsize=14)\n",
135134
"az.plot_kde(samples, ax=ax1)\n",
136-
"ax1.set_xlabel('proportion water (p)', fontsize=14)\n",
137-
"ax1.set_ylabel('density', fontsize=14);"
135+
"ax1.set_xlabel(\"proportion water (p)\", fontsize=14)\n",
136+
"ax1.set_ylabel(\"density\", fontsize=14);"
138137
]
139138
},
140139
{
@@ -161,7 +160,7 @@
161160
}
162161
],
163162
"source": [
164-
"sum(posterior[ p_grid < 0.5 ])"
163+
"sum(posterior[p_grid < 0.5])"
165164
]
166165
},
167166
{
@@ -188,7 +187,7 @@
188187
}
189188
],
190189
"source": [
191-
"sum( samples < 0.5 ) / 1e4"
190+
"sum(samples < 0.5) / 1e4"
192191
]
193192
},
194193
{
@@ -248,48 +247,48 @@
248247
}
249248
],
250249
"source": [
251-
"# plotting out intervals of defined boundaries: \n",
250+
"# plotting out intervals of defined boundaries:\n",
252251
"\n",
253252
"# wider figure\n",
254-
"plt.figure(figsize=(20,20)) \n",
253+
"plt.figure(figsize=(20, 20))\n",
255254
"\n",
256255
"### Intervals of defined boundaries:\n",
257256
"\n",
258257
"# plot p < 0.5\n",
259258
"plt.subplot(2, 2, 1)\n",
260259
"plt.plot(p_grid, posterior)\n",
261-
"plt.xlabel('proportion of water (p)', fontsize=14)\n",
262-
"plt.ylabel('Density', fontsize=14)\n",
263-
"plt.xticks([0,0.25,0.50,0.75,1.00])\n",
264-
"plt.fill_between(p_grid, posterior, where = p_grid < 0.5)\n",
260+
"plt.xlabel(\"proportion of water (p)\", fontsize=14)\n",
261+
"plt.ylabel(\"Density\", fontsize=14)\n",
262+
"plt.xticks([0, 0.25, 0.50, 0.75, 1.00])\n",
263+
"plt.fill_between(p_grid, posterior, where=p_grid < 0.5)\n",
265264
"\n",
266265
"# plot p < 0.5 & p > 0.75\n",
267266
"plt.subplot(2, 2, 2)\n",
268267
"plt.plot(p_grid, posterior)\n",
269-
"plt.xlabel('proportion of water (p)', fontsize=14)\n",
270-
"plt.ylabel('Density', fontsize=14)\n",
271-
"plt.xticks([0,0.25,0.50,0.75,1.00])\n",
272-
"plt.fill_between(p_grid, posterior, where = (p_grid > 0.5)&(p_grid < 0.75))\n",
268+
"plt.xlabel(\"proportion of water (p)\", fontsize=14)\n",
269+
"plt.ylabel(\"Density\", fontsize=14)\n",
270+
"plt.xticks([0, 0.25, 0.50, 0.75, 1.00])\n",
271+
"plt.fill_between(p_grid, posterior, where=(p_grid > 0.5) & (p_grid < 0.75))\n",
273272
"\n",
274273
"### Intervals of defined mass:\n",
275274
"\n",
276275
"# plot p < 0.5\n",
277276
"plt.subplot(2, 2, 3)\n",
278-
"plt.plot(p_grid, posterior, label = \"lower 80%\")\n",
279-
"plt.xlabel('proportion of water (p)', fontsize=14)\n",
280-
"plt.ylabel('Density', fontsize=14)\n",
281-
"plt.xticks([0,0.25,0.50,0.75,1.00])\n",
282-
"plt.fill_between(p_grid, posterior, where = p_grid < np.percentile(samples, 80))\n",
277+
"plt.plot(p_grid, posterior, label=\"lower 80%\")\n",
278+
"plt.xlabel(\"proportion of water (p)\", fontsize=14)\n",
279+
"plt.ylabel(\"Density\", fontsize=14)\n",
280+
"plt.xticks([0, 0.25, 0.50, 0.75, 1.00])\n",
281+
"plt.fill_between(p_grid, posterior, where=p_grid < np.percentile(samples, 80))\n",
283282
"plt.legend(loc=0)\n",
284283
"\n",
285284
"# plot p < 0.5 & p > 0.75\n",
286285
"perc_range = np.percentile(samples, [10, 90])\n",
287286
"plt.subplot(2, 2, 4)\n",
288-
"plt.plot(p_grid, posterior,label = \"middle 80%\")\n",
289-
"plt.xlabel('proportion of water (p)', fontsize=14)\n",
290-
"plt.ylabel('Density', fontsize=14)\n",
291-
"plt.xticks([0,0.25,0.50,0.75,1.00])\n",
292-
"plt.fill_between(p_grid, posterior, where = (p_grid > perc_range[0])&(p_grid < perc_range[1]))\n",
287+
"plt.plot(p_grid, posterior, label=\"middle 80%\")\n",
288+
"plt.xlabel(\"proportion of water (p)\", fontsize=14)\n",
289+
"plt.ylabel(\"Density\", fontsize=14)\n",
290+
"plt.xticks([0, 0.25, 0.50, 0.75, 1.00])\n",
291+
"plt.fill_between(p_grid, posterior, where=(p_grid > perc_range[0]) & (p_grid < perc_range[1]))\n",
293292
"plt.legend(loc=0);"
294293
]
295294
},
@@ -378,8 +377,8 @@
378377
"source": [
379378
"p_grid, posterior = posterior_grid_approx(success=3, tosses=3)\n",
380379
"plt.plot(p_grid, posterior)\n",
381-
"plt.xlabel('proportion water (p)', fontsize=14)\n",
382-
"plt.ylabel('Density', fontsize=14);"
380+
"plt.xlabel(\"proportion water (p)\", fontsize=14)\n",
381+
"plt.ylabel(\"Density\", fontsize=14);"
383382
]
384383
},
385384
{
@@ -467,9 +466,9 @@
467466
],
468467
"source": [
469468
"# wider figure\n",
470-
"plt.figure(figsize=(20,6)) \n",
469+
"plt.figure(figsize=(20, 6))\n",
471470
"\n",
472-
"# calculate posterior: \n",
471+
"# calculate posterior:\n",
473472
"p_grid, posterior = posterior_grid_approx(success=3, tosses=3)\n",
474473
"\n",
475474
"# PI\n",
@@ -478,21 +477,21 @@
478477
"# PI Plot\n",
479478
"plt.subplot(1, 2, 1)\n",
480479
"plt.plot(p_grid, posterior)\n",
481-
"plt.xlabel('proportion water (p)', fontsize=14)\n",
482-
"plt.ylabel('density', fontsize=14)\n",
483-
"plt.fill_between(p_grid, posterior, where = (p_grid > pi_interval[0]) & (p_grid < pi_interval[1]))\n",
484-
"plt.title('50% Percentile Interval')\n",
480+
"plt.xlabel(\"proportion water (p)\", fontsize=14)\n",
481+
"plt.ylabel(\"density\", fontsize=14)\n",
482+
"plt.fill_between(p_grid, posterior, where=(p_grid > pi_interval[0]) & (p_grid < pi_interval[1]))\n",
483+
"plt.title(\"50% Percentile Interval\")\n",
485484
"\n",
486485
"# HDPI\n",
487486
"hdpi_interval = az.hpd(samples, credible_interval=0.5)\n",
488487
"\n",
489488
"# HDPI Plot\n",
490489
"plt.subplot(1, 2, 2)\n",
491490
"plt.plot(p_grid, posterior)\n",
492-
"plt.xlabel('proportion water (p)', fontsize=14)\n",
493-
"plt.ylabel('density', fontsize=14)\n",
494-
"plt.fill_between(p_grid, posterior, where = (p_grid > hdpi_interval[0]) & (p_grid < hdpi_interval[1]))\n",
495-
"plt.title('50% HDPI');"
491+
"plt.xlabel(\"proportion water (p)\", fontsize=14)\n",
492+
"plt.ylabel(\"density\", fontsize=14)\n",
493+
"plt.fill_between(p_grid, posterior, where=(p_grid > hdpi_interval[0]) & (p_grid < hdpi_interval[1]))\n",
494+
"plt.title(\"50% HDPI\");"
496495
]
497496
},
498497
{
@@ -660,10 +659,10 @@
660659
}
661660
],
662661
"source": [
663-
"plt.plot(p_grid, loss, markevery =p_grid[loss == min(loss)][0], marker = \"o\", label = \"min loss\")\n",
664-
"plt.xlabel('decision', fontsize=14)\n",
665-
"plt.ylabel('expected proportional loss', fontsize=14)\n",
666-
"plt.title(f'Loss Function')\n",
662+
"plt.plot(p_grid, loss, markevery=p_grid[loss == min(loss)][0], marker=\"o\", label=\"min loss\")\n",
663+
"plt.xlabel(\"decision\", fontsize=14)\n",
664+
"plt.ylabel(\"expected proportional loss\", fontsize=14)\n",
665+
"plt.title(f\"Loss Function\")\n",
667666
"plt.legend(loc=0);"
668667
]
669668
},
@@ -806,11 +805,11 @@
806805
],
807806
"source": [
808807
"dummy_w = stats.binom.rvs(n=9, p=0.7, size=int(1e5))\n",
809-
"#dummy_w = stats.binom.rvs(n=9, p=0.6, size=int(1e4))\n",
810-
"#dummy_w = stats.binom.rvs(n=9, p=samples)\n",
808+
"# dummy_w = stats.binom.rvs(n=9, p=0.6, size=int(1e4))\n",
809+
"# dummy_w = stats.binom.rvs(n=9, p=samples)\n",
811810
"plt.hist(dummy_w, bins=50)\n",
812-
"plt.xlabel('dummy water count', fontsize=14)\n",
813-
"plt.ylabel('Frequency', fontsize=14);"
811+
"plt.xlabel(\"dummy water count\", fontsize=14)\n",
812+
"plt.ylabel(\"Frequency\", fontsize=14);"
814813
]
815814
},
816815
{
@@ -844,11 +843,13 @@
844843
"metadata": {},
845844
"outputs": [],
846845
"source": [
846+
"# fmt: off\n",
847847
"birth1 = np.array([1,0,0,0,1,1,0,1,0,1,0,0,1,1,0,1,1,0,0,0,1,0,0,0,1,0, 0,0,0,1,1,1,0,1,0,1,1,1,0,1,0,1,1,0,1,0,0,1,1,0,1,0,0,0,0,0,0,0, 1,1,0,1,0,0,1,0,0,0,1,0,0,1,1,1,1,0,1,0,1,1,1,1,1,0,0,1,0,1,1,0,1,0,1,1,1,0,1,1,1,1])\n",
848848
"birth2 = np.array([0,1,0,1,0,1,1,1,0,0,1,1,1,1,1,0,0,1,1,1,0,0,1,1,1,0,\n",
849849
"1,1,1,0,1,1,1,0,1,0,0,1,1,1,1,0,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,\n",
850850
"1,1,1,0,1,1,0,1,1,0,1,1,1,0,0,0,0,0,0,1,0,0,0,1,1,0,0,1,0,0,1,1,\n",
851-
"0,0,0,1,1,1,0,0,0,0])"
851+
"0,0,0,1,1,1,0,0,0,0])\n",
852+
"# fmt: on"
852853
]
853854
},
854855
{
@@ -914,7 +915,17 @@
914915
"import matplotlib\n",
915916
"import scipy\n",
916917
"\n",
917-
"print(\"\"\"This notebook was created using:\\nPython {}\\nIPython {}\\nPyMC3 {}\\nArviZ {}\\nNumPy {}\\nSciPy {}\\nMatplotlib {}\\n\"\"\".format(sys.version[:5], IPython.__version__, pm.__version__, az.__version__, np.__version__, scipy.__version__, matplotlib.__version__))"
918+
"print(\n",
919+
" \"\"\"This notebook was created using:\\nPython {}\\nIPython {}\\nPyMC3 {}\\nArviZ {}\\nNumPy {}\\nSciPy {}\\nMatplotlib {}\\n\"\"\".format(\n",
920+
" sys.version[:5],\n",
921+
" IPython.__version__,\n",
922+
" pm.__version__,\n",
923+
" az.__version__,\n",
924+
" np.__version__,\n",
925+
" scipy.__version__,\n",
926+
" matplotlib.__version__,\n",
927+
" )\n",
928+
")"
918929
]
919930
}
920931
],

0 commit comments

Comments
 (0)