Monty Hall Test Program

For the discussion of math. Duh.

Moderators: gmalivuk, Moderators General, Prelates

User avatar
Kurushimi
Posts: 841
Joined: Thu Oct 02, 2008 12:06 am UTC

Monty Hall Test Program

Postby Kurushimi » Fri Oct 22, 2010 12:30 pm UTC

So, I wrote a program to run through the monty hall simulation, where you try to pick a door to win a car, the announcer opens another door (without the car) and asks you if you want to switch your choice. I believe you should have a 66% chance of winning the car if you switch, but this simulations is making it out to be 50%, which isn't really right.

Here's how the program goes, in TI-BASIC (Except the comments at the side)

Code: Select all

0->dim(L1)
For(K,1,300) // 300 is the number of trials
randInt(1,3)->C // The location of the car
randInt(1,3)->G // The guess of the person
For(I,1,3)
If(I =/= C and I =/= G)
I->O // The door the announcer will open
End
If 1 // Whether or not the person switches his choice.
Then
For(I, 1, 3)
If (I =/= O and I =/= G)
I -> G // Switch to the one that hadn't been open and you didn't already pick.
End
End
(G = C)->L1(K)
End
sum(L1)/dim(L1) // Return of the proportion the guesser correctly guessed.


What am I doing wrong?

User avatar
Yakk
Poster with most posts but no title.
Posts: 11129
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Monty Hall Test Program

Postby Yakk » Fri Oct 22, 2010 1:27 pm UTC

You are failing to indent, and as such your code is nigh unreadable? :)

You are also using single-character variables named O. You know how much that looks like zero?

You are also using single-character variables named I. You know how much that looks like one?

Is this a cleanup of your code that does the same thing?

Code: Select all

0->dim(L1)
For(K,1,300) // 300 is the number of trials
   randInt(1,3)->C // The location of the car
   randInt(1,3)->G // The guess of the person
   For(J,1,3)
      If(J =/= C and J =/= G)
         J->A // The door the announcer will open
   End
   For(J, 1, 3)
      If (J =/= A and J =/= G)
         J -> G // Switch to the one that hadn't been open and you didn't already pick.
   End
   (G = C)->L1(K)
End
sum(L1)/dim(L1) // Return of the proportion the guesser correctly guessed.
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

User avatar
JBJ
Posts: 1263
Joined: Fri Dec 12, 2008 6:20 pm UTC
Location: a point or extent in space

Re: Monty Hall Test Program

Postby JBJ » Fri Oct 22, 2010 1:48 pm UTC

I was looking at this and struggling with the code as well, not being familiar with TI-BASIC.

From a coding perspective, the inner For loops aren't really needed. The host's behavior is set. If you choose a door without a car, he opens the remaining door without a car. If you choose the door with a car, he opens one of the other two doors at random. I don't see a random selection for the host in that situation, so I don't think you're simulating that part correctly. You don't really have to simulate it at all.

We're assuming that the contestant always changes their guess. We also know that the host will always open a door without the car.
So, if the contestant's original guess was not the car, they will win. You only need to check if the contestant's guess was right the first time.

Code: Select all

For(K,1,300)
   randInt(1,3)->C  // Location of car
   randInt(1,3)->G  // Contestant's guess
   If (G =/= C)
       // Contestant wins. Not sure how you're incrementing the win count
So, you sacked the cocky khaki Kicky Sack sock plucker?
The second cocky khaki Kicky Sack sock plucker I've sacked since the sixth sitting sheet slitter got sick.

User avatar
Yakk
Poster with most posts but no title.
Posts: 11129
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Monty Hall Test Program

Postby Yakk » Fri Oct 22, 2010 2:02 pm UTC

I figured out your bug.

Code: Select all

   For(J, 1, 3)
      If (J =/= A and J =/= G)
         J -> G // Switch to the one that hadn't been open and you didn't already pick.

you don't abort the for loop. So you could switch your door twice, once away from your original choice, and once back to your original choice.

An easy way to fix this is to use a new variable for "post-switch guess" as follows:

Code: Select all

    0->dim(L1)
    For(K,1,300) // 300 is the number of trials
       randInt(1,3)->C // The location of the car
       randInt(1,3)->G // The guess of the person
       For(J,1,3)
          If(J =/= C and J =/= G)
             J->A // The door the announcer will open
       End
       For(J, 1, 3)
          If (J =/= A and J =/= G)
             J -> P // Switch to the one that hadn't been open and you didn't already pick.
       End
       (P = C)->L1(K)
    End
    sum(L1)/dim(L1) // Return of the proportion the guesser correctly guessed.

