TU Wien:Statistik und Wahrscheinlichkeitstheorie UE (Levajkovic)/Übungen 2023W/HW06.5
Zur Navigation springen
Zur Suche springen
- The CLT - Simulation approach
We illustrate the Central Limit Theorem for a random sample from exp(1)-distribution by applying the following R-code. Explain the commands used and comment on the result.
for(n in c(1,2,3,5,10,25,50,100, 250, 500, 1000, 2000)){ result<-c() mu=1 sigma=1 for (i in 1:5000){ X<-rexp(n, 1/mu) result[i] <- (mean(X)-mu)/sigma*sqrt(n) } hist<- hist(result, breaks=seq(min(result)-1, max(result)+1,by=0.25), plot=FALSE) ylim<-range(hist$density,dnorm(0)) hist(result, breaks=seq(min(result)-1, max(result)+1,by=0.25), prob=TRUE, ylim=ylim, main=paste("n=",n),col="lightblue") x<-seq(-4,4,by=0.1) lines(x,dnorm(x),lty=1,lwd=2, col="lightpink") Sys.sleep(1.5) }
Dieses Beispiel ist als solved markiert. Ist dies falsch oder ungenau? Aktualisiere den Lösungsstatus (Details: Vorlage:Beispiel)
Lösungsvorschlag von Lessi[Bearbeiten | Quelltext bearbeiten]
--Lessi 2024-02-07T13:04:11Z
for(n in c(1,2,3,5,10,25,50,100, 250, 500, 1000, 2000)) { # iterate over different sample sizes
result <- c() # initialize empty result vector
mu = 1 # set expected value of the underlying distribution
sigma = 1 # set the standard deviation of the underlying distribution
# generate 5000 samples for our result
for (i in 1:5000){
X <- rexp(n, 1 / mu) # generate n observations of a exponential distribution
result[i] <- (mean(X) - mu) / sigma * sqrt(n) # approximate and normalize
}
hist <- hist(result, breaks = seq(min(result) - 1, max(result) + 1, by = 0.25), plot = FALSE) # create hist to compute ylim
ylim <- range(hist$density , dnorm(0)) # calculate the range for y values
hist(result, breaks = seq(min(result) - 1, max(result) + 1, by = 0.25), prob = TRUE, ylim = ylim, main = paste("n=", n), col = "lightblue") # draw the density histogram
x <- seq(-4, 4, by = 0.1) # set x values for the plot of normal distribution
lines(x, dnorm(x), lty = 1, lwd = 2, col="lightpink") # plot normal distribution
Sys.sleep(1.5) # sleep so we can see something
}
# the program shows how for increasing sample sizes the sample means approximate a normal distribution