This project report include statistics simulations to compare the Exponential Distribution in R and the Central Limit Theorem. We will use R in order to make some statistics simulations and compare it with the theorical values for some special values like mean, variance and distribution.
The mean is 1/lambda, the variance is 1/lambda^2
The exponential distribution function graph is
The lambda value define the graph
In R we can simulate the Exponential Distribution with the function rexp
rexp(n, rate = l)
Where n is the number of observations and rate is lambda.
According with the instructions, we will set lambda = 0.2 for all of the simulations. We will investigate the distribution of averages of 40 exponentials. We need to do a thousand simulations.
| Variable | Value |
|---|---|
| Lambda | 0.2 |
| Averages to Consider | 40 |
| Simulations to do | 1000 |
Let’s start with the R code:
# Load the neccesary libraries
library(ggplot2)
# Define some variables for the simulations
n_sim <- 1000 # Number of Simulations to consider
l <- 0.2 # Lambda value to consider
n_avg <- 40 # Number of averages to consider
## Init seed
set.seed(234)
# Create a Exponential Simulation Matrix with 1000 rows and 40 columns.
simulation_matrix <- matrix(rexp(n_sim * n_avg, rate=l), n_sim, n_avg)
Let’s view some of the values of the simulation matrix (40 columnsx1000 rows):
simulation_matrix[1,]
## [1] 2.45619998 3.61492234 11.02362982 8.43673612 0.09777677
## [6] 8.71762825 3.58997035 2.48513546 3.71327123 0.25306561
## [11] 5.51036065 9.16424591 16.92411721 11.98223615 15.52198265
## [16] 2.41954524 0.52725459 4.69341341 8.59840023 7.24657806
## [21] 5.40102179 2.92767636 3.08432710 3.93217916 4.53376911
## [26] 17.92643614 0.37593905 4.11949112 0.65260696 20.54910528
## [31] 3.76220331 2.45551409 1.14617219 5.80922481 5.19735092
## [36] 1.93494380 0.82742986 4.06205276 6.24606369 4.36919780
length(simulation_matrix)
## [1] 40000
Let’s plot the values as a histogram to see it they appear to be a exponential distribution:
# Convert to a value type that can be handle by ggplot
data <- as.vector(simulation_matrix)
plot1 <- ggplot(as.data.frame(data),aes(x=data)) +
ggtitle("Histogram of Simulated Mean Values") +
ylab("Frequency") + xlab("simulation_matrix") +
geom_histogram(colour="black",fill = "green")
plot1
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
So looks like a Exponential Distribution
Let’s calculate the average (mean) of each of the 40 columns to create one value per column:
simulation_matrix_means <- rowMeans(simulation_matrix)
Let’s view some of the values of the simulation matrix means (1000 rows):
simulation_matrix_means
## [1] 5.657229 5.667208 5.535180 3.723032 5.507823 3.997248 5.025216
## [8] 6.772826 5.736227 6.101867 3.925913 5.991583 5.692877 4.464324
## [15] 6.218314 5.344944 4.496846 4.641463 5.621743 5.778171 4.774568
## [22] 5.296439 4.545521 5.955859 5.072622 6.111917 5.672830 4.362348
## [29] 5.696384 5.184979 6.954949 4.254574 5.074831 3.653508 5.470250
## [36] 5.477283 6.028316 5.060207 4.769273 4.438712 4.506593 5.555074
## [43] 5.085493 4.898273 5.243702 4.596673 4.757337 4.714927 4.067481
## [50] 6.041334 4.017692 3.600305 5.020864 5.576200 4.172224 4.639192
## [57] 4.165374 4.984198 5.288722 4.866238 3.900930 5.026207 5.304629
## [64] 4.813483 4.848376 6.644694 6.398906 4.816870 4.626675 6.018132
## [71] 6.526366 6.372441 5.722873 5.453974 5.435444 5.581085 4.891760
## [78] 3.954165 4.068885 4.110362 4.050751 3.745325 4.820907 5.481554
## [85] 4.786514 3.785286 4.172319 5.259177 4.931108 6.193033 5.580318
## [92] 6.265765 5.047197 5.369611 4.538135 4.437489 5.328291 4.751345
## [99] 5.119720 4.986406 5.183684 4.449489 5.735666 4.473462 4.570657
## [106] 5.132672 4.292233 4.422100 4.163856 4.520957 3.884134 4.892971
## [113] 4.999554 5.406103 4.847153 5.913420 5.365275 5.598279 3.996213
## [120] 6.510518 5.085817 5.238863 4.648030 5.385128 4.613367 4.946951
## [127] 7.101796 4.488056 5.764962 5.168955 4.919082 4.732419 4.974091
## [134] 5.491355 5.491666 5.670335 4.237189 4.284523 5.038535 4.601082
## [141] 5.308716 5.763834 5.038926 4.515366 3.773871 4.414939 5.463532
## [148] 5.897568 3.579673 5.936057 3.652383 4.571889 5.043118 6.112036
## [155] 4.830297 5.831769 4.207108 5.597837 4.600152 4.345353 4.353784
## [162] 5.492266 5.031352 5.547490 5.874608 5.759879 7.034917 5.997436
## [169] 3.116765 6.470313 4.379775 6.518273 5.051725 4.553679 5.489930
## [176] 4.624021 5.897830 4.207034 4.678753 4.985742 5.634244 3.399068
## [183] 4.466196 4.715588 4.407936 5.194575 5.048603 5.496224 4.824869
## [190] 5.246063 4.096074 4.454762 5.217176 5.227530 4.242678 5.632810
## [197] 4.853045 4.530322 4.077674 5.372627 5.317437 4.364163 5.315568
## [204] 5.379874 5.058883 5.306265 4.535516 5.360015 4.683961 5.957545
## [211] 4.798674 4.899732 5.227714 3.956364 4.155578 4.574000 6.271067
## [218] 5.222636 5.300385 5.222348 5.780980 3.721028 7.041574 4.015044
## [225] 5.828958 6.643729 5.576306 6.174363 5.393914 4.231129 4.187458
## [232] 5.801486 5.663410 4.958645 4.292204 4.528774 4.536556 5.052599
## [239] 6.039896 3.440373 5.645575 5.365358 4.474519 3.697233 6.180042
## [246] 4.374831 4.982766 4.384564 3.553849 5.045185 4.463703 4.812237
## [253] 4.645883 3.854389 3.110589 5.919488 5.176387 4.481678 6.865070
## [260] 3.691860 4.723836 5.489943 4.543688 4.554681 5.514545 5.158628
## [267] 5.043995 6.358056 5.181033 5.546854 4.584191 6.291892 5.795338
## [274] 6.162402 4.818257 5.408374 5.285011 3.437794 4.607408 5.180077
## [281] 4.869119 4.851656 5.888481 4.497797 5.499746 4.747889 7.507255
## [288] 6.010674 6.627673 4.132458 4.082703 5.066277 5.808528 4.441072
## [295] 4.105948 4.810323 5.184359 5.464012 3.865871 4.885569 5.579152
## [302] 5.263758 5.367970 4.056863 6.499728 5.662278 5.916032 5.533500
## [309] 4.424824 5.901637 5.067080 5.096472 4.790402 5.516317 3.374318
## [316] 4.435712 5.008326 5.961079 4.545140 3.830559 5.176579 4.400867
## [323] 5.702187 4.175376 4.836249 4.571430 3.788923 5.122156 4.399506
## [330] 6.067794 4.087028 4.942344 4.355636 5.400629 4.498652 4.005685
## [337] 4.064727 5.265856 3.714069 5.110616 5.302307 5.658797 4.682945
## [344] 6.262581 5.202172 5.537783 3.490489 4.815156 4.986184 4.645968
## [351] 5.221642 5.640017 4.439996 4.709960 4.395242 4.187170 4.404241
## [358] 4.180904 5.996133 6.136664 4.715387 5.007121 5.136018 4.934489
## [365] 5.502594 4.869448 3.057186 4.590696 3.694134 4.051649 5.736702
## [372] 4.657363 5.667376 6.122209 5.086631 3.818234 4.832397 5.671142
## [379] 2.837353 4.801913 5.228842 4.457109 5.569496 5.002267 4.856567
## [386] 4.662204 5.634245 4.002708 4.448820 5.450787 4.387747 5.471958
## [393] 5.862585 7.210882 6.087118 4.581548 5.557583 5.784569 3.635499
## [400] 4.604603 4.908936 5.249968 5.495208 4.167208 5.807428 4.220248
## [407] 3.975487 4.104445 6.995505 3.441662 5.715588 5.315248 4.275493
## [414] 4.779966 4.681577 4.879204 3.946602 4.796489 6.235817 3.726343
## [421] 6.529387 6.172957 5.332558 4.587075 4.802640 4.798772 4.659451
## [428] 3.581689 4.580724 5.844944 4.355213 4.232312 4.298354 5.599817
## [435] 4.228066 5.668752 4.367086 5.033906 4.965919 5.077294 5.321402
## [442] 4.971671 4.604759 7.224203 6.192247 4.621987 4.639613 5.302096
## [449] 5.811740 4.949108 4.927655 4.538477 4.835574 5.110950 4.435997
## [456] 5.747996 4.952370 5.126008 4.553727 5.532754 4.365669 5.778061
## [463] 7.045937 4.355654 5.939615 4.827700 4.805519 5.953030 5.093748
## [470] 5.085298 4.153680 5.337526 4.585350 4.593925 4.744837 4.793229
## [477] 5.215197 6.203237 5.415405 4.700656 4.747468 5.022188 4.969118
## [484] 4.874368 5.783717 4.621528 5.378357 5.306727 5.290435 6.108949
## [491] 4.809527 5.317870 4.381267 6.589963 5.197355 4.398312 3.901753
## [498] 5.137555 4.852428 5.791418 4.101103 4.252939 5.616127 5.743888
## [505] 4.172477 3.728549 5.844843 4.775339 3.330001 5.529063 4.372165
## [512] 4.110973 4.265123 4.667733 5.089105 3.848607 4.286880 6.169653
## [519] 5.487369 4.127392 4.939834 4.846011 6.199867 5.245515 4.434347
## [526] 5.602202 4.269340 5.573300 4.381002 4.275965 4.859769 4.922225
## [533] 4.387022 6.984411 5.211993 5.541535 4.895634 5.229532 5.730451
## [540] 4.897913 5.299341 6.078700 5.516487 4.483574 4.524133 5.240981
## [547] 5.350166 5.750256 4.977798 6.569846 4.887948 4.639395 6.526680
## [554] 5.411254 4.594497 6.307000 5.609683 3.528057 4.131528 4.323739
## [561] 3.951617 4.167432 4.212134 5.427008 4.869216 4.667069 4.611565
## [568] 5.585133 4.892158 4.108474 4.440219 6.227797 4.462306 7.171943
## [575] 3.661265 5.000452 4.912292 5.070089 6.576012 5.888421 3.823892
## [582] 4.509165 4.018670 5.160663 3.797001 5.538678 5.114823 3.789708
## [589] 5.311718 4.922548 3.662018 4.762433 5.912451 3.051574 4.719461
## [596] 5.027346 4.571034 6.059861 3.947030 5.813805 4.641688 5.127923
## [603] 5.366762 5.048111 4.242595 4.258498 4.772924 5.637556 5.789256
## [610] 6.398006 4.849187 3.963450 4.879465 5.581386 4.420142 5.358007
## [617] 6.261630 6.826053 3.889628 5.205306 4.600878 4.164512 5.346675
## [624] 4.487552 4.434836 4.923885 6.456574 4.780126 7.011155 5.349516
## [631] 4.260712 4.317074 4.732863 4.755244 2.980644 3.423257 5.110459
## [638] 5.040728 5.290566 5.510832 5.102641 4.349881 5.034630 4.785623
## [645] 4.591764 4.802596 4.916951 4.498997 4.651264 5.758909 5.852141
## [652] 4.545627 4.104341 5.359245 5.461475 6.415523 5.025704 2.730246
## [659] 4.508799 4.908107 6.051077 4.719872 8.810106 5.964290 4.852031
## [666] 4.719345 4.644045 5.243109 3.859954 5.102659 5.410910 5.561114
## [673] 6.160557 4.706259 5.140198 5.391024 5.095662 4.655422 6.324748
## [680] 4.356315 4.596096 3.784755 6.104727 3.743585 4.669971 4.950939
## [687] 5.184723 4.780890 5.252503 5.233824 4.750754 5.421134 6.274276
## [694] 2.844344 4.362179 5.194267 5.862011 4.572886 3.125099 4.908965
## [701] 4.224022 4.603327 4.672702 5.300370 4.428402 3.589502 4.848372
## [708] 4.623609 4.819974 5.057567 5.587780 3.343040 4.878500 5.126034
## [715] 4.280119 5.074087 4.008689 4.564800 4.332420 4.440719 5.633075
## [722] 4.044987 4.954378 4.977874 5.414873 4.098791 4.954903 6.009770
## [729] 3.833610 3.902259 6.136017 4.280124 4.697204 4.588486 4.912305
## [736] 5.220007 3.480979 6.892331 5.298785 5.401159 5.808891 4.937490
## [743] 4.822982 5.673311 4.632529 5.622645 4.447691 4.105949 5.453909
## [750] 4.209551 4.143296 4.166062 3.892550 5.151194 4.787104 5.020814
## [757] 4.504608 5.536909 4.272105 5.094050 4.433106 5.101944 5.455198
## [764] 5.303464 6.499622 4.524545 5.141982 3.871506 5.640780 5.130780
## [771] 5.624003 4.321435 3.288873 6.547742 4.134965 3.958873 5.283580
## [778] 5.323893 4.441287 4.457808 6.541904 4.126148 5.727788 6.884982
## [785] 5.026062 6.038962 4.555655 4.467402 5.676971 3.946866 4.860538
## [792] 4.743918 5.853691 4.741644 4.931476 4.356493 4.136171 5.171598
## [799] 5.359898 4.995239 4.176345 4.346283 5.897608 4.014198 7.585821
## [806] 4.098702 4.376369 5.324819 5.043478 4.730058 4.449609 4.081654
## [813] 6.244995 4.823448 4.392645 3.043350 5.050936 5.656284 5.290397
## [820] 3.852656 4.207963 5.721704 6.773225 4.419398 6.684964 4.102892
## [827] 4.991818 7.481573 5.124925 5.345429 4.811506 5.302102 5.418276
## [834] 4.917486 5.972699 5.533974 4.732585 5.764865 4.794351 4.192792
## [841] 6.111864 4.282540 4.288545 5.417300 4.872943 5.213232 5.012703
## [848] 5.357568 5.280155 3.778913 4.699874 4.236132 5.754675 3.980688
## [855] 5.740291 5.909404 4.876028 4.962087 7.074606 5.253323 5.463520
## [862] 5.758937 5.334251 3.438798 5.264731 4.657915 4.786180 4.814604
## [869] 5.750968 5.187228 3.537604 6.120780 5.328550 5.644934 5.738693
## [876] 4.066523 4.399987 5.932913 4.576525 7.200460 5.572654 4.167909
## [883] 5.695194 4.474502 4.082900 4.281248 4.454742 4.586157 7.279303
## [890] 4.904039 4.507637 3.892563 5.528780 4.596337 4.805096 5.530045
## [897] 5.628533 6.882829 7.440473 3.948727 5.339012 6.086998 5.240206
## [904] 5.034781 7.041992 4.888243 4.741706 3.823221 5.559904 4.459373
## [911] 4.437592 4.937680 3.534118 4.373609 4.326801 5.011248 4.792838
## [918] 3.199459 5.256780 6.036802 5.286162 4.764143 4.092810 3.488997
## [925] 5.048214 5.563407 4.064681 4.603701 5.901623 4.347785 5.241274
## [932] 5.680012 4.862987 4.778856 6.074579 5.970822 3.937140 4.439983
## [939] 5.380194 3.537105 5.852415 4.110903 4.269160 5.753556 6.289734
## [946] 5.646870 5.558998 5.356336 4.916949 5.616044 4.669143 3.771030
## [953] 5.127686 6.017578 4.907918 7.933410 5.120833 4.902831 4.819822
## [960] 5.216496 3.108913 4.258256 5.359805 6.239450 4.592093 5.367908
## [967] 5.120041 4.274854 4.322175 6.118547 5.321729 5.231065 5.564170
## [974] 5.079837 4.459947 4.300510 3.629886 4.492514 5.610156 6.182711
## [981] 6.276080 5.010341 4.619481 4.685475 6.216355 4.844357 5.245384
## [988] 3.502574 6.796230 5.181541 4.922663 6.611966 4.686573 4.302888
## [995] 5.053450 5.576451 4.413211 4.444469 4.429226 4.850864
length(simulation_matrix_means)
## [1] 1000
Let’s plot as a Histogram the simulation_matrix_means to see how much should be the mean
plot2 <- ggplot(data.frame(simulation_matrix_means),aes(x=simulation_matrix_means)) +
ggtitle("Histogram of Simulated Mean Values of 40 Rows") +
ylab("Frequency") +
geom_histogram(colour="black",fill = "green")
plot2
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
We can see in the graph that the mean is located aproximately in 5.
Let’s calculate the mean of the data:
mean(simulation_matrix_means)
## [1] 5.001573
According the the theory, the mean should be 1/lambda (1/0,2), let’s calculate it:
1/l
## [1] 5
We can see that almost those values match:
| Variable | Value |
|---|---|
| Mean | 5.0015729 |
| Theorical Mean (1/lambda) | 5 |
Let’s calculate the variance of the data:
var(simulation_matrix_means)
## [1] 0.6631504
According the the theory, the variance should be (1/lambda)^2/n (1/0,2)^2/40, let’s calculate it:
(1/l)^2/n_avg
## [1] 0.625
We can see that almost those values match:
| Variable | Value |
|---|---|
| Variance | 0.6631504 |
| Theorical Variance (1/lambda)^2/n | 0.625 |
Let’s plot the data and also the distribution
plot3 <- ggplot(data.frame(simulation_matrix_means),aes(x=simulation_matrix_means)) +
ggtitle("Histogram for Simulated Mean Values of 40 Rows") +
ylab("Frequency") +
geom_histogram(aes(y=..density..),colour="black",fill = "green") +
geom_density(colour="blue",size=1.5)
plot3
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
Is possible to see in the graph that the distribution could aproximately with a Normal distribution.