I am trying to fit a betabinomial model in cmdstan and am finding problems relative to rstan, which works well.
Here is the model, which i store in a file called simple_betabin.stan
:
data{
int W;
int F[W];
int R[W];
}
parameters{
real<lower=0,upper=1> a;
real<lower=0> theta;
}
model{
vector[W] pbar;
theta ~ exponential( 3 );
a ~ beta( 3 , 7 );
R ~ beta_binomial(F , a * theta, (1 - a) * theta );
}
generated quantities{
vector[W] log_lik;
vector[W] y_hat;
for ( i in 1:W ) {
log_lik[i] = beta_binomial_lpmf( R[i] | F[i] , a * theta, (1 - a) * theta );
y_hat[i] = beta_binomial_rng(F[i] , a * theta, (1 - a) * theta );
}
}
If I fit this model in cmdstan, it fits very slowly and also gives a very poor result: Rhat of 2.2, ESS of 2.5
On the other hand, fitting in Rstan is faster and works better, with a perfect Rhat and plenty of effective samples.
library(cmdstanr)
mod <- cmdstan_model(stan_file = "code/simple_betabin.stan")
## generate data
probs <- rbeta(300, 0.3 * 40, 0.7 * 40)
trials <- rep(450, 300)
res <- rbinom(300, trials, probs)
d <- list(W = 300, F = trials, R = res)
fit <- mod$sample(
data = d,
seed = 123,
num_chains = 2,
num_cores = 2
)
fit$summary()
## compare rstan
m <- rstan::stan_model(file = "code/simple_betabin.stan")
s <- rstan::sampling(m, data = d, iter = 1000, chains = 2, cores = 2)
- Operating System: MacOS X Mojave 10.14.6
- CmdStan Version: 0.0.0.9
- Compiler/Toolkit:
- rstan version: 2.19.2