Created
September 26, 2017 15:41
-
-
Save christopherphan/3f131debd7f968f5f01871b506794827 to your computer and use it in GitHub Desktop.
Interactive Python notebook for proportion test simulation study
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Proportion test simulation study\n", | |
"\n", | |
"**Instructions to get started:**\n", | |
"\n", | |
"1. In the menu, select ``Kernel > Restart & Clear Output``.\n", | |
"2. In the menu, select ``Cell > Run All``.\n", | |
"3. Scroll down to \"Step 1: Individual Sample\"." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"%matplotlib inline\n", | |
"import random\n", | |
"import matplotlib.pyplot as plt\n", | |
"import ipywidgets as ipw\n", | |
"import IPython.display\n", | |
"import numpy as np\n", | |
"import pandas as pd\n", | |
"\n", | |
"history = []\n", | |
"\n", | |
"\n", | |
"trials_so_far = ipw.widgets.BoundedIntText(\n", | |
" value=0,\n", | |
" min=0,\n", | |
" max=1000000000,\n", | |
" step=1,\n", | |
" description='Sims so far:',\n", | |
" disabled=True\n", | |
")\n", | |
"\n", | |
"addl_trials_input = ipw.widgets.BoundedIntText(\n", | |
" value=10000,\n", | |
" min=0,\n", | |
" max=1000000,\n", | |
" step=1,\n", | |
" description=\"Add'l sims:\",\n", | |
" disabled=False\n", | |
")\n", | |
"\n", | |
"def single_sample():\n", | |
" global probability\n", | |
" global sample_size\n", | |
" probability = prob_input.value\n", | |
" sample_size = sample_size_input.value\n", | |
" return sum([int(random.random() + probability) for j in range(0, sample_size)])\n", | |
"\n", | |
"def sample_widget_output():\n", | |
" global probability\n", | |
" global sample_size\n", | |
" probability = prob_input.value\n", | |
" sample_size = sample_size_input.value\n", | |
" global history\n", | |
" history.append(single_sample())\n", | |
" return 'In the sample of size n = {:d}, with a probability of {:f} of \"success\" each trial, we had {} \"successes\".'.format(\n", | |
" sample_size, probability, history[-1])\n", | |
"\n", | |
"sample_size_input = ipw.widgets.BoundedIntText(\n", | |
" value=20,\n", | |
" min=0,\n", | |
" max=1000,\n", | |
" step=1,\n", | |
" description='Sample size:',\n", | |
" disabled=False\n", | |
")\n", | |
"\n", | |
"prob_input = ipw.widgets.FloatSlider(\n", | |
" value=0.5,\n", | |
" min=0,\n", | |
" max=1,\n", | |
" step=0.0001,\n", | |
" description='Prob of \"Success\":',\n", | |
" disabled=False,\n", | |
" continuous_update=False,\n", | |
" orientation='horizontal',\n", | |
" readout=True,\n", | |
" readout_format='.1f',\n", | |
")\n", | |
"\n", | |
"sample_button = ipw.widgets.Button(description=\"Take a sample\")\n", | |
"clear_history_button = ipw.widgets.Button(description=\"Clear history\")\n", | |
"run_simulation_button = ipw.widgets.Button(description=\"Run simulations\")\n", | |
"\n", | |
"def sample_button_action(b):\n", | |
" global probability\n", | |
" global sample_size\n", | |
" probability = prob_input.value\n", | |
" sample_size = sample_size_input.value\n", | |
" trials_so_far.value += 1\n", | |
" addl_trials_input.value -= 1\n", | |
" print(sample_widget_output())\n", | |
" \n", | |
"def clear_history_action(b):\n", | |
" global history\n", | |
" history = []\n", | |
" IPython.display.clear_output()\n", | |
" trials_so_far.value = 0\n", | |
" addl_trials_input.value = 1000\n", | |
" print(\"History cleared.\")\n", | |
"\n", | |
"\n", | |
"def run_simulation_button_action(b):\n", | |
" global probability\n", | |
" global sample_size\n", | |
" global history\n", | |
" num_trials = addl_trials_input.value\n", | |
" for j in range(0, num_trials):\n", | |
" history.append(single_sample())\n", | |
" trials_so_far.value += 1\n", | |
" addl_trials_input.value -= 1\n", | |
" history_min = min(history)\n", | |
" history_max = max(history)\n", | |
" plt.hist(history, bins=np.arange(history_min, history_max + 2,1))\n", | |
" history_counts = [history.count(k) for k in range(0, sample_size + 1)]\n", | |
" scale = max(history_counts)\n", | |
" for j in range(history_min, history_max + 1):\n", | |
" plt.text(j + 0.5, -scale*0.05, str(j))\n", | |
" plt.text(j + 0.25, history_counts[j]+scale*0.05, str(history_counts[j]))\n", | |
" plt.grid(True)\n", | |
" fig = plt.gcf()\n", | |
" ax = plt.gca()\n", | |
" ax.get_xaxis().set_ticks([])\n", | |
" fig.set_size_inches(15, 5)\n", | |
" plt.show()\n", | |
" hc_df = pd.DataFrame(history_counts, columns=[\"Number of simulations\"])\n", | |
" with pd.option_context('display.max_rows', 1000, 'display.max_columns', 5):\n", | |
" IPython.display.display(hc_df)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Step 1: Individual sample\n", | |
"\n", | |
"1. Specify the sample size and probability of \"success\" each trial.\n", | |
"2. Click \"Take a sample\" a few times to simulate a few samples. The computer will report the results.\n", | |
"3. When you are ready to take a massive number of samples and plot a histogram of all the simulated samples, scroll down to \"Step 2: Run a simulation study\"." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"IPython.display.display(sample_size_input)\n", | |
"IPython.display.display(prob_input)\n", | |
"IPython.display.display(sample_button)\n", | |
"IPython.display.display(clear_history_button)\n", | |
"\n", | |
"sample_button.on_click(sample_button_action)\n", | |
"clear_history_button.on_click(clear_history_action)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Step 2: Run a simulation study\n", | |
"\n", | |
"1. Specify how many additional samples you want. The default is to end up with 10,000 simulations overall.\n", | |
"2. Click on \"Run simulations\" and wait a few minutes." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"IPython.display.display(trials_so_far)\n", | |
"IPython.display.display(addl_trials_input)\n", | |
"IPython.display.display(run_simulation_button)\n", | |
"\n", | |
"run_simulation_button.on_click(run_simulation_button_action)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"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.4.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment