简体   繁体   中英

Simulation of random variables using Polar method

I have the following algorithm

Step 1. Generate u1 and u2~U(0,1)

Step 2. Define v1=2u1-1, v2=2u2-1 and s=v1^2+v2^2

Step 3. If s>1, come back to Step 1.

Step 4. If s<=1, x=v1(-2logs/s)^(1/2) and y=v2(-2logs/s)^(1/2)

Here is my approach to implement this algorithm in R:

    PolarMethod1<-function(N)
{

  x<-numeric(N)
  y<-numeric(N)
  z<-numeric(N)

  i<-1

  while(i<=N)
  {u1<-runif(1)
  u2<-runif(1)
  v1<-(2*u1)-1
  v2<-(2*u2)-1
  s<-(v1^2)+(v2^2)

  if(s<=1)
  {
    x[i]<-((-2*log(s)/s)^(1/2))*v1
    y[i]<-((-2*log(s)/s)^(1/2))*v2
    z[i]<-(x[i]+y[i])/sqrt(2) #standarization
    i<-i+1
  }
  else
    i<-i-1
  }

  return(z)
}
z<-PolarMethod1(10000)
hist(z,freq=F,nclass=10,ylab="Density",col="purple",xlab=" z values")
curve(dnorm(x),from=-3,to=3,add=TRUE)

The code, fortunately, does not mark any error and works quite well with N=1000 but when I change to N=10000 , instead of making a better approach to the curve displays:

N = 10000

contrast with N=1000 displays:

N = 1000

Why is that?

Is there something wrong with my code? It's supposed to be better adjusted when N increases.

Note:I added the z in the code to include both variables in the output.

You ask for 10 bins when you draw the histogram, but that's only a suggestion. You actually got 8, because to cover the range from -4 to 4 there is no division into 10 bins that ends up on nice round numbers, whereas 8 bins have very nice boundaries.

If you want more bins, then don't specify nclass . The default gave me 20 bins. Or specify breaks = "Scott" , which uses a different rule to select bins. I saw about 80 bins using this option.

Why is there a difference between 1000 and 100000 runs?

When you run 1000 simulations the z values usually go from -3.2 to 3.2. But if you increase the runs to 100k you will obtain more extreme values, z will go from -4 to 4.

The histogram is binning the z results into 10 bins. A higher range in z will result in wider bins, and wider bins usually adjust worse to the probability density.

Your bin width for 1000 runs is aproximately 0.5, but for 100k is 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM