Calculating a break-even lottery price
Simulations allow us to ask more nuanced questions that might not necessarily have an easy analytical solution. Rather than solving a complex mathematical formula, we directly get multiple sample outcomes. We can run experiments by modifying inputs and studying how those changes impact the system. For example, once we have a moderately reasonable model of global weather patterns, we could evaluate the impact of increased greenhouse gas emissions.
In the lottery example, we might want to know how expensive the ticket needs to be for it to not make sense to buy it. To understand this, we need to modify the ticket cost to see when the expected payoff is negative.
grand_prize, num_tickets, and chance_of_winning are loaded in the environment.
Diese Übung ist Teil des Kurses
Statistical Simulation in Python
Anleitung zur Übung
- Set
simsto 3000 and thelottery_ticket_costvariable to0. - If the mean value of
outcomesfalls below0,breakout of thewhileloop. - Else, increment
lottery_ticket_costby 1.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
# Initialize simulations and cost of ticket
sims, lottery_ticket_cost = ____, ____
# Use a while loop to increment `lottery_ticket_cost` till average value of outcomes falls below zero
while 1:
outcomes = np.random.choice([-lottery_ticket_cost, grand_prize-lottery_ticket_cost],
size=sims, p=[1-chance_of_winning, chance_of_winning], replace=True)
if outcomes.mean() < 0:
____
else:
____ += 1
answer = lottery_ticket_cost - 1
print("The highest price at which it makes sense to buy the ticket is {}".format(answer))