Statistical simulation _ simulation-Central limit theorem

Source: Internet
Author: User

Statistical simulation of random number distributions
1. What is the central limit theorem.

2. Two distribution simulation Center limit theorem (also known as the Laplace theorem)

#首先生成二项分布随机数
m=100;n=10;p=0.25
z <-rbinom (m,n,p)
 #对100个二项分布随机数进行标准化
x <-(z-n*p)/sqrt (n*p* ( 1-p))
hist (X,prob=t,main=paste ("n=", N))
 #添加正态曲线
curve (Dnorm (x), add=t)
Using functions to simulate
sim.clt <-function (m=100,n=10,p=0.25) {
  z <-rbinom (m,n,p)
  #对100个二项分布随机数进行标准化
  x <-(z-n*p)/sqrt (n*p* (1-p))
  hist (X,prob=t,breaks=20,main=paste ("n=", N))
  #添加正态曲线
  curve (dnorm (x) , add=t)
}
sim.clt (1000,30)

Further understanding of 3.QQ graphs: normal probability graphs
Histogram to judge whether random number obeys normal distribution, is to add normal density function curve in the graph to compare the judgment
The idea of a normal probability graph is a scatter-plot of the number of samples and the theoretical points, to see if the image is approximate to a straight line.
Normal probability graph function: Qqnorm () Qqline () to add a guide
Practice:
The random numbers of 100 normal normal distributions are generated respectively, the mean value is 10, and the standard deviation is 5, and the mean value is 10.
The exponential distribution of [0,1] on the uniform distribution. And then do their normal probability graphs separately.
Par (Mfrow=c (2,2))
x <-rnorm (MB); Qqnorm (x,main= "N (0,1)"); Qqline (x)
X <-rnorm (100,10,5); Qqnorm (x,main= "N (10,5)"); Qqline (x)
X <-rexp (100,1/10); Qqnorm (x,main= "exp (1/10)"); Qqline (x)
x <-runif (MB); Qqnorm (x,main= "unif (0,1)"); Qqline (x)
The results show that the scatter of the first two graphs is approximately distributed on the reference line, so it obeys the normal distribution
The scatter of the latter two graphs is seriously deviated from the reference line, it can be thought that these two graphs do not obey normal distribution

4. Simulate functions to generate multiple types of random numbers
#编写泛式函数sim. Fun function

 sim.fun <-function (m,f,...)
  {#n为模拟样本次数, F is the function that needs to be emulated sample <-1:m for (i-1:m) {Sample[i] <-f (...)
   ' Sample} #生成一个二项分布随机数的标准化变量 f <-function (n=10,p=0.3) {z <-rbinom (1,n,p) (z-n*p)/sqrt (n*p* (1-p))} Par (Mfrow=c (2,2)) x<-Sim.fun (1000,f); hist (X,prob=t,main=paste ("m=")) #由于m是局部变量, error x <-sim.fun (1000,f,20,0.5); hist (x,prob=t) x <-sim.fun (100,f,1 0,0.6); hist (x,prob=t) #生成n个均值和方差都相等的指数分布的随机数标准化变量 f <-Function (n,mu=10) {(Mean (Rexp (N,1/MU))-mu)/(MU/SQRT (N) )} x <-Sim.fun (100,f,1,10) hist (x,prob=t,main= "N=1") #添加正态分布密度线 x <-seq ( -1,4,0.5) points (X,dnor m (x), type= "L") # #模拟掷骰子1000次 Z <-sample (1:6,100,replace = T) barplot (table (z)) 
 #模拟1000个卡方分布随机数的均值 Sim.chi <-function (m) {sample<-1:m for (i in 1:m) Sample[i]=mean (RCHISQ (1 0,3)) #求10个服从自由度3的卡方分布的均值 (sample-3)/sqrt (2*3/10) #标准化变量} x <-Sim.chi (1000) hist (x,prob=t) 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.