804 lines
28 KiB
Plaintext
804 lines
28 KiB
Plaintext
![]() |
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-5e22290104c842b6",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# You must run this cell, but you can ignore its contents.\n",
|
||
|
"\n",
|
||
|
"import hashlib\n",
|
||
|
"\n",
|
||
|
"def ads_hash(ty):\n",
|
||
|
" \"\"\"Return a unique string for input\"\"\"\n",
|
||
|
" ty_str = str(ty).encode()\n",
|
||
|
" m = hashlib.sha256()\n",
|
||
|
" m.update(ty_str)\n",
|
||
|
" return m.hexdigest()[:10]"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-9f70118929cfc07a",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"source": [
|
||
|
"# Bayesian Statistics: Interactive Bayesian updating\n",
|
||
|
"\n",
|
||
|
"From https://github.com/NuclearTalent/Bayes2019/blob/master/topics/basics-of-bayesian-statistics/Bayesian_updating_coinflip_interactive.ipynb"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-f4d886aaea957b85",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"## <a name=\"Python\">Python/Jupyter set up</a>"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-8a69051f2683cc79",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"import numpy as np\n",
|
||
|
"\n",
|
||
|
"import scipy.stats as stats\n",
|
||
|
"from scipy.stats import norm, uniform\n",
|
||
|
"\n",
|
||
|
"import matplotlib.pyplot as plt\n",
|
||
|
"\n",
|
||
|
"import ipywidgets as widgets\n",
|
||
|
"from ipywidgets import HBox, VBox, Layout, Tab, Label, Checkbox, Button\n",
|
||
|
"from ipywidgets import FloatSlider, IntSlider, Play, Dropdown, HTMLMath \n",
|
||
|
"\n",
|
||
|
"from IPython.display import display\n",
|
||
|
"\n",
|
||
|
"import seaborn as sns\n",
|
||
|
"sns.set()\n",
|
||
|
"sns.set_context(\"talk\")\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-ef87f464e37e4cca",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"## <a name=\"Updating\">Bayesian updating examples</a>\n",
|
||
|
"\n",
|
||
|
"$ \\newcommand{\\thetavec}{\\boldsymbol{\\theta}} \\newcommand{\\pr}{\\textrm{p}}$\n",
|
||
|
"Recall Bayes' theorem with $\\thetavec$ the vector of parameters we seek and information $I$ is kept implicit.\n",
|
||
|
"\n",
|
||
|
"$$\n",
|
||
|
" \\overbrace{\\pr(\\thetavec \\mid \\textrm{data},I)}^{\\textrm{posterior}} =\n",
|
||
|
" \\frac{\\color{red}{\\overbrace{\\pr(\\textrm{data} \\mid \\thetavec,I)}^{\\textrm{likelihood}}} \\times\n",
|
||
|
" \\color{blue}{\\overbrace{\\pr(\\thetavec \\mid I)}^{\\textrm{prior}}}}\n",
|
||
|
" {\\color{darkgreen}{\\underbrace{\\pr(\\textrm{data} \\mid I)}_{\\textrm{evidence}}}}\n",
|
||
|
"$$\n",
|
||
|
"\n",
|
||
|
"If we view the prior as the initial information we have about $\\thetavec$, summarized as a probability density function, then Bayes' theorem tells us how to <em>update</em> that information after observing some data: this is the posterior pdf. Here we will give some examples of how this plays out when tossing coins.\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-4f86b9c7f450dfdc",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"### Determining the bias of a coin"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-f277b01772407ee0",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"The idea here is that we are observing successive flips of a coin, which is a proxy for any process that has a binary outcome. There is a definite true probability for getting heads, which we'll label $p_h$, but we don't know what it is. We start with a preconceived notion of the probability expressed in terms of a prior pdf for $p_h$, i.e., $\\pr(p_h)$. With each flip of the coin, we have more information, so our goal is to <em>update</em> our expectation of $p_h$, meaning we want the posterior $\\pr(p_h \\mid \\mbox{ num tosses, num heads})$. "
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-404c8f0599a0960c",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"#### Main code for coin-flipping UI"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"metadata": {
|
||
|
"code_folding": [],
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-87f9a4135ca5602b",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Initial values (can be changed by widgets)\n",
|
||
|
"n_trials_max = 5000 # maximum number of coin tosses\n",
|
||
|
"prob_heads = 0.4 # p_h, the true probability of a heads\n",
|
||
|
"x = np.linspace(0, 1, 301) # mesh for posterior plots (enough so smooth)\n",
|
||
|
"\n",
|
||
|
"class Data():\n",
|
||
|
" \"\"\"Class to hold the array of heads and tails (1s and 0s) outcomes.\"\"\"\n",
|
||
|
" def __init__(self, prob_heads=0.5, n_trials_max=5000):\n",
|
||
|
" self._data = self.generate_data(prob_heads, n_trials_max)\n",
|
||
|
" \n",
|
||
|
" def generate_data(self, prob_heads, n_trials_max):\n",
|
||
|
" \"\"\"Generate an array of heads or tails, 1 or 0, for n_trials_max\n",
|
||
|
" independent tosses according to the Bernoulli distribution.\"\"\"\n",
|
||
|
" self._data = stats.bernoulli.rvs(prob_heads, size=n_trials_max)\n",
|
||
|
" \n",
|
||
|
" def heads_in_data_to_N(self, N):\n",
|
||
|
" \"\"\"Count how many heads in the first N elements of the data.\"\"\"\n",
|
||
|
" return self._data[:N].sum()\n",
|
||
|
"\n",
|
||
|
"coin_data = Data(prob_heads, n_trials_max) \n",
|
||
|
" \n",
|
||
|
"def update_plot(N=0, jump=1, recalculate_data=True, \n",
|
||
|
" prob_heads=0.5, n_trials_max=5000,\n",
|
||
|
" alpha_1=1., beta_1=1.,\n",
|
||
|
" alpha_2=30., beta_2=30.,\n",
|
||
|
" alpha_3=0.2, beta_3=0.3\n",
|
||
|
" ):\n",
|
||
|
" \"\"\"\n",
|
||
|
" Make a new plot based on the current widget settings for the input\n",
|
||
|
" parameters.\n",
|
||
|
" \"\"\" \n",
|
||
|
" \n",
|
||
|
" font_size = 18\n",
|
||
|
" plt.rcParams.update({'font.size': font_size})\n",
|
||
|
" \n",
|
||
|
" fig = plt.figure(figsize=(12,5))\n",
|
||
|
" ax = fig.add_subplot(1, 1, 1)\n",
|
||
|
"\n",
|
||
|
" if recalculate_data:\n",
|
||
|
" coin_data.generate_data(prob_heads, n_trials_max)\n",
|
||
|
" recalculate_data_w.value = False\n",
|
||
|
"\n",
|
||
|
" heads = coin_data.heads_in_data_to_N(N) # add up the 1s (= # of heads)\n",
|
||
|
" # update using the conjugate prior, which is a beta pdf\n",
|
||
|
" y_1 = stats.beta.pdf(x, alpha_1 + heads, beta_1 + N - heads) \n",
|
||
|
" y_2 = stats.beta.pdf(x, alpha_2 + heads, beta_2 + N - heads) \n",
|
||
|
" y_3 = stats.beta.pdf(x, alpha_3 + heads, beta_3 + N - heads) \n",
|
||
|
"\n",
|
||
|
" # default y_3 distribution has two high max at endpoints for plot\n",
|
||
|
" y_max = np.max([y_1.max(), y_2.max()]) \n",
|
||
|
" \n",
|
||
|
" line1, = ax.plot(x, y_1, label=\"uniform prior\", color=\"blue\")\n",
|
||
|
" ax.fill_between(x, 0, y_1, color=\"blue\", alpha=0.1)\n",
|
||
|
" line2, = ax.plot(x, y_2, label=\"informative prior\", color=\"red\")\n",
|
||
|
" ax.fill_between(x, 0, y_2, color=\"red\", alpha=0.1)\n",
|
||
|
" line3, = ax.plot(x, y_3, label=\"anti prior\", color=\"green\")\n",
|
||
|
" ax.fill_between(x, 0, y_3, color=\"green\", alpha=0.1)\n",
|
||
|
" \n",
|
||
|
" ax.set_xlabel(\"$p_h$, probability of heads\") \n",
|
||
|
" ax.set_yticks([]) # turn off the plotting of ticks on the y-axis\n",
|
||
|
" ax.axvline(prob_heads, 0, 1.1*y_max, color=\"k\", linestyle=\"--\", lw=2)\n",
|
||
|
" ax.annotate(f'observe {N:d} tosses,\\n {heads:d} heads', \n",
|
||
|
" xy=(0.05,0.85), xycoords='axes fraction', \n",
|
||
|
" horizontalalignment='left',verticalalignment='top')\n",
|
||
|
" leg = ax.legend(loc='upper right')\n",
|
||
|
" leg.get_frame().set_alpha(0.4)\n",
|
||
|
" ax.autoscale(tight=True)\n",
|
||
|
"\n",
|
||
|
" \n",
|
||
|
"################### begin: text for help tabs ##################\n",
|
||
|
"# In HTML (could move this to an external file!)\n",
|
||
|
"overview_text = \\\n",
|
||
|
" r\"\"\"<p>Here we explore Bayesian updating for a coin flip. There is help \n",
|
||
|
" available under the other tabs.</p> \n",
|
||
|
" <ul>\n",
|
||
|
" <li>Bayes theorem tab: find out about Bayesian updating.\n",
|
||
|
" <li>Toss coin tab: find out what the controls do.\n",
|
||
|
" <li>Priors tab: change the three initial priors.\n",
|
||
|
" <li>Setup tab: change how the plots are made.\n",
|
||
|
" </ul> \n",
|
||
|
" \"\"\" \n",
|
||
|
"Bayes_text = \\\n",
|
||
|
" r\"\"\"\n",
|
||
|
" <p>Recall Bayes' theorem with $\\thetavec$ the vector of parameters \n",
|
||
|
" we seek and information $I$ is kept implicit.</p>\n",
|
||
|
"\n",
|
||
|
" $$\n",
|
||
|
" \\newcommand{\\thetavec}{\\boldsymbol{\\theta}}\n",
|
||
|
" \\overbrace{p(\\thetavec \\mid \\textrm{data},I)}^{\\textrm{posterior}} =\n",
|
||
|
" \\frac{\\color{red}{\\overbrace{p(\\textrm{data} \n",
|
||
|
" \\mid \\thetavec,I)}^{\\textrm{likelihood}}} \\times\n",
|
||
|
" \\color{blue}{\\overbrace{p(\\thetavec \\mid I)}^{\\textrm{prior}}}}\n",
|
||
|
" {\\color{darkgreen}{\\underbrace{p(\\textrm{data} \n",
|
||
|
" \\mid I)}_{\\textrm{evidence}}}}\n",
|
||
|
" $$\n",
|
||
|
"\n",
|
||
|
" <p>If we view the prior as the initial information we have about \n",
|
||
|
" $\\thetavec$, summarized as a probability density function, \n",
|
||
|
" then Bayes' theorem tells us how to <em>update</em> that \n",
|
||
|
" information after observing some data: this is the posterior pdf. \n",
|
||
|
" Here we will look at an example of how this plays out in practice:\n",
|
||
|
" flipping a (biased) coin.</p> \n",
|
||
|
"\n",
|
||
|
" <br>\n",
|
||
|
"\n",
|
||
|
" <p>The idea here is that we are observing successive flips of a coin, \n",
|
||
|
" which is a proxy for any process that has a binary outcome. \n",
|
||
|
" There is a definite true probability for getting heads, \n",
|
||
|
" which we'll label $p_h$, but we don't know what it is. \n",
|
||
|
" We start with a preconceived notion of the probability expressed \n",
|
||
|
" in terms of a prior pdf for $p_h$, i.e., $p(p_h)$. \n",
|
||
|
" With each flip of the coin, we have more information, so our goal is \n",
|
||
|
" to <em>update</em> our expectation of $p_h$, meaning we want the \n",
|
||
|
" posterior $p(p_h\\mid \\mbox{# tosses, # heads})$.</p> \n",
|
||
|
"\n",
|
||
|
" \"\"\"\n",
|
||
|
"\n",
|
||
|
"toss_coin_text = \\\n",
|
||
|
" r\"\"\"\n",
|
||
|
" The graph shows three posteriors that result from three choices for\n",
|
||
|
" the prior (see the \"Priors\" tab for details) for the number of coin\n",
|
||
|
" tosses and observed heads shown at the upper left. The true probability\n",
|
||
|
" of a heads, $p_h$, is indicated by a dashed vertical line.\n",
|
||
|
" <ul>\n",
|
||
|
" <li>The \"Next\" button advances the number of coin tosses by the\n",
|
||
|
" amount shown after \"Jump\".\n",
|
||
|
" <li>The \"Jump\" pulldown is used to set how many observations are\n",
|
||
|
" made with each press of the \"Next\" button.\n",
|
||
|
" <li>The \"Reset\" button sets the number of observations back to zero.\n",
|
||
|
" <li>The probability of heads $p_h$ can be adjusted between 0 and\n",
|
||
|
" 1 using the slider.\n",
|
||
|
" </ul>\n",
|
||
|
" \"\"\"\n",
|
||
|
"\n",
|
||
|
"setup_text = \\\n",
|
||
|
" r\"\"\"\n",
|
||
|
" Select the maximum number of trials possible. The calculations will\n",
|
||
|
" simply stop if you try to go higher.\n",
|
||
|
" \"\"\"\n",
|
||
|
"\n",
|
||
|
"initial_text = \\\n",
|
||
|
" r\"\"\"\n",
|
||
|
" The graph shows three posteriors that result from three choices for\n",
|
||
|
" the prior (set under the \"Priors\" tab) for the number of coin\n",
|
||
|
" tosses and observed heads shown at the upper left. The true probability\n",
|
||
|
" of a heads, $p_h$, is indicated by a dashed vertical line.\n",
|
||
|
" Press \"Next\" to make observations, \"Reset\" to go back to zero.\n",
|
||
|
" \"\"\"\n",
|
||
|
"initial_text_w = HTMLMath(value=initial_text)\n",
|
||
|
"\n",
|
||
|
"priors_text = \\\n",
|
||
|
" r\"\"\"\n",
|
||
|
" Hyperparameters for several different priors (all beta distributions).\n",
|
||
|
" Default prior 1 ($\\alpha_1 = 1$, $\\beta_1 = 1$) is uniform in [0,1].\n",
|
||
|
" Default prior 2 ($\\alpha_2 = 30$, $\\beta_2 = 30$) is concentrated \n",
|
||
|
" near 0.5 with very small tails.\n",
|
||
|
" Defalt prior 3 ($\\alpha_3 = .2$, $\\beta_3 = .2$) is peaked at the ends, \n",
|
||
|
" but allows for probability everywhere.\n",
|
||
|
" \"\"\"\n",
|
||
|
"priors_text_w = HTMLMath(value=priors_text)\n",
|
||
|
"################### end: text for help tabs ##################\n",
|
||
|
" \n",
|
||
|
" \n",
|
||
|
" \n",
|
||
|
" \n",
|
||
|
"################### begin: definitions of widgets ##################\n",
|
||
|
"recalculate_data_w = Checkbox(value=True) \n",
|
||
|
"prob_heads_w = FloatSlider(value=prob_heads, min=0., max=1., step=0.05,\n",
|
||
|
" description=r'true $p_h$:',\n",
|
||
|
" continuous_update=False)\n",
|
||
|
"n_trials_max_w = IntSlider(value=n_trials_max, min=100, max=10000, step=100,\n",
|
||
|
" description='max # trials:',\n",
|
||
|
" continuous_update=False)\n",
|
||
|
" \n",
|
||
|
"N_w = IntSlider(value=0, min=0, max=n_trials_max, step=1,\n",
|
||
|
" continuous_update=False)\n",
|
||
|
"next_button_w = Button(description='Next', disabled=False,\n",
|
||
|
" layout=Layout(width='80px'), button_style='', \n",
|
||
|
" tooltip='Increment number of trials by jump')\n",
|
||
|
"reset_button_w = Button(description='Reset', disabled=False,\n",
|
||
|
" layout=Layout(width='80px'), button_style='', \n",
|
||
|
" tooltip='Reset number of trials to zero')\n",
|
||
|
"\n",
|
||
|
"jump_w = Dropdown(description='Jump:',\n",
|
||
|
" layout=Layout(width='150px'),\n",
|
||
|
" options=['1', '10', '100', '1000'],\n",
|
||
|
" value='1',\n",
|
||
|
" continuos_update=False,\n",
|
||
|
" disabled=False,)\n",
|
||
|
"\n",
|
||
|
"alpha_1_w = FloatSlider(value=1., min=0., max=2., step=0.1,\n",
|
||
|
" description=r'$\\alpha_1$:',\n",
|
||
|
" continuous_update=False)\n",
|
||
|
"alpha_1_w.style.handle_color = 'blue'\n",
|
||
|
"\n",
|
||
|
"alpha_2_w = FloatSlider(value=30., min=5., max=200., step=5.,\n",
|
||
|
" description=r'$\\alpha_2$:',\n",
|
||
|
" continuous_update=False)\n",
|
||
|
"alpha_2_w.style.handle_color = 'red'\n",
|
||
|
"alpha_3_w = FloatSlider(value=0.2, min=0., max=1., step=0.1,\n",
|
||
|
" description=r'$\\alpha_3$:',\n",
|
||
|
" continuous_update=False)\n",
|
||
|
"alpha_3_w.style.handle_color = 'green'\n",
|
||
|
"beta_1_w = FloatSlider(value=1., min=0.1, max=2., step=0.1,\n",
|
||
|
" description=r'$\\beta_1$:',\n",
|
||
|
" continuous_update=False)\n",
|
||
|
"beta_1_w.style.handle_color = 'blue'\n",
|
||
|
"beta_2_w = FloatSlider(value=30., min=5., max=200., step=5.,\n",
|
||
|
" description=r'$\\beta_2$:',\n",
|
||
|
" continuous_update=False)\n",
|
||
|
"beta_2_w.style.handle_color = 'red'\n",
|
||
|
"beta_3_w = FloatSlider(value=0.2, min=0., max=1., step=0.1,\n",
|
||
|
" description=r'$\\beta_3$:',\n",
|
||
|
" continuous_update=False)\n",
|
||
|
"beta_3_w.style.handle_color = 'green'\n",
|
||
|
"\n",
|
||
|
"# Widgets for the help section, which are HTMLMath boxes in a Tab widget\n",
|
||
|
"help_overview_w = HTMLMath(value=overview_text)\n",
|
||
|
"help_Bayes_w = HTMLMath(value=Bayes_text)\n",
|
||
|
"help_toss_coin_w = HTMLMath(value=toss_coin_text)\n",
|
||
|
"help_priors_w = HTMLMath(value=priors_text)\n",
|
||
|
"help_setup_w = HTMLMath(value=setup_text)\n",
|
||
|
"\n",
|
||
|
"################### end: definitions of widgets ##################\n",
|
||
|
"\n",
|
||
|
"################### begin: explicit widget callbacks ##################\n",
|
||
|
"def update_N(b):\n",
|
||
|
" \"\"\"Increment the number of trials N by the Jump value\"\"\"\n",
|
||
|
" N_w.value += int(jump_w.value)\n",
|
||
|
" \n",
|
||
|
"def reset_N(b):\n",
|
||
|
" \"\"\"Reset the number of trials N to zero\"\"\"\n",
|
||
|
" N_w.value = 0\n",
|
||
|
" \n",
|
||
|
"def update_prob_heads(b):\n",
|
||
|
" \"\"\"Change the value of prob_heads and regenerate data.\"\"\"\n",
|
||
|
" recalculate_data_w.value = True\n",
|
||
|
" N_w.max = n_trials_max_w.value\n",
|
||
|
"\n",
|
||
|
"next_button_w.on_click(update_N)\n",
|
||
|
"reset_button_w.on_click(reset_N)\n",
|
||
|
" \n",
|
||
|
"prob_heads_w.observe(update_prob_heads, 'value') \n",
|
||
|
"n_trials_max_w.observe(update_prob_heads, 'value') \n",
|
||
|
"\n",
|
||
|
"################### end: explicit widget callbacks ##################\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"# Organize the help as Tabs (which will be itself under the Help Tab)\n",
|
||
|
"help_max_height = '500px'\n",
|
||
|
"help_tab = Tab(children=[help_overview_w, help_Bayes_w, help_toss_coin_w, \n",
|
||
|
" help_priors_w, help_setup_w], \n",
|
||
|
" layout=Layout(width='95%', max_height=help_max_height))\n",
|
||
|
"help_tab.set_title(0, 'Overview')\n",
|
||
|
"help_tab.set_title(1, 'Bayes Theorem')\n",
|
||
|
"help_tab.set_title(2, 'Toss Coin')\n",
|
||
|
"help_tab.set_title(3, 'Priors')\n",
|
||
|
"help_tab.set_title(4, 'Set-up')\n",
|
||
|
" \n",
|
||
|
"# Boxes of widgets that will go into the top-level tabs\n",
|
||
|
"textbox0 = HBox([initial_text_w])\n",
|
||
|
"hbox0 = HBox([next_button_w, jump_w, reset_button_w, prob_heads_w])\n",
|
||
|
"textbox1 = HBox([priors_text_w])\n",
|
||
|
"hbox1a = HBox([alpha_1_w, alpha_2_w, alpha_3_w])\n",
|
||
|
"hbox1b = HBox([beta_1_w, beta_2_w, beta_3_w])\n",
|
||
|
"hbox2 = HBox([n_trials_max_w])\n",
|
||
|
"hbox3 = HBox([help_tab])\n",
|
||
|
"\n",
|
||
|
"# We'll set up Tabs to organize the controls. The Tab contents are declared\n",
|
||
|
"# as tab0, tab1, ... (probably should make this a list) and the overall Tab\n",
|
||
|
"# is called tab (so its children are tab0, tab1, ...).\n",
|
||
|
"tab_height = '70px' # Fixed minimum height for all tabs. Specify another way?\n",
|
||
|
"tab0 = VBox([textbox0, hbox0], layout=Layout(min_height=tab_height))\n",
|
||
|
"tab1 = VBox([textbox1, hbox1a, hbox1b], layout=Layout(min_height=tab_height))\n",
|
||
|
"tab2 = VBox([hbox2], layout=Layout(min_height=tab_height))\n",
|
||
|
"tab3 = VBox([hbox3], layout=Layout(min_height=tab_height))\n",
|
||
|
"\n",
|
||
|
"tab = Tab(children=[tab0, tab1, tab2, tab3])\n",
|
||
|
"tab.set_title(0, 'Toss Coin')\n",
|
||
|
"tab.set_title(1, 'Priors')\n",
|
||
|
"tab.set_title(2, 'Set-up')\n",
|
||
|
"tab.set_title(3, 'Help')\n",
|
||
|
"\n",
|
||
|
"plot_out = widgets.interactive_output(update_plot,\n",
|
||
|
" dict(\n",
|
||
|
" N=N_w,\n",
|
||
|
" jump=jump_w,\n",
|
||
|
" recalculate_data=recalculate_data_w,\n",
|
||
|
" prob_heads=prob_heads_w,\n",
|
||
|
" n_trials_max=n_trials_max_w,\n",
|
||
|
" alpha_1=alpha_1_w, beta_1=beta_1_w,\n",
|
||
|
" alpha_2=alpha_2_w, beta_2=beta_2_w,\n",
|
||
|
" alpha_3=alpha_3_w, beta_3=beta_3_w,\n",
|
||
|
" )\n",
|
||
|
" )\n",
|
||
|
"\n",
|
||
|
"UI_box = VBox([tab, plot_out]);"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-a1fc76cad29d324e",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"#### User-interface for coin-flipping \n",
|
||
|
"\n",
|
||
|
"Take a look at the information under the `Help` tab to find out about what the controls do, what the priors are, etc. "
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 4,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-0f08b3f96e873a87",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"application/vnd.jupyter.widget-view+json": {
|
||
|
"model_id": "927832f349434247a193582f0559e18b",
|
||
|
"version_major": 2,
|
||
|
"version_minor": 0
|
||
|
},
|
||
|
"text/plain": [
|
||
|
"VBox(children=(Tab(children=(VBox(children=(HBox(children=(HTMLMath(value='\\n The graph shows three posteri…"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"display(UI_box);"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-9fa43fdc4a161ce2",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"Questions and tasks relating to priors and dealing with uncertainty: \n",
|
||
|
"* *What prior would you choose? How does this affect how long it takes you to arrive at the correct conclusion?* Note that the answer to this question may be $p_H$ dependent.\n",
|
||
|
"* *What would your standard be for deciding the coin was so unfair that you would walk away? That you'd call the police? That you'd try and publish the fact that you found an unfair coin in a scientific journal?* Hint: for the last one try thinking in terms of a \"p value\". If you don't know what that mean google \"hypothesis testing\".\n",
|
||
|
"* *What if you were **sure** the coin was unfair before you started? (E.g. you saw the person doctoring it.) What prior would you choose then? What happens to the posterior in this case?* This one may be best answered without any hitting of shift-return.....\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-6481ce022b1d1783",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"### Q1 With the informative prior and no coin tosses, what is the mode of the posterior distribution? Put this in the variable `posterior_informative_0_0`."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 5,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-998c8286b5041f52",
|
||
|
"locked": false,
|
||
|
"schema_version": 3,
|
||
|
"solution": true,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Type your answer here and then run this and the following cell.\n",
|
||
|
"posterior_informative_0_0 = 0.5"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 6,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": true,
|
||
|
"grade_id": "cell-11199ecf37da5d78",
|
||
|
"locked": true,
|
||
|
"points": 1,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# This is a test of the above, do not change this code.\n",
|
||
|
"assert ads_hash(int(np.round(posterior_informative_0_0*1000)))=='0604cd3138'"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-8c2f2035bb2dc08d",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"### Q2 After one toss but 0 heads and with a uniform prior distribution, what is the mode of the posterior distribution?\n",
|
||
|
"\n",
|
||
|
"Put this in the variable `uniform_informative_0_1`."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 7,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-6f48bd0fc37a2c47",
|
||
|
"locked": false,
|
||
|
"schema_version": 3,
|
||
|
"solution": true,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Type your answer here and then run this and the following cell.\n",
|
||
|
"uniform_informative_0_1 = 0.0"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 8,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": true,
|
||
|
"grade_id": "cell-ae2d2a33c719a214",
|
||
|
"locked": true,
|
||
|
"points": 1,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# This is a test of the above, do not change this code.\n",
|
||
|
"assert ads_hash(int(np.round(uniform_informative_0_1*1000)))=='5feceb66ff'"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-c716c72bac26cd85",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"### Q3 After one toss but 0 heads and with the \"anti\" prior distribution, what is the mode of the posterior distribution?\n",
|
||
|
"\n",
|
||
|
"Put this in the variable `anti_informative_0_1`."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 9,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-169a6bd947a7285e",
|
||
|
"locked": false,
|
||
|
"schema_version": 3,
|
||
|
"solution": true,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Type your answer here and then run this and the following cell.\n",
|
||
|
"anti_informative_0_1 = 0.0"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 10,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": true,
|
||
|
"grade_id": "cell-9eb8a9dbc2f2b809",
|
||
|
"locked": true,
|
||
|
"points": 1,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# This is a test of the above, do not change this code.\n",
|
||
|
"assert ads_hash(int(np.round(anti_informative_0_1*1000))-42)=='fec80006df'"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-bad4ca51b288966b",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"### Q4 How many tosses does it take for the posterior distributions to be approximately centered around the true value of $p_h$ for all prior distributions?\n",
|
||
|
"\n",
|
||
|
"Put this into the variable `n_to_converge`."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 11,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": true,
|
||
|
"grade_id": "cell-a48da985862a2d34",
|
||
|
"locked": false,
|
||
|
"points": 1,
|
||
|
"schema_version": 3,
|
||
|
"solution": true,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Type your answer here.\n",
|
||
|
"n_to_converge = 30"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"celltoolbar": "Create Assignment",
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 3 (ipykernel)",
|
||
|
"language": "python",
|
||
|
"name": "python3"
|
||
|
},
|
||
|
"language_info": {
|
||
|
"codemirror_mode": {
|
||
|
"name": "ipython",
|
||
|
"version": 3
|
||
|
},
|
||
|
"file_extension": ".py",
|
||
|
"mimetype": "text/x-python",
|
||
|
"name": "python",
|
||
|
"nbconvert_exporter": "python",
|
||
|
"pygments_lexer": "ipython3",
|
||
|
"version": "3.11.10"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 4
|
||
|
}
|