Integrate_1d in likelihood computation produces NaNs in the gradient

Oh! 1.79769e+308+1.79769e+308 overflows so cumulative_sum() produces infinities and log_softmax() is all nan.

The exp(sum(log_lik)) part that comes from softmax is always \leq 1 so x sufficiently far in the tails of the normal_lpdf will always yield zero total. You can put this at the start of the function

if (abs(x - pars[mu_index]) > 1000) {
    return 0.0;
}

Similar idea should work with categorical_logit: check at the start if the value of x is so far from reasonable that no calculation is needed to see that the result is zero and there’s no need to worry about infinities.

3 Likes