Lecture 33 – Simulations, Finding and Using Data

Data 94, Spring 2021

In [1]:
from datascience import *
import numpy as np
Table.interactive_plots()

from ipywidgets import interact, widgets
from IPython.display import display

Repetition

In [2]:
coin = np.array(['heads', 'tails'])
coin
Out[2]:
array(['heads', 'tails'], dtype='<U5')
In [3]:
flips = np.random.choice(coin, size = 10)
flips
Out[3]:
array(['tails', 'heads', 'tails', 'heads', 'tails', 'heads', 'tails',
       'heads', 'heads', 'heads'], dtype='<U5')
In [4]:
# Array of 10 elements – True if flip was 'heads', False otherwise
flips == 'heads'
Out[4]:
array([False,  True, False,  True, False,  True, False,  True,  True,
        True])
In [5]:
# Number of heads
np.count_nonzero(flips == 'heads')
Out[5]:
6

Simulating Coin Flips

Idea:

  1. Flip a coin 100 times. Write down the number of heads.
  2. Repeat step 1 many times – say, 10,000 times.
  3. Draw a histogram of the number of heads in each iteration.
In [6]:
num_heads_arr = np.array([])

for i in np.arange(10000):
    flips = np.random.choice(coin, size = 100)
    heads = np.count_nonzero(flips == 'heads')
    num_heads_arr = np.append(num_heads_arr, heads)
In [7]:
num_heads_arr
Out[7]:
array([47., 54., 55., ..., 42., 50., 51.])
In [8]:
len(num_heads_arr)
Out[8]:
10000
In [9]:
Table().with_columns('Number of Heads', num_heads_arr) \
       .hist(density = False, bins = np.arange(25.5, 76.5), title = 'Empirical Distribution of 100 Coin Flips')