Problem using integrate

Hi, my name is Jose Alejandro ordoñez, I´m writing because I have problems with the function integrate_1d. The code below shows what i´m triying to do.

scode= "
functions{
   real dplogisstan (real x, real mu, real sigma, real alpha){
     real dens = alpha*pow(logistic_cdf(x,mu,sigma),alpha-1)*exp(logistic_lpdf(x|mu,sigma));
     return dens;
}

real eapp_integrand (real x, real xc, real[] theta, real[] x_r,int[] x_i){
  real alpha = theta[1];
  real a = theta[2];
  real ez = digamma(alpha)-digamma(a);
  real vz = trigamma(alpha)+trigamma(a);
  real sigmaalpha = sqrt(1/vz);
  real sigma1 = sqrt(0.5/trigamma(a));
  real mualpha = -sigmaalpha*(digamma(alpha)-digamma(a));
  real integrand = log (1+ exp(-x/sigma1))*dplogisstan(x,mualpha,sigmaalpha,alpha);
  return integrand;
 }
 
 real eapp_integral(real alpha){
  real x_r[0];
  int x_i[0]; 
  real integral=integrate_1d(eapp_integrand, negative_infinity(),positive_infinity(), {alpha, 1.0}, x_r, x_i);
  return integral;
}
}
"

After this code, I used the function expose_stan_functions(stanc(model_code = scode)) to verify that my sintaxis is correct, nevertheless, the last function which uses integrate_1d shows me this error.

``stan
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
error in ‘model7adc79c87c32_scode’ at line 24, column 108

22:   real x_r[0];
23:   int x_i[0]; 
24:   real integral=integrate_1d(eapp_integrand, negative_infinity(),positive_infinity(), {alpha, 1.0}, x_r, x_i);
                                                                                                               ^
25:   return integral;

PARSER EXPECTED: “,”
Error in stanc(model_code = scode) :
failed to parse Stan model ‘scode’ due to the above error.
``
After that, as in the website of stan, I decided to include the argument relative_tolerance with a similar result but in this case the error was PARSER EXPECTED= “)”.

Thanks for the attention, I would be grateful with any suggestion.

Hi Jose,

There are two issues here, first (as you’ve already figured out), you need to specify the relative tolerance. After that, the issue is that the x_i and x_r parameters have to be declared in the data or transformed data blocks. What you can do in your case, is just specify that they’ll be passed to the eapp_integral function:

scode= "
functions{
   real dplogisstan (real x, real mu, real sigma, real alpha){
     real dens = alpha*pow(logistic_cdf(x,mu,sigma),alpha-1)*exp(logistic_lpdf(x|mu,sigma));
     return dens;
}

real eapp_integrand (real x, real xc, real[] theta, real[] x_r,int[] x_i){
  real alpha = theta[1];
  real a = theta[2];
  real ez = digamma(alpha)-digamma(a);
  real vz = trigamma(alpha)+trigamma(a);
  real sigmaalpha = sqrt(1/vz);
  real sigma1 = sqrt(0.5/trigamma(a));
  real mualpha = -sigmaalpha*(digamma(alpha)-digamma(a));
  real integrand = log (1+ exp(-x/sigma1))*dplogisstan(x,mualpha,sigmaalpha,alpha);
  return integrand;
 }
 
 real eapp_integral(real alpha, data real[] x_r, data int[] x_i){
  real integral=integrate_1d(eapp_integrand, negative_infinity(),positive_infinity(), {alpha, 1.0}, x_r, x_i, 0.001);
  return integral;
}
}
"

Note that the relative tolerance I’ve specified (0.001) is completely arbitrary and should be updated

Thank you andrjohns, you helped me a lot!