Skip to content

Instantly share code, notes, and snippets.

@amomin
Last active March 15, 2016 03:43
Show Gist options
  • Save amomin/e4f48df415868046439a to your computer and use it in GitHub Desktop.
Save amomin/e4f48df415868046439a to your computer and use it in GitHub Desktop.
Monty Hall
.ipynb_checkpoints/*

Just an example iPython notebook

Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import random as r\n",
"import math\n",
"\n",
"# PARAMETERS\n",
"N = 100000\n",
"\n",
"# Select a random in between 1 and 3\n",
"\n",
"def randDoor():\n",
" return 1 + int(3*r.random())\n",
"\n",
"def randOtherDoor(g, p):\n",
" if (g == p):\n",
" r = randDoor()\n",
" while (r == g):\n",
" r = randDoor()\n",
" return r\n",
" else:\n",
" return 6 - g - p\n",
"\n",
"# Door you select if you switch your guess, given that you guessed\n",
"# door g and the prize is behind door p where the host reveals\n",
"# randOtherDoor(g, p)\n",
"def switch(g, p):\n",
" o = randOtherDoor(g, p)\n",
" return 6 - g - o"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Strategy 1 wins = 33298, Strategy 2 wins = 66702\n"
]
}
],
"source": [
"strategy1wins = 0\n",
"strategy2wins = 0\n",
"for i in range(0,N):\n",
" prize = randDoor()\n",
" guess = randDoor()\n",
" reveal = randOtherDoor(guess, prize)\n",
" otherdoorleft = 6 - reveal - guess\n",
" if (prize == guess):\n",
" strategy1wins += 1\n",
" if (prize == otherdoorleft):\n",
" strategy2wins += 1\n",
"\n",
"print \"Strategy 1 wins = {0:d}, Strategy 2 wins = {1:d}\".format(strategy1wins, strategy2wins)"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[33480, 33291, 33229]\n"
]
}
],
"source": [
"prizes = map((lambda(x) : randDoor()), range(0,N))\n",
"# Print counts of times prize is behind each door\n",
"print map ((lambda(y): len(filter((lambda(x): x == y), prizes))),[1,2,3])"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Frequency strategy is correct = 0.33:\n"
]
}
],
"source": [
"guesses = map((lambda(x) : randDoor()), range(0,N))\n",
"print \"Frequency strategy is correct = {0:.2f}:\".format(float(len(filter((lambda(x) : guesses[x] == prize[x]), range(0,N))))/N)"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Frequency strategy is correct = 0.67:\n"
]
}
],
"source": [
"strategy = map (lambda(x): switch(guess[x], prize[x]), range(0,N))\n",
"print \"Frequency strategy is correct = {0:.2f}:\".format(float(len(filter((lambda(x) : strategy[x] == prize[x]), range(0,N))))/N)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment