# 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 function go, which will call montyHall many times. montyHall has mode parameter, which can control the decision method. montyHall returns 1 in 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 a car behind random door (0, 1, 2) and make 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;
}

Let's put a positions of goats into variables g0, g1 and open one of goats (randomly).

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 goat (open) and our former choice (pme), we have to make a new decision (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

ModeProbabilityComment
Mode 01/3Keeping original choice
Mode 11/3Choosing one of two "unchosen" doors
Mode 21/2Choosing one of two closed doors
Mode 31/2Choosing closed door, that is different from our previous choice

As we can see, just changing your choice (Mode 1) does not change probability (still 1/3). But if we choose different door, which is sitll closed, it is 1/2. But 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 a 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 a door that was not picked by the contestant.
• The host must always open a door to reveal a goat and never the car.

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, 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 (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;
}

If the first condition holds (me==car), the host tells us a position of one of the goats, so the situation is 50:50. But otherwise (me!=car), the host tells the exact positon of a car, which happens much more often (first choice was correct in 1 of 3 cases, it was wrong in 2 of 3 cases).

### Results

ModeProbabilityComment
Mode 01/3Keeping original choice
Mode 11/3Choosing one of two "unchosen" doors
Mode 21/2Choosing one of two closed doors
Mode 32/3Choosing closed door, that is different from our previous choice

JS Code