|
20 | 20 | "outputs": [],
|
21 | 21 | "source": [
|
22 | 22 | "%config InlineBackend.figure_format = 'retina'\n",
|
23 |
| - "az.style.use('arviz-darkgrid')" |
| 23 | + "az.style.use(\"arviz-darkgrid\")" |
24 | 24 | ]
|
25 | 25 | },
|
26 | 26 | {
|
|
75 | 75 | "outputs": [],
|
76 | 76 | "source": [
|
77 | 77 | "def posterior_grid_approx(grid_points=100, success=6, tosses=9):\n",
|
78 |
| - " \"\"\"\n", |
79 |
| - " \"\"\"\n", |
| 78 | + " \"\"\"\"\"\"\n", |
80 | 79 | " # define grid\n",
|
81 | 80 | " p_grid = np.linspace(0, 1, grid_points)\n",
|
82 | 81 | "\n",
|
83 | 82 | " # define prior\n",
|
84 | 83 | " 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", |
87 | 86 | "\n",
|
88 | 87 | " # compute likelihood at each point in the grid\n",
|
89 | 88 | " likelihood = stats.binom.pmf(success, tosses, p_grid)\n",
|
|
128 | 127 | }
|
129 | 128 | ],
|
130 | 129 | "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", |
135 | 134 | "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);" |
138 | 137 | ]
|
139 | 138 | },
|
140 | 139 | {
|
|
161 | 160 | }
|
162 | 161 | ],
|
163 | 162 | "source": [
|
164 |
| - "sum(posterior[ p_grid < 0.5 ])" |
| 163 | + "sum(posterior[p_grid < 0.5])" |
165 | 164 | ]
|
166 | 165 | },
|
167 | 166 | {
|
|
188 | 187 | }
|
189 | 188 | ],
|
190 | 189 | "source": [
|
191 |
| - "sum( samples < 0.5 ) / 1e4" |
| 190 | + "sum(samples < 0.5) / 1e4" |
192 | 191 | ]
|
193 | 192 | },
|
194 | 193 | {
|
|
248 | 247 | }
|
249 | 248 | ],
|
250 | 249 | "source": [
|
251 |
| - "# plotting out intervals of defined boundaries: \n", |
| 250 | + "# plotting out intervals of defined boundaries:\n", |
252 | 251 | "\n",
|
253 | 252 | "# wider figure\n",
|
254 |
| - "plt.figure(figsize=(20,20)) \n", |
| 253 | + "plt.figure(figsize=(20, 20))\n", |
255 | 254 | "\n",
|
256 | 255 | "### Intervals of defined boundaries:\n",
|
257 | 256 | "\n",
|
258 | 257 | "# plot p < 0.5\n",
|
259 | 258 | "plt.subplot(2, 2, 1)\n",
|
260 | 259 | "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", |
265 | 264 | "\n",
|
266 | 265 | "# plot p < 0.5 & p > 0.75\n",
|
267 | 266 | "plt.subplot(2, 2, 2)\n",
|
268 | 267 | "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", |
273 | 272 | "\n",
|
274 | 273 | "### Intervals of defined mass:\n",
|
275 | 274 | "\n",
|
276 | 275 | "# plot p < 0.5\n",
|
277 | 276 | "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", |
283 | 282 | "plt.legend(loc=0)\n",
|
284 | 283 | "\n",
|
285 | 284 | "# plot p < 0.5 & p > 0.75\n",
|
286 | 285 | "perc_range = np.percentile(samples, [10, 90])\n",
|
287 | 286 | "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", |
293 | 292 | "plt.legend(loc=0);"
|
294 | 293 | ]
|
295 | 294 | },
|
|
378 | 377 | "source": [
|
379 | 378 | "p_grid, posterior = posterior_grid_approx(success=3, tosses=3)\n",
|
380 | 379 | "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);" |
383 | 382 | ]
|
384 | 383 | },
|
385 | 384 | {
|
|
467 | 466 | ],
|
468 | 467 | "source": [
|
469 | 468 | "# wider figure\n",
|
470 |
| - "plt.figure(figsize=(20,6)) \n", |
| 469 | + "plt.figure(figsize=(20, 6))\n", |
471 | 470 | "\n",
|
472 |
| - "# calculate posterior: \n", |
| 471 | + "# calculate posterior:\n", |
473 | 472 | "p_grid, posterior = posterior_grid_approx(success=3, tosses=3)\n",
|
474 | 473 | "\n",
|
475 | 474 | "# PI\n",
|
|
478 | 477 | "# PI Plot\n",
|
479 | 478 | "plt.subplot(1, 2, 1)\n",
|
480 | 479 | "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", |
485 | 484 | "\n",
|
486 | 485 | "# HDPI\n",
|
487 | 486 | "hdpi_interval = az.hpd(samples, credible_interval=0.5)\n",
|
488 | 487 | "\n",
|
489 | 488 | "# HDPI Plot\n",
|
490 | 489 | "plt.subplot(1, 2, 2)\n",
|
491 | 490 | "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\");" |
496 | 495 | ]
|
497 | 496 | },
|
498 | 497 | {
|
|
660 | 659 | }
|
661 | 660 | ],
|
662 | 661 | "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", |
667 | 666 | "plt.legend(loc=0);"
|
668 | 667 | ]
|
669 | 668 | },
|
|
806 | 805 | ],
|
807 | 806 | "source": [
|
808 | 807 | "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", |
811 | 810 | "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);" |
814 | 813 | ]
|
815 | 814 | },
|
816 | 815 | {
|
|
844 | 843 | "metadata": {},
|
845 | 844 | "outputs": [],
|
846 | 845 | "source": [
|
| 846 | + "# fmt: off\n", |
847 | 847 | "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",
|
848 | 848 | "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",
|
849 | 849 | "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",
|
850 | 850 | "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" |
852 | 853 | ]
|
853 | 854 | },
|
854 | 855 | {
|
|
914 | 915 | "import matplotlib\n",
|
915 | 916 | "import scipy\n",
|
916 | 917 | "\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 | + ")" |
918 | 929 | ]
|
919 | 930 | }
|
920 | 931 | ],
|
|
0 commit comments