Dear all,
I am new to Stan and Julia.
I need to include a C++ function in a stan model but I don’t know how I can do it.
The function is remainder and then I include the associated library but I get an error:
SystemError: opening file "<cmath>": No such file or directory
Here a draft:
stancode = "functions {
real[] P_growth(real t, // time
real[] x, // state
real[] theta, // parameters
real[] x_r, // environmental data
int[] x_i){
real gamma = theta[1];
real lambda = theta[2];
real growth = gamma*x[1];
#include <cmath>
if (remainder(t,24) < 14) {
real loss = lambda*x[1]*x[1];
} else {
real loss = 0;
}
return {growth - loss};
}
}
data {
int<lower = 0> N; // num obs
real t_obs[N]; // obs times
real<lower = 0> y[N]; // observed variable at measurement times
real sigma;
}
parameters {
real<lower=0,upper=1> theta[2]; // parameters
real<lower=0> x0[1];
}
transformed parameters {
real x[N,1] = integrate_ode_rk45(P_growth, x0, -1, t_obs, theta,
rep_array(0.0, 0), rep_array(0, 0),
1e-6, 1e-5, 1e3);
}
model {
//theta[1] ~ normal(0.1, 2);
//theta[2] ~ normal(0.1, 2);
x0 ~ normal(1.0, 10);
y[1:N] ~ normal(x[1:N,1], sigma); // obs
}";
I tried a bit regarding this link: https://cran.r-project.org/web/packages/rstan/vignettes/external.html
and created a function for the remainder:
// define the remainder
functions {
int remainder(real n);
int remainder(real n) {
return remainder(n,24);
}
}
But I don’t really know where to place it in the Stan prog. and I tried several attempts that didn’t work.
Thanks a lot for your time and help!