This is still a bit off a real simulation, because the announcer doesn't pick a _random_ door that isn't your pick -- they pick the last door that isn't your pick. But that doesn't change the conclusion, unlike your other bug in your simulation.

Remember, write code for humans to read, because you are a human, and it helps if you can read over your code and find bugs. :)

---

JBJ, if the OP wants to simulate, yours isn't as good as his plan: yours emulates part of the game. This means yours is a function of more logic, and your conclusions are only as valid as your additional logic. Your logic happens to be right, but simulation can be used to increase confidence in pure logic (that it actually applies to a situation).
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

mike-l
Posts: 2758
Joined: Tue Sep 04, 2007 2:16 am UTC

Re: Monty Hall Test Program

Postby mike-l » Fri Oct 22, 2010 2:05 pm UTC

Ninja'd, but I have a nice suggestion at the end!

The problem is with your second for loop. You don't quit the loop once you change your guess, so, for example, when you guess 3 and the iopened door is 2, the if statement is true on I = 1, so you switch your guess to 1, but then it's true again on I = 3 so you switch back.

You can either add whatever TI-BASIC's break statement is, or just replace the entire for loop with 6-G-O -> G, since the sum of the 3 doors is 6, this gives you the door that isn't G or O.

Edit:
Here's your program without any for loops aside from the trials, and added random selection by the host if he has an option (but this doesn't affect anything unless your strategy depends on which door Monty opens)

Code: Select all

0->dim(Results)

For(K,1,300)
   randInt(1,3)->Car
   randInt(1,3)->Guessed
   If Guessed = Car Then \\ Host randomly picks one of the other 2 doors
      randInt(1,2) + Car -> Opened
      If Opened > 3
         Opened - 3 -> Opened
   Else \\ Host must pick remaining door
      6 - Guessed - Car -> Opened
   End
   6 - Opened - Guessed -> Guessed \\ Switch Guessed to non guessed/opened door
   End
   (Guessed = Car)->Results(K)
End

sum(Results)/dim(Results)
addams wrote:This forum has some very well educated people typing away in loops with Sourmilk. He is a lucky Sourmilk.

User avatar
Kurushimi
Posts: 841
Joined: Thu Oct 02, 2008 12:06 am UTC

Re: Monty Hall Test Program

Postby Kurushimi » Fri Oct 22, 2010 2:47 pm UTC

Ah, thank you. Yeah, I guess I should have indented the code. But hey, you know what they say, if code is hard to write, it should also be hard to read. Switching the variables so G is changed twice fixed it.

But yeah, on a calculator I can only have single variable names, and can't really indent my code. And it doesn't have a break statement. I really don't like TI-BASIC. =/

I was only doing this as a demonstration to prove to someone that the Monty Hall conclusion was correct, so I would have a program on hand not have to run to a computer.

@JBJ: The for loops were there to figure out which door didn't have the car and wasn't the one you picked. I know that that this doesn't really require searching through all possible doors, I just figured that takes 2 lines as opposed to the 6 or 7 actually calculating which door to open would. Opening one of the two doors at random wouldn't work since I already set the door the car was in before hand, and he will sometimes open the door with the car. Also, I explained that to my friend. He somehow still denied it. This runs exactly the way the problem is described so he'll have nothing to deny.

User avatar
Velifer
Posts: 1132
Joined: Wed Dec 26, 2007 4:05 pm UTC
Location: 40ºN, 83ºW

Re: Monty Hall Test Program

Postby Velifer » Fri Oct 22, 2010 4:28 pm UTC

Kurushimi wrote:I was only doing this as a demonstration to prove to someone that the Monty Hall conclusion was correct.


Start with, "Probability is a measure of how likely something is to occur..."
Somewhere in the middle, explain the word "paradox."
End with a two-by-four to the back of the head.
If they still don't get it, repeat the last step.

I was home sick watching "Let's Make A Deal" last week. I saw a woman trade a car ($18,000) for a 1/3 chance of winning the Big Prize ($24,000). /facepalm
Then, she didn't switch doors. /clue-by-four
Enjoy that goat.
Time flies like an arrow, fruit flies have nothing to lose but their chains -Marx

