2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,10 +1,16 @@
[[File:Monte_Hall_problem.jpg|500px||right]]
Run random simulations of the [[wp:Monty_Hall_problem|Monty Hall]] game. Show the effects of a strategy of the contestant always keeping his first guess so it can be contrasted with the strategy of the contestant always switching his guess.
:Suppose you're on a game show and you're given the choice of three doors. Behind one door is a car; behind the others, goats. The car and the goats were placed randomly behind the doors before the show. The rules of the game show are as follows: After you have chosen a door, the door remains closed for the time being. The game show host, Monty Hall, who knows what is behind the doors, now has to open one of the two remaining doors, and the door he opens must have a goat behind it. If both remaining doors have goats behind them, he chooses one randomly. After Monty Hall opens a door with a goat, he will ask you to decide whether you want to stay with your first choice or to switch to the last remaining door. Imagine that you chose Door 1 and the host opens Door 3, which has a goat. He then asks you "Do you want to switch to Door Number 2?" Is it to your advantage to change your choice? ([http://www.usd.edu/~xtwang/Papers/MontyHallPaper.pdf Krauss and Wang 2003:10])
Note that the player may initially choose any of the three doors (not just Door 1), that the host opens a different door revealing a goat (not necessarily Door 3), and that he gives the player a second choice between the two remaining unopened doors.
;Task:
Simulate at least a thousand games using three doors for each strategy <u>and show the results</u> in such a way as to make it easy to compare the effects of each strategy.
;Reference:
* [https://www.youtube.com/watch?v=4Lb-6rxZxx0 Monty Hall Problem - Numberphile]. (Video).
<br><br>

View file

@ -1,6 +1,5 @@
defmodule MontyHall do
def simulate(n) do
:random.seed(:os.timestamp)
{stay, switch} = simulate(n, 0, 0)
:io.format "Staying wins ~w times (~.3f%)~n", [stay, 100 * stay / n]
:io.format "Switching wins ~w times (~.3f%)~n", [switch, 100 * switch / n]
@ -9,7 +8,7 @@ defmodule MontyHall do
defp simulate(0, stay, switch), do: {stay, switch}
defp simulate(n, stay, switch) do
doors = Enum.shuffle([:goat, :goat, :car])
guess = :random.uniform(3) - 1
guess = :rand.uniform(3) - 1
[choice] = [0,1,2] -- [guess, shown(doors, guess)]
if Enum.at(doors, choice) == :car, do: simulate(n-1, stay, switch+1),
else: simulate(n-1, stay+1, switch)

View file

@ -5,7 +5,7 @@ simulate=:3 :0
scenario=. ((pick@-.,])pick,pick) bind x
stayWin=. =/@}.
switchWin=. pick@(x -. }:) = {:
r=.(stayWin,switchWin)@scenario"0 i.1000
r=.(stayWin,switchWin)@scenario"0 i.y
labels=. ];.2 'limit stay switch '
smoutput labels,.":"0 y,+/r
)

View file

@ -0,0 +1,50 @@
program MontyHall;
uses
sysutils;
const
NumGames = 1000;
{Randomly pick a door(a number between 0 and 2}
function PickDoor(): Integer;
begin
Exit(Trunc(Random * 3));
end;
var
i: Integer;
PrizeDoor: Integer;
ChosenDoor: Integer;
WinsChangingDoors: Integer = 0;
WinsNotChangingDoors: Integer = 0;
begin
Randomize;
for i := 0 to NumGames - 1 do
begin
//randomly picks the prize door
PrizeDoor := PickDoor;
//randomly chooses a door
ChosenDoor := PickDoor;
//if the strategy is not changing doors the only way to win is if the chosen
//door is the one with the prize
if ChosenDoor = PrizeDoor then
Inc(WinsNotChangingDoors);
//if the strategy is changing doors the only way to win is if we choose one
//of the two doors that hasn't the prize, because when we change we change to the prize door.
//The opened door doesn't have a prize
if ChosenDoor <> PrizeDoor then
Inc(WinsChangingDoors);
end;
Writeln('Num of games:' + IntToStr(NumGames));
Writeln('Wins not changing doors:' + IntToStr(WinsNotChangingDoors) + ', ' +
FloatToStr((WinsNotChangingDoors / NumGames) * 100) + '% of total.');
Writeln('Wins changing doors:' + IntToStr(WinsChangingDoors) + ', ' +
FloatToStr((WinsChangingDoors / NumGames) * 100) + '% of total.');
end.

View file

@ -1,14 +1,14 @@
/*REXX program simulates a # of trials of the classic Monty Hall problem*/
parse arg t .; if t=='' then t=1000000 /*Not specified? Then use default*/
wins.=0 /*wins.0=stay; wins.1=switching.*/
/*door values: 0=goat 1=car */
do t /*perform this loop T times. */
door.=0 /*set all doors to zero. */
car=random(1,3); door.car=1 /*TV show hides a car randomly. */
?=random(1,3) ; _=door.? /*contestant picks a random door.*/
wins._=wins._+1 /*bump the type of win strategy. */
end /*DO t*/
say 'switching wins ' format(wins.0/t*100,,1)"% of the time."
say ' staying wins ' format(wins.1/t*100,,1)"% of the time."; say
say 'performed' t "times." /*stick a fork in it, we're done.*
/*REXX program simulates a number of trials of the classic Monty Hall problem. */
parse arg # d . /*obtain the optional args from the CL.*/
if #=='' | #=="," then #=1000000 /*Not specified? Then use 1 million. */
if d=='' | d=="," then d= 3 /* " " " " three doors.*/
wins.=0 /*wins.0 ≡ stay, wins.1 ≡ switching.*/
do #; door. =0 /*initialize all doors to a value of 0.*/
car=random(1, d); door.car=1 /*the TV show hides a car randomly. */
?=random(1, d); _=door.? /*the contestant picks a random door. */
wins._ = wins._ + 1 /*bump the type of win strategy. */
end /*#*/ /* [↑] perform the loop # times. */
/* [↑] door values: 0≡goat 1≡car */
say 'switching wins ' format(wins.0 / # * 100, , 1)"% of the time."
say ' staying wins ' format(wins.1 / # * 100, , 1)"% of the time." ; say
say 'performed ' # " times with " d ' doors.' /*stick a fork in it, we're all done. */