Constraint checking and resulting distribution (truncated normal)

Hi Stan Experts,

I have a question regarding when constraints are checked and the resulting distribution. Looking at section 5.6 in the Stan Reference Manual did not quite answer my question and I’m not too familiar with the inner workings of Stan, so any insight will be greatly appreciated.

Here is a snippet of my Stan code:

parameters {
	real<lower=0> alpha;

model {
  alpha ~ normal(1, 5);

Alpha is drawn from a normal distribution and is constrained / truncated with real<lower=0>. I would like to figure out at what point the constraints are checked and how this influences the resulting distribution. (I am trying to replicate / simulate this distribution in python in the same way that Stan does).

  1. Alpha is drawn from the normal(1, 5). Here, with no constraints, it is possible to have a sample < 0. Is this sample discarded immediately? This would continue until a value that satisfies the constraint is found. With repeated samples, I believe this would be equivalent to sampling from a normal distribution and subsetting the samples to be > 0.

  2. Alpha is drawn from a truncatedNormal(1, 5) where the truncation is at 0. (Which in this case could be substituted with halfNormal since the truncation is at 0).

It is my understanding (from quick simulations) that options 1 and 2 would produce different distributions. Can anyone give any insight about how Stan deals with constraints with regard to sampling from distributions when they are specified according to the code above (parameter & model blocks)?

Thanks in advance for your insight!

1 Like

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.