Universit´e Joseph Fourier L2/STA230
Lab 3: Law of large numbers and Central Limit Theorem
Objectives: Simulate random variables and illustrate the law of large numbers and the Central Limit Theorem.
Exercise 1
1. Simulate 1000 samples of size 5 with a binomial distribution of parametersn= 10 andp= 0.2.
X5 = matrix(rbinom(5000, 10, 0.2), nrow=1000, ncol=5)
Compute the mean of each sample with the functionrowMeansand call the vector of meansmean5 2. Plot a histogram of the 1000 means. Add the theoretical density of a Gaussian distribution with
mean the mean of the 1000 means, and a variance equal to the variance of the 1000 means.
x5 = seq(min(mean5), max(mean5), 0.01)
lines(x5, dnorm(x5, mean=mean(mean5), sd=sd(mean5)), col=2) 3. Repeat with 1000 samples of size 20. Repeat with 1000 samples of size 50.
Exercise 2
1. Draw a sample of sizeN = 104of the standard normal distribution. Display its first 100 values.
2. Split the graphic window in two. Superpose a histogram and the density function on one sub- window. Superpose the empirical and theoretical CDF on the other subwindow.
3. Foriin 1, ...n, compute the sample mean, plot, add the theoretical expectation to the graphic. Do the same for the variance.
4. Sort the initial sample by increasing order with the functionsort. Compute the image of (1 :N)/N by the quantile function of the normal distribution. Plot the first vector against the second.
S <- sort(X) # increasing order Q <- qnorm((1:N)/(N+1)) # quantiles plot(Q,S,pch=".",col="blue") # qqplot abline(a=0,b=1,col="red") # add theoretical qqnorm(X)