Sta 104 - Simulation Homework #2: Moments and Law of Large Numbers
Sta 104 - Simulation Homework #2: Moments and Law of Large Numbers
2.0844412 -1.3456977
0.4355480 0.1099622
1.4291247
1.5060559
In order to calculate the first central moment we would take the mean of the results of the sample and
then subtract the mean of the distribution therefore to calculate the estimate we would use
> mean(rnorm(10, 1, 1)) - 1
[1] -0.1181707
> mean(rnorm(10, 1, 1)) - 1
[1] -0.3879468
Note that we calculate the estimate twice and since each sample is different for each the results are slightly
different. We can write a function based on this to automate the process of running multiple simulations
and returning the results
> sim_normal = function(nsim, nsamp, mean = 0, sd = 1) {
+
sapply(1:nsim, function(x) mean(rnorm(nsamp, mean, sd)) +
mean)
+ }
> sim_normal(5, 10, 1, 1)
[1] -0.76619306 -0.60979706 -0.27886474 0.61659223 -0.04230209
For a larger number of simulations it makes sense to use summary statistics like mean and variance to
describe the resulting distribution, before doing this we need to save the results in a variable so that we
can calculate the mean and variance of the same results (since every run of sim_normal returns different
results).
1500
1000
0
500
Frequency
2000
2500
Histogram of x
1.0
0.5
0.0
0.5
1.0
1.5
Since this distribution is centered around 0 the mean is not very interesting but variance tells us something
about the spread of the distribution and hence tells us something about how close are results are getting
to zero. In order to explore this we can look at the variance we obtain as we increase the number
samples in each of the simulated iterations and observe how that affects the variance of the simulated
distribution.
Simulation Functions
sim_normal = function(nsim, nsamp, mean = 0, sd = 1) {
sapply(1:nsim, function(x) mean(rnorm(nsamp, mean, sd))-mean )
}
sim_exp = function(nsim, nsamp, lambda) {
sapply(1:nsim, function(x) mean(rexp(nsamp, lambda))-1/lambda )
}
sim_gamma = function(nsim, nsamp, k, theta) {
sapply(1:nsim, function(x) mean(rgamma(nsamp, shape=k, scale=theta))-k*theta )
}
Assignment
On your own answer the following questions:
1. Below is a table of results for simulations with different numbers of iterations. For the missing cells
run the appropriate simulation and enter the observed variance of the simulation distribution that
you observe. For all results use nsim=10000.
Number of samples
Variance
Skewness
10
100
1,000
10,000
N (0, 12 )
N (0, 52 )
N (0, 102 )
N (0, 252 )
Exp(1)
Exp(1/5)
Exp(1/10)
Exp(1/25)
Gamma(1, 2)
Gamma(2, 2)
Gamma(3, 2)
Gamma(4, 2)
2. Create a line plot for the normal, exponential, and gamma results separately where the x-axis is the
number of samples and the y-axis is the observed variance. Each plot should contain a separate line
for each of the parameter settings (4 lines on the normal plot, 4 lines on the exponential plot, and
4 lines on the gamma plot respectively).
3. Describe any patterns that you observe as they relate to the speed of convergence of the different
distributions based on their variance and skewness.