This week, we’ll empirically verify Central Limit Theorem. We’ll write code to run a small simulation on some distributions and verify that the results match what we expect from Central Limit Theorem.

1. Problem set 1

  1. Write a function that will produce a sample of a random variable that is distributed as follows:

\[f\left( x \right) =\begin{cases} x, & 0\leq x\leq 1 \\ 2-x, & 1 < x\leq 2 \end{cases}\]

That is, when your function is called, it will return a random variable between 0 and 2 that is distributed according to the above PDF. Please note that this is not the same as writing a function and sampling uniformly from it.

piecewise_1 <- function(n) {
  x <- 2 * runif(n)
  y <- ifelse(x < 1, x, 2 - x)
  return(y)
}
piecewise_1(1)
## [1] 0.401568
curve(ifelse(x < 1, x, 2 - x), from=0, to=2, ylab="f(x)")

  1. Write a function that will produce a sample of random variable that is distributed as follows:

\[f\left( x \right) =\begin{cases} 1-x, & 0\leq x\leq 1 \\ x-1, & 1 < x\leq 2 \end{cases}\]

piecewise_2 <- function(n) {
  x <- 2 * runif(n)
  y <- ifelse(x < 1, 1 - x, x - 1)
  return(y)
}
piecewise_2(1)
## [1] 0.4392308
curve(ifelse(x < 1, 1 - x, x - 1), from=0, to=2, ylab="f(x)")

  1. Draw 1000 samples (call your function 1000 times each) from each of the above two distributions and plot the resulting histograms. You should have one histogram for each PDF. See that it matches your understanding of these PDFs.
n <- 10^3
sample_1 <- piecewise_1(n)
sample_2 <- piecewise_2(n)
hist(sample_1)

hist(sample_2)

  1. Write a program that will take a sample set size \(n\) as a parameter and the PDF as the second parameter, and perform 1000 iterations where it samples from the PDF, each time taking n samples and computes the mean of these n samples. It then plots a histogram of these 1000 means that it computes.
sampling_dist <- function(n, f) {
  m <- 10^3
  means <- numeric(m)
  for (i in 1:m) {
    means[i] <- mean(f(n))
  }
  hist(means, main = paste("Histogram of means, n =", n))
}
n <- 10^3
sampling_dist(n, piecewise_1)

sampling_dist(n, piecewise_2)

  1. Verify that as you set \(n\) to something like 10 or 20, each of the two PDFs produce normally distributed mean of samples, empirically verifying the Central Limit Theorem. Please play around with various values of \(n\) and you’ll see that even for reasonably small sample sizes such as 10, the CLT holds.
par(mfrow=c(2,3))
sampling_dist(10, piecewise_1)
sampling_dist(20, piecewise_1)
sampling_dist(30, piecewise_1)
sampling_dist(10, piecewise_2)
sampling_dist(20, piecewise_2)
sampling_dist(30, piecewise_2)