0
$\begingroup$

I want to generate random variables from a distribution function using inverse sampling with the additional condition that the sampling should be conditioned, i.e., random generated variables should be greater or less than a given value.

The inverse of my cdf is :

invcdf <- function(y) a2 * log(a1/y - 1) + a3

a1,a2 and a3 are parameters.

I used inverse sampling to generate n rv as follows :

invcdf(runif(n))

Now, the problem is that I want the values generated greater than a value. How should I introduce this condition in random sampling?

When I use this to have value greater than 500 :

invcdf(runif(10,500,1e6))

I get this error message : Warning message: In log((a0/y) - 1) : NaNs produced This is due to the fact that R is trying to take the logarithm of a negative number for a high value of y (a1=1).

I already try to repeat the process until having values satsifying my constraints is this way :

repeat{
 x=invcdf(runif(1))
 if(x>500){
   break
 }

But it tooks a lot of time and sometimes can't provide the value that I need.

$\endgroup$
0
$\begingroup$

I found an answer to my question. It can be helpful for other users. The idea is to repeat the process until having values satsifying my constraints!

repeat{
  x=invcdf(runif(1))
  if(x>100){
  break
}

The method works for a reasonable number of variables to generate. It is not very efficient if we want to generate a great number of variables.

| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.