Previously published AI-related blog posts:
- Artificial Intelligence – Bayes Network
- Artificial Intelligence – Machine Learning from Supervised learning
Earlier in our AI blog series we have covered topics that have been quite comprehensive and theoretical, so it’s time we show you the more the practical side. We’re going to start with an example using Bayes Rule and prove it through simulations in Python. To sweeten the deal even further, this post could potentially win you a car one day.
The Infamous Monty Hall Problem
If you have taken a statistics course at an upper level or seen the movie “21”, then you have probably heard of the Monty Hall problem. If not, suppose that you’re on a game show and you have three closed doors in front of you, where behind two of the doors there are goats, and behind the last one there’s a car.
You guess one of the doors, and the game show hosts opens one of the doors you didn’t pick, say the third door, revealing a goat and leaving your guess and the second door closed. The host then gives you the option to keep the door you have or switch to the second door. What do you do?
Your gut feeling might say that It’s now a 50/50 shoot of getting the car, while in fact if you switch the odds are 2/3 in your favor. Let’s take this step by step:
Before any guesses have been made, there is a 1/3 probability for each door, which adds up to 1, obviously. After one door is removed, the probability of the first door you picked has not changed, but you do gain more information about the other closed door. The probability must add up to 1 (if you had the option to open both of them), and therefore 1/3 + x = 1, gives you a 2/3 probability if you switch. The reason why you change the probability if you switch (and not stay) is because you gain information about the closed door (the switch) when the host reveals one false door. Opening one door updates your information about the switch, while you haven’t learned anything new about your original pick because it was never an option to open your original guess. This might be a bit confusing, so let’s try to solve this through Bayes Rule:
We have three doors, so the odds of the car being behind one of them is P(CarA), P(CarB), P(CarC), are all equal to 1/3. Now let’s shift our focus a little bit, and look at the probability that the host opens a certain door.
Let’s say that we pick door A, and door B is opened with a goat behind it. So, we look at the probability of the car being behind door A, given that the host opens door B.
P(CarA|B) = P(B|CarA)*P(CarA) /P(B)
(We recommend that you read this post to understand the equation better.)
We need to find what is the probability that the host opens door B when we picked door A.
P(B) = P(B|CarA)*P(CarA)+P(B|CarB)*P(CarB)+P(B|CarC)*P(CarC).
So, let’s break up this equation into three separate equations, and remember, we picked door A:
- P(B|CarA): the odds of opening door B if the car is behind door A, is a ½, since the host can in theory pick door B or C
- P(B|CarB): the odds of the host opening door B if the car is in B is zero, since he will never pick the door with the car to open.
- P(B|CarC): Finally, the odds of the host opening door B, given that we picked door A and the car is in door C has to be 1. It’s the only option.
This gives us:
Similarly, if we do the same exercise where we pick A and the car is in C – P(CarC|B) we will get the probability of 2/3.
To prove this, we’ve added a code to test out the math for 10000 simulations. Try it yourself!
Monty Hall Problem in Python
In this script we simulate 10000 timers that we pick a door at random and remove one of the two other doors that has a goat behind it. We then count the number of times we stay at the original door and the number of times we switch doors.
import numpy as np # N Samples N = 10000 #Define an array of the different doors with the car at random cars = np.random.randint(0,high=3,size=N)+1 #define an array of the different doors, and picks at random picks = np.random.randint(0,high=3,size=N)+1 #Counters for win if stay and switch count_stay =0 count_switch =0 for i in range(N): #define array of 3 doors doors_round1 = [1,2,3] #First we have to remove both the car and the pick doors_round1.remove(picks[i]) if cars[i] != picks[i]: doors_round1.remove(cars[i]) #Will open one door at random. #If Cars and Picks are the same door, it can only choose one. open_door = doors_round1[np.random.randint(len(doors_round1))] doors_round2 = [1,2,3] doors_round2.remove(open_door) #Switch picks doors_round2.remove(picks[i]) pick2 = doors_round2 if cars[i] == picks[i]: count_stay = count_stay + 1 if cars[i] == pick2: count_switch = count_switch + 1 print("\nStay: %d"%(100*count_stay/N)) print("Switch: %d"%(100*count_switch/N))
The output of running these 10000 simulations gives you:
So, next time you’re on a game show or just want to win a bar bet with your friends, you know what to do. You can even prove it Bayes-style.