from datascience import *
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plots
plots.style.use('fivethirtyeight')
cookies = make_array('chocolate chip', 'oatmeal raisin', 'peanut butter')
cookies
for cookie in cookies:
feelings = cookie + ' cookies are delicious!'
print(feelings)
cookie = cookies.item(0)
feelings = cookie + ' cookies are delicious!'
print(feelings)
cookie = cookies.item(1)
feelings = cookie + ' cookies are delicious!'
print(feelings)
cookie = cookies.item(2)
feelings = cookie + ' cookies are delicious!'
print(feelings)
for cookie in cookies:
if cookie == "oatmeal raisin":
feelings = cookie + ' cookies are not delicious!'
print(feelings)
else:
feelings = cookie + ' cookies are delicious!'
print(feelings)
coin = ['heads', 'tails']
np.random.choice(coin, 6)
tosses = np.random.choice(coin, 6)
tosses
sum(tosses == 'heads')
outcomes = make_array()
tosses = np.random.choice(coin, 100)
outcomes = np.append(outcomes, sum(tosses == 'heads'))
outcomes
outcomes = make_array()
for i in np.arange(10000):
tosses = np.random.choice(coin, 100)
outcomes = np.append(outcomes, sum(tosses == 'heads'))
outcomes
outcome_table = Table().with_column(
'Number of heads in 100 tosses',
outcomes
)
outcome_table.hist(bins=np.arange(25.5, 75.5))
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:
Contestant makes an initial choice of door, but the door stays closed
One of the other doors with a goat behind it is opened
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?
hidden_behind_doors = ['first goat', 'second goat', 'car']
hidden_behind_doors
goats = ['first goat', 'second goat']
goats
def other_goat(goat):
if goat == 'first goat':
return 'second goat'
elif goat == 'second goat':
return 'first goat'
other_goat('first goat')
other_goat('second goat')
other_goat('apple')
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)]
play_monty_hall()
results = Table(['Guess', 'Revealed', 'Remaining'])
results
results.append(play_monty_hall())
results
trials = Table(['Guess', 'Revealed', 'Remaining'])
for i in np.arange(10000):
trials.append(play_monty_hall())
trials
#Initial guesses are approximately evenly distributed
results_g = trials.group('Guess')
results_g
#Switching after the reveal as car approximately 2/3 of the time
results_r = trials.group('Remaining')
results_r
both = results_g.join("Guess", results_r, "Remaining").relabel("Guess", "Item")
both.relabel("count", "Original Guess").relabel("count_2", "Remaining Door")
both.barh("Item")
toss = np.arange(1, 51, 1)
results = Table().with_columns(
"Toss", toss,
"Chance of at least one Head", 1 - (1/2)**toss)
results
results.scatter("Toss")