achan1058
Posts: 1783
Joined: Sun Nov 30, 2008 9:50 pm UTC

Re: Monty Hall Test Program

Postby achan1058 » Fri Oct 22, 2010 5:15 pm UTC

18k for 1/3 chance of 24k? Is she mad or something?

User avatar
mmmcannibalism
Posts: 2150
Joined: Tue Jun 30, 2009 6:16 am UTC

Re: Monty Hall Test Program

Postby mmmcannibalism » Fri Oct 22, 2010 5:36 pm UTC

achan1058 wrote:18k for 1/3 chance of 24k? Is she mad or something?


Well the other 2/3 aren't 0 so it depends on the other possible cases.
Izawwlgood wrote:I for one would happily live on an island as a fuzzy seal-human.

Oregonaut wrote:Damn fetuses and their terroist plots.

User avatar
Yakk
Poster with most posts but no title.
Posts: 11129
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Monty Hall Test Program

Postby Yakk » Fri Oct 22, 2010 6:31 pm UTC

This comment should be in coding -- but don't use O and I as variables if you have a choice (and they must be capitals). Both of them are bad ideas. And you have 24 other options!
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

voidPtr
Posts: 140
Joined: Sun Apr 26, 2009 6:53 pm UTC

Re: Monty Hall Test Program

Postby voidPtr » Fri Oct 22, 2010 10:12 pm UTC

Kurushimi wrote:But yeah, on a calculator I can only have single variable names, and can't really indent my code. And it doesn't have a break statement. I really don't like TI-BASIC. =/



It can't indent code, uses the really odd notation -> and =/=, doesn't have a break statement, and only allows single-letter variable names (capital letters at that). TI-BASIC must have been written by an engineer. No self-respecting mathematician would create a programming language that bad..would they? :)

achan1058
Posts: 1783
Joined: Sun Nov 30, 2008 9:50 pm UTC

Re: Monty Hall Test Program

Postby achan1058 » Sat Oct 23, 2010 12:14 am UTC

mmmcannibalism wrote:
achan1058 wrote:18k for 1/3 chance of 24k? Is she mad or something?


Well the other 2/3 aren't 0 so it depends on the other possible cases.
I would venture a guess that the other 2/3 isn't anything close to 15k......

mike-l
Posts: 2758
Joined: Tue Sep 04, 2007 2:16 am UTC

Re: Monty Hall Test Program

Postby mike-l » Sat Oct 23, 2010 11:10 am UTC

achan1058 wrote:
mmmcannibalism wrote:
achan1058 wrote:18k for 1/3 chance of 24k? Is she mad or something?


Well the other 2/3 aren't 0 so it depends on the other possible cases.
I would venture a guess that the other 2/3 isn't anything close to 15k......


I would venture a guess that the car isn't equivalent to 18k either. Is it completely liquid? Probably not. Extra taxes on the conversion of the car to $$, probably. Other factors we don't know about? Very likely. So unless she's on the game show in order to win money to buy a car, we probably shouldn't value the car at the estimated retail value.
addams wrote:This forum has some very well educated people typing away in loops with Sourmilk. He is a lucky Sourmilk.

User avatar
Kurushimi
Posts: 841
Joined: Thu Oct 02, 2008 12:06 am UTC

Re: Monty Hall Test Program

Postby Kurushimi » Sat Oct 23, 2010 8:00 pm UTC

voidPtr wrote:
Kurushimi wrote:But yeah, on a calculator I can only have single variable names, and can't really indent my code. And it doesn't have a break statement. I really don't like TI-BASIC. =/



It can't indent code, uses the really odd notation -> and =/=, doesn't have a break statement, and only allows single-letter variable names (capital letters at that). TI-BASIC must have been written by an engineer. No self-respecting mathematician would create a programming language that bad..would they? :)


Well the -> and =/= are actually one character on the calculator. But yeah. It's pretty much a pretty crappy language.

But hey, for goto labels, I get TWO WHOLE CHARACTERS! w00t.
Velifer wrote:Start with, "Probability is a measure of how likely something is to occur..."
Somewhere in the middle, explain the word "paradox."
End with a two-by-four to the back of the head.
If they still don't get it, repeat the last step.


Would a base ball bat covered with salty nails work too?


Return to “Mathematics”

Who is online

Users browsing this forum: No registered users and 8 guests