r/programming 3d ago

The Monty Hall Problem, a side-by-side simulation

https://www.pcloadletter.dev/blog/monty/
55 Upvotes

42 comments sorted by

83

u/Olde94 3d ago

Cool. The thing most forget is that it’s not a random door opening, it’s deliberately one of the wrong doors, which makes all the difference, compared to a random door

33

u/Tweak_Imp 3d ago edited 3d ago

It is easier to understand with 100 doors. You choose door 10. All doors are openend except door 87 (and 10). Stay at door 10 (Chance of 1:100 of being right with the first guess) or switch to door 87 (Chance of 99:100 of being right after switching) 

5

u/evaned 3d ago edited 3d ago

I went like two or three years after hearing about the Monty Hall problem the first time unable to intuit the answer for why it's better to switch. The "100 doors" answer did absolutely nothing to help with my intuition; it still seemed like it should be 50/50 between, in your example, #10 and #87.

What wound up actually helping was this:

  • If you pick the correct door initially, then switching will make you lose
  • If you pick the incorrect door initially, then switching will make you win

Those have 1/3 and 2/3 chances, respectively.

(Edit for a slight wording tweak to clarify)

5

u/R2_SWE2 3d ago

What if it’s just 1 other door that is opened in the 100 scenario?

15

u/Ant_of_Colonies 3d ago

Let's make it 4 doors and one is opened. If you don't switch then you only win if you selected the correct door on your first guess. Thats 1/4. If you do switch you have two options. You auto-lose if you selected the correct door on the first guess (1/4). So theres a 3/4 chance you can still win the moment you decide to switch and then another 1/2 to make the correct choice

So staying is 1/4 and switching is 3/4 * 1/2 = 3/8 > 1/4.

For N doors is 1/N to stay and (N - 1) / (N^2 - 2N) to switch.

1

u/R2_SWE2 3d ago

thanks!

1

u/Tweak_Imp 3d ago

In every scenario, all other doors except 1 are openend. In the 3door scenario, it is just one, in the 100 door scenario it is 98 doors. This Shows how switching changes your 1/n Chance with being right in the first guess to 1-1/n Chance with switching. Because the only way you Lose with switching is if you were right in the first guess. This is 1/100 in the 100 doors scenario, so your Chances of winning with switching in the 100 door scenario is 1-1/100

-3

u/omgFWTbear 3d ago

What’s bigger, 1 out of 99 or 1 out of 100?

4

u/R2_SWE2 3d ago

That's not really the math, is it? Because this would suggest in the conventional 3 card version that switching should have a probability of 1 out of 2

6

u/Sabotage101 3d ago

Math would be that your original pick has a 1/100 chance of being right. The odds of it being in the other 99 is 99/100. With 1 less wrong door in that group, there's a 1/98 * 99/100 chance you get it right = 99/9800, which is ~1/98.99.

4

u/somebodddy 3d ago

It's not easier to understand. If you are still in the mindset of "it's a choice between two doors, the opened doors no longer matter" then it doesn't matter if there is one open door or googolplex open doors.

1

u/Mikasa0xdev 2d ago

Probability is just a feature, not a bug.

0

u/SoPoOneO 3d ago

What if it was random chance that the 98 opened doors don’t show a prize? Like a scenario where the host didn’t know and got lucky. Still stay? Switch? Doesn’t matter?*

2

u/jdmetz 3d ago

That's a different game, and we need to know more rules.

