I wouldnāt say it occurs frequently, but it does occur more than a few times especially between 10%- 20% sampling. A lot of the proposed values gets rejected. I am using the message from the sampler to judge that.
Also, when I call the function within the generated quantities block, it returns the following error
Error in function boost::math::gamma_p_inv<double>(double, double): Overflow Error in line 30, column 4 to column 42
and the posterior means of the variable is always NaN
. You can try the following stan code
functions {
real gamma_icdf(real x, real alpha, real beta);
}
data {
int<lower = 1> N;
int<lower = 1> K;
vector[N] x;
vector[K] p;
real q;
}
parameters {
real<lower = 0> alpha;
real<lower = 0> beta;
}
transformed parameters{
real b;
b=gamma_icdf(q, alpha, beta);
}
model {
alpha ~ normal(10, 10);
beta ~ normal(5, 5);
x ~ gamma(alpha, beta);
}
generated quantities {
vector[K] q2;
for(k in 1:K) {
q2[k] = gamma_icdf(p[k], alpha, beta);
}
}
and I called it using the following R codes
prep_hpp("gamma_model2.stan",
file.path(getwd(),"gamma_icdf.hpp"))
probs <- c(0, 0.001, 0.01, 0.025, 0.05, 0.25, 0.5, 0.75, 0.95, 0.975, 0.99, 0.999, 1)
mod <- cmdstan_model("gamma_model2.stan",
include_paths = utils::shortPathName(getwd()),
cpp_options = list(USER_HEADER =
file.path(utils::shortPathName(getwd()),
"gamma_icdf.hpp")),
stanc_options = list("allow-undefined"))
x <- rgamma(100, shape = 10, rate = 5)
fit <- mod$sample(
data = list(
N = length(x),
K = length(probs),
p = probs,
x = x,
q = 0.5
),
seed = 123,
chains = 3,
parallel_chains = 3,
refresh = 500
)
All the values eturned for q2 are NaNs.