# Using integrate\_1d to specify Newdistribution\_lcdf for a new distribution in the Brms package

**URL:** <https://discourse.mc-stan.org/t/using-integrate-1d-to-specify-newdistribution-lcdf-for-a-new-distribution-in-the-brms-package/33050>\
**Category:** Modeling\
**Tags:** integrator, rstan, specification, brms\
**Created:** [October 12, 2023, 10:16pm UTC](https://discourse.mc-stan.org/t/using-integrate-1d-to-specify-newdistribution-lcdf-for-a-new-distribution-in-the-brms-package/33050 "2023-10-12T22:16:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wizkid04](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@wizkid04](https://discourse.mc-stan.org/u/wizkid04)\
**Post date:** [October 12, 2023, 10:16pm UTC](https://discourse.mc-stan.org/t/using-integrate-1d-to-specify-newdistribution-lcdf-for-a-new-distribution-in-the-brms-package/33050/1 "2023-10-12T22:16:38Z")

</div>

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.

```stan
 /* 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));
  }
  

```

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [October 13, 2023, 6:59am UTC](https://discourse.mc-stan.org/t/using-integrate-1d-to-specify-newdistribution-lcdf-for-a-new-distribution-in-the-brms-package/33050/2 "2023-10-13T06:59:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wizkid04](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@wizkid04](https://discourse.mc-stan.org/u/wizkid04)\
**Post date:** [October 13, 2023, 3:48pm UTC](https://discourse.mc-stan.org/t/using-integrate-1d-to-specify-newdistribution-lcdf-for-a-new-distribution-in-the-brms-package/33050/3 "2023-10-13T15:48:06Z")

</div>

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

```plaintext
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.

```

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [October 13, 2023, 4:09pm UTC](https://discourse.mc-stan.org/t/using-integrate-1d-to-specify-newdistribution-lcdf-for-a-new-distribution-in-the-brms-package/33050/4 "2023-10-13T16:09:29Z")

</div>

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:

```stan
  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}));
  }

```

---

<div class="post-metadata">

**Author:** ![wizkid04](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@wizkid04](https://discourse.mc-stan.org/u/wizkid04)\
**Post date:** [October 13, 2023, 6:36pm UTC](https://discourse.mc-stan.org/t/using-integrate-1d-to-specify-newdistribution-lcdf-for-a-new-distribution-in-the-brms-package/33050/5 "2023-10-13T18:36:45Z")

</div>

> [@andrjohns](#):
>
> ```no-highlight
> 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.

````no-highlight
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[].```
````

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [October 13, 2023, 6:46pm UTC](https://discourse.mc-stan.org/t/using-integrate-1d-to-specify-newdistribution-lcdf-for-a-new-distribution-in-the-brms-package/33050/6 "2023-10-13T18:46:34Z")

</div>

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`:

```stan
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}));
  }

```

---

<div class="post-metadata">

**Author:** ![wizkid04](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@wizkid04](https://discourse.mc-stan.org/u/wizkid04)\
**Post date:** [October 14, 2023, 9:29pm UTC](https://discourse.mc-stan.org/t/using-integrate-1d-to-specify-newdistribution-lcdf-for-a-new-distribution-in-the-brms-package/33050/7 "2023-10-14T21:29:24Z")

</div>

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

```plaintext
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

```
