Open paul-buerkner opened 2 years ago
The compiler error is there for a reason; the _lupdf mechanism in Stan math library is tied to the autodiff and cannot work correctly outside of the model block. This issue was discussed and closed in stanc3 repo. Some context in this Discourse comment.
Thank you! That makes sense. Closing this issue.
the _lupdf mechanism in Stan math library is tied to the autodiff and cannot work correctly outside of the model block.
While it's tied to autodiff, that doesn't mean it can't work outside the model block. The _lupdf
versions of functions drop terms in the log density that don't depend on autodiff variables (i.e., constants) when the template parameter propto is set to true
. The value is true
when the log_prob
function is called for sampling or VI and false
when called for optimization.
I've found it inconvenient that I can't use _lupdf
in functions, which has led me to just use code duplication rather than functions in my Stan code.
The transformed parameters block is just like the model block right down to where the code is generated in the C++. That is, it's generated inside the context where the propto
template parameter is available. In particular, I believe it should be possible to do this:
transformed parameters {
real lp1 = normal_lupdf(y | mu, sigma);
}
model {
target += lp1;
This would do exactly what I wanted it to do if you just generated _lupdf
the same way as in the model block. The discussion on stanc3 just says this is impossible, but I think it should be possible.
The transformed parameters block is just like the model block right down to where the code is generated in the C++.
Both the model and transformed block are in the same log_prob call with propto = true, yes. However, the actual output for the transformed parameters is calculated in write_array, which does not use auto diff.
So in your above case, the target would be updated correctly during sampling/VI, but the output of lp1
in the CSV would be all zeros.
A weird hack to fix this would be to always generate the calls with _lpdf in write_array only, though that seems like a bad workaround.
Summary:
Allow
_lupdf
and_lupmf
in the transformed parameters block.Description:
For implementing prior sensitivity checks via power scaling (https://arxiv.org/abs/2107.14054; a paper together with @avehtari), we need to store the prior contributions to the log posterior. For this purpose, we currently define a quantity
log_prior
in the transformed parameters block to which we subsequently add_lpdf
and_lpmf
statements of the priors. This appears to work nicely. However, when I want to use the unnormalized versions_lupdf
and_lupmf
instead, I get a compiler error.Reproducible Steps:
Try to compile
Current Output:
Compiler error:
Expected Output:
Stan compiles this model.
Additional Information:
Provide any additional information here.
Current Version:
v2.28.2