mardi 2 février 2016

Generate random variables from a distribution function using inverse sampling

I have a specific density function and I want to generate random variables knowing the expression of the density function.

For example, the density function is :

df=function(x) { - ((-a1/a2)*exp((x-a3)/a2))/(1+exp((x-a3)/a2))^2 }

From this expression I want to generate 1000 random elements with the same distribution.

I Know I should use the inverse sampling method. For this, I use the CDF function of my PDF which is calculated as follows:

cdf=function(x) { a1/(1+exp((x-a3)/a2))

The idea is to generate uniform distribution and then map it with my CDF functions to get inverse mapping. Some thing like this:

random.generator<-function(n) sapply(runif(n),cdf) 

and then call it with the desired number of random variables to generate.

random.generator(1000) 

Is this approach correct?




Aucun commentaire:

Enregistrer un commentaire