# Initialization failed, initial values rejected

Hello all,

I am quite new to Stan and Bayesian Inference in a way so I apologize in advance if my question seems a bit basic. I am trying to create a bayesian hierarchical model with my data in R. Everything was fine until I started using non integer data, so I created a simple test model (see below) as a learning tool. However, I can’t seem to make it sample. I tried to look in other posts but couldn’t find a solution.

When I run my R script, my initial value gets rejected and I get the following errors:

Rejecting initial value:
Log probability evaluates to log(0), i.e. negative infinity.
Stan can’t start sampling from this initial value.

Initialization between (-1, 1) failed after 100 attempts.

data {
int<lower = 1> n;
int<lower = 1> p;
matrix[n, p] X;
real<lower = 0> y[n];
real log_offset;
}
parameters{
vector<lower = 0.1>[p] alpha;
vector<lower = 0.1>[p] beta;
}

model {
alpha ~ normal(2,0.7);
beta ~ normal(5,1);
y ~ beta(X*alpha,X*beta);
}


Can anyone give me an hint on how to solve this?

My guess is that X * alpha and / or X * beta can be negative for some values of alpha and beta, in which case they are not admissible for the beta density.

Doesn’t seem like it… Right now, X is just a 2D matrix and Both alpha and beta can’t be < 0:

X
1 12132.590
1 1431.087
1 4817.301
1 13756.923
1 122476.234
1 42906.550
1 77812.259
1 129201.544
1 25187.523
1 42906.550
1 57166.579

Ah, well then the second column of X needs to be scaled so that the numbers are much smaller. Otherwise, you will have numerical problems. Try dividing by 10000 or so.

Well, I thought of that before but it didn’t help much. I still get the same errors:

X
[,1] [,2]
[1,] 1 1.2132590
[2,] 1 0.1431087
[3,] 1 0.4817301
[4,] 1 1.3756923
[5,] 1 12.2476234
[6,] 1 4.2906550
[7,] 1 7.7812259
[8,] 1 12.9201544
[9,] 1 2.5187523
[10,] 1 4.2906550
[11,] 1 5.7166579

The error message is the same, more specifically:

Rejecting initial value:
Log probability evaluates to log(0), i.e. negative infinity.
Stan can’t start sampling from this initial value.

Initialization between (-1, 1) failed after 100 attempts.
Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
[1] “Error in sampler\$call_sampler(args_list[[i]]) : Initialization failed.”
[3] “In readLines(file, warn = TRUE) :”
[4] " incomplete final line found on ‘C:\Users\Diogo\Documents\Diogo\R data\beta4.stan’"
[1] “error occurred during calling the sampler; sampling not done”

Does it work if you specify init = "0" ?

No, it’s the same. Why would it, though? I thought Stan initialized parameters with random values in the range of [-2;2] if nothing else was specified.

The init = "0" makes the initial values be exactly zero in the unconstrained space. Can you post the output of

stan_rdump(c("y", "x"), file = "")


?

Sure!

y <- c(0.227272727, 0.05, 0, 0.24, 0.065, 0.152542373, 0.056737589, 0.012048193, 0.065217391, 0.025974026, 0.086538462)

X <- structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1.213259, 0.1431087, 0.4817301, 1.3756923, 12.2476234, 4.290655, 7.7812259, 12.92015444, 2.5187523, 4.290655, 5.7166579), .Dim = c(11, 2))

The problem is that your third value of y is literally zero, so the density is either going to be zero or infinity depending on whether the shape parameters are greater than 1.

1 Like

Oh, wow… How did I miss that. Thank you so much! I had been struggling with this for a while, now!
Model ran just fine! I can now use the full extent of my data now.

Thank you so much!