I am trying to fit the following Bayesian model in Stan using ADVI:
\mathbf{Y}_{ij} \sim \mbox{Dirichlet}(\alpha_i \mathbf{1}),
\alpha_i \sim \mbox{Gamma}(1, 1),
where \mathbf{Y}_{ij} is a 5-dim vector for i = 1, 2, 3 and j = 1, \dots, 20. When \alpha degenerates to a scalar, the model works fine. However, whenever I want different \alpha values for the Dirichlet distribution, I keep getting the following error:
Error in sampler$call_sampler(c(args, dotlist)) :
Exception: Error in function boost::math::lgamma(double): numeric overflow (in ‘model440468d045de_test_test’ at line 21)
A few notes:
-
My actual model is more complicated than this, but it got stuck at this Dirichlet distribution thing.
-
It seems that Stan does not allow
vector[] ~ Dirichlet(vector[]).
Therefore whenever I am varying the Dirichlet parameters I receive the above error message. How can I get around this issue? I really want to vary my Dirichlet parameters. -
Another issue, which is more related to the vb function, is that I realize that sometimes a model that works perfectly well crashes when I increase the number of iterations to adapt the stepsize (adapt_iter). This doesn’t make sense to me, since I thought more iterations to adapt the stepsize should yield better (more stable) result. Also, if whether a model works or not depends on the number of iterations I specified, then how do I run simulation studies? (I understand that vb is still an experimental algorithm, but I really hope I can use it for my simulations.)
Any comments are appreciated.
Here is my Stan model:
data {
int<lower=0> N; //# of observations = 60
int<lower=0> K; //Dimension of Y = 5
int<lower=0> I; //# of Groups = 3
int<lower = 0, upper = I> x[N];
simplex[K] y[N];
}
parameters {
real<lower = 0> alpha[I];
}
transformed parameters {
vector<lower = 0>[K] dirch_temp[N];
for (n in 1:N)
dirch_temp[n] = rep_vector(alpha[x[n]], K);
}
model {
alpha ~ gamma(1, 1);
for (n in 1:N) {
y[n] ~ dirichlet(dirch_temp[n]);
}
}
And here is my R code:
y = matrix(NA, nrow = 60, ncol = 5)
for (n in 1:20){
y[n, ] = rgamma(5, 1, 1)
y[n, ] = y[n, ] / sum(y[n, ])
}
for (n in 21:40){
y[n, ] = rgamma(5, 1.1, 1)
y[n, ] = y[n, ] / sum(y[n, ])
}
for (n in 41:60){
y[n, ] = rgamma(5, .9, 1)
y[n, ] = y[n, ] / sum(y[n, ])
}
test_data <- list(N=60,
K = 5,
I = 3,
x = c(rep(1,20), rep(2,20), rep(3, 20)),
y = y)
test_model = stan_model(file = 'nmf_test_temp.stan')
nmf_vb = vb(test_model,
data = test_data,
seed = 235432)