Everyone is famous [Monty Hall Problem](http://ja.wikipedia.org/wiki/%E3%83%A2%E3%83%B3%E3%83%86%E3%82%A3%E3% Do you know 83% BB% E3% 83% 9B% E3% 83% BC% E3% 83% AB% E5% 95% 8F% E9% A1% 8C)?
I'll leave the detailed explanation to the link above, but here's an overview of the problem.
There are 3 closed doors
One of the three doors A, B, C contains a free gift (car).
The other two contain a loss (goat)
You choose one door
The moderator will open the lost door of the doors you have not selected
Well, you may or may not reselect the door
By the way, at this time, is it more likely that the car will win if the door is reselected? Or is the probability the same without having to reselect?
At first you don't know which of the three doors the car is in.
Of the unselected doors, the loss will be open.
In this state, the door may or may not be reselected.
(The image is [Wikipedia](http://en.wikipedia.org/wiki/%E3%83%A2%E3%83%B3%E3%83%86%E3%82%A3%E3%83%BB% Reprinted from E3% 83% 9B% E3% 83% BC% E3% 83% AB% E5% 95% 8F% E9% A1% 8C))
Please think about it for the time being.
The Guinness Book of Records has the highest IQ in the world [Marilyn Boss Savant](http://en.wikipedia.org/wiki/%E3%83%9E%E3%83%AA%E3%83%AA] % E3% 83% B3% E3% 83% BB% E3% 83% 9C% E3% 82% B9% E3% 83% BB% E3% 82% B5% E3% 83% 90% E3% 83% B3% E3 % 83% 88) was controversial in 1990, saying that reselecting would double the odds of hitting a car than not reselecting.
On the other hand, readers flooded with 10,000 letters to the editor saying "her answer is wrong", of which 1000 are doctoral degree holders and "even if you change the door, the probability is 50-50 (1/5). Because it is 2), it cannot be doubled to 2/3. "
A mathematician who has published 1500 papers in his lifetime [Paul Erdős](http://ja.wikipedia.org/wiki/%E3%83%9D%E3%83%BC%E3%83%AB%E3 % 83% BB% E3% 82% A8% E3% 83% AB% E3% 83% 87% E3% 82% B7% E3% 83% A5) is "impossible", says Robert of George Mason University. "As a professional mathematician, I'm worried about the lack of mathematical knowledge of the general public. Admitting my mistakes will improve the situation," said Dr. Scott Smith of the University of Florida. Let the world's best intelligence index holders themselves stop the folly of spreading mathematical ignorance to the world immediately and get shameful! ", Criticized by other prominent doctors one after another. Controversial.
What about the correct answer?
Computer simulation of this problem by the Monte Carlo method proves that Savanto's answer was correct. In other words, if you reselect the door after the out-of-door is revealed, the probability is doubled.
Let's simulate with Python.
import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import font_manager
from random import choice
def montyhall(N, doors):
"""Returns a vector of winning or not solving the Monty Hall problem"""
#Prepare a vector with the same length as the number of trials
arr_picked = np.zeros(N)
arr_switch = np.zeros(N)
for i in range(N):
car = choice(doors) #Randomly decide the door that the car is in
picked = choice(doors) #Choose the door you think is the correct answer
#The moderator opens the door with the goat
goat = choice(list(set(doors) - set([picked, car])))
#Reselect
switch = choice(list(set(doors) - set([picked, goat])))
#Store the result of correctness at the corresponding position of each vector
if picked == car:
arr_picked[i] = 1 #1 if the answer is correct if not reselected
if switch == car:
arr_switch[i] = 1 #1 if the answer is correct when reselected
#Return two vectors
return (arr_picked, arr_switch)
def plot(N, arr_picked, arr_switch):
"""Plot the results"""
#It is convenient to specify the font according to the environment
if sys.platform == "darwin":
font_path = "/Library/Fonts/Osaka.ttf"
else:
font_path = "/usr/share/fonts/truetype/fonts-japanese-gothic.ttf"
prop = font_manager.FontProperties(fname=font_path)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
X = np.arange(N) + 1 #Bring the number of trials to the X axis
picked_car = arr_picked.cumsum() #Find the cumulative number of correct answers
switch_car = arr_switch.cumsum()
ax.plot(X, picked_car, label='picked up')
ax.plot(X, switch_car, label='switched car')
ax.set_title('Total number of Monty Hall problems', fontproperties=prop)
ax.legend(loc='best')
plt.savefig('image.png')
def main(args):
N = 10000 #Simulate 10000 times
doors = np.array([1, 2, 3])
#Solve the Monty Hall problem
(arr_picked, arr_switch) = montyhall(N, doors)
#Plot the results
plot(N, arr_picked, arr_switch)
#Calculate the cumulative number of wins
win_picked = arr_picked.sum()
win_switch = arr_switch.sum()
print("If you did not change the door: %f %% (%d)" %
(100.0 * win_picked / N, win_picked))
print("If you change the door: %f %% (%d)" %
(100.0 * win_switch / N, win_switch))
#=>During 10000 games
#If you did not change the door: 33.060000 % (3306)
#If you change the door: 66.940000 % (6694)
↑ The correct answer rate of the switched car (when reselected) is clearly doubled.
This problem is the basis of Bayesian statistics [posterior probabilities](http://en.wikipedia.org/wiki/%E4%BA%8B%E5%BE%8C%E7%A2%BA%E7%8E Often used to explain% 87). In other words, the probability that there are three doors and a car is in one of them is the "prior probability", and the probability that the moderator opens the door and sees the goat is the "posterior probability". Let's talk about Bayesian statistics at another time.
Even problems that were mistaken by a well-known mathematician 20 years ago can now be easily demonstrated using a computer at hand using only free software. At the extreme, we can say that we have acquired data analysis capabilities that surpass even mathematicians just 20 years ago. This is an exaggeration, of course, but it is certain that you can reveal the truth of things by moving your hands like this, moving your calculator, and actually analyzing the data. Acquire the ability to scientifically verify with a computer even when faced with real problems.
Recommended Posts