# Monty Hall problem makes no sense, or does it?

It has been 8 years since I heard about Monty Hall problem for the first time. I never completely trusted the offical solution. The only solution I would completely trust is my own simulation of thousands of games, so I decided to make one and prove the rest of the world wrong.

Here is a description of a problem from Wikipedia:

Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your advantage to switch your choice?

## Simulation

Let me start writing a Javascript code, each part will be previously explained. My simulation will start in a function go, which will call montyHall many times. montyHall has a mode parameter, which can control the decision strategy. montyHall returns 1 when player wins, and 0 when player loses.

function go()
{
for(var m=0; m<4; m++)
{
var lim = 100000, sum = 0;
for(var i=0; i<lim; i++)  sum += montyHall(m);
console.log(sum/lim);
}
}

Let's implement the montyHall function. Let's put the car behind a random door (0, 1, 2) and make a player pick a random door (0, 1, 2).

function montyHall(mode)
{
var car = Math.floor(Math.random()*3);
var me = Math.floor(Math.random()*3);
//... revealing one goat
//... maybe changing the value of "me"
return car == me ? 1 : 0;
}

After placing the car randomly, let's put two goats (variables g0, g1) behind the remaining doors and reveal one of them.

var g0, g1;
if(car==0) { g0=1; g1=2; }
if(car==1) { g0=0; g1=2; }
if(car==2) { g0=0; g1=1; }

var open = Math.random()<0.5 ? g0 : g1;
var pme = me;

We also stored users previous decision into pme. Now, knowing an open door with a goat (open) and our former choice (pme), we have to make a new decision (a new value of me).

### Mode 0

In this mode, we just keep our previous decision.

me = pme;

### Mode 1

In this mode, we change our choice randomly to one of two "unchosen" doors. It is one of the ways how I used to understand a problem.

if(pme==0) me = Math.random()<0.5 ? 1 : 2;
if(pme==1) me = Math.random()<0.5 ? 0 : 2;
if(pme==2) me = Math.random()<0.5 ? 0 : 1;

### Mode 2

In this mode, we change our choice randomly to one of two still closed doors. It is another way to understand a problem.

if(open==0) me = Math.random()<0.5 ? 1 : 2;
if(open==1) me = Math.random()<0.5 ? 0 : 2;
if(open==2) me = Math.random()<0.5 ? 0 : 1;

### Mode 3

Previous methods were aware of either open door, or previously chosen doors, not both at the same time. Let's choose a door, that is still closed, but different from our previous choice.

if(pme!=0 && open!=0) me = 0;
if(pme!=1 && open!=1) me = 1;
if(pme!=2 && open!=2) me = 2;

### Results

ModeWin ProbabilityComment
Mode 01/3Keeping the original choice
Mode 11/3Random of two "unchosen" doors (may be open)
Mode 21/2Random of two closed doors (may be the previous one)
Mode 31/2Choosing the closed door, that is different from our previous choice

As we can see, just changing our choice (Mode 1) does not change the probability (still 1/3). But if we choose different door, which is sitll closed, it is 1/2. But the correct answer is said to be 2/3, so did we prove the whole world wrong?

## Correct simulation

Modes 1 and 2 in our previous simulation seem to be strange, since they ignore one of known facts. But Mode 3 should fix it. But according to "current science", changing our choice to another closed door should let us win 2/3 of the time. So is our decission procedure in Mode 4 still wrong? No. The way of revealing a goat is wrong.

By revealing one of goats, the host tells us much more, than just pointing out two possible positions of the car with 50:50 chance. There are 2 important rules, that are not pronounced directly and may not be obvious to everyone.

• The host must always open the door that was not picked by the contestant.
• The host must always open the door to reveal a goat and never the car.

Note, that in our previous implementation, the host could open the door chosen by the contestant (which violates the first rule). With these rules in mind, let's remake the way we open the door, while keeping all the code as it is. If we have chosen the car, the host will randomly open one of goats.

var open;
if(me==car) open = Math.random()<0.5 ? g0 : g1;

Otherwise, there is just one possible way to open the door (the host can not open the car or the door chosen by us, and since both these door are different, just one door with a goat remains).

else
{
if(me!=0 && car!=0) open = 0;
if(me!=1 && car!=1) open = 1;
if(me!=2 && car!=2) open = 2;
}

When there is a car behind our door at the beginning (33% chance), the host reveals the goat, we choose another closed door (the goat) and we lose. But when we choose the door with a goat at the beginning (66% chance), the host reveals the second goat and the other closed door must contain the car - we win.

### Results

 Mode 0 1/3 Keeping the original choice Mode 1 1/3 Random of two "unchosen" doors (may be open) Mode 2 1/2 Random of two closed doors (may be the previous one) Mode 3 2/3 Choosing the closed door, that is different from our previous choice

JS Code