Stan does neither of these two things. I’ll explain in a moment, but first an aside:
Your options 1 and 2 sample from the same distribution. However, neither option is equivalent to a half normal, because the truncation happens at zero but the normal distribution is centered at 1
. Perhaps an error related to this misunderstanding
explains why your simulations seem to yield different distributions for 1 and 2.
So what does Stan do?
Stan imposes the constraint by constructing a new variable under the hood which for illustrative purposes we can call alpha_unconstrained
, and then recovering alpha
as exp(alpha_unconstrained)
. Note that alpha
is now guaranteed to be positive. Stan then increments the target density according to your sampling statement alpha ~ normal(1, 5);
and automatically adds the Jacobian adjustment associated with the constraining transform exp
. This results in sampling from the truncated normal distribution obtained by both your procedure 1 and your procedure 2.