Overview

In this project we will investigate the exponential distribution in R and compare it with the Central Limit Theorem. 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. Set lambda = 0.2 for all of the simulations. You will investigate the distribution of averages of 40 exponentials. Note that you will need to do a thousand simulations. Illustrate via simulation and associated explanatory text the properties of the distribution of the mean of 40 exponentials. You should Show the sample mean and compare it to the theoretical mean of the distribution. Show how variable the sample is (via variance) and compare it to the theoretical variance of the distribution. Show that the distribution is approximately normal.

Reading the data

Please set the working directory as it is in your working environment Generate random exponential data. Run for 1000 simulations and store the mean of the 1000 samples of size 40 each into the variable MeanValue

lambda <- 0.2
set.seed(100)
num.sim <- 1000 
obs <- 40
Mean <- matrix(nrow = obs, ncol = num.sim)
MeanValue <- NULL
row <- 0

for (i in 1:num.sim) { 
num <- rexp(obs, rate = lambda)

for (j in 1:obs)
{
  Mean[j,i] <- num[j]
}
  row <-  row + 1
  MeanValue <-  c(MeanValue,mean(Mean[,row]))   ##Vector Merging##
}
MeanValue
##    [1] 4.137412 6.051703 4.415869 4.404714 3.210413 5.475307 4.405938
##    [8] 6.573635 5.399291 4.913581 5.270833 5.245338 3.489136 5.499480
##   [15] 4.667905 4.029489 4.837020 4.659193 4.804947 4.266194 4.876275
##   [22] 4.992668 5.048847 5.443799 5.316159 4.696366 6.214523 4.491013
##   [29] 4.250917 5.077068 5.490958 4.080904 5.772725 5.245535 4.382754
##   [36] 4.351221 4.615198 4.855618 5.453732 4.470885 4.579284 6.155089
##   [43] 4.719326 4.360390 3.954548 5.729702 5.990261 4.696019 4.825721
##   [50] 4.097824 6.120734 4.456318 5.166855 7.016974 3.976363 4.703121
##   [57] 3.882630 4.419039 6.246239 5.476690 5.301541 4.155758 4.274667
##   [64] 4.210415 4.483140 4.983141 4.695782 5.231677 4.873275 6.151140
##   [71] 5.772805 4.826807 5.475401 5.094310 6.227933 5.094274 4.404543
##   [78] 3.693990 6.510461 4.782247 6.017365 4.889367 6.914490 4.376945
##   [85] 4.608293 5.701879 6.368986 4.547347 4.658561 4.535844 4.393762
##   [92] 6.616689 4.684620 5.855813 5.256976 4.302409 5.664008 5.785165
##   [99] 4.871710 3.752175 6.793792 5.518639 6.211640 4.486272 5.288114
##  [106] 4.829533 5.364045 4.346294 4.496657 3.517903 4.589798 4.909284
##  [113] 4.378043 4.210586 6.306978 4.947929 4.400051 4.464929 5.136735
##  [120] 5.093569 4.758963 6.558873 4.782724 5.088803 4.819699 4.379657
##  [127] 5.257724 4.673349 5.030259 5.226276 5.438042 5.285290 5.736336
##  [134] 4.696898 6.933156 4.788351 5.365116 4.345822 5.311713 5.383678
##  [141] 5.739155 5.927790 5.464787 5.308428 5.683163 4.460062 4.071444
##  [148] 5.448933 4.196715 5.226009 6.010409 4.800209 5.229450 3.724684
##  [155] 3.930874 4.087413 5.661817 3.555018 5.208574 6.014650 5.113961
##  [162] 4.655674 3.971670 5.466243 4.049603 4.507714 4.820720 6.073329
##  [169] 5.660610 5.144422 5.487069 5.842082 5.931487 4.253963 4.442642
##  [176] 6.179989 6.560733 4.288661 6.538269 5.259518 4.031219 5.839082
##  [183] 4.128054 4.037809 5.715027 4.543495 5.053747 5.411416 5.603193
##  [190] 5.514033 4.995377 6.422328 4.754392 5.260557 6.583730 3.779074
##  [197] 4.209312 5.625433 4.788676 4.682410 4.306911 5.030245 5.301620
##  [204] 6.000763 5.933392 5.858737 4.944562 4.779708 5.576011 7.563746
##  [211] 5.307596 4.207391 6.664046 4.718484 4.603852 4.587370 4.914402
##  [218] 5.486353 5.125146 4.994030 5.752345 3.170384 4.586184 4.115345
##  [225] 4.750930 4.674457 4.915225 4.303825 3.801816 4.651990 6.389502
##  [232] 4.018825 6.456962 6.020955 4.801620 5.747472 3.959263 5.030371
##  [239] 5.576363 5.057142 4.663505 5.386421 5.138033 4.395530 4.458705
##  [246] 3.932928 4.568729 5.064355 4.739985 5.314920 4.521556 5.832973
##  [253] 5.272300 4.619878 4.702378 5.746574 4.037650 5.536130 4.399185
##  [260] 6.912590 4.741532 4.588128 4.581290 4.101013 4.893947 4.966189
##  [267] 6.084374 5.309078 5.116588 5.733787 4.174999 3.663623 5.038586
##  [274] 5.738097 5.667134 5.942851 4.755195 5.511772 4.649814 4.886187
##  [281] 6.060364 4.119721 4.136783 5.303805 5.759502 4.598824 6.142093
##  [288] 5.184066 5.875907 4.153662 5.111900 4.740993 4.080351 5.705999
##  [295] 4.184725 4.364535 5.195110 4.340380 5.823393 4.871163 4.744207
##  [302] 4.545416 3.807422 4.642581 5.075388 5.288081 5.422665 5.084391
##  [309] 5.945404 3.638872 4.959259 6.229304 5.822581 5.981069 5.124253
##  [316] 4.769759 5.151241 4.338543 5.972531 5.278440 5.216722 5.297485
##  [323] 4.516420 6.114466 6.467133 4.739778 5.314325 4.962085 6.944442
##  [330] 5.053820 4.551839 4.528476 5.741542 5.620851 5.091541 3.869317
##  [337] 4.440076 5.502143 4.171986 3.084336 5.515481 5.510580 6.956443
##  [344] 4.463415 4.943063 6.123445 5.155424 4.688449 4.006975 4.476492
##  [351] 7.522995 5.671570 4.710401 5.569969 4.955232 4.889692 4.651190
##  [358] 6.196234 4.774756 5.147872 3.991192 4.659840 4.947507 5.102912
##  [365] 4.575386 4.432811 3.600497 5.767371 4.100186 4.703733 4.523133
##  [372] 4.445612 6.124381 5.326484 4.775336 5.820245 4.636335 4.241498
##  [379] 5.775158 3.670064 5.599699 4.506490 4.508991 4.158576 4.378722
##  [386] 4.577614 6.390183 5.474994 4.711528 5.182342 3.922910 4.170333
##  [393] 8.037469 4.904176 5.115950 3.622421 3.609732 3.148386 4.863944
##  [400] 3.663556 4.725090 5.624612 4.590886 5.316827 4.901966 5.757928
##  [407] 3.882548 4.824074 4.994138 4.804835 3.459357 5.966864 4.707022
##  [414] 5.950366 4.158348 4.850753 4.077323 4.763056 4.234498 5.549948
##  [421] 4.698856 3.601333 4.503013 3.942289 5.053736 4.387418 4.170397
##  [428] 5.504732 5.458942 4.983588 5.969809 5.217581 3.872161 5.265631
##  [435] 4.630304 4.712702 5.575896 4.730985 6.106720 3.600335 3.652461
##  [442] 6.448100 3.383473 5.191225 6.137892 5.250603 3.691596 6.679625
##  [449] 4.518806 4.318032 4.400229 4.644270 4.913481 4.237262 3.835371
##  [456] 5.290636 4.578244 4.302242 4.123264 4.679664 4.851709 3.835483
##  [463] 6.876619 5.688986 4.767406 4.559699 4.106647 4.264679 6.504244
##  [470] 5.667873 4.846333 4.846170 3.955386 5.757250 4.556100 6.467268
##  [477] 4.970229 5.277166 5.360070 5.128957 5.935464 5.661427 5.464598
##  [484] 5.949574 4.023883 4.882890 6.183052 5.068453 5.715049 3.774161
##  [491] 6.050154 6.004510 5.378313 4.793745 4.211116 6.719214 6.329596
##  [498] 4.528435 5.306988 5.449574 5.302636 4.137561 5.552366 4.452205
##  [505] 6.465744 4.500258 5.469012 3.991952 4.918781 4.471937 5.316032
##  [512] 4.673254 4.782978 4.400166 5.698049 4.987897 5.031140 5.114239
##  [519] 4.273784 5.624805 5.405518 4.508678 3.559115 6.303089 4.828990
##  [526] 5.177566 4.840617 5.177122 5.802684 5.118800 4.590465 4.853788
##  [533] 4.311707 3.914259 5.057718 4.628589 6.104604 3.472250 4.351538
##  [540] 4.126551 4.541217 3.877075 5.504392 4.612650 5.292131 6.546699
##  [547] 4.723470 6.214756 5.804205 5.223282 3.887100 3.859332 4.081551
##  [554] 4.921117 4.770950 3.305096 5.330104 6.430119 5.395820 4.159748
##  [561] 5.448141 5.127379 4.720533 4.904471 4.336835 6.744483 4.755493
##  [568] 5.489682 4.435847 4.969363 3.132848 4.158491 5.835058 3.752318
##  [575] 5.048376 4.969323 4.242945 5.881981 4.506364 5.514885 4.732460
##  [582] 5.022065 4.588712 5.820358 5.436276 3.659789 7.351785 5.659923
##  [589] 4.888480 3.857125 5.541970 4.954278 5.975009 4.696506 5.539186
##  [596] 3.832109 6.601316 4.235237 4.646224 4.699600 3.835862 4.810910
##  [603] 5.477882 4.844038 4.069142 3.954027 5.119530 4.897552 4.737650
##  [610] 4.076234 5.192550 4.506880 6.746780 5.418166 4.009594 3.951102
##  [617] 4.885596 4.844713 4.326917 5.440850 4.447693 5.217715 5.259804
##  [624] 4.580838 7.129881 6.073129 6.385558 3.826986 5.087706 5.343596
##  [631] 4.242512 4.598482 6.369803 3.170002 4.215469 4.448473 4.536198
##  [638] 5.532813 6.612099 6.766117 6.047693 4.008167 5.852839 5.466126
##  [645] 4.909961 5.664562 5.965318 4.617853 4.882342 5.267688 5.728437
##  [652] 3.971909 4.510533 4.828708 5.028942 5.987234 4.356181 4.829649
##  [659] 4.420487 5.468365 3.727348 4.995002 5.433344 4.787139 6.045757
##  [666] 3.777125 5.302180 4.961203 6.305335 4.410942 5.026239 5.262387
##  [673] 3.590740 5.026425 4.431246 4.407766 3.805958 5.021164 6.468814
##  [680] 4.409767 4.557257 5.206990 3.040121 4.825590 4.610093 5.417259
##  [687] 4.713172 3.886303 3.853445 5.106671 6.488020 5.780669 5.385401
##  [694] 5.603002 5.108325 6.365715 4.870882 5.625669 5.353262 4.390691
##  [701] 4.896813 3.859161 4.611451 6.225516 6.252761 5.075091 5.019889
##  [708] 5.876237 6.489905 4.676661 3.771509 4.081194 5.128169 4.010260
##  [715] 4.663870 5.628013 4.917229 5.257015 3.932698 6.375221 4.822732
##  [722] 4.949796 4.771133 4.545248 3.970203 4.498710 6.444222 4.069727
##  [729] 4.411356 4.942342 4.764956 5.101452 4.755882 5.403712 5.178409
##  [736] 5.864272 5.368784 4.623461 3.754556 6.926689 4.665004 4.369131
##  [743] 5.099303 4.339397 3.164135 5.535492 4.331207 3.044933 5.319126
##  [750] 4.557626 3.662812 3.361456 3.859844 5.027973 4.689432 5.568290
##  [757] 5.439879 4.466532 5.563210 4.064077 5.390568 5.011201 5.165436
##  [764] 5.746757 5.311803 5.367623 4.501975 6.303307 4.632415 5.536085
##  [771] 5.195096 6.173376 3.941823 5.832247 5.114583 4.770888 4.722086
##  [778] 5.562755 4.608571 3.860622 5.980901 4.858051 6.954678 4.993993
##  [785] 3.634339 4.000147 4.366614 5.401177 5.031792 5.680463 5.932832
##  [792] 5.941511 4.881643 4.574999 4.239395 5.165813 4.989257 4.714774
##  [799] 5.493279 5.177125 6.618796 5.387445 5.208867 4.142561 5.546738
##  [806] 3.994644 4.641463 5.686279 5.466468 4.757925 3.732564 5.425528
##  [813] 4.671082 4.700242 5.358449 3.726109 5.551336 4.777235 4.881103
##  [820] 5.098631 3.436997 4.846459 4.549786 4.418160 6.225531 5.751275
##  [827] 4.528871 5.302197 5.096517 4.503079 5.062319 4.757002 4.431199
##  [834] 3.064196 4.810792 4.006186 5.682169 4.808037 4.728698 4.692764
##  [841] 5.378999 5.818021 5.422205 4.772505 5.254974 3.738301 5.513906
##  [848] 4.897802 4.821940 5.069041 5.584097 5.495694 5.570574 4.567019
##  [855] 6.389075 5.330360 4.376850 5.571822 4.766459 4.993392 5.950908
##  [862] 6.955092 6.819730 4.296588 5.157326 5.536047 3.376880 5.712404
##  [869] 5.162419 4.565757 4.882675 5.592565 3.503131 4.732131 5.334890
##  [876] 5.654742 4.577679 5.543405 4.314940 6.436867 5.896510 3.727881
##  [883] 5.799657 3.314832 5.100751 6.256062 4.381012 4.745839 5.155939
##  [890] 5.418881 4.373399 5.469449 5.492808 4.895437 3.677077 5.948753
##  [897] 5.532657 5.955931 5.880345 4.572781 4.732671 5.200527 4.726611
##  [904] 3.759112 4.758862 4.618243 5.978511 4.133269 4.785843 3.912219
##  [911] 6.389524 3.804724 6.555949 4.316951 5.202987 4.262623 3.839813
##  [918] 5.143944 4.731861 4.516755 5.084608 4.849468 6.063842 6.091851
##  [925] 4.897905 4.860660 3.294095 5.167793 5.399279 4.044787 5.252001
##  [932] 5.711396 6.650932 4.816015 7.083522 4.408819 5.057084 6.165431
##  [939] 5.072230 5.045001 6.522287 5.313191 3.147113 5.504754 4.192196
##  [946] 3.208889 5.233285 4.420874 5.937161 5.282299 5.084698 5.299057
##  [953] 4.801604 4.008787 4.031739 4.401014 5.812473 4.470935 4.408152
##  [960] 5.859561 5.591524 4.462039 4.690571 6.139878 5.455571 3.737938
##  [967] 5.614931 6.003804 5.565513 6.087244 5.470017 4.708282 4.619613
##  [974] 3.908706 4.363821 4.796810 5.812001 5.194529 4.723131 3.766896
##  [981] 4.962445 5.701115 4.367799 4.812532 5.509945 6.025234 5.791559
##  [988] 6.413988 4.183605 5.313755 5.968145 5.248682 4.006927 5.620183
##  [995] 6.245736 4.787905 4.614564 5.560678 4.799211 5.800774

