# Integrate\_1d not convergent in for loop

**URL:** <https://discourse.mc-stan.org/t/integrate-1d-not-convergent-in-for-loop/18627>\
**Category:** Modeling\
**Tags:** rstan\
**Created:** [October 13, 2020, 6:06pm UTC](https://discourse.mc-stan.org/t/integrate-1d-not-convergent-in-for-loop/18627 "2020-10-13T18:06:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francisco\_Choi](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/francisco_choi/32/9232_2.png) [@Francisco\_Choi](https://discourse.mc-stan.org/u/Francisco_Choi)\
**Post date:** [October 13, 2020, 6:06pm UTC](https://discourse.mc-stan.org/t/integrate-1d-not-convergent-in-for-loop/18627/1 "2020-10-13T18:06:31Z")

</div>

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.

```stan
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!

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [October 20, 2020, 3:58pm UTC](https://discourse.mc-stan.org/t/integrate-1d-not-convergent-in-for-loop/18627/2 "2020-10-20T15:58:03Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Francisco\_Choi](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/francisco_choi/32/9232_2.png) [@Francisco\_Choi](https://discourse.mc-stan.org/u/Francisco_Choi)\
**Post date:** [October 20, 2020, 6:15pm UTC](https://discourse.mc-stan.org/t/integrate-1d-not-convergent-in-for-loop/18627/3 "2020-10-20T18:15:06Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [October 20, 2020, 9:06pm UTC](https://discourse.mc-stan.org/t/integrate-1d-not-convergent-in-for-loop/18627/4 "2020-10-20T21:06:27Z")

</div>

Here’s a situation where we replaced an integrate\_1d call with the ode integrator: [Difference in behavior between integrate\_1d and integrate](https://discourse.mc-stan.org/t/difference-in-behavior-between-integrate-1d-and-integrate/18259/11)

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

---

<div class="post-metadata">

**Author:** ![Francisco\_Choi](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/francisco_choi/32/9232_2.png) [@Francisco\_Choi](https://discourse.mc-stan.org/u/Francisco_Choi)\
**Post date:** [October 20, 2020, 10:31pm UTC](https://discourse.mc-stan.org/t/integrate-1d-not-convergent-in-for-loop/18627/5 "2020-10-20T22:31:40Z")

</div>

I see. Really appreciate your response!
