Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 160 additions & 0 deletions chapters/13/3/Confidence_Intervals.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,166 @@
" - The original sample is very small, say less than 10 or 15.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### More Repetitions, Same Sample\n",
"A bootstrap calculation involves two different numbers: the size of the original sample (in our example, 1,174 mothers) and the number of repetitions, which is how many times we resample. The advice above about using thousands of repetitions is about the second number.\n",
"\n",
"To see what the number of repetitions does, let's keep the original sample fixed and construct the 95% confidence interval for the proportion of smokers ten times each with 100, 1,000, and 10,000 repetitions. To save time, we resample just the `Maternal Smoker` column, using `np.random.choice` to draw 1,174 times at random with replacement. Even so, the code might take a little while to run: it resamples 111,000 times in all."
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"smoking = births.column('Maternal Smoker')\n",
"\n",
"def bootstrap_proportion(num_repetitions):\n",
" proportions = make_array()\n",
" for i in np.arange(num_repetitions):\n",
" resample = np.random.choice(smoking, len(smoking))\n",
" new_proportion = np.count_nonzero(resample) / len(resample)\n",
" proportions = np.append(proportions, new_proportion)\n",
" return proportions\n",
"\n",
"repetitions = make_array()\n",
"left_ends = make_array()\n",
"right_ends = make_array()\n",
"for num_repetitions in make_array(100, 1000, 10000):\n",
" for i in np.arange(10):\n",
" proportions = bootstrap_proportion(num_repetitions)\n",
" repetitions = np.append(repetitions, num_repetitions)\n",
" left_ends = np.append(left_ends, percentile(2.5, proportions))\n",
" right_ends = np.append(right_ends, percentile(97.5, proportions))\n",
"\n",
"intervals = Table().with_columns(\n",
" 'Repetitions', repetitions,\n",
" 'Left', left_ends,\n",
" 'Right', right_ends,\n",
" 'Width', right_ends - left_ends\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For each number of repetitions, how far apart are the ten left ends, and how far apart are the ten right ends?"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th>Repetitions</th> <th>Left spread</th> <th>Right spread</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>100 </td> <td>0.00851789 </td> <td>0.0127768 </td>\n",
" </tr>\n",
" <tr>\n",
" <td>1000 </td> <td>0.00340716 </td> <td>0.00425894 </td>\n",
" </tr>\n",
" <tr>\n",
" <td>10000 </td> <td>0.000851789</td> <td>0.000851789 </td>\n",
" </tr>\n",
" </tbody>\n",
"</table>"
],
"text/plain": [
"Repetitions | Left spread | Right spread\n",
"100 | 0.00851789 | 0.0127768\n",
"1000 | 0.00340716 | 0.00425894\n",
"10000 | 0.000851789 | 0.000851789"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def spread(values):\n",
" return max(values) - min(values)\n",
"\n",
"intervals.select('Repetitions', 'Left', 'Right').group('Repetitions', spread)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And how wide are the intervals, on average?"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th>Repetitions</th> <th>Width average</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>100 </td> <td>0.0545997 </td>\n",
" </tr>\n",
" <tr>\n",
" <td>1000 </td> <td>0.055707 </td>\n",
" </tr>\n",
" <tr>\n",
" <td>10000 </td> <td>0.0556218 </td>\n",
" </tr>\n",
" </tbody>\n",
"</table>"
],
"text/plain": [
"Repetitions | Width average\n",
"100 | 0.0545997\n",
"1000 | 0.055707\n",
"10000 | 0.0556218"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"intervals.select('Repetitions', 'Width').group('Repetitions', np.average)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With only 100 repetitions, the ten intervals disagree with each other: their left ends are spread out over more than 0.003, and so are their right ends. With 10,000 repetitions, the ends are much closer together. The ten left ends are less than 0.003 apart, and so are the ten right ends. More repetitions give a more stable answer.\n",
"\n",
"But the intervals don't get noticeably narrower. In all three rows, the average width is between 0.05 and 0.06.\n",
"\n",
"The width of a bootstrap interval reflects how much the proportion of smokers varies from one random sample of 1,174 mothers to another. More repetitions give a more precise picture of that variability, but they don't reduce it. Resampling more often does not add mothers to the sample, and it cannot fix a sample that was not drawn at random. To get a narrower interval at the same level of confidence, you have to start with a larger sample.\n",
"\n",
"If you run these cells again, the numbers will change, but the pattern will not."
]
}
],
"metadata": {
Expand Down