{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# PHYS 310 HW Problem\n",
"## Simulation of PHYS 211 M&M Experiment"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ 6 Colors: Yellow, Blue, Orange, Red, Green, and Blue\n",
"+ Assume 60 M&Ms in every bag\n",
"+ Assume equal probabilities (well mixed, large \"reservoir\")\n",
"+ Assume 24 students (bags) per section\n",
"\n",
"NOTE: In this notebook I use the module `scipy.stats` for all statistics \n",
"functions, including generation of random numbers. There are other modules with some overlapping functionality, e.g., the regular python `random` module, and the `scipy.random` module, but I do not use them here. The `scipy.stats` module includes tools for a large number of distributions, it includes a large and growing set of statistical functions, and there is a unified class structure. (And namespace issues are minimized.) See https://docs.scipy.org/doc/scipy/reference/stats.html."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import scipy as sp\n",
"from scipy import stats\n",
"\n",
"import matplotlib as mpl # As of July 2017 Bucknell computers use v. 2.x \n",
"import matplotlib.pyplot as plt\n",
"\n",
"# Following is an Ipython magic command that puts figures in the notebook.\n",
"# For figures in separate windows, comment out following line and uncomment\n",
"# the next line\n",
"# Must come before defaults are changed.\n",
"%matplotlib notebook\n",
"#%matplotlib\n",
"\n",
"# As of Aug. 2017 reverting to 1.x defaults.\n",
"# In 2.x text.ustex requires dvipng, texlive-latex-extra, and texlive-fonts-recommended, \n",
"# which don't seem to be universal\n",
"# See https://stackoverflow.com/questions/38906356/error-running-matplotlib-in-latex-type1cm?\n",
"mpl.style.use('classic')\n",
" \n",
"# M.L. modifications of matplotlib defaults using syntax of v.2.0 \n",
"# More info at http://matplotlib.org/2.0.0/users/deflt_style_changes.html\n",
"# Changes can also be put in matplotlibrc file, or effected using mpl.rcParams[]\n",
"plt.rc('figure', figsize = (6, 4.5)) # Reduces overall size of figures\n",
"plt.rc('axes', labelsize=16, titlesize=14)\n",
"plt.rc('figure', autolayout = True) # Adjusts supblot parameters for new size"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Part 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To get started, sample one bag of M&Ms, and count the numberof brown M&Ms.
\n",
"Do this by generating 60 random integers from the set 0,1,2,3,4,5, and \n",
"let's say that \"brown\" = 5."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 1, 1, 3, 2, 0, 4, 5, 4, 3, 1, 3, 5, 0, 5, 2, 5, 3, 2, 3, 3, 4, 2,\n",
" 3, 3, 4, 3, 3, 1, 2, 2, 1, 2, 5, 5, 3, 4, 1, 3, 4, 3, 0, 2, 0, 4, 1,\n",
" 0, 2, 0, 1, 1, 5, 4, 1, 0, 4, 1, 1, 0, 0])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bag = sp.stats.randint.rvs(0,6,size = 60) # or sp.random.randint(0,6,60)\n",
"bag"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Count the number of each color in the bag using sp.bincount(bag). The first element in the array is the number of occurences of 0 in \"bag,\" the second element is the number of occurences of 1, etc."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 9, 13, 9, 13, 9, 7])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sp.bincount(bag)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- For our \"brown\" = 5 choice, the number of brown M&Ms is the last element in the array returned by bincount, or sp.bincount(bag)[5]."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sp.bincount(bag)[5]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Now sample many bags\n",
"- Record number of brown M&Ms in each bag"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# Long version of sampling many bags\n",
"nb = 24 # number of bags \n",
"data_section = sp.zeros(nb) # array in which to store data for a lab section\n",
"for i in range(nb):\n",
" bag = sp.stats.randint.rvs(0,6,size=60)\n",
" data_section[i] = sp.bincount(bag)[5]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([14, 13, 10, 16, 13, 11, 11, 7, 7, 10, 11, 13, 13, 6, 17, 5, 9,\n",
" 9, 12, 8, 6, 15, 13, 10])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Concise version of sampling many bags\n",
"nb = 24 # number of bags\n",
"data_section = sp.array([sp.bincount(sp.stats.randint.rvs(0,6,size=60))[5] for i in range(nb)])\n",
"data_section"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(10.791666666666666, 3.1882488227168775, 0.65079856572379458)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sp.mean(data_section), sp.std(data_section), sp.std(data_section)/sp.sqrt(len(data_section))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Answer for Part 1, the results from a single lab section:\n",
"$\\overline{N}_\\text{section} = 9.9 \\pm 0.6$ "
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"application/javascript": [
"/* Put everything inside the global mpl namespace */\n",
"window.mpl = {};\n",
"\n",
"\n",
"mpl.get_websocket_type = function() {\n",
" if (typeof(WebSocket) !== 'undefined') {\n",
" return WebSocket;\n",
" } else if (typeof(MozWebSocket) !== 'undefined') {\n",
" return MozWebSocket;\n",
" } else {\n",
" alert('Your browser does not have WebSocket support.' +\n",
" 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
" 'Firefox 4 and 5 are also supported but you ' +\n",
" 'have to enable WebSockets in about:config.');\n",
" };\n",
"}\n",
"\n",
"mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
" this.id = figure_id;\n",
"\n",
" this.ws = websocket;\n",
"\n",
" this.supports_binary = (this.ws.binaryType != undefined);\n",
"\n",
" if (!this.supports_binary) {\n",
" var warnings = document.getElementById(\"mpl-warnings\");\n",
" if (warnings) {\n",
" warnings.style.display = 'block';\n",
" warnings.textContent = (\n",
" \"This browser does not support binary websocket messages. \" +\n",
" \"Performance may be slow.\");\n",
" }\n",
" }\n",
"\n",
" this.imageObj = new Image();\n",
"\n",
" this.context = undefined;\n",
" this.message = undefined;\n",
" this.canvas = undefined;\n",
" this.rubberband_canvas = undefined;\n",
" this.rubberband_context = undefined;\n",
" this.format_dropdown = undefined;\n",
"\n",
" this.image_mode = 'full';\n",
"\n",
" this.root = $('