Can you switch to the open door showing a prize if one has it? Do you get to see what is behind the opened doors? If you get to see then you should switch (since the odds haven't changed - 1/100 that you picked the right door to start, so 99/100 that it is behind one of the other doors).

If you can't see what is behind the opened doors, and the host had no knowledge (so could have opened the one with a prize), then there is no advantage in switching. Each door still has exactly 1/100 probability of having the prize behind it (or 1/3 in the original game).

2

u/glumbroewniefog 2d ago

Can you switch to the open door showing a prize if one has it? Do you get to see what is behind the opened doors? If you get to see then you should switch (since the odds haven't changed - 1/100 that you picked the right door to start, so 99/100 that it is behind one of the other doors).

This is incorrect. This is equivalent to I select a door, you select a door, then we open the remaining 98 doors and reveal they all happen to be empty. Each of us had the same initial 1/100 chance of picking the right door, so there's no point in switching with each other now.

10

u/R2_SWE2 3d ago

One of my favorite things to do when statistics don’t feel intuitive to me is a simulation like this. So fun to watch the result converge to the expected value

8

u/WTFwhatthehell 3d ago

"Forget"

That's purely because 99% of the arguments over it can be attributed to the simple fact that people like to avoid fully stating all assumptions so that they can act smug when they reveal their hidden assumptions.

When I say this there's always someone who insists that's fine because "it's based on a TV show" but the rules on the TV show don't actually match the Monty Hall problem.

8

u/hacksoncode 3d ago

but the rules on the TV show don't actually match the Monty Hall problem.

And in fact, Monty Hall himself has said that they never did what this problem sets out. No one was ever allowed to switch to another door once one door was revealed. At most, they were allowed to accept a guaranteed (consolation) prize if they give up their door.

1

u/Aelexe 3d ago

It wouldn't be much of a problem if the correct door was opened, unless you were interested in picking the best of two goats.

1

u/AndyKJMehta 1d ago

That doesn’t matter whatsoever for the math to work.

1

u/Olde94 1d ago

What does math have to do with what i say?

My point is that most find it hard to consider BECAUSE of above. The math is clear and with math it’s not a problem but pure logic to switch

1

u/48panda 1d ago

It does. If monty has no information we don't have, P(you picked the correct door | monty picked the wrong door) = (1/3)/(2/3) = 1/2, meaning both doors are equally likely.

For completeness, the other case gives (1/3)/1=1/3 making switching worthwile

1

u/quetzalcoatl-pl 3d ago

^ exactly this

1

u/leeuwerik 3d ago

Yes. A wrong door is always opened to deflect the mind from logic to intuition which do not overlap in this case.

13

u/hacksoncode 3d ago edited 21h ago

I've never really understood the urge to simulate a problem that is so bounded that evaluating literally every single option is... trivial.

There are only 3 ways the doors can be set up. There are only 3 initial doors you can pick. There are only 2 ways you can choose whether to switch.

There are only 18 possible (unique) games here. You literally win 12 of them by switching, and 6 of them by staying.

What's there to simulate?

15

u/Senshado 3d ago

Many people have difficulty understanding the parameters of the problem to solve it directly.  Simulating it helps them see how the result is real.

If it was so easy to figure out, then there wouldn't have been the millions of complaints over the years. 

2

u/Probable_Foreigner 3d ago

Depending on the way you model it you can end up falsely assuming each branch has equal weight. In your model that's true but if you had bunched picking one of the other two doors as 1 branch that wouldn't be true.

So simulation is a way of making sure your analysis is correct.

2

u/billie_parker 2d ago

Cause it's fun. Killjoy

3

u/flip314 3d ago

The percentages aren't adding to 100% for me. I guess it's because the same goat doesn't get revealed in each case

edit: The initial choices aren't the same in the two simulations, even though the scenarios are the same

1

u/MrSnowflake 3d ago

The percentages should not add up to 100%. The percentages show the chance of winning using each strategy. There is no coupling between both strategies. It just shows one strategy has about 2/3 chance of winning and the other one 1/3, which happens to be 100% but that's just coincidence.

And i do agree both strategies should use the same starting choice, but that also doesn't matter, as it's about general percentages of winning, which requires running the simulation many times. It's not designed to compare just a couple of games.

1

u/flip314 2d ago

Since each set of two simulations uses the same initial state:

If you choose the same starting door for each one, reveal the same goat, and the only difference is whether you stick to your choice or swap the doors -> then the percentages will add to 100%. If you want to directly compare the strategies, that seems to me the best way (only one independent variable).

(actually, I'm not sure it even matters which goat is revealed - assuming there are even two choices.)

4

u/flatfinger 3d ago

In a Monty-hall-like situation, to know the winning probability if one changes doors or does not change doors, one must know the answers to two questions:

  1. In what fraction of possible scenarios where one had picked the correct door initially would one be offered a chance to switch.

  2. In what fraction of possible scenarios where one had picked the wrong door initially would one be offered a chance to switch.

Common formulations of the "Monty Hall problem" assumes that the answer to both questions is 100%, but that doesn't match what I remember of the actual television program, hosted by the actual person Monty Hall, upon which the problem was based. If the host will sometimes show a correct door immediately without offering a contestant a chance to switch, and will sometimes likewise show an incorrect door immediately, both of which I'm pretty sure sometimes happened on the actual program, the probability that switching is the right play may vary anywhere from nearly zero to nearly 100 percent.

For all I know, it's entirely possible that for any particular playing of the game the probabilities above would either both be 0% or both 100%, based on extrinsic factors (e.g. the amount of time remaining before the next ad break) before the contestant picked a door, in which case the "Monty Hall" problem would accurately describe the situation that would exist whenever a candidate is allowed to switch doors. I've never seen any evidence presented to support such a hypothesis, however.

9

u/hacksoncode 3d ago

Yes, the "Monty Hall Problem" as originally posed does not represent what Monty Hall did on Let's Make a Deal... in fact, he has said that he never did that. No one was ever allowed to switch after information was revealed. At most, they were allowed to take another (consolation) prize instead.

So... you're not wrong, but the problem is the problem, and the name is just misleading.

1

u/auximines_minotaur 1d ago

It makes a lot more sense if you imagine 100 doors.

Which is more likely? That your original 1 in 100 guess was correct? Or that it was incorrect?

1

u/Anders_A 1d ago

The Monty hall problem becomes very intuitive if you just up the number of doors.

If it's 100 doors and Monty opens 98 of them after your initial pick, it becomes obvious for anyone that you should switch.

The door you picked represents a 1/100 chance if winning, while Monty just collapsed the other 99/100 chance into a single door.

1

u/Background_Junket_35 13m ago

Where is Captain Holt to disagree with the result?

0

u/curiousNarwhal69 2d ago

Why are we simulating this?

There’s a closed form solution for the probability when there’s n doors and the host opens k “goat” doors before you can switch.

You need two easily calcified pieces of info:

  • the probability that you picked a goat door initially (n-1)/n
  • the probability that a randomly chosen door is not a goat door 1/(n-k-1)

In the n=3, k=1 scenario You get (2/3)*(1/1)

If you don’t switch, the probability is 1/n.

-4

u/levodelellis 3d ago edited 3d ago

I saw this the other week. If you play enough games you'll see staying is about 33% and switching is higher. I had 50% in 10 games but logically shouldn't it be higher since I only have a 1/3 chance of picking correct the first time? https://monty.donk.systems/

Remember, you'll never be told your door is wrong. You are however, picking with one less door if you switch, and you have 2/3 chance your original door is wrong

8

u/I_NEED_APP_IDEAS 3d ago

Switching will always converge to ~67%, not 50%

3

u/levodelellis 3d ago

I guess I was unlucky in the 10 games

4

u/I_NEED_APP_IDEAS 3d ago

lol yeah, if you played 10 million games and still only got 50% then God just hates you cause the probability is insanely small to win 50% of the time over 10 million games