Rejecting initial value issue

Dear all,

I am writing my first stan program, with Rstan interface. In the model below, I intended to declare d to be a vector of latent (unobserved) Bernoulli variables. But I keep getting this “Rejecting initial value” error from rstan:
"
Chain 1: Rejecting initial value:
Chain 1: Error evaluating the log probability at the initial value.
Chain 1: Exception: bernoulli_lpmf: n is -2147483648, but must be in the interval [0, 1] (in ‘model33787360159d_RE_model’ at line 23)
"
and eventually
"
Chain 1:
Chain 1: Initialization between (-2, 2) failed after 100 attempts.
Chain 1: Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
[1] “Error in sampler$call_sampler(args_list[[i]]) : Initialization failed.”
[1] “error occurred during calling the sampler; sampling not done”
"

data {
  int N;
  int P;
  int X[N,P];
}

parameters {
  vector[P] a1;
  vector[P] a0;
  real b1;
  real b0;
  real<lower=0, upper = 1> prev;
}

model {
  matrix[N, P] p;
  int d[N];
  real z[N];
  matrix[N,P] S;
  matrix[N,P] C;
  for(i in 1:N){
      d[i] ~ bernoulli(prev);
      z[i] ~ std_normal();
    for(j in 1:P){
      S[i, j] = Phi(a1[j] + b1*z[i]);
      C[i, j] = 1 - Phi(a0[j] + b0*z[i]);
      p[i, j] = (S[i, j]^d[i]) * (1 - C[i, j])^(1 - d[i]);
      X[i, j] ~ binomial(1, p[i, j]);
    }
  }
}

The dataset I used for now is just a arbitrarily simulated dataset using:
N = 500
P = 5
X = matrix(rbinom(N*P, 1, 0.3), nrow = 500, ncol = 5)

Could anyone point out what went wrong in the model?

Thank you,
Yang

This indicates overflow of 32bit integer. Can you recompile the model and try it again with initial parameters specified?

Stan does not allow integer parameters, which is the primary issue with your model. You’ll want to search for how to marginalize out the integer parameters, though I don’t know how that’s done for a binomial model.

Also keep in mind that if you want Stan to sample a parameter then it has to be in the parameters block. You can declare integers in the model block, but they are not treated as parameters. If you move your declaration of ‘d’ into the parameters block, you should get an error saying integers are not allowed.

2 Likes

@simonbrauer is right and I completely missed the int in model block. I wonder if the current version still emits an overflow error instead of correct msg. @rok_cesnovar

Thank you so so much for your comments. Actually, I specified d to be a binary random variable that accounts for the latent status for each individual, but d itself is not of my interest and this is why I didn’t include it in the parameter block.

The current RStan version definitely still emits an overflow error.

RStan is either at 2.21 if the user install from CRAN, or at 2.26 if they installed rstan from the Stan r-packages repository.