Generating random numbers

I have a probability distribution whose inverse cdf could not find directly and I have used uniroot function in stan to define quantile function to generate random numbers but it is not working. Is there any alternative way to generate random numbers _rng.

Unfortunately, very hard to provide a more concrete answer without seeing the details. In abstract, the solution you discuss should work. What do you mean by “is not working”? Do you get errors or incorrect values? Alternative ways would depend heavily on the specific of the distribution.

Additionally, since _rng functions are constrained to generated quantities, you may always do the random number generation in the host language where there might be a library already available implementing the necessary RNG.

my stan code is like this

functions{real poissonISG_lpdf(real y, real alpha, real beta, real lambda){
  w = beta/x;
	u = exp(-w);	
	z = log(1 - exp(-lambda));
	// pdf of new distribution poission inverted shifted gompertz
  return sum(log(beta)+log(lambda)-z-2*log(x)+log(1+alpha*(1-u))-w-alpha*u-lambda*(1-u)*exp(-alpha*u));
 }
  real poissonISG_rng(real alpha, real beta, real lambda){   \\quantile function
    u = uniform_rng(0, 1);
    qtl = length(u);
	z = 1+(1/lambda) * log(1-(1-exp(-lambda))*(1-u));
	lower = 0.001;
	upper = 10000;
    for (i in 1:length(u)){
      x = uniroot(function(x)  log(1.0 - exp(-beta/x)) - alpha * exp(-beta/x) - log(1.0-z[i]), c(lower, upper), tol = 0.00001); 
      qtl[i] = x$root;    
      } 
    return (qtl);
  }
}

data{
  int N;
  real y[N];
}

parameters{
  real <lower=0> alpha;
  real <lower=0> beta;
  real <lower=0> lambda;
}


model{
  for(i in 1 : N){
    y[i]~ poissonISG(alpha, beta, lambda);
  }
  alpha~ gamma(0.5, 0.5);
  beta~ gamma(0.1, 0.1);
  lambda~ gamma(0.1, 0.1);
}

generated quantities{
  vector [N] yrep;
  for(i in 1 : N){
    yrep[i]=  poissonISG_rng(alpha, beta, lambda);
  }
}

and my data set is

y <- c(1.312, 1.314, 1.479, 1.552, 1.700, 1.803, 1.861, 1.865, 1.944, 1.958, 
       1.966, 1.997, 2.006, 2.021, 2.027, 2.055,2.063, 2.098, 2.14, 2.179, 2.224, 2.240, 
       2.253, 2.270, 2.272, 2.274, 2.301, 2.301, 2.359, 2.382, 2.382, 2.426,2.434, 2.435, 
       2.478, 2.490, 2.511, 2.514, 2.535, 2.554, 2.566, 2.57, 2.586, 2.629, 2.633, 2.642, 
       2.648, 2.684,2.697, 2.726, 2.770, 2.773, 2.800, 2.809, 2.818, 2.821, 2.848, 2.88, 
       2.954, 3.012, 3.067, 3.084, 3.090, 3.096,3.128, 3.233, 3.433, 3.585, 3.585)

get the errors like this

rstan:::rstudio_stanc("D:/PISG_Bayesian/PSIG.stan")
Error in stanc(filename, allow_undefined = TRUE) : 0

Syntax error in 'string', line 8, column 61 to column 62, parsing error:

Ill-formed phrase. "{" should be followed by a statement, variable declaration or expression.

It appears you are trying to use the R function uniroot and some associated R syntax for lambda functions within your Stan code. This cannot work, Stan cannot use R functions and syntax. If you want to solve equations in Stan program, you need to use Stan’s solver and associated syntax, see 10.1 Algebraic equation solver | Stan Functions Reference

Alternatively, you can always do the rng step as a post processing step over the final fit in R, in which case you can keep using any R functionality.

Additionally, note that you can format code nicely here on the forums by putting it in triple backticks (`). I’ve edited the post for you to make it more readable.

Best of luck with your model!

Also use forward slashes

thank you very much, I will do that.

thanks

hello Martin, I am new to R programming and Stan also and I have no such knowledge in programming. This is my first attempt at Stan.
Please help me, I have tried many times to use the function algebra solver but could not code up correctly. Please help me I just wanted to estimate the parameters of that model and generate the random numbers.

Unfortunately, the algebra solver is one of the more difficult pieces of Stan, so if you are a novice to programming in general, this might involve a difficult (though definitely not impossible) learning curve. You may try following the example at 12 Solving Algebraic Equations | Stan User’s Guide and ask questions if you are having trouble following the example.

It is a bit hard to provide guidance for such a general description of the problem. Do you have a working RNG code in R? Does the Stan model without the RNG work as expected?

Ya without rng it works well and may I perform a posterior predictive check without rng?

You need a RNG to run a posterior predictive check, but you can run the RNG in R. Do you have an R version of the RNG that works? If yes, can you share the R code? If there are advantages for you to have the RNG in Stan, we can definitely work through it, but it seems you are adapting an existing R code to Stan, so that might be an easier way.

I’ll repeat that it is very hard to help with so little information, please share more details - what you think you understand in the linked documents, what you don’t. What you tried, and what results you got. What do you want to achieve with this model, etc…