{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"tags": [
"remove_cell"
]
},
"outputs": [],
"source": [
"# HIDDEN\n",
"from datascience import *\n",
"from prob140 import *\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"plt.style.use('fivethirtyeight')\n",
"%matplotlib inline\n",
"from scipy import stats"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Examples ##"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this section we will work with the binomial distribution in a variety of settings. The formula and code are straightforward enough. What you have to keep in mind are the conditions under which the binomial distribution is valid. It is the distribution of:\n",
"\n",
"- the number of successes\n",
"- in a known number of independent trials\n",
"- with the same probability of success on each trial.\n",
"\n",
"So don't use it for the number of aces in a hand of cards dealt without replacement."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Example 1: Random Number Generator ###\n",
"A random number generator draws repeatedly at random with replacement from the 10 digits $\\{0, 1, 2, 3, 4, 5, 6, 7, 8, 9\\}$.\n",
"\n",
"**Question 1.** The generator is run 20 times. Find the chance that 0 appears more than two times.\n",
"\n",
"**Answer 1.**\n",
"- Are we looking at a number of successes? Yes, when 0 is \"success\".\n",
"- Known number of independent trials? Yes, $n = 20$, and the draws are with replacement.\n",
"- Same probability of success on each trial? Yes, $p = 0.1$.\n",
"\n",
"The answer can be written in two equivalent ways:\n",
"\n",
"$$\n",
"\\sum_{k=3}^{20} \\binom{20}{k} 0.1^k 0.9^{20-k}\n",
"= 1 - \\sum_{k=0}^2 \\binom{20}{k} 0.1^k 0.9^{20-k}\n",
"$$\n",
"\n",
"The second form would be quicker if you were going to complete the numerical calculations by hand. But you are not going to be doing that. Rather, you will do:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.3230731948105334"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum(stats.binom.pmf(np.arange(3, 21), 20, 0.1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"or, equivalently,"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.3230731948105343"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1 - sum(stats.binom.pmf(np.arange(3), 20, 0.1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Question 2.** A class has seven GSIs and Tutors. Each of them runs the random number generator 20 times, independently of the others. What is the chance that at most three of them get two 0's?\n",
"\n",
"**Answer 2.** You can't think of this just as one set of $7 \\times 20 = 140$ runs of the generator, because you have to keep track of each of the 7 sets of 20 runs separately.\n",
"\n",
"So now what's a \"trial\"? It's a course staff member: GSI or tutor. There are 7 of them, and their results are independent of each other. \"Success\" means \"gets two 0's in 20 runs\". We want the chance of at most three successes. \n",
"\n",
"We're all set to use the binomial: $n = 7$, $k = 0, 1, 2, 3$, and $p$ is the probability of getting two 0's in 20 runs. Hence"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.8923613061483988"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p = stats.binom.pmf(2, 20, 0.1)\n",
"\n",
"sum(stats.binom.pmf(np.arange(4), 7, p))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As a math formula, this is\n",
"\n",
"$$\n",
"\\sum_{k=0}^3 \\binom{7}{k}p^k(1-p)^{7-k} ~~ \\text{where } \n",
"p = \\binom{20}{2} 0.1^2 0.9^{18}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Example 2: Both Heads and Tails ###\n",
"Toss a coin 10 times. \n",
"\n",
"**Question.** What is the chance that you get at least 3 heads and at least 3 tails?\n",
"\n",
"**Answer.** Here is a great first step in working with any distribution: identify the set of possible values that make your event occur.\n",
"\n",
"Those are 3, 4, 5, 6, and 7 heads, and now the answer is clear:\n",
"\n",
"$$\n",
"\\sum_{k=3}^7 \\binom{10}{k} 0.5^k 0.5^{10-k}\n",
"$$\n",
"\n",
"which is"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.8906250000000009"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum(stats.binom.pmf(np.arange(3, 8), 10, 1/2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Resist the impulse to start off by calculating $P(H \\ge 3)$, the chance of at least three heads, with $H$ denoting the number of heads. It's not very helpful here. To see why, let $T$ be the number of tails in the 10 tosses. Then\n",
"\n",
"$$\n",
"P(H \\ge 3, T \\ge 3) = P(H \\ge 3)P(T \\ge 3 \\mid H \\ge 3)\n",
"$$ \n",
"\n",
"The first factor is easily found, but the second isn't clear at all. It's not $P(T \\ge 3)$ because $H$ and $T$ are far from independent: $T = 10 - H$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Example 3: Conditional Distribution of the Trial that Results in the First Success ###\n",
"I have rolled a die 24 times. \n",
"\n",
"**Question.** Find the conditional distribution of the roll in which I saw \"six\", given that I saw 1 six in the 24 rolls.\n",
"\n",
"**Answer.** Let $R$ be the roll in which the six appeared, and let $S$ be the total number of sixes in the 24 rolls. We want the conditional distribution of $R$ given $S = 1$.\n",
"\n",
"When you are trying to find a distribution, always start with its possible values. I got 1 six, so its position $R$ can be any roll in the range 1 through 24.\n",
"\n",
"For $k = 1, 2, \\ldots 24$,\n",
"\n",
"$$\n",
"\\begin{align*}\n",
"P(R = k \\mid S = 1) &= \\frac{P(R = k, S = 1)}{P(S=1)} \\\\ \\\\\n",
"&= \\frac{P(k-1 \\text{ non-sixes, then six, then } 24-k \\text{ non-sixes})}{P(S = 1)} \\\\ \\\\\n",
"&= \\frac{(5/6)^{k-1} (1/6) (5/6)^{24-k}}\n",
"{\\binom{24}{1}(1/6)^1(5/6)^{23}} \\\\ \\\\\n",
"&= \\frac{1}{24}\n",
"\\end{align*}\n",
"$$\n",
"\n",
"Note the absence of $k$ in the answer. Given that I got 1 six, the position in which that six appeared is uniformly distributed across all the rolls."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 1
}