Sunday, February 8, 2009

Monty Hall Problem (Python Style)

Okay, now for the python formulation of the Monty Hall Problem.
#!/usr/bin/env python
import random # import python library random
random.seed() # seed random number generator

def letsMakeADeal(nDoors, switch):
"""Simulate Monty Hall Problem of Lets Make a Deal
Inputs:
nDoors - integer number of doors
switch - switch (True) or stay (False)
Return:
Win (True) or loose (False)"""
prizeDoor = random.randint(1,nDoors) # car is behind a random door
playerDoor = random.randint(1,nDoors) # player chooses random door

if playerDoor == prizeDoor: # If player selects prize door and...
if switch: win = False # player switches, then player loses
else: win = True # player stays, then player wins
if playerDoor != prizeDoor: # If player doesn't select prize door and...
if switch: win = True # player switches, then player wins
else: win = False # player stays, then player looses
return win # return if the player wins (True)
# or looses (False)

nTrials = 1000 # set number of trials
nDoors = 3 # set number of doors
swWins = 0 # initial number of switch wins
stWins = 0 # initial number of stay wins

for i in range(nTrials): # run nTrials number of trials
# run simulation assuming that you will switch doors
if letsMakeADeal(nDoors=nDoors, switch=True): # if switch wins
swWins = swWins + 1 # increment the number of switch wins by one
# run simulation assuming that you will not switch doors
if letsMakeADeal(nDoors=nDoors, switch=False): # if stay wins
stWins = stWins + 1 # increment the number of stay wins by one

swWinPcnt = swWins/float(nTrials)*100 # calculate swich win percent
stWinPcnt = stWins/float(nTrials)*100 # calculate stay win percent

print "Switch Wins %%: %0.2f" % (swWinPcnt)
print "Stay Wins %%: %0.2f" % (stWinPcnt)
The output from this prints the win percentage for staying and the win percentage for switching after doing a thousand trials of each. Some sample outputs are:
Switch Wins %: 65.60
Stay Wins %: 34.40
>>>
Switch Wins %: 65.70
Stay Wins %: 35.10
>>>
Switch Wins %: 68.30
Stay Wins %: 35.30
>>>
Your mileage may vary depending on the number of trails you run and the number of doors. Of course, changing the number of doors changes the probability accordingly. If you have 4 doors, there is a 75% chance you will win if you switch and only a 25% chance you will win if you stay. This is reflected by modifying the number of doors (nDoors) in the program and rerunning:
Switch Wins %: 73.70
Stay Wins %: 26.40
>>>
So there you have it folks, a python program that calculates your chances of winning Lets Make a Deal.

Monty Hall problem

And now for the exciting conclusion (a couple days later than expected). The solution to the Monty Hall problem from Lets Make a Deal, for those of you who didn't already read the wikipedia article, is that it is always better to switch. Many people didn't believe this, nor did I for that matter, until simulations were written proving the chances of winning. So, being as I didn't believe the answer after reading about it, and not having access to the simulations which were described, I decided to write my own in python.

I've been thinking of ways to do this so that people will actually be able to use my code and I've decided to list all required libraries and the versions I'm using. Probably all of my code will be done in python 2.5.4 because a lot of the libraries I use are still only available in that version (scipy, numpy, matplotlib...).

But before I post a bunch of code, I'll explain the thought process. When you look at the Monty Hall problem it all boils down to a couple of important points.

1. Did you originally pick the door that the car is behind?
2. Should you switch doors at the end?

The probability of 1. is easy to calculate. The chance that you picked the right door to begin with is 1/(# of doors). Since, if you did not pick the right door originally then when one door is eliminated before asking to stay or switch, that door will have the car, the chances of the car being behind the other door is (# of doors - 1)/(# of doors).

So, If there are 3 doors and you pick door #1, there is a 1/3 chance you picked the door with the car and a 2/3 chance that you did not. When one of the doors is eliminated, there is still a 2/3 chance that the car is behind the door you didn't pick because the car will never be eliminated according to the rules of the game. So, in staying you only have a 1/3 chance of winning and switching gives a 2/3 chance of winning.

Being as I'm a little short on time, I'll post the python code later.

Tuesday, February 3, 2009

Lets Make a Deal

So, I was reading this book: The Drunkard's Walk: How Randomness Rules Our Lives and it started talking about this game show Lets Make a Deal. Now specifically the book was referring to the Monty Hall Problem:
"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 1990)"
Now, this seemingly simple problem does fool the majority of people, even Nobel prize winners, so don't feel too bad, I believed the answer proposed by the book to be wrong too. So I wrote a python script to simulate the game. And believe me, there is a correct answer, it is always better to do one thing or the other: to switch or to stay. Stay tuned tomorrow for the exciting conclusion.

making stuff

Hi, I'm Sparky. As you probably gathered from the title of this blog, I'll hopefully be making stuff and posting the process and results. I'm notoriously bad at actually keeping up with blogs, so we'll see how this goes.

My profile says that I'm a mechanical engineer, though that doesn't really characterize the things in which I'm interested. I've always had a passion for computers and programming though I've never had any formal training in either. For that reason, I ask that you forgive me for any coding errors or obvious electronics mistakes. I'm reasonably fluent in Python, PHP, C++, and Perl. I hope to post any random scripts that I create so people can learn from them and use them as examples; most scripting will probably be in python as its easy to run and modify.

It also looks like I'm going to have to learn Processing so that I can use it with my arduino (pictured below) that I got for christmas. The arduino, for those not in the know, is essentialy an atmega168 microcontroller on a board that enables one to quickly and easily modify and run code to control the outside world. This is my first foray into what is commonly called physical computing, and I'm fairly excited.


My arduino duemilanove (2009 in italian)

I've got a few projects in mind for this little guy, the first of which will probably be a robot. Keep in mind that I've had practically no experience in electronics except for the two courses and one lab I took in undergraduate engineering so if you see any big problems, please leave a comment. In fact, you should probably leave comments just for the heck of it. Now, to only do something worthy of blogging about...