[PYTHON] Solve the Monty Hall problem

Monty Hall problem

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.

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.

250px-Monty_closed_doors.svg.png

Of the unselected doors, the loss will be open.

250px-Monty_open_door_chances.svg.png

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.

Controversial issues

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?

Simulate the problem

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)

image.png

↑ The correct answer rate of the switched car (when reselected) is clearly doubled.

Summary

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

Solve the Monty Hall problem
Do you understand the Monty Hall problem?
Let's write a simple simulation program for the "Monty Hall problem"
How to solve the bin packing problem
Solve the traveling salesman problem with OR-Tools
Solve the maximum subarray problem in Python
Try to solve the fizzbuzz problem with Keras
Try to solve the Python class inheritance problem
Solve the knapsack problem using pyomo and glpk
[At Coder] Solve the problem of binary search
Examine the dual problem
Solve the verbal arithmetic
Try to solve the internship assignment problem with Python
I tried to solve the problem with Python Vol.1
About the traveling salesman problem
Solve the Japanese problem when using the CSV module in Python.
The problem becomes easier to solve depending on the formulation method
Solve the problem of missing libcudart in Ubuntu 16.04 + CUDA 8.0 + Tensorflow environment
Try to solve the N Queens problem with SA of PyQUBO
I wanted to solve the ABC164 A ~ D problem with Python
Solve the initial value problem of ordinary differential equations with JModelica
Solve the Python knapsack problem with a branch and bound method
I tried to solve the shift scheduling problem by various methods
Solve the subset sum problem with a full search in Python
Try to solve the function minimization problem using particle swarm optimization
Think about the minimum change problem
About the Ordered Traveling Salesman Problem
Solve the delay of interferometer observation
Illustration of the results of the knapsack problem
Use Raspberry Pi to solve the problem of insufficient mobile Wi-Fi connection
The 16th offline real-time how to write reference problem to solve with Python
Try to solve the traveling salesman problem with a genetic algorithm (Theory)
The 19th offline real-time how to write reference problem to solve with Python
I tried to solve the E qualification problem collection [Chapter 1, 5th question]
I tried to solve the inverted pendulum problem (Cart Pole) by Q-learning.
Intuitive explanation that does not rely on mathematical formulas for the Monty Hall problem and simulation with Python