Stan not sampling from lognormal prior

I can sample from some prior distributions using a script such as

parameters{
  real eta_s2;
}
model{
  eta_s2 ~ normal(0,5);
}

However, I cannot sample from a lognormal distribution using a similar script:

parameters{
  real<lower = 0> eta_s2;
}
model{
  eta_s2 ~ lognormal(0,2);
}

Suppose the Stan scripts are called “prior.stan”. Running the second script outputs the following

prior = stan("prior.stan", iter =10000, seed = 1, chains = 1, warmup = 3000, cores = 1)

Warning messages:
1: There were 6982 divergent transitions after warmup. Increasing adapt_delta above 0.8 may help. See
http://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup 
2: There were 18 transitions after warmup that exceeded the maximum treedepth. Increase max_treedepth above 10. See
http://mc-stan.org/misc/warnings.html#maximum-treedepth-exceeded 
3: Examine the pairs() plot to diagnose sampling problems

Inference for Stan model: prior2.
1 chains, each with iter=10000; warmup=3000; thin=1; 
post-warmup draws per chain=7000, total post-warmup draws=7000.

                mean se_mean  sd 2.5% 25%          50%           75%         97.5% n_eff      Rhat
eta_s2 2.983147e+305     NaN Inf    0   0 2.062704e+16 1.527771e+163 5.650556e+294   NaN       NaN

Why does such a simple script result in so many divergent transitions?

I encounter the same problems as you on rstan 2.21.2, but cmdstan 2.26.1 (run via cmdstanr) works fine. Also, this parameterization of the same model in rstan 2.21.2 works fine:

parameters{
  real<lower = 0> eta_s2;
}
transformed parameters{
  real log_eta = log(eta_s2);
}
model{
  log_eta ~ normal(0,2);
  target += -log(eta_s2);
}

Maybe somebody more familiar with the development history of the lognormal distribution in Stan can jump in, but it looks like a better parameterization of the log-normal was included in Stan somewhere between 2.21 and 2.26.

In a more abstract sense, the answer to “why this happens” is because it appears the parameterization of the lognormal used in 2.21 uses a probability density function that requires large stepsizes for adequate exploration in some regions and small stepsizes for adequate exploration elsewhere.

1 Like

Thank you for the reply. This is interesting.

I wonder if some other Stan scripts that have been diverging for me are simply because I am using an older version of RStan or because I am not using cmdstan.

Is RStan 2.21.2 the latest version of RStan?