Warning

Dear all,

I have a warning while executing my stan model with cmdstan, but I cannot get the reason of it.

The model is

///////////////////////// DATA /////////////////////////////////////
data {
int<lower = 0> N; // number of users
int<lower = 0> Q; // number of user covariates
int<lower = 0, upper = 1> Y[N];
matrix[Q,N] X; // design matrix of user covariates
vector[N] f; // design matrix of books
matrix[Q,Q] L0; // empirical variance-covariance matrix of beta
int<lower = 0> L1; // empirical variance of alpha
vector[Q] m0; // mean of beta
int<lower = 0> m1; // mean of alpha
}

//////////////////// PARAMETERS /////////////////////////////////
parameters {
row_vector[Q] beta; // coefficients related to users
real alpha; // coefficients related to books
real mu; // user-level random effect
real<lower = 0> tau;
real<lower = 0> eta;

}

transformed parameters {
// Probability trasformation from linear predictor
real<lower=0> odds[N];
real<lower=0, upper=1> prob[N];

for (i in 1:N)
{
odds[i] = exp(betaX[,i] + alphaf[i] + mu);
prob[i] = odds[i] / (odds[i] + 1.0);
}
}

////////////////// MODEL ////////////////////////
model {

// Likelihood
for (i in 1:N)
{
  Y[i] ~ bernoulli(prob[i])
} 

// Priors

mu ~ normal( 0, sqrt(100));
beta ~ multi_normal( m0, inv_sqrt(tau)*L0);
alpha ~ normal( m1, inv_sqrt(eta)*L1);
tau ~ gamma(0.01, 0.01);
eta ~ gamma(0.01, 0.01);

}

The warning I get is

Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
validate transformed params: prob[k0__] is -nan, but must be greater than or equal to 0
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.

Could you please help me understanding the problem?

Thank you very much!

Ilaria

For some i, odds[i] overflows to infinity and thus prob[i] is infinity over infinity, which evaluates to NaN.

What you should be doing to preserve numerical stability is to eliminate the transformed parameters block entirely and in the model block get rid of that loop and just do

target += bernoulli_logit_lpmf(Y | beta * X + alpha + mu);

It works perfectly, thank you very much!

Ilaria