From d5899865c1fb8f9feebb89a5da06df60175bcacf Mon Sep 17 00:00:00 2001 From: Prawal Pokharel <57414987+prawalpokharel@users.noreply.github.com> Date: Tue, 17 Nov 2026 22:22:57 -0600 Subject: [PATCH] Add an example on bootstrap repetitions to 13.3 Add a short subsection after "Care in Using the Bootstrap Percentile Method". It builds the 95% interval for the proportion of smokers ten times each with 100, 1,000 and 10,000 repetitions, then compares how far apart the ends are and how wide the intervals are. The text ties the width to the 1,174 mothers in the sample. Existing cells and outputs are unchanged. The new code cells have outputs from a fresh run of the notebook. --- chapters/13/3/Confidence_Intervals.ipynb | 160 +++++++++++++++++++++++ 1 file changed, 160 insertions(+) diff --git a/chapters/13/3/Confidence_Intervals.ipynb b/chapters/13/3/Confidence_Intervals.ipynb index 1d53967c7..a664ee3ab 100644 --- a/chapters/13/3/Confidence_Intervals.ipynb +++ b/chapters/13/3/Confidence_Intervals.ipynb @@ -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": [ + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Repetitions Left spread Right spread
100 0.00851789 0.0127768
1000 0.00340716 0.00425894
10000 0.000851789 0.000851789
" + ], + "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": [ + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Repetitions Width average
100 0.0545997
1000 0.055707
10000 0.0556218
" + ], + "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": {