Four students put their stats textbooks into a hat. The teacher shuffles the stats textbooks in the hat then hands the textbooks back to the students in a random order. What is the probability that:
I don't think this counts as monte carlo because rather than generating random samples I generate all arrangements.
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=False)
def make_chart(probabilities, title):
"""
Generate a pie chart showing the probability distributio
"""
fig = {
'data': [{
'labels': list(probabilities.keys()),
'values': list(probabilities.values()),
'type': 'pie'
}],
'layout': {
'title': title
}
}
iplot(fig)
import itertools
NUM_STUDENTS = 4
def score_hand_back(handback):
"""
Examine the given handback and determine how many of the books
went to the correct student
"""
return sum(1 for idx, val in enumerate(handback) if idx == val)
assert(score_hand_back([0, 1, 2, 3]) == 4)
assert(score_hand_back([0, 3, 1, 2]) == 1)
def normalize(d):
"""
Normalize the values in the given dictionary such that
the sum of all values is 1
"""
return {key: val / sum(d.values()) for (key, val) in d.items()}
assert(normalize({0: 5, "fish": 15}) == {0: 1/4, "fish": 3/4})
def simulate_hats():
"""
Simulate every possible handback. Count how many times each
outcome occurs. Return a dictionary where the key is the number of
students who got the correct book and the value at that key is the
percentage of the time that happens.
"""
occurances = {0: 0, 1: 0, 2: 0, 4: 0}
possible_handbacks = itertools.permutations(range(NUM_STUDENTS))
for handback in possible_handbacks:
occurances[score_hand_back(handback)] += 1
return normalize(occurances)
make_chart(simulate_hats(), "Probability that n students get correct textbook")
Red Robin has a new red wine smoothie. Each smoothie comes with one of four lucky charms (the charm you receive with the smoothie is random). When you have collected all four lucky charms, you get a free smoothie. How many smoothies do you have to order on average to get all four smoothies?
import random
from collections import defaultdict
NUM_CHARMS = 4
def run_trial():
"""
Simulate giving someone smoothies until they have all four charms.
Return the number of smoothies they needed to get all four.
"""
all_smoothies = set(range(NUM_CHARMS))
num_tries = 0
received_smoothies = set()
while received_smoothies != all_smoothies:
received_smoothies.add(random.choice(range(NUM_CHARMS)))
num_tries += 1
return num_tries
def avg(lst):
"""
return the average of all the numbers in a list
"""
return sum(lst) / len(lst)
def simulate_smoothies(iterations=10000):
"""
run |iterations| trials and return the average
"""
ret = defaultdict(lambda: 0)
for _ in range(iterations):
ret[run_trial()] += 1
return ret
make_chart(simulate_smoothies(),
"Probability that customer will have to order n smoothies before collecting all four")