math 3070 1. simulation: central limit theorem. example ... › ~treiberg ›...

7
Math 3070 § 1. Treibergs Simulation: Central Limit Theorem. Name: Example May 16, 2011 R Session: R version 2.10.1 (2009-12-14) Copyright (C) 2009 The R Foundation for Statistical Computing ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type ’license()’ or ’licence()’ for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type ’contributors()’ for more information and ’citation()’ on how to cite R or R packages in publications. Type ’demo()’ for some demos, ’help()’ for on-line help, or ’help.start()’ for an HTML browser interface to help. Type ’q()’ to quit R. [R.app GUI 1.31 (5538) powerpc-apple-darwin8.11.1] [Workspace restored from /Users/andrejstreibergs/.RData] > > ################ SIMULATION TO SEE SAMPLING DISTRIBUTION #################### > > # Simulation is used to get the samplig distribution of the mean. > # This is suggested by problem 5.3[44] of Devore 5th ed. > # We generate 500 random samples of sizes n=5,10,15,20 > # from the Weibull Distribution with alpha=2 and beta=5. > # For information on Weibull Distribution: > help(Weibull) starting httpd help server ... done > > m=500; alpha=2; beta=5 > > # Get MEAN, SD and VAR for this beta distribution. > mu=beta*gamma(1+1/alpha);mu [1] 4.431135 > sigma2=beta^2*(gamma(1+2/alpha))-mu^2;sigma2 [1] 5.365046 > > # Put four graphs on a page. The matrix entries give the order of graphs. > layout(matrix(c(1,3,2,4),ncol=2)) > > # loop through sample sizes. Then loop through 500 random samples > # of size j taken from Weibull(alpha,beta), compute the mean of the sample > # and record the mean in the vector res. For each 500 means, plot histogram and QQ plot. 1

Upload: others

Post on 23-Jun-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Math 3070 1. Simulation: Central Limit Theorem. Example ... › ~treiberg › M3074Simulation.pdf> # Superimpose the bell curve for given mean and sd. > > for (j in c(5,10,20,30)){+

Math 3070 § 1.Treibergs

Simulation: Central Limit Theorem. Name: ExampleMay 16, 2011

R Session:

R version 2.10.1 (2009-12-14)Copyright (C) 2009 The R Foundation for Statistical ComputingISBN 3-900051-07-0

R is free software and comes with ABSOLUTELY NO WARRANTY.You are welcome to redistribute it under certain conditions.Type ’license()’ or ’licence()’ for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.Type ’contributors()’ for more information and’citation()’ on how to cite R or R packages in publications.

Type ’demo()’ for some demos, ’help()’ for on-line help, or’help.start()’ for an HTML browser interface to help.Type ’q()’ to quit R.

[R.app GUI 1.31 (5538) powerpc-apple-darwin8.11.1]

[Workspace restored from /Users/andrejstreibergs/.RData]

>> ################ SIMULATION TO SEE SAMPLING DISTRIBUTION ####################>> # Simulation is used to get the samplig distribution of the mean.> # This is suggested by problem 5.3[44] of Devore 5th ed.> # We generate 500 random samples of sizes n=5,10,15,20> # from the Weibull Distribution with alpha=2 and beta=5.> # For information on Weibull Distribution:> help(Weibull)starting httpd help server ... done>> m=500; alpha=2; beta=5>> # Get MEAN, SD and VAR for this beta distribution.> mu=beta*gamma(1+1/alpha);mu[1] 4.431135> sigma2=beta^2*(gamma(1+2/alpha))-mu^2;sigma2[1] 5.365046>> # Put four graphs on a page. The matrix entries give the order of graphs.> layout(matrix(c(1,3,2,4),ncol=2))>> # loop through sample sizes. Then loop through 500 random samples> # of size j taken from Weibull(alpha,beta), compute the mean of the sample> # and record the mean in the vector res. For each 500 means, plot histogram and QQ plot.

1

Page 2: Math 3070 1. Simulation: Central Limit Theorem. Example ... › ~treiberg › M3074Simulation.pdf> # Superimpose the bell curve for given mean and sd. > > for (j in c(5,10,20,30)){+

