Integrate_1d not compiling

When I try to compile this simple application of integrate_1d

functions{
     real[] integrand(real x, // Function argument
     real[] f,                    // Complement of function argument
                                     //  on the domain (defined later)
     real[] theta,                // parameters
     real[] x_r,                  // data (real)
     int[] x_i) {                 // data (integer)
    real df_dx[1]; 
     
    df_dx[1] = exp(-0.5* square(x))  ;
    return(df_dx);
  }
 
}
data { 
    real x;
}

transformed data {
  real x_r[0];
  int x_i[0]; 
  real theta[0]; 
}
 

generated quantities{
  real value = integrate_1d(integrand,
                              negative_infinity(),
                              x,
                              theta, 
                              x_r,  
                              x_i);
   
}

I get this in the Console:

R>rstan:::rstudio_stanc(“mre.stan”)
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
error in ‘model2db7379c0142_mre’ at line 32, column 33

30:                               theta, 
31:                               x_r,  
32:                               x_i);
                                    ^
33:    

PARSER EXPECTED: “,”
Error in stanc(filename, allow_undefined = TRUE) :
failed to parse Stan model ‘mre’ due to the above error.

That is not what the manual says should happen but according to this thread https://discourse.mc-stan.org/t/how-to-use-integrate-1d/9218?u=jgcolman the problem is that integrate_1d requires the variable

rel_tol

to be supplied.

So why then if I try to compile this:

functions{
     real[] integrand(real x, // Function argument
     real[] f,                    // Complement of function argument
                                     //  on the domain (defined later)
     real[] theta,                // parameters
     real[] x_r,                  // data (real)
     int[] x_i) {                 // data (integer)
    real df_dx[1]; 
     
    df_dx[1] = exp(-0.5* square(x))  ;
    return(df_dx);
  }
 
}
data { 
    real x;
}

transformed data {
  real x_r[0];
  int x_i[0]; 
  real theta[0]; 
}
 

generated quantities{
  real value = integrate_1d(integrand,
                              negative_infinity(),
                              x,
                              theta, 
                              x_r,  
                              x_i,
                              0.01);
   
}

in which I have supplied

rel_tol

do I get this:

R>rstan:::rstudio_stanc(“mre1.stan”)
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
first argument to integrate_1d must be the name of a function with signature (real, real, real[], real[], int[]) : real
error in ‘model2db766a2b5b7_mre1’ at line 33, column 34

31:                               x_r,  
32:                               x_i,
33:                               0.01);
                                     ^
34:    

PARSER EXPECTED: “)”
Show Traceback

Rerun with Debug
Error in stanc(filename, allow_undefined = TRUE) :
failed to parse Stan model ‘mre1’ due to the above error.

It is probable that there is some silly error here as the example in the post I referred to does work for me.

Hey!

I don’t have much expertise here, but this should work:

functions{
  
  real integrand(
    real x,        // Function argument
    real f,        // Complement of function argument on the domain (defined later)
    real[] theta,  // parameters
    real[] x_r,    // data (real)
    int[] x_i)     // data (integer)
    {
      
      real df_dx = exp(-0.5* square(x));
      
      return(df_dx);
    }
}

Note that the return type of the function is real and not an array of reals real[]. Also the second argument should be real as well, not an array of reals.

Hope this helps!
Cheers,
Max

1 Like

Thanks Max_Mantei: it was indeed some silly error arising from too much exposure to the lack of type discipline in R.

1 Like

Hi, jgcolman how are you.

I have the same problem with one of my functions, I would like to know how do you solve this problem, for your functions, could you tell me, please.