# Changing grainsize yields "Gradient evaluated at the initial value is not finite"

**URL:** <https://discourse.mc-stan.org/t/changing-grainsize-yields-gradient-evaluated-at-the-initial-value-is-not-finite/34192>\
**Category:** Modeling\
**Created:** [February 21, 2024, 5:27am UTC](https://discourse.mc-stan.org/t/changing-grainsize-yields-gradient-evaluated-at-the-initial-value-is-not-finite/34192 "2024-02-21T05:27:28Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![mhollanders](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mhollanders/32/17623_2.png) [@mhollanders](https://discourse.mc-stan.org/u/mhollanders)\
**Post date:** [February 22, 2024, 11:47pm UTC](https://discourse.mc-stan.org/t/changing-grainsize-yields-gradient-evaluated-at-the-initial-value-is-not-finite/34192/9 "2024-02-22T23:47:30Z")

</div>

Hey,

Thanks, you motivated me to keep digging. I found the issue causing the problem. Taking the log of `tpm.1[t]` inside the `t` loop does not cause the grainsize issue:

```no-highlight
        // primary occasion intervals
        for (t in first[ii]:(n_prim - 1)) {
          
          // ecological TRM
          trm[t][1, 1] = -(psi[1][t] + phi[1][t]);
          trm[t][1, 2] = psi[2][t];
          trm[t][2, 1] = psi[1][t];
          trm[t][2, 2] = -(psi[2][t] + phi[2][t]);
          trm[t][3, 1] = phi[1][t];
          trm[t][3, 2] = phi[2][t];
          trm[t][:, 3] = zeros_vector(3);
          
          // ecological TPM
          tpm.1[t] = matrix_exp(trm[t] * tau[t])[:, 1:2];
          if (lp) {
            tpm.1[t] = log(tpm.1[t]);
          }
          
        } // t

```

However, taking the log of all of `tpm.1` outside of the `t` loop does cause the issue. This makes sense, because there `nan` values for all values of `t` before `first[ii]`, and that is causing the familiar log probability issue.

So, as expected, it wasn’t Stan, but it was me. Thanks for contributing everyone, and sorry for the oversight.

Edit: Actually, no, that doesn’t make sense. The log version has no trouble getting going with `grainsize = 1`. At least, I’ve identified that the thing that triggers is doing `tpm.1 = log(tpm.1)` outside of the `t` loop, but that it has nothing to do with the partial sum function. So, at the very least I’ve isolated what’s triggering it, but I think it does still seem like there’s a bug.

Edit2: FWIW, the following also works with any grainsize:

```no-highlight
// log ecological TPM
if (lp) {
  tpm.1[first[ii]:(n_prim - 1)] = log(tpm.1[first[ii]:(n_prim - 1)]);
}

```

So the problem seems to occur when I try to do `log(tpm.1)`, where `tpm.1` is an object of type `array[n_prim - 1] matrix[3, 2]`, and some of the matrices contained in `tpm.1` are filled with `nan`. Again, it still works with `grainsize = 1`.

---

_[View the full topic](https://discourse.mc-stan.org/t/changing-grainsize-yields-gradient-evaluated-at-the-initial-value-is-not-finite/34192)._
