Using integrate_1d to specify Newdistribution_lcdf for a new distribution in the Brms package

All,
This seems to be an easy problem to me but I don’t seem to be able to find a solution.
I am not sure if I should share the full code. But I decided to just share a small part. I can share more if needed. Thank you in advance for any pointer you can give.

I am trying to use a non-local prior in my model specification in Brms. Precisely, I am try to use the exponential moment prior (see vignette(“mombf”)). The density of that distribution is:
f(x|\mu, \tau, \phi ) = \exp(\sqrt{2} - \tau\phi/(x-\mu)^2)Normal(x|\mu, \sqrt{\tau\phi})

Its CDF is difficult to get analytically and is approximated using an integral. Below is the snippet of code I can use to do that.

 /* specify the inverse moment distribution (rpackage: mombf) 
  
   * we use the specification in 'vignette(\"mombf\")'  
   */ 
  real iMOMdens(real x, real xc,  array[] real theta, array[] real x_r, array[] int x_i) {
  
  real mu = theta[1];
  real phi = theta[2];
  real tau = theta[3];
  real yest  = x-mu;
  
  return exp( (sqrt(2) - tau*phi/pow(yest,2)) * normal_lpdf(yest | 0, sqrt(tau*phi)));
  
  }
  
   /* compute the log cdf  */
  real iMOM_lcdf(real q, real mu, real phi, real tau){
  
  //array[0] real x_r;
  //array[0] int x_i;
   real x_r;
   int x_i;
  
  return log(integrate_1d(iMOMdens,negative_infinity(), q, { mu, phi, tau}, x_r, x_i));
  }
  

Was there a specific question or issue you had with this?

Yes, the code above does not work. I get the following error (see below).

Compiling Stan program...
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
0

Semantic error in 'string', line 93, column 13 to column 85:

Ill-typed arguments supplied to function 'integrate_1d'. Available signatures:
((real, real, real[], data real[], data int[]) => real, real, real, real[], data real[], data int[]) => real
((real, real, real[], data real[], data int[]) => real, real, real, real[], data real[], data int[], data real) => real
Instead supplied arguments of incompatible type: (real, real, real[], real[], int[]) => real, real, real, real[], real, int.

The error is saying that the function passed to integrate_1d (iMOMdens) should have its signature specified such that x_r and x_i are data-only.

Try this:

  real iMOMdens(real x, real xc, array[] real theta, data array[] real x_r, data array[] int x_i) {
    real mu = theta[1];
    real phi = theta[2];
    real tau = theta[3];
    real yest  = x-mu;
  
    return exp( (sqrt(2) - tau*phi/pow(yest,2)) * normal_lpdf(yest | 0, sqrt(tau*phi)));
  }
  
   /* compute the log cdf  */
  real iMOM_lcdf(real q, real mu, real phi, real tau){
    return log(integrate_1d(iMOMdens,negative_infinity(), q, { mu, phi, tau}, {0}, {0}));
  }

unfortunately, it does not work. I still get the same error.

Compiling Stan program...
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
0

Semantic error in 'string', line 102, column 15 to column 87:

