Reputation: 4732
I am learning rstan
and at the moment I'm solving exercises from Gelmans "Bayesian Data Analysis". For reference this is about the example 5 in chapter 3.
It keeps failing with:
Initialization failed after 100 attempts.
Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
error occurred during calling the sampler; sampling not done
this is my R code:
library(rstan)
scode <- "
transformed data {
real o_data[5];
o_data[1] <- 10;
o_data[2] <- 10;
o_data[3] <- 12;
o_data[4] <- 11;
o_data[5] <- 9;
}
parameters {
real mu;
real<lower=0> sigma;
real tru_val[5];
}
model {
mu ~ uniform(0.0,20.0);
sigma ~ gamma(2,1);
for (i in 1:5) {
tru_val[i] ~ normal(mu,sigma);
tru_val[i] ~ uniform(o_data[i]-0.5, o_data[i]+0.5);
}
}
"
afit <- stan(model_code = scode, verbose=TRUE)
The funny thing is - if I change the second tru_val sampling to tru_val[i] ~ normal(o_data[i],0.5);
the model will evaluate just fine.
So far I tried in the stan code:
increment_log_p
statementsI noticed something surprising, as I printed the values of tru_val that - no matter which order the statements - I make it prints values around 0 typically between -2 and +2 - even when I set mu <- 10; sigma <- 1;
(in the data section) and the sampling statement tru_val[i] ~ uniform(9.5,10.5)
. I don't really see how it can get these numbers.
I really hope someone can shine some light onto this.
Upvotes: 3
Views: 3341
Reputation: 203
The constraints of the variable need to match the support of the distribution you're using. For tru_val[i] ~ uniform(9.5, 10.5)
, tru_val
has to be defined as real<lower=9.5,upper=10.5> tru_val[5]
.
In this statement, tru_val[i] ~ normal(mu, sigma)
, Stan is not drawing a sample from a normal distribution and setting it to tru_val[i]
. It is calculating the joint distribution function (in log space); in this case, it's evaluating the normal probability distribution function of tru_val[i]
given mu
and sigma
(in log space).
(The best place to ask questions is on the Stan users mailing list.)
Upvotes: 3