Consider the following Beta Binomial Model
data{
int n;
array[n] int N;
array[n] int y;
}
parameters{
real<lower=0, upper=1> mu;
real<lower=0> kappa;
}
model{
kappa ~ cauchy(0, 1);
mu ~ beta(1, 1);
y ~ beta_binomial(N, mu/kappa, (1-mu)/kappa);
}
generated quantities{
array[n] int ppc = beta_binomial_rng(N, mu/kappa, (1-mu)/kappa);
}
Suppose I had 100 seperate binomial experiments, each of which had the same number of trials. Unbeknownst to me, each trial had an identical probability of success
N <- rpois(100, 50)
y <- rbinom(100, N, 0.1)
Given these data, I know that kappa
should be 0, and so I expect there to be divergences from this model.
library(cmdstanr)
library(cmdstanr)
library(tidyverse)
library(marginaleffects)
library(bayesplot)
library(tidybayes)
N <- rpois(100, 50)
y <- rbinom(100, N, 0.1)
code <- "
data{
int n;
array[n] int N;
array[n] int y;
}
parameters{
real<lower=0, upper=1> mu;
real<lower=0> kappa;
}
model{
kappa ~ cauchy(0, 1);
mu ~ beta(1, 1);
y ~ beta_binomial(N, mu/kappa, (1-mu)/kappa);
}
generated quantities{
array[n] int ppc = beta_binomial_rng(N, mu/kappa, (1-mu)/kappa);
}
"
data <- list(N=N, y=y, n=length(N))
model <- code %>% write_stan_file() %>% cmdstan_model()
fit <- model$sample(
data=data,
parallel_chains = parallel::detectCores(),
seed = 0)
fit$diagnostic_summary()
Warning: 1123 of 4000 (28.0%) transitions ended with a divergence.
See https://mc-stan.org/misc/warnings for details.
Warning: 2877 of 4000 (72.0%) transitions hit the maximum treedepth limit of 10.
See https://mc-stan.org/misc/warnings for details.
$num_divergent
[1] 131 98 710 184
$num_max_treedepth
[1] 869 902 290 816
$ebfmi
[1] 2.009023 1.993664 1.970364 2.044654
Not surprised. However, when I visualize the chains, its clear that some of them get “stuck”
How would I diagnose why these chains get stuck? The beta binomial model above is simple but just an example. I know why the model is pathological, but I might not know why in some other model in which I experience this behaviour.
When I see chains do this, how can I diagnose what is happening and where in my model I should consider reparameterizing?
EDIT: The same behaviour occurs even when I specify the beta binomial parameters as mu*kappa, (1-mu)*kappa