Integrate_1d not convergent in for loop

Hi, I want to use integrate_1d function in rstan in for loop. Even though it is working, it is not convergent.
Since it is to integrate directly such as \int_{0}^{ \mbox{observed_failure}[i]} \mbox{exp} (\rho * \beta_2 * treat[i] * x) dx, I also tried the code using the integrated value ( \mbox{exp}(\rho * \beta_2 * treat[i] * \mbox{observed_failure}[i] ) - 1) / (\rho * \beta_2 * treat[i]) and it was normally working and convergent. For this reason, I can say that there was a problem regarding integrate_1d codes which were the only parts that I changed.

functions {
  real func(real x, real xc, real[] theta, real[] x_r, int[] x_i){
    real beta2 = theta[1];
    real rho = theta[2];
    int treat = x_i[1];
    return(exp(rho *beta2 * treat * x ));
  }
} 
data {
   ...
    int treat[new_N];
   ...
}
transformed data {
  real x_r[0]; // I do not have real data
  int x_i[1]; // I only have treat data which is integer 
}
parameters {
   real beta0;
   real beta1;
   real beta2;
   ...
}
model {
 ...
  for (i in 1:new_N) {
    h[i] = h0 * exp(eta*age[i] + rho*( beta1*treat[i] +  beta2 * treat[i] * observed_failure[i] + U[i]));
    integ[i] =  integrate_1d(func, 0, observed_failure[i], {beta2, rho}, x_r, {treat[i]}, 1e-8);
    S[i] = exp(- h0 * exp(eta * age[i]) * exp(rho * (beta0 + beta1 * treat[i]) + U[i]) * integ[i]);
  }
 ...
} 

fit=stan(data=data1, model_code=stan_codes, iter=2000, warmup=1000,
thin=1, chains=chain, pars=pars, init=inits, control = list(adapt_delta = 0.99))

When I run fit above, it is working but not convergent which shows “There were 1191 divergent transitions after warmup”.

Is my code about integrate_1d wrong…?

Here is my version of R:

R 4.0.2
x86_64-apple-darwin17.0
stan_version 2.21.0

Thank you for your help in advance!

Sorry for the slow response!

I think your code looks right. integrate_1d can be a bit fragile. That’s something we need to look at.

In the interim though I think you can do your integral in closed form:

\int_0^{F_i} e^{\rho \beta_2 t_i x} dx \\ = e^{\rho \beta_2 t_i} \int_0^{F_i} e^x dx \\ = e^{\rho \beta_2 t_i} (e^{F_i} - 1)
1 Like

Hi! Thank you for the reply.

Yes I know that I have a closed form. However, I have a much more complicated form for another example, so before getting into that model, this example should be verified using integrate_1d function.

If it is fragile, I think I try to other methods such as MC integration or Gaussian quadrature.

Thank you!

1 Like

Here’s a situation where we replaced an integrate_1d call with the ode integrator: Difference in behavior between integrate_1d and integrate

Not great but it worked. Try the integrate_1d quadrature in your fancier case before you toss it though. It’s pretty problem dependent.

I see. Really appreciate your response!