MontyHall_Simulation
  • AI Chat
  • Code
  • Report
  • Beta
    Spinner

    Monty Hall Puzzle

    • There are 3 doors to be chosen from for a definete prize behind one of the doors
    • After choosing the first time, the host reveals the door and provides an opportunity to switch before the prize is revealed
    • If the door revealed at the first time is not the prize door, then host provides a choice to switch or stay

    Problem Statement

    • Task is to identify the probability of winning in the case of :
    1. staying with Player's choice
    2. switching to Other choice
    3. switching to Host's choice

    Assumptions

    • The Host always lies! :D
    # import libraries
    import numpy as np
    # function to define doors with prizes and no prizes
    def define_doors():
        """
        Return :
        --------
        Randomly define door numbers for player's choice, other doors, prize & host choice
        """
        # list of doors
        doors = ["Door 1", "Door 2", "Door 3"]
        
        # random doors for each option
        player_choice = np.random.choice(doors)
        prize_door = np.random.choice(doors)
        host_choice = np.random.choice(np.setdiff1d(doors, [player_choice, prize_door], assume_unique=True))
        other_choice = np.random.choice(np.setdiff1d(doors, [player_choice, host_choice], assume_unique=True))
        
        return player_choice, prize_door, host_choice, other_choice
    
    player_choice, prize_door, host_choice, other_choice = define_doors()
    #print(f"Player-{player_choice}\nPrize-{prize_door}\nHost-{host_choice}\nOther-{other_choice}")    
    # function to suggest if switch is a good choice or not
    def oracle(choice : str)-> str:
        """
        determines if player's choice to switch, stay or listen to host is correct or not
        
        Args:
        ----
        switch - string, either switch, stay or host
        
        Return:
        ------
        boolean - if switch was a right choice or not
        """
        # execute define_doors() for choices
        player_choice, prize_door, host_choice, other_choice = define_doors()
        
        # if staying with player_choice
        if choice == "stay":
            return(player_choice == prize_door)
        # if switch to other_choice
        elif choice == "switch":
            return(other_choice == prize_door)
    
    #oracle(choice='stay')
        
        
    # simulate to determine if player_choice to STAY is winning strategy
    choices = ['stay', 'switch']
    for choice in choices :
        result_set = [oracle(choice=choice) for i in range(100000)]
        wins_prop = np.mean(result_set)
        print(f"Winning percentage for {choice} : {wins_prop}")

    Conclusion

    • The winning percentages for 1 million iterations :

      • STAY on Player's choice = 33.411 %
      • SWITCH to Other choice = 66.851 %
    • Hence, the takeaways are as follows :

      • SWITCH to Other choice has a greater chance of winning the prize.