[PYTHON] A story that I was very convinced when I wrote the code for the Monty Hall problem and calculated the winning percentage

What is the Monty Hall problem?

"There are three closed doors in front of the player, behind one door is a new prize car, and behind the two doors is a goat, which means off. The player hits the new car door. After the player selects one door, the moderator Monty opens the remaining door with the goat and shows the goat. The player is now told that he may change the door of his choice to the remaining unopened door. Should the player change the door? " ([Wikipedia](https://ja.wikipedia.org/wiki/Monty Hall problem))

The correct answer is "change", and the winning percentage when changed is 66.7%.

When I start studying probability statistics and machine learning, one day it comes out in connection with Bayes' theorem.

The Monty Hall problem (Monty Hall problem) is a problem of probability theory, which is one of the examples of posterior probability or subjective probability in Bayes' theorem. It derives from a game controversy in the American game show show "Let's make a deal," hosted by Monty Hall (real name Monte Halperin). It is a kind of psychological trick, and even if the results derived from probability theory are explained, there are still many people who are not convinced, so it is also called a dilemma or paradox. It is a good example of "a problem in which the answer that seems intuitively correct and the answer that is logically correct are different".

I wrote the code and calculated the winning percentage

At first, I simply dropped the problem statement into the code and executed it, but after thinking a little, it became the following form, and I was convinced without executing it.

python


from random import randint

itr = 10000
cnt = 0
for i in range(itr):
    t = randint(1, 3)
    c = randint(1, 3)
    if t != c:
        cnt += 1
print(cnt/itr)

If the first choice is a loss, you can switch to Atari. The probability of choosing a loss first is 66.7%, so if you switch, the winning percentage is 66.7%.

Recommended Posts

A story that I was very convinced when I wrote the code for the Monty Hall problem and calculated the winning percentage
A story that was convenient when I tried using the python ip address module
Let's write a simple simulation program for the "Monty Hall problem"
I wrote the code for Gibbs sampling
Intuitive explanation that does not rely on mathematical formulas for the Monty Hall problem and simulation with Python
Somehow the code I wrote worked and I was impressed, so I will post it
A story that I was addicted to when I made SFTP communication with python
The story when I was using IntelliJ on Linux and could not input Japanese
When writing to a csv file with python, a story that I made a mistake and did not meet the delivery date
A story that Seaborn was easy, convenient and impressed
A story that I was addicted to at np.where
The story I was addicted to when I specified nil as a function argument in Go
I wrote a script that splits the image in two
I wrote AWS Lambda, and I was a little addicted to the default value of Python arguments
Code that I wish I had remembered when I participated in AtCoder for the first time (Reflection 1 for the next time)
A story that didn't work when I tried to log in with the Python requests module