What’s the difference between specifying y ~ poisson_log(…) vs specifying this as:

```
for(i in 1:N) {
y[i] ~ poisson_log(log_rate);
}
```

as e.g. in:

opened 11:48PM - 16 Aug 16 UTC

new function
good first issue

#### Summary:
*[From @bob-carpenter: I edited the title to highlight the root… cause. You can skip all the discussion; just missing the underlying functions at this point.]*
I'm fitting a model with the `poisson_log` function (i.e. a Poisson distribution parameterized by log(lambda) instead of lambda).
I'm running into two problems:
- Large number of divergent transitions with a simple model (well over 50% of the post-warmup iterations)
- If I add truncation, I start getting errors about negative lambda values and my `log(rate)` parameter is squashed up against 0.
My best guess is that truncation flips a switch somewhere that leads to `stan::math::poisson_log_log` when it should go to to `stan::math::poisson_log`. I can't explain the divergent transitions, but I think it also might be related. Maybe an incorrect nonlinear transform?
#### Reproducible Steps:
```
compiled = stan_model(model_code =
"data {
int<lower=1> N;
int<lower=1> y[N];
}
parameters {
real log_rate;
}
model {
log_rate ~ normal(0.0, 1.0);
for(i in 1:N){
y[i] ~ poisson_log(log_rate) T[1, ];
}
}
"
)
y = rpois(1000, 1)
y = y[y>0]
data = list(y = y, N = length(y))
m = sampling(compiled, data = data)
```
As noted above, the non-truncated version (i.e. the above code with T[1, ] removed on line 12) also has problems.
#### Current Output:
Sampling without truncation:
- `There were 2768 divergent transitions after warmup. Increasing adapt_delta above 0.8 may help.`
- samples of log_rate approximately Gaussian and can span 0, depending on the distribution of `y` in `data`.
Sampling with truncation:
- If the model initializes, I get many iterations of `Exception thrown at line 12: poisson_log: Rate parameter is -2.24632e-13, but must be >= 0! 1`
- With an earlier (more complicated) version of the model that didn't initialize, I instead got `[596] "Exception thrown at line 12: stan::math::poisson_log: Rate parameter is -0.989166, but must be >= 0!"` Note the reference to `stan::math::poisson_log` instead of `stan::math::poisson_log_log`
- `There were 1065 divergent transitions after warmup. Increasing adapt_delta above 0.8 may help`
#### Expected Output:
MCMC samples
#### Current Version:
rstan 2.11.1

1 Like

The two approaches are equivalent, but the loop is less efficient. See this section of the manual for more background: 24.8 Vectorization | Stan User’s Guide

2 Likes