Can I express that the lower bound on a parameter should be greater than zero?

If not, is there a best practice workaround?

There are many models that require parameters greater than zero, this must be a common problem?

Here is an example where I want to do predictive checks for a gamma regression.

parameters {
  real x;
  real<lower=0> shape;
  real<lower=0> scale;

model {
  shape ~ normal(10,2.5);
  scale ~ normal(1,1);

  x ~ gamma(shape,scale);

I can only express that shape and scale have a lower bound of 0, but I would like to say

  real<lower>0> scale;

i.e., the bound of shape and scale should not include zero.

Good question. The math is easy. With a lower bound constraint in Stan,

parameters {
  real<lower=0> scale;

we take an unconstrained parameter in \mathbb{R} then apply \exp() to it. The result is always in (0, \infty) mathematically (exponentiation of finite value is strictly positive).

The problem is floating point arithmetic. With double precision (64 bits) as Stan uses, the exp(x) operation will underflow to 0 when x is below -500 or so. If you then try to use that scale in something like

y ~ normal(0, scale);

then Stan will report a rejection because the normal requires scale > 0. So you won’t wind up getting sampled values where scale is 0 if that’s what you were worried about.

You can even be very explicit about it if you want and put this in the model:

if (scale == 0) reject("scale cannot be 0");

If this happens a lot in your fits, then you may want to think about more informative priors.


Many thanks @Bob_Carpenter!

(I just found this answer written but not submitted :( )

So if I understand you correctly, there is no way then to get scale to be strictly positive but if I understand you correctly, it is not a problem in practice because should the sampler (by an unlikely coincidence) draw exactly zero for the scale parameter it will just throw that away with a rejection and continue.

Thanks for the question, @Leif_Jonsson, I was curious as well.

If I may share my understanding of Bob’s reply,

  1. theoretically <lower = 0> gives a strict positive value,
  2. in practice, exp(-500) may underflow to scale 0,
  3. which is fortunately rejected by Stan which requires scale parameter of normal distribution to be positive.

Sorry to be picky, but I thought if lower bound was not for scale parameter (e.g. rare disease prevalence), we won’t have a safety net in 3, ending up with zero value for known-positive.