I have a very basic question for constrained parameter/transform parameters (e.g. lower>0)
Should there be any different in terms of the parameter posterior between sampling from unconstrained space and then do transformation (exp in the case of >0) and directly sampling with constrained parameters?
data {
}
parameters {
real<lower=0> x_con;
real x_unc;
}
transformed parameters {
real<lower=0> x_trn;
x_trn = exp(x_unc);
}
model {
x_con ~ normal (0,1);
x_unc ~ normal (0,1);
}
It seems that x_con (directly “sampling” of constrained variable) and x_trn (“sampling” from unconstrained and then transform) yield 2 different posterior.
In addition, Stan complained about a few (<10) divergent transitions. Why would this happen?
As it should. x_con has a truncated normal posterior distribution (i.e. Normal without the negative values)
whilst x_trn has a log-normal distribution due to the exponential transformation. See :Log-normal distribution - Wikipedia
Also check out Chapter 35, “Transformations of Constrained Variables” and page 80, “User-Transformed Variables” in the 2.17.0 Stan manual. That has more info on exactly the question you’re asking.
When you specify the constraint in the parameters block, extra terms get secretly added to the target log density. When you specify constraints in the transformed parameters block, no extra terms are added or variables transformed. Those constraints are just checked for correctness at runtime (to help you avoid errors).