|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "<p align=\"center\">\n", |
| 8 | + " <img src=\"https://github.com/GeostatsGuy/GeostatsPy/blob/master/TCG_color_logo.png?raw=true\" width=\"220\" height=\"240\" />\n", |
| 9 | + "\n", |
| 10 | + "</p>\n", |
| 11 | + "\n", |
| 12 | + "## Interactive Bayesian Coin Demonstration from Sivia (1996)\n", |
| 13 | + "\n", |
| 14 | + "### Sivia, D.S., 1996, Data Analysis: A Bayesian Tutorial\n", |
| 15 | + "\n", |
| 16 | + "* interactive plot demonstration with ipywidget package\n", |
| 17 | + "\n", |
| 18 | + "#### Michael Pyrcz, Associate Professor, University of Texas at Austin \n", |
| 19 | + "\n", |
| 20 | + "##### [Twitter](https://twitter.com/geostatsguy) | [GitHub](https://github.com/GeostatsGuy) | [Website](http://michaelpyrcz.com) | [GoogleScholar](https://scholar.google.com/citations?user=QVZ20eQAAAAJ&hl=en&oi=ao) | [Book](https://www.amazon.com/Geostatistical-Reservoir-Modeling-Michael-Pyrcz/dp/0199731446) | [YouTube](https://www.youtube.com/channel/UCLqEr-xV-ceHdXXXrTId5ig) | [LinkedIn](https://www.linkedin.com/in/michael-pyrcz-61a648a1) | [GeostatsPy](https://github.com/GeostatsGuy/GeostatsPy)\n", |
| 21 | + "\n", |
| 22 | + "#### The Bayesian Coin Example\n", |
| 23 | + "\n", |
| 24 | + "I have a coin and you need to figure out if it is a fair coin!\n", |
| 25 | + "\n", |
| 26 | + "* a fair coin would have a 50% probability of heads (and a 50% probability of tails)\n", |
| 27 | + "\n", |
| 28 | + "You start with your prior assessment of my coin, a prior distribution over the probability of heads $Prob(Coin)$\n", |
| 29 | + "\n", |
| 30 | + "* it could be based on how honest you think I am\n", |
| 31 | + "\n", |
| 32 | + "Then you perform a set of coin tosses to build a likelihood distribution, $P(Tosses | Coin)$\n", |
| 33 | + "\n", |
| 34 | + "* the more coin tosses, the narrower this distribution\n", |
| 35 | + "\n", |
| 36 | + "Then you update the prior distribution with the likelihood distribution to get the posterior distribution, $P(Coin | Tosses)$.\n", |
| 37 | + "\n", |
| 38 | + "\n", |
| 39 | + "\\begin{equation}\n", |
| 40 | + "P( Coin | Tosses ) = \\frac{P( Tosses | Coin ) P( Coin )}{P( Tosses )}\n", |
| 41 | + "\\end{equation}\n", |
| 42 | + "\n", |
| 43 | + "\n", |
| 44 | + "#### Objective \n", |
| 45 | + "\n", |
| 46 | + "Provide an example and demonstration for:\n", |
| 47 | + "\n", |
| 48 | + "1. interactive plotting in Jupyter Notebooks with Python packages matplotlib and ipywidgets\n", |
| 49 | + "2. provide an intuitive hands-on example of Bayesian updating \n", |
| 50 | + "\n", |
| 51 | + "#### Getting Started\n", |
| 52 | + "\n", |
| 53 | + "Here's the steps to get setup in Python with the GeostatsPy package:\n", |
| 54 | + "\n", |
| 55 | + "1. Install Anaconda 3 on your machine (https://www.anaconda.com/download/). \n", |
| 56 | + "2. Open Jupyter and in the top block get started by copy and pasting the code block below from this Jupyter Notebook to start using the geostatspy functionality. \n", |
| 57 | + "\n", |
| 58 | + "#### Load the Required Libraries\n", |
| 59 | + "\n", |
| 60 | + "The following code loads the required libraries." |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "code", |
| 65 | + "execution_count": 6, |
| 66 | + "metadata": {}, |
| 67 | + "outputs": [], |
| 68 | + "source": [ |
| 69 | + "%matplotlib inline\n", |
| 70 | + "from ipywidgets import interactive # widgets and interactivity\n", |
| 71 | + "from ipywidgets import widgets # widgets and interactivity\n", |
| 72 | + "import matplotlib.pyplot as plt # plotting\n", |
| 73 | + "import numpy as np # working with arrays\n", |
| 74 | + "from scipy.stats import triang # parametric distributions\n", |
| 75 | + "from scipy.stats import binom\n", |
| 76 | + "from scipy.stats import norm" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "markdown", |
| 81 | + "metadata": {}, |
| 82 | + "source": [ |
| 83 | + "#### Make Our Interactive Plot \n", |
| 84 | + "\n", |
| 85 | + "For this demonstration we will: \n", |
| 86 | + "\n", |
| 87 | + "* declare a set of 4 widgets in a HBox (horizontal box of widgets). \n", |
| 88 | + "\n", |
| 89 | + "\n", |
| 90 | + "* define a function 'f' that will read the output from these widgets and make a plot\n", |
| 91 | + "\n", |
| 92 | + "You may have some flicker and lag. I have not tried to optimize performance for this demonstration. " |
| 93 | + ] |
| 94 | + }, |
| 95 | + { |
| 96 | + "cell_type": "code", |
| 97 | + "execution_count": 9, |
| 98 | + "metadata": { |
| 99 | + "scrolled": false |
| 100 | + }, |
| 101 | + "outputs": [], |
| 102 | + "source": [ |
| 103 | + "# 4 slider bars for the model input\n", |
| 104 | + "a = widgets.FloatSlider(min=0.0, max = 1.0, value = 0.5, description = 'coin bias')\n", |
| 105 | + "d = widgets.FloatSlider(min=0.01, max = 1.0, value = 0.1, step = 0.01, description = 'coin uncert.')\n", |
| 106 | + "b = widgets.FloatSlider(min = 0, max = 1.0, value = 0.5, description = 'prop. heads')\n", |
| 107 | + "c = widgets.IntSlider(min = 5, max = 1000, value = 100, description = 'coin tosses')\n", |
| 108 | + "\n", |
| 109 | + "ui = widgets.HBox([a,d,b,c],)\n", |
| 110 | + "\n", |
| 111 | + "def f(a, b, c, d): # function to make the plot \n", |
| 112 | + " heads = int(c * b)\n", |
| 113 | + " tails = c - heads\n", |
| 114 | + " \n", |
| 115 | + " x = np.linspace(0.0, 1.0, num=1000)\n", |
| 116 | + " \n", |
| 117 | + " prior = norm.pdf(x,loc = a, scale = d)\n", |
| 118 | + " prior = prior / np.sum(prior)\n", |
| 119 | + " \n", |
| 120 | + " plt.subplot(221)\n", |
| 121 | + " \n", |
| 122 | + " plt.plot(x, prior) # prior distribution of coin fairness\n", |
| 123 | + " plt.xlim(0.0,1.0)\n", |
| 124 | + " plt.xlabel('P(Coin Heads)'); plt.ylabel('Density'); plt.title('Prior Distribution')\n", |
| 125 | + " plt.ylim(0, 0.05)\n", |
| 126 | + " plt.grid()\n", |
| 127 | + " \n", |
| 128 | + " \n", |
| 129 | + " plt.subplot(222) # results from the coin tosses \n", |
| 130 | + " plt.pie([heads, tails],labels = ['heads','tails'],radius = 0.5*(c/1000)+0.5, autopct='%1.1f%%', colors = ['#ff9999','#66b3ff'], explode = [.02,.02], wedgeprops = {\"edgecolor\":\"k\",'linewidth': 1} )\n", |
| 131 | + " plt.title(str(c) + ' Coin Tosses')\n", |
| 132 | + " \n", |
| 133 | + " likelihood = binom.pmf(heads,c,x)\n", |
| 134 | + " likelihood = likelihood/np.sum(likelihood)\n", |
| 135 | + " \n", |
| 136 | + " plt.subplot(223) # likelihood distribution given the coin tosses\n", |
| 137 | + " plt.plot(x, likelihood)\n", |
| 138 | + " plt.xlim(0.0,1.0)\n", |
| 139 | + " plt.xlabel('P(Tosses | Coin Bias)'); plt.ylabel('Density'); plt.title('Likelihood Distribution')\n", |
| 140 | + " plt.ylim(0, 0.05)\n", |
| 141 | + " plt.grid()\n", |
| 142 | + "\n", |
| 143 | + " post = prior * likelihood\n", |
| 144 | + " post = post / np.sum(post) \n", |
| 145 | + " \n", |
| 146 | + " plt.subplot(224) # posterior distribution\n", |
| 147 | + " plt.plot(x, post)\n", |
| 148 | + " plt.xlim(0.0,1.0)\n", |
| 149 | + " plt.xlabel('P(Coin Bias | Tosses)'); plt.ylabel('Density'); plt.title('Posterior Distribution')\n", |
| 150 | + " plt.ylim(0, 0.05)\n", |
| 151 | + " plt.grid()\n", |
| 152 | + " \n", |
| 153 | + " plt.subplots_adjust(left=0.0, bottom=0.0, right=2.0, top=1.6, wspace=0.2, hspace=0.3)\n", |
| 154 | + " plt.show()\n", |
| 155 | + "\n", |
| 156 | + "interactive_plot = widgets.interactive_output(f, {'a': a, 'd': d, 'b': b, 'c': c})\n", |
| 157 | + "interactive_plot.clear_output(wait = True) # reduce flickering by delaying plot updating\n" |
| 158 | + ] |
| 159 | + }, |
| 160 | + { |
| 161 | + "cell_type": "markdown", |
| 162 | + "metadata": {}, |
| 163 | + "source": [ |
| 164 | + "### Bayesian Coin Example from Sivia, 1996, Data Analysis: A Bayesian Tutorial\n", |
| 165 | + "* interactive plot demonstration with ipywidget package\n", |
| 166 | + "\n", |
| 167 | + "#### Michael Pyrcz, Associate Professor, University of Texas at Austin \n", |
| 168 | + "\n", |
| 169 | + "##### [Twitter](https://twitter.com/geostatsguy) | [GitHub](https://github.com/GeostatsGuy) | [Website](http://michaelpyrcz.com) | [GoogleScholar](https://scholar.google.com/citations?user=QVZ20eQAAAAJ&hl=en&oi=ao) | [Book](https://www.amazon.com/Geostatistical-Reservoir-Modeling-Michael-Pyrcz/dp/0199731446) | [YouTube](https://www.youtube.com/channel/UCLqEr-xV-ceHdXXXrTId5ig) | [LinkedIn](https://www.linkedin.com/in/michael-pyrcz-61a648a1) | [GeostatsPy](https://github.com/GeostatsGuy/GeostatsPy)\n", |
| 170 | + "\n", |
| 171 | + "### The Problem\n", |
| 172 | + "\n", |
| 173 | + "What is the PDF for the coin probability of heads, P(Coin Heads)? Start with a prior model and update with coin tosses.\n", |
| 174 | + "\n", |
| 175 | + "* **coin bias**: expectation for your prior distribution for probability of heads\n", |
| 176 | + "\n", |
| 177 | + "* **coin uncert.**: standard deviation for your prior distribution for probability of heads\n", |
| 178 | + "\n", |
| 179 | + "* **prop. heads**: proportion of heads in the coin toss experiment\n", |
| 180 | + "\n", |
| 181 | + "* **coin tosses**: number of coin tosses in thecoin toss experiment\n" |
| 182 | + ] |
| 183 | + }, |
| 184 | + { |
| 185 | + "cell_type": "code", |
| 186 | + "execution_count": 10, |
| 187 | + "metadata": {}, |
| 188 | + "outputs": [ |
| 189 | + { |
| 190 | + "data": { |
| 191 | + "application/vnd.jupyter.widget-view+json": { |
| 192 | + "model_id": "f48225601ded4ecaa4449638db3439ea", |
| 193 | + "version_major": 2, |
| 194 | + "version_minor": 0 |
| 195 | + }, |
| 196 | + "text/plain": [ |
| 197 | + "HBox(children=(FloatSlider(value=0.5, description='coin bias', max=1.0), FloatSlider(value=0.1, description='c…" |
| 198 | + ] |
| 199 | + }, |
| 200 | + "metadata": {}, |
| 201 | + "output_type": "display_data" |
| 202 | + }, |
| 203 | + { |
| 204 | + "data": { |
| 205 | + "application/vnd.jupyter.widget-view+json": { |
| 206 | + "model_id": "5b8ee61a61b143e9895c8381f809a380", |
| 207 | + "version_major": 2, |
| 208 | + "version_minor": 0 |
| 209 | + }, |
| 210 | + "text/plain": [ |
| 211 | + "Output()" |
| 212 | + ] |
| 213 | + }, |
| 214 | + "metadata": {}, |
| 215 | + "output_type": "display_data" |
| 216 | + } |
| 217 | + ], |
| 218 | + "source": [ |
| 219 | + "display(ui, interactive_plot) # display the interactive plot" |
| 220 | + ] |
| 221 | + }, |
| 222 | + { |
| 223 | + "cell_type": "markdown", |
| 224 | + "metadata": {}, |
| 225 | + "source": [ |
| 226 | + "#### Comments\n", |
| 227 | + "\n", |
| 228 | + "This was a simple demonstration of interactive plots in Jupyter Notebook Python with the ipywidgets and matplotlib packages. \n", |
| 229 | + "\n", |
| 230 | + "I have many other demonstrations on data analytics and machine learning, e.g. on the basics of working with DataFrames, ndarrays, univariate statistics, plotting data, declustering, data transformations, trend modeling and many other workflows available at https://github.com/GeostatsGuy/PythonNumericalDemos and https://github.com/GeostatsGuy/GeostatsPy. \n", |
| 231 | + " \n", |
| 232 | + "I hope this was helpful,\n", |
| 233 | + "\n", |
| 234 | + "*Michael*\n", |
| 235 | + "\n", |
| 236 | + "#### The Author:\n", |
| 237 | + "\n", |
| 238 | + "### Michael Pyrcz, Associate Professor, University of Texas at Austin \n", |
| 239 | + "*Novel Data Analytics, Geostatistics and Machine Learning Subsurface Solutions*\n", |
| 240 | + "\n", |
| 241 | + "With over 17 years of experience in subsurface consulting, research and development, Michael has returned to academia driven by his passion for teaching and enthusiasm for enhancing engineers' and geoscientists' impact in subsurface resource development. \n", |
| 242 | + "\n", |
| 243 | + "For more about Michael check out these links:\n", |
| 244 | + "\n", |
| 245 | + "#### [Twitter](https://twitter.com/geostatsguy) | [GitHub](https://github.com/GeostatsGuy) | [Website](http://michaelpyrcz.com) | [GoogleScholar](https://scholar.google.com/citations?user=QVZ20eQAAAAJ&hl=en&oi=ao) | [Book](https://www.amazon.com/Geostatistical-Reservoir-Modeling-Michael-Pyrcz/dp/0199731446) | [YouTube](https://www.youtube.com/channel/UCLqEr-xV-ceHdXXXrTId5ig) | [LinkedIn](https://www.linkedin.com/in/michael-pyrcz-61a648a1)\n", |
| 246 | + "\n", |
| 247 | + "#### Want to Work Together?\n", |
| 248 | + "\n", |
| 249 | + "I hope this content is helpful to those that want to learn more about subsurface modeling, data analytics and machine learning. Students and working professionals are welcome to participate.\n", |
| 250 | + "\n", |
| 251 | + "* Want to invite me to visit your company for training, mentoring, project review, workflow design and / or consulting? I'd be happy to drop by and work with you! \n", |
| 252 | + "\n", |
| 253 | + "* Interested in partnering, supporting my graduate student research or my Subsurface Data Analytics and Machine Learning consortium (co-PIs including Profs. Foster, Torres-Verdin and van Oort)? My research combines data analytics, stochastic modeling and machine learning theory with practice to develop novel methods and workflows to add value. We are solving challenging subsurface problems!\n", |
| 254 | + "\n", |
| 255 | + "* I can be reached at [email protected].\n", |
| 256 | + "\n", |
| 257 | + "I'm always happy to discuss,\n", |
| 258 | + "\n", |
| 259 | + "*Michael*\n", |
| 260 | + "\n", |
| 261 | + "Michael Pyrcz, Ph.D., P.Eng. Associate Professor The Hildebrand Department of Petroleum and Geosystems Engineering, Bureau of Economic Geology, The Jackson School of Geosciences, The University of Texas at Austin\n", |
| 262 | + "\n", |
| 263 | + "#### More Resources Available at: [Twitter](https://twitter.com/geostatsguy) | [GitHub](https://github.com/GeostatsGuy) | [Website](http://michaelpyrcz.com) | [GoogleScholar](https://scholar.google.com/citations?user=QVZ20eQAAAAJ&hl=en&oi=ao) | [Book](https://www.amazon.com/Geostatistical-Reservoir-Modeling-Michael-Pyrcz/dp/0199731446) | [YouTube](https://www.youtube.com/channel/UCLqEr-xV-ceHdXXXrTId5ig) | [LinkedIn](https://www.linkedin.com/in/michael-pyrcz-61a648a1)\n" |
| 264 | + ] |
| 265 | + }, |
| 266 | + { |
| 267 | + "cell_type": "code", |
| 268 | + "execution_count": null, |
| 269 | + "metadata": {}, |
| 270 | + "outputs": [], |
| 271 | + "source": [] |
| 272 | + } |
| 273 | + ], |
| 274 | + "metadata": { |
| 275 | + "kernelspec": { |
| 276 | + "display_name": "Python 3", |
| 277 | + "language": "python", |
| 278 | + "name": "python3" |
| 279 | + }, |
| 280 | + "language_info": { |
| 281 | + "codemirror_mode": { |
| 282 | + "name": "ipython", |
| 283 | + "version": 3 |
| 284 | + }, |
| 285 | + "file_extension": ".py", |
| 286 | + "mimetype": "text/x-python", |
| 287 | + "name": "python", |
| 288 | + "nbconvert_exporter": "python", |
| 289 | + "pygments_lexer": "ipython3", |
| 290 | + "version": "3.6.5" |
| 291 | + } |
| 292 | + }, |
| 293 | + "nbformat": 4, |
| 294 | + "nbformat_minor": 2 |
| 295 | +} |
0 commit comments