Problem
For purely recreational purposes I would like to solve the Monty Hall problem with Mathematica using the function Probability
(dedicated to the calculation of probabilities).
About the Monty Hall problem and its solution
Here is a possible formulation of the famous Monty Hall problem:
Suppose you’re given the choice of three doors: behind one door is a car, each door having the same probability of hiding it; behind the others, goats. You pick a door and the game organizer, who knows what’s behind the doors, opens another door which has a goat. They then says to you: “Do you want to pick the other door?”.
Is it to your advantage to switch your choice? Or more precisely: what is the probability that the car is behind the other door?
This is a well-known probability problem, and its solution may sometimes appear counterintuitive. The answer being: yes it is advantageous to switch your choice, the probability of finding the car behind the other door is $\frac{2}{3}$.
One way to arrive at this result is to use Bayes’ theorem. Let $C_i$ denote the event “the car is behind the door $i$”. We consider the case where door 3 has just been chosen. At this point: $P(C_1) = P(C_2) = P(C_3) = \frac{1}{3}$.
With disjunction of cases, one can notice that if the car is behind door 1, the organizer shall open door 2; if the car is behind door 2, the organizer shall open door 1; and finally if the car is behind door 3, the organizer may open either door 1 or 2 (each outcome being equiprobable).
We can then consider that door 1 has been opened by the organizer (thus discovering a goat behind it), while denoting this event $O_1$. To determine the probability that the car is behind the other door (door 2), we can calculate the conditional probability using the information we’ve just obtained:
$$ P(C_2 | O_1) = \frac{P( O_1 | C_2) P(C_2)}{P(O_1)} = \frac{P( O_1 | C_2) P(C_2)}{\sum_{i=1}^3 P(O_1 | C_i) P(C_i)} = \frac{\frac{1}{3}}{\frac{1}{2}} = \frac{2}{3}. $$
One can notice that the same reasoning applies regardless of the door chosen initially and the door opened subsequently. We can then conclude that the probability of finding the car behind the other door is always $\frac{2}{3}$.
My attempt to solve the problem with Mathematica
Obviously, it is very simple here to simulate the situation with Mathematica a large number of times in order to obtain the probability numerically. But I’m trying to solve the problem analytically using the function Probability
to get an exact result.
I therefore took up the situation described above: the door 3 has been chosen, and the door 1 has been subsequently opened by the organizer, and we want to determine the probability that the winning door is the other door (door 2). So I tried:
In[1]:= Probability[
(c == 2) \[Conditioned] (o == 1 && (c == 1 \[Implies] (o == 2)) && (c == 2 \[Implies] (o == 1))),
{
c \[Distributed] DiscreteUniformDistribution[{1, 3}],
o \[Distributed] DiscreteUniformDistribution[{1, 2}]
}
]
I considered two random variables in Mathematica: c
, the number of the winning door, following a discrete uniform distribution between 1 and 3; and o
, the number of the opened door, following a discrete uniform distribution between 1 and 2 (since door 3 has been chosen, it can no longer be opened). The Probability
function considers a priori that these variables are independent. So I used the expression after \[Conditioned]
to express the door opened by the organizer, and the link between that door and the winning door.
Unfortunately, I don’t get the expected result:
Out[1]= 1/2
I think I understand why Mathematica comes up with this output: it simplifies the expression after \[Conditioned]
to o == 1 && c != 1
and eliminates information about o
(since it considers the variables as independent) thus leading to the aforementioned result.
Thenceforth, I am not sure how to model the problem with the Probability
function in such a way as to correctly express the link between the winning door and the opened door.