Simulating the PHYS 211 M&M lab

NOTE: In this notebook I use the stats sub-module of scipy for all statistics 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 stats sub-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.

In [1]:
import numpy as np
from scipy import stats

import matplotlib as mpl
import matplotlib.pyplot as plt
In [2]:
# Following is an Ipython magic command that puts figures in notebook.
%matplotlib notebook
        
# M.L. modification of matplotlib defaults
# Changes can also be put in matplotlibrc file, 
# or effected using mpl.rcParams[]
plt.style.use('classic')
plt.rc('figure', figsize = (6, 4.5)) # Reduces overall size of figures
plt.rc('axes', labelsize=16, titlesize=14)
plt.rc('figure', autolayout = True) # Adjusts supblot params for new size

Intro

  • 6 Colors: Yellow, Blue, Orange, Red, Green, and Blue
  • Assume 60 M&Ms in every bag
  • Assume equal probabilities (well mixed, large "reservoir")
  • Assume 24 students (bags) per section

To get started, sample one bag of M&Ms, and count the numberof brown M&Ms.
Do this by generating 60 random integers from the set 0, 1, 2, 3, 4, 5, and let's say that "brown" = 0.

In [3]:
bag = stats.randint.rvs(0,6,size = 60) # or sp.random.randint(0,6,60)
print(bag)
[5 1 4 4 1 2 3 2 2 2 2 1 2 0 5 2 4 0 0 5 4 4 0 5 1 5 4 4 3 1 5 5 5 3 5 3 5
 4 2 2 0 5 2 4 3 0 1 1 5 0 5 3 2 1 1 4 2 3 2 4]
  • Count the number of each color in the bag using np.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.
In [4]:
np.bincount(bag)
Out[4]:
array([ 7,  9, 13,  7, 11, 13])
  • For our "brown" = 0 choice, the number of brown M&Ms is the last element in the array returned by bincount, or sp.bincount(bag)[0].
In [5]:
np.bincount(bag)[0]
Out[5]:
7
  • Now sample many bags
  • Record number of brown M&Ms in each bag
In [6]:
# Long version of sampling many bags
nb = 24                     # number of bags 
data_section = np.zeros(nb) # array in for data for a lab section
for i in range(nb):
    bag = stats.randint.rvs(0,6,size=60)
    data_section[i] = np.bincount(bag)[0]

data_section
Out[6]:
array([12., 10., 13., 10., 10.,  6., 15.,  8., 14.,  7., 12.,  5., 11.,
        6., 11.,  8., 13., 15.,  9.,  8., 10.,  8.,  8., 10.])
In [7]:
# Concise version of sampling many bags
nb = 24            # number of bags
data_section = np.array([np.bincount(stats.randint.rvs(0,6,size=60))[0] for i in range(nb)])
data_section
Out[7]:
array([ 9, 11,  9,  7,  7, 11, 10,  9,  8, 12, 11,  8,  8,  6,  8, 14, 18,
       12, 13, 12,  7,  5, 11, 10])
In [8]:
np.mean(data_section), np.std(data_section), np.std(data_section)/np.sqrt(len(data_section)-1)
Out[8]:
(9.833333333333334, 2.823512391016236, 0.5887430317956406)

Answer for results from this single lab section:

$\overline N = 9.8 \pm 0.6$

In [9]:
plt.figure()
nbins = 20
low = 0
high = 20
plt.hist(data_section,nbins,[low,high])
plt.xlim(0,20)
plt.title("Histogram of brown M&Ms per bag - Single Section",fontsize=14)
plt.xlabel("Number of brown M&Ms")
plt.ylabel("Occurences");

Version information

version_information is from J.R. Johansson (jrjohansson at gmail.com); see Introduction to scientific computing with Python for more information and instructions for package installation.

version_information is installed on the linux network at Bucknell

In [10]:
%load_ext version_information
In [11]:
version_information numpy, scipy, matplotlib
Out[11]:
SoftwareVersion
Python3.7.8 64bit [GCC 7.5.0]
IPython7.17.0
OSLinux 3.10.0 1062.9.1.el7.x86_64 x86_64 with centos 7.8.2003 Core
numpy1.19.1
scipy1.5.0
matplotlib3.3.0
Sat Aug 22 11:03:02 2020 EDT
In [ ]: