The Monty Hall Problem, or How I Learned to Stop Worrying and Write the Code.

The #1 community for Gun Owners in Indiana

Member Benefits:

  • Fewer Ads!
  • Discuss all aspects of firearm ownership
  • Discuss anti-gun legislation
  • Buy, sell, and trade in the classified section
  • Chat with Local gun shops, ranges, trainers & other businesses
  • Discover free outdoor shooting areas
  • View up to date on firearm-related events
  • Share photos & video with other members
  • ...and so much more!
  • CathyInBlue

    Grandmaster
    Rating - 0%
    0   0   0
    I don't know what got me thinking about this again, but like so many influences, it got into my brain and wouldn't get back out unless exorcised through intense cogitation. We all know it, even if only vaguely. The problem goes something like this...

    You're on the game show Let's Make a Deal, with your host, Monty Hall. You're presented with three doors, labelled 1, 2, and 3. Behind one of the doors is a new car, or bedroom set, or trip to Aruba, or whatever. It's a desirable prize. Behind the other two are goats. Might be of the barnyard variety, or a fake, wrecked car, or some other generic whammy, or no-prize. Your task is to pick one of the doors, the one you think leads to the prize.

    After you've made your selection, with a one in three chance of selecting the car immediately, Monty selects one of the other doors not chosen and reveals the goat behind it. Here's where the fun begins. The question now is whether to take advantage of the opportunity Monty gives you to change your selection to the other door not yet revealed, or to stand pat with the choice you made originally.

    I'd always said that it doesn't matter, because you have information for the second choice you didn't have for the first one, namely that the car was definitely NOT behind the door Monty revealed, the second choice is disconnected from the first one and is a brand new choice with its own probabilities, namely that the car is either A) behind the door you already chose, and you should stand pat, or B) behind the door you haven't chosen, and so you should change your selection. A or B, both with equal probability (1/3 each in the first case, 1/2 each in the second). Since both actions, staying, changing, had equal probability of being the action which yields the win of the car, it doesn't matter which action is chosen. The probabilities don't mitigate for a preference for either choice.

    I was wrong.

    I don't know specificly how I was wrong, why I was wrong, or how to correct myself so that I'm right, but I've come to accept that I was, indeed wrong.

    Reading the Wikipedia page on the Monty Hall Problem, I saw one explanation of the solution that just went with straight forward empiracism. A Monte Carlo simulation of 30 games with completely random choices. Of the times the player won, twice as many wins came as the result of changing as came as a result of staying with the original choice. "No way," I thought. I gotta do this myself.

    So I did.

    Code:
    #!/bin/bash
    
    # Monty Hall Problem
    
    declare -A Door
    declare -a Door_indices
    declare -i num_doors=${num_doors:-3}
    declare -i car pick reveal
    
    echo "Monty Hall Problem with $num_doors doors."
    [[ ${num_doors} -lt 3 ]] && echo "Need at least 3 doors." >&2 && exit 0
    
    # Set all doors to goats.
    for i in $(seq ${num_doors}); do
      Door[${i}]='goat'
    done
    
    # Except the one with the car.
    car=$((RANDOM % ${#Door[@]} + 1))
    Door[${car}]='car'
    
    # Pick a random door.
    pick=$((RANDOM % ${#Door[@]} + 1))
    
    # Reveal a random door, not the one picked, or the one with the car.
    Door_indices=(0 "${!Door[@]}")
    unset Door_indices[${pick}]
    unset Door_indices[${car}]
    Door_indices=("${Door_indices[@]:1}")
    reveal=${Door_indices[$((RANDOM % ${#Door_indices[@]}))]}
    
    # Flip a coin to decide whether to switch.
    switch=$((RANDOM % 2))
    
    echo "Picked door ${pick}."
    echo "Revealed door ${reveal} to have a ${Door[${reveal}]}."
    
    case ${switch} in
      0) echo 'Staying with first door.' ;;
      1)
        # Pick another random door, not the first one picked, or the one revealed.
        Door_indices=(0 "${!Door[@]}")
        unset Door_indices[${pick}]
        unset Door_indices[${reveal}]
        Door_indices=("${Door_indices[@]:1}")
        pick=${Door_indices[$((RANDOM % ${#Door_indices[@]}))]}
    
        echo "Switching to door $pick."
      ;;
    esac
    
    # Report the out doors.
    for i in $(seq ${num_doors}); do
      echo -n "Door ${i} hid a ${Door[${i}]}. "
    done
    echo
    
    # Case summary.
    case ${switch} in
      0) if [ ${Door[${pick}]} == 'car' ]; then
         echo 'Case A: Stayed and won.'; else
         echo 'Case B: Stayed and lost.'; fi ;;
      1) if [ ${Door[${pick}]} == 'car' ]; then
         echo 'Case C: Switched and won.'; else
         echo 'Case D: Switched and lost.'; fi ;;
    esac
    Yeah, my programming language of choice is the Bourne-Again Shell. Big whoop! Wanna fight about it? I even went to the trouble of making it count the doors starting from 1, instead of from the computer-friendly 0, and to make it generic for any number of doors, not just the default of 3.

    The code is straight forward and there are no errors with regard to the decisions that have to be made and how they are permitted to be made. Some examples of the output of the program:

    Code:
    Monty Hall Problem with 3 doors.
    Picked door 2.
    Revealed door 3 to have a goat.
    Staying with first door.
    Door 1 hid a goat. Door 2 hid a car. Door 3 hid a goat. 
    Case A: Stayed and won.
    Code:
    Monty Hall Problem with 3 doors.
    Picked door 2.
    Revealed door 3 to have a goat.
    Staying with first door.
    Door 1 hid a car. Door 2 hid a goat. Door 3 hid a goat. 
    Case B: Stayed and lost.
    Code:
    Monty Hall Problem with 3 doors.
    Picked door 3.
    Revealed door 2 to have a goat.
    Switching to door 1.
    Door 1 hid a car. Door 2 hid a goat. Door 3 hid a goat. 
    Case C: Switched and won.
    Code:
    Monty Hall Problem with 3 doors.
    Picked door 3.
    Revealed door 1 to have a goat.
    Switching to door 2.
    Door 1 hid a goat. Door 2 hid a goat. Door 3 hid a car. 
    Case D: Switched and lost.
    The Monte Carlo simulation from the Wikipedia article only used 30 games. I figured if I'm gonna convince myself of this, I need to do just a few more than that. I did it for a million, carved off just the last summary line, which classifies the nature of the game simulated, and then collate the results and count the number of each class of game that randomly occurred... a million times.

    Code:
    $ for i in $(seq 1000000); do monty | tail -1 ; done | sort | uniq -c
     166885 Case A: Stayed and won.
     333611 Case B: Stayed and lost.
     333329 Case C: Switched and won.
     166175 Case D: Switched and lost.
    I've been so, so wrong. I just don't know how.
     
    Last edited:

    bluewraith

    Master
    Rating - 100%
    4   0   0
    Jun 4, 2011
    2,253
    48
    Akron
    I've heard of it before, but never thought to code anything up myself. Good job! :D

    (also, you can edit titles by going into the advanced editor.)


    Came for the Monty Python, stayed for the Monty Hall.
     

    CathyInBlue

    Grandmaster
    Rating - 0%
    0   0   0
    I've heard of it before, but never thought to code anything up myself. Good job! :D

    (also, you can edit titles by going into the advanced editor.)


    Came for the Monty Python, stayed for the Monty Hall.
    Okay. I changed the title in the advanced editor, but that doesn't change the title in the Break Room listing.

    And if there isn't an academic phenomenon called the "Monty Python Problem", there damn well should be.
     

    hoosierdaddy1976

    I Can't Believe it's not Shooter
    Site Supporter
    Rating - 100%
    18   0   0
    Mar 17, 2011
    6,557
    149
    newton county
    are you simply stating that you wrote a code that confirms what's been proven, or are you looking for an explanation?

    if it's the first, congrats; i never got above getting a sentence to endlessly loop in BASIC, if it's the second, let me know and i'll tell you why it works that way.
     
    Last edited:

    bluewraith

    Master
    Rating - 100%
    4   0   0
    Jun 4, 2011
    2,253
    48
    Akron
    Okay. I changed the title in the advanced editor, but that doesn't change the title in the Break Room listing.

    And if there isn't an academic phenomenon called the "Monty Python Problem", there damn well should be.


    It may have something to do with the thread having replies in it, or a lockout timer, but I'm not for sure. I know I've changed titles before and they have updated on the list.
     

    hoosierdaddy1976

    I Can't Believe it's not Shooter
    Site Supporter
    Rating - 100%
    18   0   0
    Mar 17, 2011
    6,557
    149
    newton county
    the fallacy is in thinking that the revealed door is random, when 2/3 of the time, it is actually driven by your original choice.

    there are three choices: goat A, goat B, and car. monty will always reveal a goat, there wouldn't be much point in revealing the car before offering the switch.

    you have a 1 in 3 chance of originally picking the car, consequently there is a 2 in 3 chance of the car being behind one of the doors you didn't pick. since monty will have to reveal the goat, the probability of the car being behind the door that is left remains 2 in 3.

    if you pick goat A, monty is then forced to reveal goat B, and vice versa. it is only when you pick the car first that monty has a choice of doors to reveal.

    let me know if there are any clarifications needed.

    eta. just thought of a way to put it more simply. 2/3 of the time, you are going to choose a goat. monty will then reveal the other goat, leaving the car as the switch choice. 1/3 of the time you will choose the car. monty will reveal one goat, leaving the other as the switch choice. probability favors switching.
     
    Last edited:

    steve666

    Master
    Rating - 0%
    0   0   0
    Jan 12, 2010
    1,563
    38
    Indianapolis Eastside
    Saw this on the TV show Numb3ers. They did a fair job of presenting a simplified, laymans answer that even I could understand about why it works this way.
    images
     

    HDSilvrStreak

    Sharpshooter
    Rating - 100%
    5   0   0
    Oct 26, 2009
    723
    18
    Fishers
    What I find most interesting is that you had the requisite skills& ability to actually program and prove the hypothesis, but yet still needed to seek an explanation as to why it worked.
     

    SideArmed

    Master
    Rating - 100%
    3   0   0
    Apr 22, 2011
    1,739
    38
    What I find most interesting is that you had the requisite skills& ability to actually program and prove the hypothesis, but yet still needed to seek an explanation as to why it worked.

    And made the slip up of putting Monty Python in the thread title.


    I too am disapoint.

    I was hoping for some witty Python banter.
     

    MikeDVB

    Grandmaster
    Rating - 100%
    7   0   0
    Mar 9, 2012
    8,688
    63
    Morgan County
    99% of my administrator tools are in bash, just haven't ever seen anybody use bash for anything not utilitarian :). When not bash, usually Python or Perl.
     

    jblomenberg16

    Grandmaster
    Rating - 100%
    67   0   0
    Mar 13, 2008
    9,920
    63
    Southern Indiana
    the fallacy is in thinking that the revealed door is random, when 2/3 of the time, it is actually driven by your original choice.

    there are three choices: goat A, goat B, and car. monty will always reveal a goat, there wouldn't be much point in revealing the car before offering the switch.

    you have a 1 in 3 chance of originally picking the car, consequently there is a 2 in 3 chance of the car being behind one of the doors you didn't pick. since monty will have to reveal the goat, the probability of the car being behind the door that is left remains 2 in 3.

    if you pick goat A, monty is then forced to reveal goat B, and vice versa. it is only when you pick the car first that monty has a choice of doors to reveal.

    let me know if there are any clarifications needed.

    eta. just thought of a way to put it more simply. 2/3 of the time, you are going to choose a goat. monty will then reveal the other goat, leaving the car as the switch choice. 1/3 of the time you will choose the car. monty will reveal one goat, leaving the other as the switch choice. probability favors switching.


    Great explanation!
     
    Top Bottom