I was trying to do one dimensional integration in Rstan using external C++ code. I looked at the posts: 1D integration and integration-in-stan . I wrote the header file test.hpp as follows
When including Stan code in your post it really helps if you make it as readable as possible by using Stan code chunks (```stan) with clear spacing and indentation. For example, use
#ifndef test_hpp
#define test_hpp
#include <stan/math.hpp>
#include <vector>
#include <iostream>
#include <limits>
#include <sstream>
template <typename T0__, typename T1__,typename T2__>
typename boost::math::tools::promote_args<T0__, T1__,T2__>::type
integral_GK(const T0__& ll, const T1__& ul,const T2__& nu, std::ostream* pstream__) {
auto f1 = [&](double x) {
return std::exp(x);
};
double error;
double ll_new=ll * nu;
double ul_new=ul * nu;
double Q = stan::math::integrate_1d(f1, ll_new, ul_new, {}, {}, {},&error)/nu;
return Q;
}
And run the Stan code :
mc2="
functions { real integral_GK(real ll, real ul, real nu); }
model {} // use the integral_GK() function somehow
"
, it works fine!!! But when I have another code in the header file ātest.hppā:
template <typename T0__, typename T1__,typename T2__>
typename boost::math::tools::promote_args<T0__, T1__,T2__>::type
test1(const T0__& ll, const T1__& ul,const T2__& beta, const T2__& M0, std::ostream* pstream__) {
auto f2 = [&](double x) {
return std::exp(-beta*(x-M0));
};
std::vector<double> theta={beta,M0};
double error;
double ll_new=ll;
double ul_new=ul;
double Q = stan::math::integrate_1d(f2, ll_new, ul_new, theta, {},{}, &error);
return Q;
}
and run the Stan code
test2="functions{
real test1( real lower, real upper, real beta,
real M0);
real logL (int N, real[] m, real beta, real M0){
real temp=0.0;
for (i in 1:N){
temp += beta*exp(-beta*(m[i]-M0));
}
return (temp);
}
}
data{
int <lower=0> N;
real<lower=0> m[N];
real<lower=0> M0;
real<lower=0> lower;
real<lower=0> upper;
}
parameters{
real<lower=0> beta;
}
model{
//priors
beta~exponential (1);
//likelihood
target +=logL(N,m,beta,M0) - test1(
lower, upper, beta, M0);
}
"
, I get the following COMPILATION error:
In file included from file23582db6d7ac.cpp:91:
/Users/Lulu /Desktop/test.hpp:50:16: error: no matching function for call to 'integrate_1d'
double Q = stan::math::integrate_1d(f2, ll_new, ul_new, theta, {},{}, &error);
^~~~~~~~~~~~~~~~~~~~~~~~
. Why is this happening? Any idea? Could anyone help me with this?