lab assignment # 2: the central limit theorem and ...john/305last0607/lab2sol.pdfstat 305 {...

4
STAT 305 – Solution: Introduction to Statistical Inference Winter 2007 Lab Assignment # 2: The Central Limit Theorem and Simulations in R Question 1. a) X is a discreet random variable and hence the mean for X μ = 6 X i=1 x i p i = 6 X i=1 (i)( 1 6 )=( 1 6 ) 6 X i=1 (i)=3.5 and the variance σ 2 = 6 X i=1 (x i - μ) 2 p i = 1 6 6 X i=1 (i - 3.5) 2 =3.5 By the central limit theorem, for large n ¯ X N (μ, σ 2 /n). Hence, ¯ X N (3.5, 0.117) The standard deviation is sd = 0.117 = 0.34 We can plot this in R > x=seq(2.5,4.5,0.001) >plot(x,dnorm(x,mean=3.5,sd=0.34),type= l ,main= The distribution of the sample mean us The plot is given in the figure 1. b) and c) We need to get samples for ¯ X . One sample from ¯ X means sampling from X 30 times and computing the mean. We choose to get 1000 samples for ¯ X . The code in R is given in the following: >barX=rep(0,1000) >for (i in 1:1000){barX[i]=mean(sample(1:6,30,replace=T))} >mean(barX) >var(barX) >hist(barX,freq=NULL) 1

Upload: others

Post on 28-Jun-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lab Assignment # 2: The Central Limit Theorem and ...john/305Last0607/lab2sol.pdfSTAT 305 { Solution: Introduction to Statistical Inference Winter 2007 Lab Assignment # 2: The Central

STAT 305 – Solution: Introduction to Statistical Inference Winter 2007

Lab Assignment # 2: The Central Limit Theorem and Simulations in R

Question 1.

a)X is a discreet random variable and hence the mean for X

µ =6∑

i=1

xipi =6∑

i=1

(i)(16) = (

16)

6∑

i=1

(i) = 3.5

and the variance

σ2 =6∑

i=1

(xi − µ)2pi =16

6∑

i=1

(i− 3.5)2 = 3.5

By the central limit theorem, for large n X̄ ∼ N(µ, σ2/n). Hence,

X̄ ∼ N(3.5, 0.117)

The standard deviation is sd =√

0.117 = 0.34We can plot this in R

> x=seq(2.5,4.5,0.001)

>plot(x,dnorm(x,mean=3.5,sd=0.34),type='l',main='The distribution of the sample mean using CLT')

The plot is given in the figure 1.

b) and c)We need to get samples for X̄. One sample from X̄ means sampling from X30 times and computing the mean. We choose to get 1000 samples for X̄.The code in R is given in the following:

>barX=rep(0,1000)

>for (i in 1:1000){barX[i]=mean(sample(1:6,30,replace=T))}

>mean(barX)

>var(barX)

>hist(barX,freq=NULL)

1

Page 2: Lab Assignment # 2: The Central Limit Theorem and ...john/305Last0607/lab2sol.pdfSTAT 305 { Solution: Introduction to Statistical Inference Winter 2007 Lab Assignment # 2: The Central

2.0 2.5 3.0 3.5 4.0 4.5 5.0

0.0

0.2

0.4

0.6

0.8

1.0

1.2

x

Den

sity

of n

orm

al, m

ean=

3.5,

sd=

0.34

Figure 1: The distribution of the sample mean using CLT.

Histogram of barX

barX

Den

sity

2.5 3.0 3.5 4.0 4.5

0.0

0.2

0.4

0.6

0.8

1.0

1.2

Figure 2: The distribution of X̄ using CLT and the histogram using simu-lations

2

Page 3: Lab Assignment # 2: The Central Limit Theorem and ...john/305Last0607/lab2sol.pdfSTAT 305 { Solution: Introduction to Statistical Inference Winter 2007 Lab Assignment # 2: The Central

We get mean=3.501 and var=0.103 using R. The plot obtained in (a) andthe histogram obtained in (b) using simulations is given in the figure 2.Question 2.

a)We want to find the probability using different number of iterations. Letschange the number from 1 to 10000. We can change this if necessary af-ter observing the results. We create a vector p to record the computedprobability for each iteration. Hence, p should have a length of 10000. If1 ≤ i ≤ 10000 is the number of iterations, we compute the probability bysampling from the uniform distribution for both X and Y , i times. Then,we count how many times X + Y exceeds 2. The probability will be thenumber of counts divided by i, the number of iterations.

>p=seq(1,10000)

>for (i in 1:10000){X=runif(i,0,1)

Y=runif(i,0,1)

p[i]=sum(X+Y>2)/i

}

>plot(1:10000,p)

b)

>p=seq(1,10000)

>for (i in 1:10000){X=runif(i,0,1)

Y=runif(i,0,1)

p[i]=sum(X+Y-5*X*sqrt(Y)>0)/i

}

>plot(1:10000,p)

> mean(p[9900:10000])

[1] 0.25

Based on the figure, we observe after 9000 iterations, the estimated prob-ability is almost fixed and hence we take the mean of p between 9900 and10000 iterations as our final answer.

3

Page 4: Lab Assignment # 2: The Central Limit Theorem and ...john/305Last0607/lab2sol.pdfSTAT 305 { Solution: Introduction to Statistical Inference Winter 2007 Lab Assignment # 2: The Central

0 2000 4000 6000 8000 10000

−1.

0−

0.5

0.0

0.5

1.0

iterations

prob

abili

ty

Figure 3: The probability computed using different number of iterations forpart a)

0 2000 4000 6000 8000 10000

0.0

0.1

0.2

0.3

0.4

iterations

prob

abili

ty

Figure 4: The probability computed using different number of iterations forpart b)

4