> # Superimpose the bell curve for given mean and sd.>> for (j in c(5,10,20,30)){+ res=c()+ for (i in 1:m){+ res[i]=mean(rweibull(j,alpha,beta))+ }+ hist(res,prob=TRUE,main=paste("Weibull(5,2) Samp. Dist. Xbar with n=",j),col="gray")+ curve(dnorm(x,mu,sqrt(sigma2/j)),add=TRUE,col=j/5+1)+ qqnorm(res)+1 qqline(res,col=j/5+1)+ }>

2

Page 3: Math 3070 1. Simulation: Central Limit Theorem. Example ... › ~treiberg › M3074Simulation.pdf> # Superimpose the bell curve for given mean and sd. > > for (j in c(5,10,20,30)){+

3

Page 4: Math 3070 1. Simulation: Central Limit Theorem. Example ... › ~treiberg › M3074Simulation.pdf> # Superimpose the bell curve for given mean and sd. > > for (j in c(5,10,20,30)){+

4

Page 5: Math 3070 1. Simulation: Central Limit Theorem. Example ... › ~treiberg › M3074Simulation.pdf> # Superimpose the bell curve for given mean and sd. > > for (j in c(5,10,20,30)){+

>> ########### SIMULATION OF MEANS FROM LOGNORMAL DISTRIBUTION #################>> # Do prob 5.3[45] of Devore 5th ed.> # Choose samples from lognormal with E(ln X)=3 and V(ln X)=1> # Information about Lognormal Distribution> help(dlnorm)>> # Find the mean and sd of this lognormal dist.>> mul=3; sigmal=1; sigma2l=sigma1l^2>> mu=exp(mul+sigma2l/2);mu[1] 33.11545> sigma2=mu^2*(exp(sigma2l)-1);sigma2[1] 1884.325>> # Loop through sample sizes. Then loop through 500 random samples> # of size j taken from LogNormal(mul,sigmal), compute the mean of the sample> # and record the mean in the vector res. For each 500 means, plot histogram and QQ plot.>> for (j in c(10,20,30,50)){+ res=c()+ for (i in 1:m){+ res[i]=mean(rlnorm(j,mul,sigmal))+ }+ hist(res,prob=TRUE,main=paste("Lognormal(3,1) SampDist. Xbar, n=",j),col=gray(.8))+ curve(dnorm(x,mu,sqrt(sigma2/j)),add=TRUE,col=j/10+1)+ qqnorm(res)+ qqline(res,col=j/10+1)+ }>

5

Page 6: Math 3070 1. Simulation: Central Limit Theorem. Example ... › ~treiberg › M3074Simulation.pdf> # Superimpose the bell curve for given mean and sd. > > for (j in c(5,10,20,30)){+

Lognormal(3,1) SampDist. Xbar, n= 10

res

Density

20 40 60 80 100

0.000

0.010

0.020

0.030

-3 -2 -1 0 1 2 3

2040

6080

Normal Q-Q Plot

Theoretical Quantiles

Sam

ple

Qua

ntile

sLognormal(3,1) SampDist. Xbar, n= 20

res

Density

20 40 60 80

0.00

0.01

0.02

0.03

0.04

-3 -2 -1 0 1 2 3

2040

6080

Normal Q-Q Plot

Theoretical Quantiles

Sam

ple

Qua

ntile

s

6

Page 7: Math 3070 1. Simulation: Central Limit Theorem. Example ... › ~treiberg › M3074Simulation.pdf> # Superimpose the bell curve for given mean and sd. > > for (j in c(5,10,20,30)){+

Lognormal(3,1) SampDist. Xbar, n= 30

res

Density

20 30 40 50 60 70 80

0.00

0.02

0.04

-3 -2 -1 0 1 2 3

2030

4050

6070

80

Normal Q-Q Plot

Theoretical Quantiles

Sam

ple

Qua

ntile

sLognormal(3,1) SampDist. Xbar, n= 50

res

Density

20 30 40 50 60

0.00

0.02

0.04

0.06

-3 -2 -1 0 1 2 3

2030

4050

60Normal Q-Q Plot

Theoretical Quantiles

Sam

ple

Qua

ntile

s

7