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