{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Parameter Estimation and Bootstrap"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the past handful of pages, we've been doing a lot of work around testing if samples fit some existing assumptions we have about their populations. But what if we want to start *making new assumptions* about the population -- i.e. we want to estimate a population parameter using our sample."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The issue with just guessing the statistic"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It seems very convenient to just guess a value for the parameter based on the statistic -- after all, representative samples should exhibit statistics similar to the parameter.\n",
"\n",
"Alas, a cautionary tale awaits you.\n",
"\n",
"You, a famed data magician, are tasked with demonstrating your powers by predicting the proportion of students at your school who prefer apples over oranges. You survey a bunch of students and proclaim that the proportion is $0.7$, because that's what you saw in your sample. Soon, the school performs its annual \"Apples vs Oranges\" census, and it turns out the true proportion is only $0.6$... you were wrong. Your powers have been denounced and now you sit in shambles wondering how you could have prevented this from happening.\n",
"\n",
"Unfortunately, samples are affected by random chance. Our *best guess* for the population parameter *is* an equation involving the statistic, but each time we take a sample that statistic is going to look a bit different, so our best guess estimate is going to be different, too!\n",
"\n",
"Data science is not data magic. Data science is all about recognizing and embracing uncertainty. So when we make predictions we don't just give one number, we give two:\n",
"1. What is our best guess for the parameter\n",
"2. How different could our guess have been"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Our best guess for the parameter"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In your statistics classes you'll learn about 'maximum likelihood estimates'. These are basically just estimates for the population parameter that result in the highest probability of observing your sample statistic. They're usually a pretty good way to come up with a best guess, or {dterm}`point estimate` for a parameter.\n",
"\n",
"A couple common point estimates are:\n",
"- Mean: We estimate that the population mean $\\mu$ is equal to the sample mean $\\bar x$\n",
"- Variance: We estimate that the population variance is equal to $\\frac{1}{n}\\sum(x_i - \\bar x)^2$\n",
"\n",
"When guessing something like the population maximum, the sample maximum is probably *not* a good choice because if our sample doesn't contain the population max then we're going to be totally wrong! We might choose to come up our own formula for guessing the max, like two times the mean $2\\bar x$, or something else.\n",
"\n",
"In all of these cases, our point estimate is going to look different every time our sample statistic looks different, and our sample statistic will look different pretty much every time we take a new sample. If we want to measure how 'good' a point estimate is, we should think about the question \"*how different could my estimate have been if I saw a different sample?*\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## How different could our statistic have been"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When we collect samples from a population distribution, each sample will resemble the population distribution in shape. Due to the randomness of sampling, though, there will be a bit of variability in the sample, and thus in the statistic as well."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### The empirical sampling distribution\n",
"\n",
"We can see this directly by loading in a population distribution, then taking a bunch of samples and plotting the empirical {dterm}`sampling distribution` (distribution of sample statistics). Let's look at the salary dataset again. Because it's a very skewed population, we'll be computing the median salary and overlay this true population proportion on each of our plots. We'll use samples of size 100 for simplicity."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import babypandas as bpd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"population = bpd.read_csv('../../data/salaries.csv', names=['Salary'])\n",
"pop_median = population.get('Salary').median()\n",
"print('Population median:', pop_median)\n",
"population.plot(kind='hist', density=True)\n",
"plt.title('Population distribution')\n",
"plt.xlabel('Salary')\n",
"plt.ylabel('Density')\n",
"\n",
"plt.axvline(pop_median, c='k', linestyle='dashed', label='Population Median')\n",
"plt.legend();"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"trials = 10_000\n",
"\n",
"sample_medians = []\n",
"\n",
"for i in range(trials):\n",
" sample = population.sample(100)\n",
" median = sample.get('Salary').median()\n",
" sample_medians.append(median)\n",
" \n",
"plt.hist(sample_medians, density=True)\n",
"plt.title('Empirical distribution of sample medians, n=100')\n",
"plt.xlabel('Sample median')\n",
"plt.ylabel('Density')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With this distribution of the sample statistic computed it's very easy to see how different any one observed statistic could have been. There are a whole range of values that we could have randomly seen. Relatively often we could randomly find a sample with a statistic in the middle -- pretty close to the true population median of $100,000$. But not all the time.\n",
"\n",
"### A range of likely estimates\n",
"\n",
"If we were to take a random sample and make a point estimate for the population median, it seems like most of the time we might estimate anywhere between $90,000$ and $110,000$ -- a solid *ten thousand dollars* below or above the true median. Very rarely we would get super unlucky with our sample and estimate outside of that range.\n",
"\n",
"We could feel a lot more confident about our estimate if we gave a range of values that might capture the population median, instead of just our point estimate. E.g. if we got a sample median of $95,000$ it might be nice to say: \n",
"\"Based on the sampling distribution I know that I could have gotten values both above and below this, so I'll guess that the population median is *somewhere within* ten thousand dollars of $95,000$.\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The bootstrap"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So, when we set out to estimate a parameter, we'd like to use the distribution of our point estimate in order to come up with a *range* of estimates that probably capture the true population value.\n",
"\n",
"Only issue: we can't go out get all of the brand new samples needed to make an empirical sampling distribution. In the real world we can't just take thousands of samples due to a lack of funding and patience. Instead, we'll usually need to make due with only one sample, and that's all."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sample = population.sample(100)\n",
"sample.plot(kind='hist', density=True)\n",
"plt.title('Sample, n=100')\n",
"plt.xlabel('Salary')\n",
"plt.ylabel('Density')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So, we only have one sample; we don't have access to the population; we want to create a sampling distribution so we can be confident in a range of possible values to estimate. Our question becomes: \n",
"*How can we create a sampling distribution when we don't have a population to sample from?*\n",
"\n",
"True, we don't have access to the population distribution to take new samples from. However, if we could get access to a distribution that looks *similar* to the population... well then maybe we can get away with taking new samples from that similar distribution instead.\n",
"\n",
"As it turns out, we *do* have a distribution that looks similar to the population -- our sample itself!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### The bootstrapped sampling distribution\n",
"\n",
"To generate a sampling distribution from our single sample, simply pretend that our sample *is* the population, and take {dterm}`resamples` from that 'population'.\n",
"\n",
"This process is called {dterm}`bootstrapping`. The term originates from a 19th century saying of \"to pull yourself up by your bootstraps\" -- essentially performing an impossible feat with no external help (like if a single lone sample were able to produce an entire sampling distribution with no external samples).\n",
"\n",
"Just like with our normal sampling distribution process, the sample size should be fixed to a value we're interested in -- in this case **the resample size should be the same size of our original sample**. And finally, it's worth noting that **we must take resamples with replacement** -- can you imagine what would happen if we didn't?\n",
"\n",
"Let's try it out."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# This is our one and only sample\n",
"# s = np.random.randint(10000)\n",
"# print(s)\n",
"#714,7698,4582,3493,7318,759,683,8441\n",
"# np.random.seed(s)\n",
"np.random.seed(8441)\n",
"sample = population.sample(100)\n",
"sample_median = sample.get('Salary').median()\n",
"sample.plot(kind='hist', density=True)\n",
"plt.axvline(sample_median, c='lightblue', linestyle='dashed', label='Sample Median')\n",
"plt.legend()\n",
"print('Sample median:', sample_median)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# This time, all references to `population` have been replaced with `sample`,\n",
"# and all `sample` have been replaced with `resample`.\n",
"#\n",
"# Other than that, this code looks pretty similar to previous sampling distrs!\n",
"\n",
"trials = 10_000\n",
"\n",
"resample_medians = []\n",
"\n",
"for i in range(trials):\n",
" \n",
" # Remember that we need to sample with replacement\n",
" resample = sample.sample(100, replace=True)\n",
" median = resample.get('Salary').median()\n",
" resample_medians.append(median)\n",
" \n",
"plt.hist(resample_medians, density=True)\n",
"plt.title('Distribution of resample medians, n=100')\n",
"plt.xlabel('Bootstrapped median')\n",
"plt.ylabel('Density')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Somehow (through the powers of probabilities and statistics) we managed to squeeze an entire sampling distribution out of a single sample. This 'bootstrapped' sampling distribution or resample medians doesn't look exactly the same as our empirical sampling distribution of sample medians -- but let's look at the *range* that it produces.\n",
"\n",
"Based on this one sample, we conducted bootstrapping and determined that most resample medians fall somewhere between about $98,000$ and $120,000$, and very rarely outside of this range. Knowing that our sample median might not be the exact right answer, we can use this range of possibilities to estimate with a good amount of confidence that the true population median is *captured somewhere within* the interval between $98,000$ and $120,000$.\n",
"\n",
"That way, if it turns out that our point estimate isn't perfect (it usually won't be) we don't need to be ashamed and ridiculed -- we can *plan* on our single-sample estimate being approximate, and simply provide the interval that our estimate covers.\n",
"\n",
"It so happens that our range estimates from this example does successfully capture the true population median of $100,000$, nice!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.hist(resample_medians, density=True)\n",
"plt.title('Distribution of resample medians, n=100')\n",
"plt.xlabel('Bootstrapped median')\n",
"plt.ylabel('Density')\n",
"\n",
"# Let's overlay our point estimate and the true median\n",
"# plt.axvline(sample_median, c='lightblue', label='Sample Estimate')\n",
"plt.axvline(pop_median, c='k', linestyle='dashed', label='Population Median')\n",
"\n",
"# Finally, overlay our eyeballed range of typical bootstrapped estimates\n",
"plt.plot([98_000, 120_000], [0, 0], c='gold', lw=8, label='Typical estimates')\n",
"\n",
"plt.legend(loc='upper right');"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using this general process, we can actually be pretty confident that *most* of times that we pull a random sample and generate a bootstrapped distribution of estimates the range *will* capture the true population parameter -- we'll find out more about this on the next page."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The bootstrap isn't magical"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As a final point, it's worth noting that the bootstrap -- much like us -- does not work by magic.\n",
"\n",
"Before using the bootstrap, it's worth understanding a few key limitations. First of which: the quality of a bootstrap is bounded by the quality of the original sample it comes from.\n",
"\n",
"```{margin}\n",
"This is parallel to a very common saying in machine learning, regarding the quality of training data and the performance of a model: \"garbage in, garbage out\".\n",
"```\n",
"\n",
"In order to produce a useful bootstrapped distribution, our original sample should employ proper sampling techniques and be large enough to resemble the population it comes from. Remember that the reason we allow ourselves to take resamples from the original sample is because we've convinced ourselves that the original sample resembles the population."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## FAQ: Bootstrapping versus Permuting\n",
"\n",
"Every quarter, a common question that arises is: \"when do we resample versus when do we shuffle?\" Both are a way of squeezing information out of samples so this question is understandable. Ultimately, they have different setup and different goals.\n",
"\n",
"- Bootstrapping involves taking **resamples** (sampling the entire size of your data *with* replacement). We do this when we have a **single sample** and want to use it to generate a **sampling distribution** for a statistic (and a confidence interval, as we'll soon see).\n",
"\n",
"- Permuting involves **shuffling** (sampling the entire size of your data *without* replacement) the labels between two groups, so that a data point in one group may get randomly moved over to the other group and vice versa. We do this when we have **two samples** and want to conduct a **hypothesis test for a shared population**."
]
}
],
"metadata": {
"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.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}