Sample Mean and Theoretical Mean of the distribution

## Calculate the sample mean
sampleMean <-  mean(MeanValue)
sampleMean
## [1] 4.999702
## Calculate the theoretical mean
theoryMean <- 1/lambda
theoryMean
## [1] 5

Sample Variance and Theoretical Variance of the distribution

sampleVariance <- var(MeanValue)
theoryVariance <- (1/(lambda^2)/obs)
sampleVariance
## [1] 0.6432442
theoryVariance
## [1] 0.625

Create a comparison table of the sample and theoretical mean and variance

compareTable <- matrix(c(sampleMean,theoryMean,sampleVariance,theoryVariance), 
                       ncol = 2, byrow = TRUE)
colnames(compareTable) <- c("Sample", "Theoretical")
rownames(compareTable) <- c("Mean", "Variance")
compareTable
##             Sample Theoretical
## Mean     4.9997019       5.000
## Variance 0.6432442       0.625
print("SampleMean and theoryMean are relatively very close")
## [1] "SampleMean and theoryMean are relatively very close"
print("This proves the Central Limit Theorem")
## [1] "This proves the Central Limit Theorem"

To show that the distribution is approximately normal

This we can show by plotting histogram and also the normal and the normal plot using qqline()

hist(MeanValue, col = "red", xlab = " Mean of 1000 samples of size 40 each", ylab = "Frequency", breaks = 20,
     main = "Histogram of simulated sample means", axes = theoryMean)
abline(v=theoryMean, lw = 5)

qqnorm(MeanValue)
qqline(MeanValue, col = "red")

round((sampleMean + c(-1,1)*qnorm(.975)*sd(MeanValue)/sqrt(num.sim)),3)
## [1] 4.950 5.049

Conclusion

The central limit theorem holds good for the simulation exercise done for generating 1000 samples of size 40 each. The means of the samples and the theoretical value are comparable and also the variance.