📜 ⬆️ ⬇️

Monty Hall Paradox and Simulation

Parado Monty Hall


The Monty Hall paradox is one of the known problems of probability theory, the solution of which, at first glance, is contrary to common sense. The task is formulated as a description of a hypothetical game based on the American television show "Let's Make a Deal", and is named after the host of this show. The most common formulation of this problem, published in 1990 in Parade Magazine, is as follows:
Imagine that you become a member of a game in which you need to choose one of three doors. Behind one of the doors is a car, behind two other doors - a goat. You choose one of the doors, for example, number 1, then the presenter, who knows where the car is and where the goats open, opens one of the remaining doors, for example, number 3, behind which there is a goat. After that, he asks you if you would like to change your choice and choose door number 2. Will your chances of winning a car increase if you accept the offer of the presenter and change your choice?

Decision


When solving this problem, it is usually argued like this: after the presenter has opened the door, behind which there is a goat, the car can only be behind one of the two remaining doors. Since the player cannot get any additional information about which door is behind the car, the probability of finding the car behind each of the doors is the same, and changing the initial door selection does not give the player any advantage. However, this line of reasoning is incorrect. If the presenter always knows which door is behind, always opens the goat behind the door, and always invites the player to change his choice, then the probability that the car is behind the door chosen by the player is 1/3, and accordingly, the probability that the car is located behind the remaining door is 2/3. Thus, changing the initial choice increases the chances of a player winning a car by 2 times. This conclusion contradicts the intuitive perception of the situation by the majority of people; therefore, the described task is called the Monty Hall paradox.
Details can be found in the relevant Wikipedia article.

Modeling


Since for some reason the abstract logical thinking didn’t fail me this time, I had to call brute force to the rescue - a simulation model in which I carried out further testing.
Script code (PHP). Code notes are welcome.
Result:
Number of games: 10,000.
Gamer wins 6664 times.
Gamer looses 3336 times.

 <?php $wins=0; $looses=0; $change_choice = 1; //  for ($i=1;$i<=10000;$i++){ $car_position = rand(1,3); #Let's begin #Step 1 $gamer_first_choice = rand(1,3); $showman_choice = rand(1,3); while($showman_choice==$gamer_first_choice || $showman_choice==$car_position){ $showman_choice=rand(1,3); } #echo "Car is in ".$car_position." door. Gamer choose ".$gamer_first_choice." door, showman opens ".$showman_choice." door. Continue..."; #Step 2 if ($change_choice!=0){ for($second=1;$second<=3;$second++){ if($second!=$gamer_first_choice && $second!=$showman_choice){ $gamer_second_choice = $second; //  } } } #echo "Gamer's first choice is ".$gamer_first_choice.". Showman opens ".$showman_choice." door. Gamer changes choice to ".$gamer_second_choice."."; if ($gamer_second_choice==$car_position) { #echo 'Gamer won!'; $wins++; } else { #echo 'Gamer loose.'; $looses++; } } echo "Number of games: ".$i.". Gamer wins ".$wins." times. Gamer looses ".$looses." times."; ?> 
<?php $wins=0; $looses=0; $change_choice = 1; // for ($i=1;$i<=10000;$i++){ $car_position = rand(1,3); #Let's begin #Step 1 $gamer_first_choice = rand(1,3); $showman_choice = rand(1,3); while($showman_choice==$gamer_first_choice || $showman_choice==$car_position){ $showman_choice=rand(1,3); } #echo "Car is in ".$car_position." door. Gamer choose ".$gamer_first_choice." door, showman opens ".$showman_choice." door. Continue..."; #Step 2 if ($change_choice!=0){ for($second=1;$second<=3;$second++){ if($second!=$gamer_first_choice && $second!=$showman_choice){ $gamer_second_choice = $second; // } } } #echo "Gamer's first choice is ".$gamer_first_choice.". Showman opens ".$showman_choice." door. Gamer changes choice to ".$gamer_second_choice."."; if ($gamer_second_choice==$car_position) { #echo 'Gamer won!'; $wins++; } else { #echo 'Gamer loose.'; $looses++; } } echo "Number of games: ".$i.". Gamer wins ".$wins." times. Gamer looses ".$looses." times."; ?>

')
Alternate version written by friends on perl-e:

 #!/usr/bin/perl use strict; use warnings; my $iteration = 10000; my $change_choice = 1; print "\n"; print modelingMontyHall($change_choice); print "\n"; sub modelingMontyHall { my $change_choice = shift; my $success_counter = 0; my ( $i, $j ); my @doors; # 0 - animal, 1 - car my $gamer_choice; my $showman_open_door; my $result = 0; my $success_door; if ($change_choice) { print "Gamer change choice\n" } else { print "Gamer NOT change choice\n" }; for ( $i = 0 ; $i < $iteration ; $i++ ) { # Preparing @doors = ( 0, 0, 0 ); $success_door = int( rand() * 3 ); $doors[$success_door] = 1; # door with car # First step $gamer_choice = 0; for ( $j = 0 ; $j < 3 ; $j++ ) { if ( ( $j != $gamer_choice ) and ( $doors[$j] == 0 ) ) { $showman_open_door = $j; last; } } # Second step if ($change_choice) { for ( $j = 0 ; $j < 3 ; $j++ ) { if ( ( $j != $gamer_choice ) and ( $j != $showman_open_door ) ) { $gamer_choice = $j; last; } } } # Count result if ( $doors[$gamer_choice] ) { $result++; } } return $result; } 
#!/usr/bin/perl use strict; use warnings; my $iteration = 10000; my $change_choice = 1; print "\n"; print modelingMontyHall($change_choice); print "\n"; sub modelingMontyHall { my $change_choice = shift; my $success_counter = 0; my ( $i, $j ); my @doors; # 0 - animal, 1 - car my $gamer_choice; my $showman_open_door; my $result = 0; my $success_door; if ($change_choice) { print "Gamer change choice\n" } else { print "Gamer NOT change choice\n" }; for ( $i = 0 ; $i < $iteration ; $i++ ) { # Preparing @doors = ( 0, 0, 0 ); $success_door = int( rand() * 3 ); $doors[$success_door] = 1; # door with car # First step $gamer_choice = 0; for ( $j = 0 ; $j < 3 ; $j++ ) { if ( ( $j != $gamer_choice ) and ( $doors[$j] == 0 ) ) { $showman_open_door = $j; last; } } # Second step if ($change_choice) { for ( $j = 0 ; $j < 3 ; $j++ ) { if ( ( $j != $gamer_choice ) and ( $j != $showman_open_door ) ) { $gamer_choice = $j; last; } } } # Count result if ( $doors[$gamer_choice] ) { $result++; } } return $result; }


Results of the run:
Gamer NOT change choice: 2562
Gamer change choice: 7586

PS: By the way, after writing this particular script, I began to relate to the pseudo-random number generator in the Zend engine with a bit of distrust. Just look again at the results of the run in PHP and Perl :)
PPS: I apologize for the design of the code. Everything was done "on the knee" and the task of improving readability did not stand.

Source: https://habr.com/ru/post/55321/


All Articles