Negative value from exponential distribution

The fix is to declare constraints:

parameters {
  vector[N] eta;
  vector<lower=0>[N] tau;
  real<lower=0> delta;
}

Explanation:

The model block defines a probability density function, not a sampling procedure.
For examples, when you write

data {
  real x;
} parameters {
  real mu;
} model {
  x ~ normal(mu, 1);
}

it wouldn’t make sense to sample x which has a known value. Instead the “distribution statement” only calculates the probability of observing the particular values of data and parameters. All parameters have been assigned their values before the model block runs.

The parameter values come from the HMC algorithm. For every sample, HMC guesses values for the parameters, runs the model block to evaluate the probability of that guess, then produces a new guess based on what it learned, evaluates that guess, and so on until it decides to accept one of these candidates.
(The number of candidates evaluated for each accepted sample is recorded as n_leapfrog__ column in Stan’s output.)

HMC looks only at the parameters block when creating the initial guess. If you don’t declare lower bound constraints there, some parameters may be assigned negative values, and the probability evaluates to zero. HMC cannot learn from this sort of “impossible” candidate value.

5 Likes