After verifying the Monty Hall problem with Ruby, a story that I could understand well and did not understand well

What is the Monty Hall problem?

The Monty Hall problem is a ** problem of probability that is not intuitively convincing **. I didn't feel refreshed when I looked it up on the internet, so I decided to verify it myself.

You can get an overview on this site, but I will explain a little on this page as well.

Overview of the Monty Hall problem

When you select a box, it will open one of the outliers. After that, when you can change the selected box, the question is what is the best way to change or not change the options.

Box 1 Box 2 Box 3
Hit Off Off

For example, suppose the game organizer opens the out-of-box with Box 2 selected from the three boxes above (not yet opened). The situation is like below

Box 1 Box 2 Box 3
Hit Off Off
choosing OPEN

If the selection can be changed when Box 3 is found to be out of order, should it be changed? Is the problem.

Intuitively, there are two choices, hit or miss, so it seems that it doesn't matter which one you choose, but mathematically it is as follows.

As it is Change choices
Probability of winning 33% 67%

When I first saw this, I thought, "That's stupid ...". I wasn't quite convinced by looking at the explanation, so when I verified it using programming, I was very disappointed, so I will explain it. (But after all, new mysteries have increased)

Code and verify immediately

We will verify using Ruby. (Although it can be refactored a little more, there are some parts that are redundantly written so that it is easy to understand what you are doing. Still, it may be a little difficult to understand ... lol)

code

ruby


#Number of trials (about 1 million times is enough)
number = 1_000_000

#Prepare hits and misses
win = 1
lose = 0

#Prepare a box
boxes = [win, lose, lose]

#Case 1 (when the options are not changed)
count = 0
number.times do
  #Randomly decide which box to choose
  random_number = [0, 1, 2].sample
  selected_box = boxes[random_number]
  #Open the out-of-box (this process doesn't make any sense because you won't change the options next)
  random_number != 2 ? boxes.delete_at(2) : boxes.delete_at(1)
  #Judge whether it is a hit with the selected box
  count += 1 if selected_box == win
end
#Probability calculation
prob_1 = (count.to_f / number.to_f).round(5) * 100

puts "Probability of not changing options: #{prob_1}%"

#Case 2 (when changing options)
count = 0
number.times do
  #Select a box
  random_number = [0, 1, 2].sample
  selected_box = boxes[random_number]
  #Open the outlying box and change the selected box
  if random_number == 0
    boxes.delete_at(2)
    selected_box = boxes[1]
  elsif random_number == 1
    boxes.delete_at(2)
    selected_box = boxes[0]
  else
    boxes.delete_at(1)
    selected_box = boxes[0]
  end
  #Judgment
  count += 1 if selected_box == win
end
#Probability calculation
prob_2 = (count.to_f / number.to_f).round(5) * 100

puts "Probability of changing choices: #{prob_2}%"


result

The result of executing the file. You can see that your intuition is definitely wrong 6222a43d3cc8b2ba6e6aba05f660e0b6.png

A little commentary

I will try to solve this problem with Ruby and explain what I thought it was. The point is ** case classification according to the first choice **

When you select a box, it will tell you one box that is out of alignment, so this problem can be divided into the following two patterns.

Each probability is naturally as follows

Probability of choosing the winning box first Probability of choosing the wrong box first
33% 67%

Next, let's look at the story of changing or not changing options from here.

If you do not change your options

If you don't change your options, you have to choose the winning box first. Originally, the probability of winning is 33%, so the probability of winning in this case is 33%.

When changing options

When changing the options, if you first select the winning box, you will lose, and if you select the wrong box, you will win. In other words, in order to finally win, you must first select the out-of-box. At first, the probability of choosing a gap is 67%, so in this case the probability of winning is 67%.

A little doubt

I've explained so far, but some questions came up while I was writing. Let's say another participant arrives when the box is opened.

`At this time, from the perspective of another participant, which box is the winner? ``

It's okay to come up with this question, but I can't explain it well, so if you are familiar with mathematics, please comment.

Finally

It ended up being a little moody, but it was an interesting problem! The Monty Hall problem is simple but deep.

Recommended Posts

After verifying the Monty Hall problem with Ruby, a story that I could understand well and did not understand well
The story that I could not build after installing multiple Java on Windows
A story that I was really into when I did triple DES with ruby
I faced a problem that JS is not read after page transition and JS is read when loaded.
A story that even a man who does not understand C language could add new functions to Ruby 2.6
I can't use SQS that can read the questionnaire with a scanner → I could use it after changing the java ver
A quick look at the Monty Hall problem
Solved the problem that profile and logout were not displayed on the screen after signup.
Solved the problem that the test could not be executed from the command line with spring-boot-starter-test
The story that did not disappear when I tried to delete mysql on ubuntu
[Ruby] I made a crawler with anemone and nokogiri.
[JQuery] A guy that even beginners could understand well
The story that the forced update could not be implemented
The story that led to solving the error because postgres did not start with docker-compose up
The story that the request parameter from the iPhone application could not be obtained successfully with the Servlet
Use cryptographically secure pseudo-random numbers to verify that the solution to the Monty Hall problem is not 50%
I made a program in Java that solves the traveling salesman problem with a genetic algorithm
A story that a Ruby beginner made and released a LINE BOT that tells the train time in 2 months
The story that the DB connection and other timeouts did not time out according to the set value
I tried to express the result of before and after of Date class with a number line
After conducting a Jersey API test, I encountered a phenomenon where the JSR310 API could not be deserialized.
[Rails] A story that continued to incorrectly verify the reason why the update action did not pass (update)
I tried to solve the problem of "multi-stage selection" with Ruby
A story that I struggled to challenge a competition professional with Java
About the problem that the image is not displayed after AWS deployment
[Gradle] The story that the class file did not exist in the jar file
I made a Ruby container image and moved the Lambda function
Solved the problem that the scroll event did not fire in JQuery
What I did when the DB did not start with docker-compose up
A story that struggled with the introduction of Web Apple Pay
The story that the build error did not stop when using Eclipse 2020
A story that addresses the problem that REMOTE_ADDR cannot be acquired in a cluster built with Docker Swarm + Traefik (1.7).
A story that I was addicted to twice with the automatic startup setting of Tomcat 8 on CentOS 8
A story that I wanted to write a process equivalent to a while statement with the Stream API of Java8
How to operate IGV using socket communication, and the story of making a Ruby Gem using that method
I want to download a file on the Internet using Ruby and save it locally (with caution)
The story of introducing Gradle as a retrofit to an existing system that did not manage packages
There is LSP and I tried to create an environment to write Java with Vim (NeoVim), but after all I could not beat the IDE ...
What I did when I was addicted to the error "Could not find XXX in any of the sources" when I added a Gem and built it