In: Statistics and Probability
This is the code for the LLN question iid random variable from
exponential distribution with mean beta. Beta is fixed 1 for
illustration.
# Testing LLN
plot.ybar <- function(n, m = 1e5, beta = 1) {
Ybars <- rep(0,m)
for(i in 1:m) {
Y.sample <- rexp(n,1/beta)
Ybars[i] <- mean(Y.sample)
}
p <- hist(Ybars, xlim=c(0,4), freq=F,main = paste("Distribution
of Ybar when n=",n, sep=""))
}
par(mfrow = c(2,2))
for (n in c(2, 10, 50, 200)) {
plot.ybar(n)
}
Using similar code in R, how can I do the experiment for Bernoulli(0.1) and Uniform(0,4)
Note that the only difference we need to make is in the input argument of the function plot.ybar i.e. instead of "beta", if we have bernoulli distribution, then we must replace "beta" with Bernoulli parameter "p" and similarly for unifrm distribution, we must replace with parameters of uniform distribution i.e. "a" and "b".
Code for Bernoulli(0.1) :
plot.ybar <- function(n, m = 1e5, prob = 0.1) {
Ybars <- rep(0,m)
for(i in 1:m) {
Y.sample <- rbern(n,prob)
Ybars[i] <- mean(Y.sample)
}
p <- hist(Ybars, xlim=c(0,4), freq=F,main = paste("Distribution
of Ybar when n=",n, sep=""))
}
par(mfrow = c(2,2))
for (n in c(2, 10, 50, 200)) {
plot.ybar(n)
}
Code for Uniform(0,4) :
plot.ybar <- function(n, m = 1e5, a = 0, b=4) {
Ybars <- rep(0,m)
for(i in 1:m) {
Y.sample <- runif(n,a,b)
Ybars[i] <- mean(Y.sample)
}
p <- hist(Ybars, xlim=c(0,4), freq=F,main = paste("Distribution
of Ybar when n=",n, sep=""))
}
par(mfrow = c(2,2))
for (n in c(2, 10, 50, 200)) {
plot.ybar(n)
}