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.
\[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)")
\[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)")
n <- 10^3
sample_1 <- piecewise_1(n)
sample_2 <- piecewise_2(n)
hist(sample_1)
hist(sample_2)
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)
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)