본문 바로가기
R

(R) Central limit theorem

by jangpiano 2020. 9. 27.
반응형

<Central Limit Theorem>


<definition of CLT>


I will use a Poisson distribution as the 'any distribution' to show what central limit theorem means using R. 


<rpois(n, lambda)>



Where n is the number of random values to return and lambda is the unique parameter of the Poisson distribution which is the mean and variance of the distribution.


#making 1 random sample from Poisson distribution with lambda 1. 

> rpois(1,1)                         

[1] 0

> rpois(1,1)

[1] 3

> rpois(1,1)

[1] 1

> rpois(1,1)

[1] 2

> rpois(1,1)

[1] 1

> rpois(1,1)

[1] 0


#making 5 random samples from Poisson distribution with lambda 1. 

> rpois(5,1)

[1] 0 1 0 2 2

> rpois(5,1)

[1] 6 2 3 1 1


#making 10 random samples from Poisson distribution with lambda 1. 

> rpois(10,1)

 [1] 0 1 1 1 2 0 2 0 0 1

> rpois(10,1)

 [1] 3 1 4 1 0 1 2 0 1 1

> rpois(10,1)

 [1] 1 2 3 2 2 1 1 2 0 1


#making 20 random samples from Poisson distribution with lambda 1. 

> rpois(20,1)

 [1] 3 1 1 3 1 0 1 1 3 1 1 0 2 3 0 2 0 0 1 0

> rpois(20,1)

 [1] 1 0 0 0 0 1 1 0 1 1 1 1 1 0 2 0 5 2 2 2


#making 100 random samples from Poisson distribution with lambda 1. 

> rpois(100,1)

  [1] 0 1 1 3 0 1 0 2 1 0 2 0 3 1 1 1 2 0 1 1 1 2 0 2 0

 [26] 2 0 3 2 0 1 2 2 1 1 1 2 1 3 1 1 2 1 1 1 0 0 2 0 0

 [51] 1 1 2 3 4 2 0 1 3 1 1 1 2 0 0 0 1 0 0 4 3 0 2 0 1

 [76] 2 3 0 0 2 0 2 1 1 2 2 2 0 0 2 1 1 3 0 1 3 3 0 1 0


<Sample mean of random samples of Poisson distribution>


> n=1

> sam.means<-numeric(10000)

> for (i in 1:10000){

+   ran.sam<-rpois(n,1)

+   sam.means[i]<-mean(ran.sam) 

+   }        



> mean(sam.means)

[1] 0.988

> var(sam.means)

[1] 0.9755536

> table(sam.means)

sam.means

   0    1    2    3    4    5    6    7 

3679 3740 1826  577  143   28    4    3


> hist(sam.means)


> n=5

> sam.means<-numeric(10000)

> for (i in 1:10000){

+   ran.sam<-rpois(n,1)

+   sam.means[i]<-mean(ran.sam) 

+   }        


> mean(sam.means)

[1] 0.99256

> var(sam.means)

[1] 0.1964683


>hist(sam.means)


> n=20

> sam.means<-numeric(10000)

> for (i in 1:10000){

+   ran.sam<-rpois(n,1)

+   sam.means[i]<-mean(ran.sam) 

+   }        


> mean(sam.means)

[1] 0.99826

> var(sam.means)

[1] 0.09941691


> hist(sam.means)



> n=100

> sam.means<-numeric(10000)

> for (i in 1:10000){

+   ran.sam<-rpois(n,1)

+   sam.means[i]<-mean(ran.sam) 

+   }        


> mean(sam.means)

[1] 0.99828

> var(sam.means)

[1] 0.009953777


> hist(sam.means)


> n=500

> sam.means<-numeric(10000)

> for (i in 1:10000){

+   ran.sam<-rpois(n,1)

+   sam.means[i]<-mean(ran.sam) 

+   }        


> mean(sam.means)

[1] 1.000027

> var(sam.means)

[1] 0.002008001


> hist(sam.means)

>hist(rnorm(10000,1,1/500))

 

 

-->Mean of 500 random samples from Poi(1) approximately follows normal distribution with mean 1 and variance 1/500


> n=1000

> sam.means<-numeric(10000)

> for (i in 1:10000){

+   ran.sam<-rpois(n,1)

+   sam.means[i]<-mean(ran.sam) 

+   }        


> mean(sam.means)

[1] 1.000444

> var(sam.means)

[1] 0.0009981923


> hist(sam.means)


hist(rnorm(10000,1,1/1000))

 

 


-->Mean of 1000 random samples from Poi(1) approximately follows normal distribution with mean 1 and variance 1/1000.

반응형