Last active
September 5, 2017 10:59
-
-
Save MHenderson/6293169 to your computer and use it in GitHub Desktop.
Notes and solutions to exercises from "Introduction to Probability" by Grinstead and Snell.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Grinstead and Snell's Introduction to Probability\n", | |
"\n", | |
"Github repository: https://gist.github.com/MHenderson/6293169\n", | |
"\n", | |
"nbviewer page: http://nbviewer.ipython.org/6293169" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Chapter 1 Discrete Probability Distributions" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Exercises" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"1. Modify the program CoinTosses to toss a coin n times and print out after every 100 tosses the proportion of heads minus 1/2. Do these numbers appear to approach 0 as n increases? Modify the program again to print out, every 100 times, both of the following quantities: the proportion of heads minus 1/2, and the number of heads minus half the number of tosses. Do these numbers appear to approach 0 as n increases?" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import random\n", | |
"random.seed(0)\n", | |
"n = 500\n", | |
"heads = 0\n", | |
"tails = 0\n", | |
"for i in range(n):\n", | |
" rand = random.randint(0,1)\n", | |
" if (rand): heads += 1\n", | |
" else: tails += 1\n", | |
" if (i + 1) % 100 == 0:\n", | |
" print \"P(heads)-.5 =\" , float(heads)/i - .5\n", | |
" print \"heads-(n/2) =\" , heads - (i / 2)\n", | |
" print ''\n", | |
" " | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"P(heads)-.5 = 0.136363636364\n", | |
"heads-(n/2) = 14\n", | |
"\n", | |
"P(heads)-.5 = 0.0376884422111\n", | |
"heads-(n/2) = 8\n", | |
"\n", | |
"P(heads)-.5 = 0.0183946488294\n", | |
"heads-(n/2) = 6\n", | |
"\n", | |
"P(heads)-.5 = 0.0187969924812\n", | |
"heads-(n/2) = 8\n", | |
"\n", | |
"P(heads)-.5 = 0.0190380761523\n", | |
"heads-(n/2) = 10\n", | |
"\n" | |
] | |
} | |
], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"As P(heads) would approach 0.5 with arbitrarily large n, P(heads)-0.5 would approach zero.\n", | |
"\n", | |
"The number of heads minus half the number of tosses would not necessarily approach zero with large n." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"2. XXX\n", | |
"\n", | |
"3. In the early 1600s, Galileo was asked to explain the fact that, although the number of triples of integers from 1 to 6 wuth sum 9 is the same as the number of such triples with sum 10, when three dice are rolled, a 9 seemed to come up less often than a 10 -- supposedly in the experience of gamblers.\n", | |
" \n", | |
" (a) Write a program to simulate the roll of three dice a large number of times and keep track of the proportion of times that the sum is 9 and the proportion of times it is 10.\n", | |
" \n", | |
" (b) Can you conclude from your simulations that the gamblers were correct?" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"(a) Below is a function `roll_three_dice(times)` that simulates rolling three dice `times` number of times. The output is an ordered pair the first element of which is the number of nines rolled and the second element of which is the number of tens rolled." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def roll_three_dice(times):\n", | |
" nines = 0 \n", | |
" tens = 0 \n", | |
" for i in range(times):\n", | |
" one = random.randint(1, 6) \n", | |
" two = random.randint(1, 6) \n", | |
" three = random.randint(1, 6)\n", | |
" if one + two + three == 9:\n", | |
" nines += 1\n", | |
" elif one + two + three == 10:\n", | |
" tens += 1 \n", | |
" else:\n", | |
" continue\n", | |
" return nines, tens\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"In the next cell we simulate rolling three dice 100000 times. The total number of nines rolled and the total number of tens rolled are calculated. From these numbers we calculate the percentage of the total number of rolls that came up nine and the percentage of rolls that came up ten." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"experiment_count = 100000\n", | |
"nines, tens = roll_three_dice(experiment_count)\n", | |
"\n", | |
"print \"Total experiments: \" , experiment_count\n", | |
"\n", | |
"print \"Nines: {} times, or {:.2f} percent.\".format(nines, (float(nines) / experiment_count) * 100)\n", | |
"print \"Tens: {} times, or {:.2f} percent.\".format(tens, (float(tens) / experiment_count) * 100)\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Total experiments: 100000\n", | |
"Nines: 11689 times, or 11.69 percent.\n", | |
"Tens: 12487 times, or 12.49 percent.\n" | |
] | |
} | |
], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"So, it seems from this experiment at least, that rolling a ten is more likely than rolling a nine." | |
] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment