Evaluating the resulted simulated data

90 Views Asked by At

I am simulating data using the Rejection method where the density function of X is given by f(x)= C * e^(x) for all x in [0,1]. I defined g(x) = 1 and C to be the maximum of f(x) which is equal to 1/(e-1).

I used the following code to simulate data:

rejection <- function(f, C, g, rg, n) {
  naccepts <- 0
  result.sample <- rep(NA, n)

  while (naccepts < n) {
    y <- rg(1)
    u <- runif(1)

    if ( u <= f(y) / (C*g(y)) ) {
      naccepts <- naccepts + 1
      result.sample[naccepts] = y
    }
  }

  result.sample
}

f <- function(x) ifelse(x>=0 & x<=1, (exp(x)), 0)
g <- function(x) 1
rg <- runif
C <-  1/(exp(1) -1)

result <- rejection(f, C, g,rg, 1000)

Then, I use the histogram to compare the simulated data with the curve of original pdf as

hist(result,freq = FALSE)
curve(f, 0, 1, add=TRUE)

But the resulted plot is kind of weired! the plot is here so I am looking for any help to clarify what is wrong in my work.

2

There are 2 best solutions below

2
On

Your maximum value is wrong. For exp(x) in the range [0...1] interval, normalized PDF would be

f(x) = exp(x)/(exp(1) - 1)

Thus maximum of f(x) is exp(1)/(exp(1) - 1)

UPDATE

Ok, here is code which follows wiki article on Rejection sampling

sampleRej <- function(f, g, rg, M, n) { # rejection sampling following wiki
    naccepts <- 0
    result   <- rep(NA, n)

    while (naccepts < n) {
        y <- rg(1)
        u <- runif(1)

        if ( u <= f(y) / (M*g(y)) ) {
            naccepts <- naccepts + 1
            result[naccepts] = y
        }
    }

    result
}

g <- function(x) { # Normalized uniform distribution PDF
    1.0
}

f <- function(x) { # normalized sampling PDF
    exp(x)/(exp(1.0) - 1.0)
}

rg <- function(n) { # function to sample from g, which is uniform
    runif(n)
}


M <- exp(1.0)/(exp(1.0) - 1.0) # f(x) maximum value
q <- sampleRej(f, g, rg, M, 100000) # sample 100K points

# and plot everything together
curve(f, 0.0, 1.0)
hist(q, freq=FALSE, add=TRUE)

and it produces graph like one below, looks good to me

enter image description here

4
On

This shows the whole curve and histogram:

curve(f, 0, 1)
hist(result,freq = FALSE, add=TRUE)

But of course now the histogram is somewhat small in the plot...