Hi,
I’m trying to use the gamma_rng in the simplest possible way but I don’t understand why the (fake) responses have the same sd and in overall their distribution is very similar.
I tried to replicate the work by using R but the distributions are more variable.
My question is:
Why the gamma_rng returns so consistent distributions? I would expect something (more variable) closer to what R code results.
Here is the Stan code
N = 100
d_sim <- list(N = N)
pm <- stan_model(
model_code = "
data {
int<lower=0> N;
}
generated quantities {
real mu; // conditional mean of the dependent variable
real rate; //beta parameter for the gamma distribution
real<lower=0> inv_phi;
mu = normal_rng(0.5, 0.05);
// mu = normal_rng(0.5, 0.005); UPDATE by mistake I added one more decimal.
inv_phi = normal_rng(20, 2);
// The response
vector<lower=0>[N] value;
// Simulate data from observational model
for(n in 1:N){
rate = inv_phi/mu; // Rate
// The likelihood p(value|shape, rate)
value[n] = gamma_rng(inv_phi, rate);
}
}")
pfit <- sampling(pm,
data = d_sim,
iter=2000, warmup= 0,
chains=1,
cores = 1,
seed = 1,
algorithm="Fixed_param"
)
I don’t see why I get sd = 0.13 for all the values
Then, generated responses have consistent distributions
Example in R
I tried to replicate this example by using R
inv_ph <- 20;
mu <- 0.5;
alpha <- inv_ph
beta <- inv_ph/mu
curve(dgamma(x, shape = alpha, rate = beta),
xlim = c(0, 2), ylim = c(0,4))
for(i in 1:20){
inv_ph <- rtnorm(1, 20, 2, lower = 0);
mu <- rnorm(1, 0.5, .05);
alpha <- inv_ph
beta <- inv_ph/mu
dat <- rgamma(1e5, shape = alpha, rate = beta)
curve(dgamma(x, shape = alpha, rate = beta),
xlim = c(0, 2), add = T, col = 'red')
}
Which result a different outcome compared to rstan