I am quite new to the Stan. I am trying to use the following Stan code to simulate the distribution of some sentence length and word place for each sentence in some corpus.
functions {
real genpoiss_truncated_lpmf(int y, real theta, real lambda, int truncation) {
if (y > truncation) {
return -1000; // Probability is zero outside the truncation
}
if ((theta * pow(theta + lambda * y, y-1) * exp(-theta - lambda * y)) / tgamma(y + 1) == 0) {
return -1000;
}
return log((theta * pow(theta + lambda * y, y-1) * exp(-theta - lambda * y)) / tgamma(y + 1));
}
}
data {
int<lower=1> N; // total number of observations
int place[N];
int unit_length[N];
}
transformed data {
int back_place[N];
for (i in 1:N) {
back_place[i] = unit_length[i] - place[i];
}
}
parameters {
real<lower = 0> theta1;
real<lower = -1, upper = 1> lambda1;
real<lower = 0> theta2;
real<lower = -1, upper = 1> lambda2;
real<lower = 0> mu1;
real<lower = 0> phi1;
real<lower = 0, upper = 1> psi;
}
transformed parameters {
real lprior = 0;
lprior += gamma_lpdf(theta1 | 2, 0.5);
lprior += gamma_lpdf(lambda1 | 1, 1);
lprior += gamma_lpdf(theta2 | 2, 0.5);
lprior += gamma_lpdf(lambda2 | 1, 1);
lprior += gamma_lpdf(mu1 | 1, 1);
lprior += gamma_lpdf(phi1 | 1, 1);
}
model {
target += lprior;
for (i in 1:N) {
target += log_sum_exp(log(psi) + genpoiss_truncated_lpmf(place[i] | theta1, lambda1, unit_length[i])
+ neg_binomial_2_lpmf(unit_length[i] | mu1, phi1),
log1m(psi) + genpoiss_truncated_lpmf(back_place[i] | theta2, lambda2, unit_length[i])
+ neg_binomial_2_lpmf(unit_length[i] | mu1, phi1)); //change to the same negative binomial
}
}
generated quantities {
real log_lik[N];
for (i in 1:N){
log_lik[i] = psi*(neg_binomial_2_lpmf(unit_length[i] | mu1, phi1)
+ genpoiss_truncated_lpmf(place[i] | theta1, lambda1, unit_length[i]));
log_lik[i] += (1-psi)*(neg_binomial_2_lpmf(unit_length[i] | mu1, phi1)
+ genpoiss_truncated_lpmf(back_place[i] | theta2, lambda2, unit_length[i]));
}
}
But it gives the following error like this:
Chain 1: Rejecting initial value:
Chain 1: Log probability evaluates to log(0), i.e. negative infinity.
Chain 1: Stan can't start sampling from this initial value.
Warning: There were 3445 divergent transitions after warmup. See
https://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
to find out why this is a problem and how to eliminate them.Warning: Examine the pairs() plot to diagnose sampling problems
Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
Running the chains for more iterations may help. See
https://mc-stan.org/misc/warnings.html#tail-ess
I would be really grateful if someone can help with this. Thank you.