---
title: "SamplingMeans"
subtitle: "and the Central Limit Theorem"
date: "Not yet"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

Last time, we learned about discrete random variables and their distributions. Today, we'll 
take a look at the process of sampling as a random variable itself. From this perspective,
there's a distribution associated with sampling and an understanding of this distribution
allows 

## Sampling as a random variable

The process of computing a statistic based on a random sample can be thought of a random variable in 
the following sense:

 * Suppose we draw a random sample of 100 people from a large population and compute their average height. We get some number. 
 * Suppose we do it again. We'll almost certainly get a different number.
 * Now suppose we do that a bunch of times. We'll get a bunch of different numbers. The histogram of 
 those numbers represents the *sampling distribution* associated with the process.

The amazing thing is that the sampling distribution


```{r}
pick_one = function(x) {
  u = runif(1)
  if(u<1/4) {
    return('a')
  }
  else {
    return('b')
  }
}
barplot(table(sapply(1:12, pick_one)))
```

```{r}
n = 100
prop = function(x) {
  trials = sapply(1:n, pick_one)
  return(length(trials[trials=='a'])/n)
}
hist(sapply(1:100, prop))
```

```{r}
d = rexp(100000)
hist(d, col='gray')
```

```{r}
sample_mean = function(x) {
  return(mean(sample(d,100)))
}
sample_mean(1)
```


```{r}
hist(sapply(1:1000, sample_mean))
```