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

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:

        // 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:

// 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.

1 Like