Skip to content

Instantly share code, notes, and snippets.

@jeffhussmann
Created January 24, 2013 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffhussmann/4623711 to your computer and use it in GitHub Desktop.
Save jeffhussmann/4623711 to your computer and use it in GitHub Desktop.
{
"metadata": {
"name": "jhussmann_012313_monty"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": "import random\n\nparameters = [(7, 3),\n (7, 4),\n (7, 5),\n (8, 3),\n (8, 4),\n (8, 5),\n ]\n\nnum_trials = 100000\n\nfor N, n in parameters:\n stay_wins = 0\n switch_wins = 0\n\n for t in range(num_trials):\n doors = set(range(N))\n \n prize = random.sample(doors, 1)\n first_pick = random.sample(doors, 1)\n\n openable_doors = doors - set(prize + first_pick)\n opened = random.sample(openable_doors, n)\n\n switchable_doors = doors - set(opened + first_pick)\n switched_pick = random.sample(switchable_doors, 1)\n \n if prize == first_pick:\n stay_wins += 1\n if prize == switched_pick:\n switch_wins += 1\n\n ratio = float(switch_wins) / stay_wins\n theoretical_ratio = float(N - 1) / (N - n - 1) \n print 'N={0}, n={1}\\t {2} {3} {4:.3f} {5:.3f}'.format(N,\n n,\n switch_wins,\n stay_wins,\n ratio,\n theoretical_ratio,\n )",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "N=7, n=3\t 28631 14403 1.988 2.000\nN=7, n=4\t 42844 14208 3.015 3.000"
},
{
"output_type": "stream",
"stream": "stdout",
"text": "\nN=7, n=5\t 86043 13957 6.165 6.000"
},
{
"output_type": "stream",
"stream": "stdout",
"text": "\nN=8, n=3\t 21812 12432 1.755 1.750"
},
{
"output_type": "stream",
"stream": "stdout",
"text": "\nN=8, n=4\t 28901 12493 2.313 2.333"
},
{
"output_type": "stream",
"stream": "stdout",
"text": "\nN=8, n=5\t 43843 12475 3.514 3.500"
},
{
"output_type": "stream",
"stream": "stdout",
"text": "\n"
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": "",
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment