entry #1
written by lychee
submitted at
0 likes
guesses
- hyacinth (by oleander)
- lychee (by kimapr)
- oleander (by hyacinth)
comments 0
Notes_260304_012825.sdocx data
started at ; stage 2 at ; ended at
now I know my probabilities, next time won't you guess with me? explain the monty hall problem. submissions may be written in any language.
the monty hall problem is a well-known probability question with a result many find unintuitive. here's how it goes.
imagine you're on a game show where you have the chance to win a car. the host presents to you 3 doors and tells you that behind one of the doors is a car; behind the other two, goats. she invites you to pick a door, and you pick one – let's say you pick the first one. the host, who knows the location of the car, opens another door with a goat behind it – in this case, door number 2. she tells you, "whatever's behind your door, door #1, is yours to keep if you open it now. but you can also choose to swap and open the other door, door #3, instead. you can only open one of them." which door should you choose? does it matter?
the answer to this puzzle is that you should always swap and open the other door. in this example, there is a ⅔ chance that door #3 has a car behind it.
but why? your challenge is to demonstrate the solution to this problem programmatically, via any way or how. as any language is allowed, and this problem is open-ended, there is no fixed API.
you can download all the entries
written by lychee
submitted at
0 likes
written by kimapr
submitted at
0 likes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | function randomWithCondition(cond, ...) local number repeat number = math.random(...) until cond(number) return number end function simulateOneIsCar(doSwapOrNot) local doorWithCar = math.random(3) local initialDoorChoice = math.random(3) local revealedDoor = randomWithCondition(function(n) return n ~= doorWithCar and n ~= initialDoorChoice end, 3) local unrevealedDoor = randomWithCondition(function(n) return n ~= revealedDoor and n ~= initialDoorChoice end, 3) if doSwapOrNot then return unrevealedDoor == doorWithCar else return initialDoorChoice == doorWithCar end end function simulate(doSwapOrNot) local total = 0 local goats = 0 local cars = 0 local prob local timeoutInit = 100000 local timeout = timeoutInit for n = 1, math.huge do total = total + 1 if simulateOneIsCar(doSwapOrNot) then cars = cars + 1 else goats = goats + 1 end local newProb = cars / total * 100 if prob and math.floor(newProb * 1000 + .5) == math.floor(prob * 1000 + .5) then timeout = timeout - 1 else timeout = timeoutInit end prob = newProb if timeout <= 0 then break end end return prob end function assign(key, value) _G[key] = value return value end print ([[ The Morty Call problem is a famous statistics conundrum in which the problem resolver is presented with the following situation: - 3 doors, 2 with "goat" behind them, 1 with "car" - the presented-to must pick one door and obtains the "*" behind it - for unspecified reasons, it is desirable to obtain uncaring cold machine ("car") rather than friend ("goat") - after initial pick, presenter will reveal the "*" behind another door, where "*" is guaranteed to be "goat" - the presented-to may now change its choice with the newly-obtained information available The problem statement is: Which choice leads to more desirable outcomes (that is, obtainage of "car")? - changing the choice to the other door that was not revealed by the presenter, making use of the new information - double-down on the initial pick. It is postulated that "changing the choice to the other door" is the correct choice to make in accordance with problem statement and preamble. We consulted our probaballistics law team to find out if this postulate is true or not. Here are their findings: - "CHANGE CHOICE": "car" is ]]..("%.1f%%"):format(assign('pOfChange', simulate(true)))..[[ likely - "KEEP INITIAL": "car" is ]]..("%.1f%%"):format(assign('pOfDont', simulate(false)))..[[ likely Therefore, analysing our results, the conclusion is that: - "]]..(pOfChange > pOfDont and "CHANGE CHOICE" or "KEEP INITIAL")..[[" shall be the correct solution to the Morty Call problem. ]]) |
written by hyacinth
submitted at
0 likes
written by oleander
submitted at
0 likes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | Sure! Here's a simple Python program that uses a simulation to prove the solution to the Monty Hall problem. import random #This function simuates the Monty Hall problem for a number of trials given by the input variable numTrials. #It returns two variables, stayWin and switchWin, for the amount of times the player would win by staying with the original choice and the number of the times the player would win by switching their chosen door. def montyHallSimulation(numTrials): stayWin = 0 switchWin = 0 for _ in range(numTrials): #Step 1 #For each simulation, one of the three doors is picked to be the one containing the car using the random module. carDoor = random.randint(1,3) #Step 2 #The door that is the contestant's first choice is also randomly picked. choiceDoor = random.randint(1,3) #Step 3 #Checking the two doors that were not initally picked by the contestant in a random order, we find one of the two that contains a goat to reveal. #The third door is the remaining one that the contestant has the choice to switch to. checkDoors = [1,2,3] checkDoors.remove(choiceDoor) random.shuffle(checkDoors) if checkDoors[0] != carDoor: goatDoor = checkDoors[0] remainingDoor = checkDoors[1] else: goatDoor = checkDoors[1] remainingDoor = checkDoors[0] #Step 4 #Given the contestant's initial chosen door and the third remaining door, we assign a point to one of our two variables based on what options would lead them to win. if carDoor == choiceDoor: stayWin += 1 if carDoor == remainingDoor: switchWin += 1 #When the loop is done running for each of the trials, we return our two variables as the final result. return (stayWin, switchWin) #Here we run our function to simulate 1000 trials. numTrials = 1000 stayWin, switchWin = montyHallSimulation(numTrials) print(f"For {numTrials} trials of the Monty Hall problem:") print(f"The correct choice was to stay {stayWin} times, or {stayWin / numTrials * 100}% of the time.") print(f"The correct choice was to switch {switchWin} times, or {switchWin / numTrials * 100}% of the time.") Try running this code. We should expect to see that switching doors is the correct choice about two thirds of time, or 66.67%. Does this help you understand the Monty Hall problem? |
post a comment