# Lecture 33 – Simulations, Finding and Using Data¶

## Data 94, Spring 2021¶

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

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


## Repetition¶

In :
coin = np.array(['heads', 'tails'])
coin

Out:
array(['heads', 'tails'], dtype='<U5')
In :
flips = np.random.choice(coin, size = 10)
flips

Out:
array(['tails', 'heads', 'tails', 'heads', 'tails', 'heads', 'tails',
'heads', 'heads', 'heads'], dtype='<U5')
In :
# Array of 10 elements – True if flip was 'heads', False otherwise

Out:
array([False,  True, False,  True, False,  True, False,  True,  True,
True])
In :
# Number of heads

Out:
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 :
num_heads_arr = np.array([])

for i in np.arange(10000):
flips = np.random.choice(coin, size = 100)

In :
num_heads_arr

Out:
array([47., 54., 55., ..., 42., 50., 51.])
In :
len(num_heads_arr)

Out:
10000
In :
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')