In [ ]:
from datascience import *
import numpy as np

%matplotlib inline
import matplotlib.pyplot as plots
plots.style.use('fivethirtyeight')


## For statements and experiments¶

In [ ]:
cookies = make_array('chocolate chip', 'oatmeal raisin', 'peanut butter')

In [ ]:
for cookie in cookies:
print(feelings)

In [ ]:
cookie = cookies.item(0)
print(feelings)

In [ ]:
cookie = cookies.item(1)
print(feelings)

In [ ]:
cookie = cookies.item(2)
print(feelings)

In [ ]:
for cookie in cookies:
print(feelings)
else:
print(feelings)

In [ ]:
coin = ['heads', 'tails']

In [ ]:
np.random.choice(coin, 6)

In [ ]:
tosses = np.random.choice(coin, 6)
tosses

In [ ]:
sum(tosses == 'heads')

In [ ]:
outcomes = make_array()

In [ ]:
tosses = np.random.choice(coin, 100)
outcomes = np.append(outcomes, sum(tosses == 'heads'))
outcomes

In [ ]:
outcomes = make_array()
for i in np.arange(10000):
tosses = np.random.choice(coin, 100)
outcomes = np.append(outcomes, sum(tosses == 'heads'))


In [ ]:
outcomes

In [ ]:
outcome_table = Table().with_column(
'Number of heads in 100 tosses',
outcomes
)
outcome_table.hist(bins=np.arange(25.5, 75.5))


## Monty Hall Problem¶

From old TV game show from the 1960s called "Let's Make a Deal"

Contestant is presented three closed doors

Behind one door is a fancy car, and goats are behind the other two doors (The contestant does not know which door has the car)

Steps of the game:

1. Contestant makes an initial choice of door, but the door stays closed

2. One of the other doors with a goat behind it is opened

3. There are two closed doors remaining (one being the contestant's initial). The contestant now gets to choose which of the two doors to open.

What should the contestant do? Which door to open if she wants the car? Stick with her initial choice, or switch to the other door?

In [ ]:
hidden_behind_doors = ['first goat', 'second goat', 'car']
hidden_behind_doors

In [ ]:
goats = ['first goat', 'second goat']
goats

In [ ]:
def other_goat(goat):
if goat == 'first goat':
return 'second goat'
elif goat == 'second goat':
return 'first goat'

In [ ]:
other_goat('first goat')

In [ ]:
other_goat('second goat')

In [ ]:
other_goat('apple')

In [ ]:
def play_monty_hall():
"""
Returns a list of 3 items:
[contestant's first guess, what Monty reveals, what's behind other door]
"""
first_guess = np.random.choice(hidden_behind_doors)

if first_guess == 'first goat':
return [first_guess, 'second goat', 'car']

elif first_guess == 'second goat':
return [first_guess, 'first goat', 'car']

elif first_guess == 'car':
reveal = np.random.choice(goats)
return ['car', reveal, other_goat(reveal)]

In [ ]:
play_monty_hall()

In [ ]:
results = Table(['Guess', 'Revealed', 'Remaining'])
results

In [ ]:
results.append(play_monty_hall())

In [ ]:
results

In [ ]:
trials = Table(['Guess', 'Revealed', 'Remaining'])

for i in np.arange(10000):
trials.append(play_monty_hall())

In [ ]:
trials

In [ ]:
#Initial guesses are approximately evenly distributed
results_g = trials.group('Guess')
results_g

In [ ]:
#Switching after the reveal as car approximately 2/3 of the time
results_r = trials.group('Remaining')
results_r

In [ ]:
both = results_g.join("Guess", results_r, "Remaining").relabel("Guess", "Item")
both.relabel("count", "Original Guess").relabel("count_2", "Remaining Door")

In [ ]:
both.barh("Item")


## Probability: At Least One Head¶

In [ ]:
toss = np.arange(1, 51, 1)
results = Table().with_columns(
"Toss", toss,
"Chance of at least one Head", 1 - (1/2)**toss)
results

In [ ]:
results.scatter("Toss")

In [ ]: