Documente Academic
Documente Profesional
Documente Cultură
Simulation
The exponential distribution can be simulated in R with rexp(n, lambda) where lambda is the rate parameter.
The mean of exponential distribution is 1/lambda and the standard deviation is also 1/lambda. For this
project, we will set lambda = 0.2 for all of the simulations. We will simulate 1,000 times of 40 exponentials
and investigate the distribution. We will store the simulate data to simdata variable as matrix where each
row is the 40 samples
## Initialize variables
n=40
lambda=0.2
sim=1000
## Simulate data
simdata<-matrix(rexp(n*sim, lambda), sim, n)
head(simdata)
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
[,1]
[,2]
[,3]
[,4]
[,5]
[,6]
[1,] 6.374017 1.293578 6.807734 4.8760658 12.0715314 6.173311
[2,] 2.728914 6.691434 8.567115 4.9139389 0.4717333 11.626117
[3,] 5.627465 16.347664 19.043141 2.0302783 0.7286167 1.273446
[4,] 34.403983 1.780344 3.768525 0.6006413 24.9243717 9.476574
[5,] 18.008220 7.402575 5.253922 3.6546294 2.3218509 2.211779
[6,] 2.535587 4.123374 23.700231 0.5615619 0.3891447 6.396274
[,7]
[,8]
[,9]
[,10]
[,11]
[,12]
[1,] 6.1720331 11.0516545 2.201348 1.0945661 3.1208264 5.38401103
[2,] 2.7707858 4.4809916 9.124007 0.6012095 0.8303089 3.29590280
[3,] 2.1275496 6.0462539 3.505114 0.9817137 8.9204362 1.22502308
[4,] 0.2809769 2.2604836 4.495021 3.0629984 2.1797546 12.41520979
[5,] 7.1493059 1.9848335 12.788744 8.3167803 4.4419968 0.50188772
[6,] 0.5588886 0.3570148 4.783951 3.2055609 11.1635941 0.04747952
[,13]
[,14]
[,15]
[,16]
[,17]
[,18]
[1,] 8.2335945 8.1065753 3.9142549 2.206253 8.6187280 5.112030
[2,] 1.7538093 19.5773227 9.8805489 3.073041 4.6470782 14.844922
[3,] 0.8994896 2.2065014 1.3356708 4.530257 2.2380568 5.468162
[4,] 3.1080904 0.5568631 6.9326036 3.814846 3.6630580 0.536950
[5,] 7.4295333 4.7441684 2.9834501 2.792538 0.6233632 5.969806
[6,] 3.8941004 4.6457556 0.2220027 2.076916 0.8832747 13.972879
[,19]
[,20]
[,21]
[,22]
[,23]
[,24]
1
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
## [1] 4.985551
The simulated means is centered in 4.9855507, which is very close to the theoritical center which is 5. Now
you see that the distribution of sample means is centered around the theoritical center of the distribution.
It is good to show variability of the sample mean distribution, theoritically as n is sufficiently large, the
variance is equal to sigma2/n and the square root of it is which commonly known as the standard error of
the mean. Code below shows how it work.
theorySE<-5/sqrt(40) ## theoritical standard error
actualSE<-sd(mns)
print(c(theorySE, actualSE))
g<-ggplot(data.frame(vs), aes(x=vs))
g+geom_histogram(binwidth=5, color="black", aes(y=..density..))+labs(title="Sample Variance Density of 4
0.04
Density
0.03
0.02
0.01
0.00
0
20
40
60
80
Variance of 40 Selections
Now we show how consistent our estimator s2 or sample variance. We can see clearly below the distribution
of the sample variances and how it is centered around the theoritical variance.
Density
0.4
0.2
0.0
3
Mean of 40 Selections
That red curve is the density function of the standard normal distribution.