{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true,
"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": [
"## Sums of Simple Random Samples ##"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"When the random variables that are being added are not independent, finding the variance of the sum does involve finding covariances. As before, let $X_1, X_2, \\ldots X_n$ be random variables with sum\n",
"\n",
"$$\n",
"S_n = \\sum_{i=1}^n X_i\n",
"$$\n",
"\n",
"The variance of the sum is\n",
"\n",
"$$\n",
"Var(S_n) ~ = ~ \\sum_{i=1}^n Var(X_i) + \\mathop{\\sum \\sum}_{1 \\le i \\ne j \\le n} Cov(X_i, X_j)\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Before we apply this formula, let's start out by finding a simple covariance."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Indicators ###\n",
"Let $A$ and $B$ be two events. Let $I_A$ be the indicator of $A$ and let $I_B$ be the indicator of $B$. This is going to be one of the rare instances where we use an expected product to find a covariance. That's because we know that products of indicators are themselves indicators.\n",
"\n",
"$$\n",
"Cov(I_A, I_B) = E(I_AI_B) - E(I_A)E(I_B) = P(AB) - P(A)P(B)\n",
"$$\n",
"\n",
"You can see that the covariance is 0 if $A$ and $B$ are independent, consistent with the more general result of the previous section. When $A$ and $B$ are not independent, covariance helps us understand the nature of the dependence. For example, if $Cov(I_A, I_B)$ is positive, then\n",
"\n",
"$$\n",
"P(AB) > P(A)P(B) ~~~ \\implies ~~~ P(A)P(B \\mid A) > P(A)P(B)\n",
"~~~ \\implies ~~~ P(B \\mid A) > P(B)\n",
"$$\n",
"\n",
"That is, given that $A$ has occurred, the chance of $B$ is higher than it is overall. This is called *positive association* or *positive dependence* of $A$ and $B$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Variance of the Hypergeometric ###\n",
"Suppose you have a population of $N$ elements of which $G$ are good. Let $X$ be the number of good elements in simple random sample of $n$ elements drawn from the population. Remember that simple random samples are drawn without replacement.\n",
"\n",
"We know that\n",
"\n",
"$$\n",
"X = \\sum_{j=1}^n I_j\n",
"$$\n",
"\n",
"where $I_j$ is the indicator that draw $j$ yields a good element. \n",
"\n",
"By symmetry, we know that $E(I_j) = \\frac{G}{N}$ for each $j$. That is why\n",
"\n",
"$$\n",
"E(X) ~ = ~ n \\frac{G}{N} ~ = ~ np ~~~~ \\text{where } p = \\frac{G}{N}\n",
"$$\n",
"\n",
"That's the same formula as for the binomial.\n",
"\n",
"We also know that $Var(I_j) = \\frac{G}{N} \\cdot \\frac{B}{N}$ where $B = N-G$ is the number of bad elements in the population.\n",
"\n",
"Also by symmetry, $Cov(I_j, I_k)$ is the same for each pair $j, k$ where $j \\ne k$. The example above tells us how to calculate this common value.\n",
"\n",
"$$\n",
"Cov(I_j, I_k) = \\frac{G}{N} \\cdot \\frac{G-1}{N-1} - \\frac{G}{N} \\cdot \\frac{G}{N}\n",
"$$\n",
"\n",
"Therefore\n",
"\n",
"$$\n",
"\\begin{align*}\n",
"Var(X) &= \\sum_{j=1}^n Var(I_j) + \\mathop{\\sum \\sum}_{1 \\le j \\ne k \\le n} Cov(I_j, I_k) \\\\ \\\\\n",
"&= n \\frac{G}{N} \\cdot \\frac{B}{N} ~ + ~ n(n-1) \\Big{(} \\frac{G}{N} \\cdot \\frac{G-1}{N-1} - \\frac{G}{N} \\cdot \\frac{G}{N} \\Big{)} \\\\ \\\\\n",
"&= n \\frac{G}{N} \\cdot \\frac{B}{N} ~ + ~ n(n-1) \\frac{G}{N} \\Big{(} \\frac{G-1}{N-1} - \\frac{G}{N} \\Big{)} \\\\ \\\\\n",
"&= n \\frac{G}{N} \\cdot \\frac{B}{N} ~ - ~ n(n-1) \\frac{G}{N} \\cdot \\frac{N-G}{N(N-1)} \\\\ \\\\\n",
"&= n \\frac{G}{N} \\cdot \\frac{B}{N} \\cdot \\Big{(} 1 - \\frac{n-1}{N-1} \\Big{)} \\\\ \\\\\n",
"&= n \\frac{G}{N} \\cdot \\frac{B}{N} \\cdot \\Big{(} \\frac{N-n}{N-1} \\Big{)} \\\\ \\\\\n",
"&= npq \\cdot \\frac{N-n}{N-1}\n",
"\\end{align*}\n",
"$$\n",
"\n",
"where $p = \\frac{G}{N}$ and $q = 1-p$. \n",
"\n",
"Notice that the formula is the same as the formula for the variance of the binomial, apart from the factor of $\\frac{N-n}{N-1}$.\n",
"\n",
"We can generalize this result to the case where the population isn't binary."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Variance of a Simple Random Sample Sum ###\n",
"Suppose we have a population of $N$ numbers which need not be only zeros and ones. Suppose the population has mean $\\mu$ and standard deviation $\\sigma$. Draw a simple random sample of size $n$ from the population. For $j$ in the range 1 through $n$, let $X_j$ be the $j$th value drawn.\n",
"\n",
"Let $S_n = X_1 + X_2 + \\cdots + X_n$. Then $E(S_n) = n\\mu$, and \n",
"\n",
"$$\n",
"Var(S_n) ~ = ~ \\sum_{i=1}^n Var(X_i) + \\mathop{\\sum \\sum}_{1 \\le i \\ne j \\le n} Cov(X_i, X_j) ~ = ~ n\\sigma^2 + n(n-1)Cov(X_1, X_2)\n",
"$$\n",
"\n",
"by symmetry.\n",
"\n",
"How can we find $Cov(X_1, X_2)$? It's not a good idea to try and multiply the two variables, as they are dependent and their distributions might be unpleasant. The expected product will be hard to find.\n",
"\n",
"What we can use is the observation that the equation we derived above for $Var(S_n)$ is valid for any sample size. In particular, it is valid in the case when we take a census, that is, when we sample all the elements of the population. In that case $n = N$ and the equation is\n",
"\n",
"$$\n",
"Var(S_N) = N\\sigma^2 + N(N-1)Cov(X_1, X_2)\n",
"$$\n",
"\n",
"Why is helpful? To answer this, think about the variability in $S_N$. We have sampled the entire population without replacement. Therefore $S_N$ is just the total of the entire population. There is no sampling variability in $S_N$, because there is only one possible sample of size $N$.\n",
"\n",
"That means $Var(S_N) = 0$. We can use this to solve for $Cov(X_1, X_2)$.\n",
"\n",
"$$\n",
"0 = N\\sigma^2 + N(N-1)Cov(X_1, X_2) ~~~~~ \\implies ~~~~~\n",
"Cov(X_1, X_2) = -\\frac{\\sigma^2}{N-1}\n",
"$$\n",
"\n",
"Now plug this into the formula for $Var(S_n)$ for any smaller sample size $n$.\n",
"\n",
"$$\n",
"Var(S_n) ~ = ~ n\\sigma^2 - n(n-1)\\frac{\\sigma^2}{N-1} ~ = ~\n",
"n\\sigma^2 \\Big{(} 1 - \\frac{n-1}{N-1} \\Big{)} ~ = ~\n",
"n\\sigma^2 \\frac{N-n}{N-1}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Recall that the variance of the sample sum is $n\\sigma^2$ when the sample is drawn with replacement. When the sample is drawn without replacement, the formula is the same apart from the factor of $\\frac{N-n}{N-1}$. \n",
"\n",
"That is exactly what we saw in the special case of the binary population. In the final section of this chapter we will investigate this relation between sampling with and without replacement."
]
}
],
"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
}