So I am building a complex model and doing some prior predictive checks. Essentially the way things work right now is that
- in
model
I sample the parameters
- in
transformed parameters
the parameters get transformed into quantities of interest
- nothing in
transformed parameters
gets passed back into model
(because I am doing a prior predictive check; when I move to the next step this will ultimately create a prediction that will get passed into the likelihood statement)
Here’s the rub. When I comment out the transformed parameters
block, everything samples okay. When it is uncommented, I get this warning:
Chain 1: Rejecting initial value:
Chain 1: Gradient evaluated at the initial value is not finite.
Chain 1: Stan can't start sampling from this initial value.
it doesn’t matter if I set init = 0
or whatever.
Since no variables from transformed parameters
make it into the model
block, I don’t understand how having the transformed parameters
commented or uncommented should change whether the gradient is finite or not.
I’ve checked using print
statements to ensure that none of the variables in transformed parameters
are inf
or nan
.
I’d rather not share the code publicly, so more wondering how this can happen and generally what I can do to fix. I am using rstan
version 2.19.1
Hi Thomas,
The transformed parameters
block should only be used for quantities that are then also used in the model
block. Try moving your computation for these quantities into the generated quantities
block, since the computations in generated quantities
don’t involve any gradient calculations.
The problem is that I am doing a prior predictive check, and ultimately want to pass in the result of the transformed parameters
block back into model
. I’m just not doing it right now.
Is the idea that when I ultimately do that, the error will go away?
Oh, I see what you mean sorry. No, the idea was more that the calculations were resulting in a non-finite gradient for one (or more) of the variables, so by moving them to generated quantities
you no longer need to worry about the gradients.
In Stan, any variables/computations in the transformed parameters
block will require the gradients also be computed, whereas the generated quantities
block will only compute the value.
I would still try running the model with the calculations in generated quantities
first, just to check that the calculations are consistent with what you expect. It might be that there is a syntax/indexing error that’s tripping you up, and it won’t be apparent until you run the model.
If the syntax is correct, that gets a bit harder to debug. I’d start by iteratively commenting out or simplifying different calculations in the transformed parameters
block, so you can narrow down which specific line is causing the issue.
Unfortunately, there’s no simple way to print the gradients at each iteration in a Stan model (print
will print the value, but not the gradient). This forum post outlines how to print the gradients to a separate file, but I’m not sure how informative that output is (I’ve never used it).
If all else fails, feel free to PM me your model and I can take a closer look.
Good luck!
Thank you!
This is really helpful. I will begin the iterative commenting-of-things-in-and-out.
1 Like