mardi 3 avril 2018

Inverse CDF method to stimulate a random sample

I have a problem where I have written this piece of code, however I think there might be an issue with it.

This is the question: Write an R function called pr1 that simulates a random sample of size n from the distribution with the CDF which is given as..

F_X(x) = 0 for x<=10
     (x-10)^3/1000 for 10<x<20
    1 for x=>20

x = 10 ( 1 + u^(1/3)) #I have used the inverse CDF method here and I now want to simulate a random sample of size n from the distribution.

Here is my code:

 pr1 = function(n)

 { u = runif(n,0,1)
   x = 10 * ( 1 + u^(1/3))
   x }

 pr1(5)

#This was just to check an example with n=5

My question is, since the CDF is 10< x <20, will this affect my code in any way?

Thank you




Aucun commentaire:

Enregistrer un commentaire