Code covered by the BSD License  

Highlights from
Monty Hall

4.0

4.0 | 2 ratings Rate this file 8 Downloads (last 30 days) File Size: 2.46 KB File ID: #26398
image thumbnail

Monty Hall

by Marcus

 

18 Jan 2010 (Updated 26 Apr 2010)

Simulate n rounds of Monty Hall problem with a variable number of doors.

| Watch this File

File Information
Description

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. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your advantage to switch your choice? -Whitaker

This program demonstrates that yes, your chances of winning are improved if you switch.

MATLAB release MATLAB 7.9 (2009b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
19 Jan 2010 Mark Shore

The most intuitive explanation to this alleged paradox for those weak in mathematics and probability (no one reading this of course) is that the host never opens a door hiding a car.

20 Jan 2010 John D'Errico

This loop does not simulate the Monty Hall problem at all.

    for i = 1:n % Fast sim
        Car = randi(d)-1; % Place Car
        Choose = randi(d)-1; % Choose Door
        %while (Monty==Car || Monty==Choose), Monty=randi(d)-1; end
        % As you can see here, the door that Monty opens has no effect on
        % the outcome.
        if Choose==Car, lose=lose+1; else win=win+1; end % Tally score
        tally=[tally,[win;lose]];
    end

Effectively, it counts the number of times a pair of d sided dice match. This happens 1/d of the time in the long run (1/3 for a three sided die.) But that does not simulate the Monty Hall problem. It merely predicts the number of times that you would have chosen the correct door at random, if both your choice and the door placement are at random, AND you choose to stick with your original choice.

Worse, the statement that the door that Monty opens has no effect on the outcome is simply incorrect, and shows a mis-appreciation of the Monty Hall problem.

So nothing of any value is generated by the code.

There is SOME help provided, but it is mediocre help. I would give the help a rating of about 3.5 stars, if I was rating the help purely on its own. I'll show the top of this function:

function [ ] = montyhall(n,d,detail)
%montyhall Simulates n rounds of the Monty Hall problem and gives win/loss
% d : Number of doors
% detail=0 : Show final win/loss tally and estimated probability
% detail=1 : Shows win/loss chart and estimated probability at end
% detail=2 : Shows results for each round
...

I like that there is an attempt to describe the parameters. I like that there is an H1 line (a descriptive first comment line, with useful keywords in it.) But, suppose that you type

>> help montyhall

in MATLAB? What will you see?

 montyhall Simulates n rounds of the Monty Hall problem and gives win/loss
           d : Number of doors
           detail=0 : Show final win/loss tally and estimated probability
           detail=1 : Shows win/loss chart and estimated probability at end
           detail=2 : Shows results for each round
           
  The Monty Hall problem is a probability puzzle based on the American
  television game show Let's Make a Deal. The name comes from the show's
  host, Monty Hall. The problem is also called the Monty Hall paradox,
  as it is a veridical paradox in that the result appears absurd but is
  demonstrably true.
  http://en.wikipedia.org/wiki/Monty_Hall_problem
  
 
 ------------------------%

As you can see, the result tells you about the parameters, but not necessarily what order they should be in. It does not tell you if the parameters have any default values, or what they are. Are any of these parameters optional?

A good thing is to add a line that shows the actual calling syntax for your function. This lets the user understand how to run your function without needing to edit the code. Forcing them to edit your code just to learn how to use it introduces the possibility of bugs being introduced by accident.

Next, tell the user what they need to know about the parameters. Providing defaults that they never know exist is of no value!

There are no tests to check for erroneous parameters provided.

Next, I like that an outside link was provided to wikipedia. This lets an interested party learn more. I would have been happier had a good explanation of this paradoxical result been provided. While there are internal comments in the code (good!) those comments are confusing and suggests the author seems not yet to truly understand the resolution of this interesting problem.

How about the code itself? This code is better than the first attempt this author submitted. It uses a simple loop to simulate the process. In fact, that simulation could have been done fully vectorized (without loops), although I will not claim that it would have been more understandable code to a novice in that vectorized form.

The biggest issue I have is that the block of code I showed above fails to actually simulate the process under study, and seems not to even understand the issues. As such it forces me to drop my rating of this to about 2.5 stars. Sorry, I've chosen to round it down to 2 stars. (Better than my previous rating of 1 star as I applied to the previous incarnation of this submission, so this is an improvement.)

20 Jan 2010 Marcus

Thanks for the helpful review John D'Errico,
I think you misunderstand when I said "the door that Monty opens has no effect on the outcome."; I simply meant that the line of code was irrelevant as the variable 'Monty' was never used again in the function.

I will update the file again soon with some more detailed comments in the code and a better h1 line.
Thanks, Marcus

22 Jan 2010 Petter

John, the program is correct.

24 Jan 2010 John D'Errico  
Please login to add a comment or rating.
Updates
19 Jan 2010

Optimized coding;
Much faster execution;
Simpler output.

20 Jan 2010

Add more comments and explanation;
Very slight code modifications.

22 Jan 2010

Typo correction.

26 Apr 2010

fixed bug where round by round mode for more than 3 doors only showed monty opening one door when in fact he opens all other doors except two

Tag Activity for this File
Tag Applied By Date/Time
monty Marcus 19 Jan 2010 10:53:51
hall Marcus 19 Jan 2010 10:53:51
monty hall Marcus 19 Jan 2010 10:53:51
game show Marcus 19 Jan 2010 10:53:51
statistics Marcus 19 Jan 2010 10:53:51
probability Marcus 19 Jan 2010 10:53:51
monty hall university of utah 21 Sep 2011 18:31:41
m hall university of utah 21 Sep 2011 18:31:51

Contact us at files@mathworks.com