Ill-typed arguments supplied to function 'integrate_1d'. Available signatures:
((real, real, real[], data real[], data int[]) => real, real, real, real[], data real[], data int[]) => real
((real, real, real[], data real[], data int[]) => real, real, real, real[], data real[], data int[], data real) => real
Instead supplied arguments of incompatible type: (real, real, real[], data real[], data int[]) => real, real, real, real[], int[], int[].```

Ah looks like the older stanc in rstan 2.26 is promoting values differently, the x_r argument to integrate_1d needs to be 0.0:

real iMOMdens(real x, real xc, array[] real theta, data array[] real x_r, data array[] int x_i) {
    real mu = theta[1];
    real phi = theta[2];
    real tau = theta[3];
    real yest  = x-mu;
  
    return exp( (sqrt(2) - tau*phi/pow(yest,2)) * normal_lpdf(yest | 0, sqrt(tau*phi)));
  }
  
   /* compute the log cdf  */
  real iMOM_lcdf(real q, real mu, real phi, real tau){
    return log(integrate_1d(iMOMdens,negative_infinity(), q, { mu, phi, tau}, {0.0}, {0}));
  }

@andrjohns I did just that but I even more errors.

Compiling Stan program...
make cmd is
  make -f '/Library/Frameworks/R.framework/Resources/etc/Makeconf' -f '/Library/Frameworks/R.framework/Resources/share/make/shlib.mk' -f '/Users/rszoh/.R/Makevars' CXX='$(CXX14) $(CXX14STD)' CXXFLAGS='$(CXX14FLAGS)' CXXPICFLAGS='$(CXX14PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX14LDFLAGS)' SHLIB_LD='$(SHLIB_CXX14LD)' SHLIB='file8e122b77be02.so' OBJECTS='file8e122b77be02.o'

make would use
clang++ -arch x86_64 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I"/Users/rszoh/Library/R/x86_64/4.3/library/Rcpp/include/"  -I"/Users/rszoh/Library/R/x86_64/4.3/library/RcppEigen/include/"  -I"/Users/rszoh/Library/R/x86_64/4.3/library/RcppEigen/include/unsupported"  -I"/Users/rszoh/Library/R/x86_64/4.3/library/BH/include" -I"/Users/rszoh/Library/R/x86_64/4.3/library/StanHeaders/include/src/"  -I"/Users/rszoh/Library/R/x86_64/4.3/library/StanHeaders/include/"  -I"/Users/rszoh/Library/R/x86_64/4.3/library/RcppParallel/include/"  -I"/Users/rszoh/Library/R/x86_64/4.3/library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DUSE_STANC3 -DSTRICT_R_HEADERS  -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION  -DBOOST_NO_AUTO_PTR  -include '/Users/rszoh/Library/R/x86_64/4.3/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp'  -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1   -I/opt/R/x86_64/include    -fPIC  -falign-functions=64 -Wall -g -O2  -O3 -mtune=native -arch x86_64 -ftemplate-depth-256 -c file8e122b77be02.cpp -o file8e122b77be02.o
if test  "zfile8e122b77be02.o" != "z"; then \
	  echo clang++ -arch x86_64 -std=gnu++14 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L"/Library/Frameworks/R.framework/Resources/lib" -L/opt/R/x86_64/lib -o file8e122b77be02.so file8e122b77be02.o  '/Users/rszoh/Library/R/x86_64/4.3/library/rstan/lib//libStanServices.a' -L'/Users/rszoh/Library/R/x86_64/4.3/library/StanHeaders/lib/' -lStanHeaders -L'/Users/rszoh/Library/R/x86_64/4.3/library/RcppParallel/lib/' -ltbb   -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation; \
	  clang++ -arch x86_64 -std=gnu++14 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L"/Library/Frameworks/R.framework/Resources/lib" -L/opt/R/x86_64/lib -o file8e122b77be02.so file8e122b77be02.o  '/Users/rszoh/Library/R/x86_64/4.3/library/rstan/lib//libStanServices.a' -L'/Users/rszoh/Library/R/x86_64/4.3/library/StanHeaders/lib/' -lStanHeaders -L'/Users/rszoh/Library/R/x86_64/4.3/library/RcppParallel/lib/' -ltbb   -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation; \
	fi
Error in compileCode(f, code, language = language, verbose = verbose) :
^file8e122b77be02.cpp:1710:68: note: in instantiation of member function 'rstan::stan_fit<model8e12646b54f4__namespace::model8e12646b54f4_, boost::random::additive_combine_engine<boost::random::linear_congruential_engine<unsigned int, 40014, 0, 2147483563>, boost::random::linear_congruential_engine<unsigned int, 40692, 0, 2147483399>>>::call_sampler' requested here &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::call_sampler) ^38 warnings and 1 error generated.make: *** [file8e122b77be02.o] Error 1
Error in sink(type = "output") : invalid connection