CommencerCommencer gratuitement

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.

Cet exercice fait partie du cours

Statistical Simulation in Python

Afficher le cours

Instructions

  • Set sims to 3000 and the lottery_ticket_cost variable to 0.
  • If the mean value of outcomes falls below 0, break out of the while loop.
  • Else, increment lottery_ticket_cost by 1.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# 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))
Modifier et exécuter le code