Warnings in cmdstan but not in rstan

Hi,
I am trying to fit this model with stan. It works quite well with rstan, however, I need to run it on my school’s super computer where I am not able to install dependencies for rstan so I turn to cmdstan. But for some reason, with cmdstan, I kept receiving this warning:

Exception: bernoulli_lpmf: Probability parameter is nan, but must be in the interval [0, 1] (in ‘/tmp/RtmpdOSLHP/model-617c1552e3a4.stan’, line 107, column 8 to column 76)

I have no idea why the same model with same data cannot work smoothly on cmdstan. Should I change the specification of my model?

functions{
  vector predictors_rng(int N, real mu, real sigma) {
  vector[N] x;
  for (i in 1:N) {
      x[i] = normal_rng(mu, sigma);
  }
  return x;
}
}
data{
  int N;
  int Choice[905];
  int customer_num[N];//number of readers of each book
  int chap_num[N];//number of time intervals of each book
  vector[905] signal;
  vector[905] price;
  vector[905] vote;
  vector[905] gender;
  vector[905] register_age;
  vector[905] bul_count;
  vector[905] cost;
  real varq0;
  real Q;
}
transformed data{
  vector[905] A;
  int dim[N];
  A=predictors_rng(905,0,1);
  for(i in 1:N){
    dim[i]=customer_num[i]*chap_num[i];
  }
}
parameters{
  real<lower=0,upper=5> q0; //prior mean
  real<lower=0> varad; //var of ad(signal)
  real a;      //quality sensitivity
  real b;    //price sensitivity
  real c; //vote
  real d; //gender
  real e; //register
  real f; //bullet
  real g; //cost
}
model{
  vector[905] B=A*sqrt(varad)+Q;
  for(i in 1:N){
    matrix[customer_num[i],chap_num[i]] A_t;
    matrix[customer_num[i],chap_num[i]] signal_t;
    matrix[customer_num[i],chap_num[i]] q;//quality
    matrix[customer_num[i],chap_num[i]] q_sd;//quality sd
    matrix[customer_num[i],chap_num[i]] U;
    matrix[customer_num[i],chap_num[i]] price_t;
    matrix[customer_num[i],chap_num[i]] vote_t;
    matrix[customer_num[i],chap_num[i]] gender_t;
    matrix[customer_num[i],chap_num[i]] register_age_t;
    matrix[customer_num[i],chap_num[i]] bul_count_t;
    matrix[customer_num[i],chap_num[i]] cost_t;
    matrix[customer_num[i],chap_num[i]] Pr;
    matrix[customer_num[i],chap_num[i]] Choice_t;
    if(i==1){
      A_t=to_matrix(B[1:customer_num[i]*chap_num[i]],customer_num[i],chap_num[i]);
      signal_t=to_matrix(signal[1:customer_num[i]*chap_num[i]],customer_num[i],chap_num[i]);
      price_t=to_matrix(price[1:customer_num[i]*chap_num[i]],customer_num[i],chap_num[i]);
      vote_t=to_matrix(vote[1:customer_num[i]*chap_num[i]],customer_num[i],chap_num[i]);
      gender_t=to_matrix(gender[1:customer_num[i]*chap_num[i]],customer_num[i],chap_num[i]);
      register_age_t=to_matrix(register_age[1:customer_num[i]*chap_num[i]],customer_num[i],chap_num[i]);
      bul_count_t=to_matrix(bul_count[1:customer_num[i]*chap_num[i]],customer_num[i],chap_num[i]);
      cost_t=to_matrix(cost[1:customer_num[i]*chap_num[i]],customer_num[i],chap_num[i]);
      Choice_t=to_matrix(Choice[1:customer_num[i]*chap_num[i]],customer_num[i],chap_num[i]);

    }
    else{
      A_t=to_matrix(B[(1+sum(dim[1:(i-1)])):sum(dim[1:i])],customer_num[i],chap_num[i]);
      signal_t=to_matrix(signal[(1+sum(dim[1:(i-1)])):sum(dim[1:i])],customer_num[i],chap_num[i]);
      price_t=to_matrix(price[(1+sum(dim[1:(i-1)])):sum(dim[1:i])],customer_num[i],chap_num[i]);
      vote_t=to_matrix(vote[(1+sum(dim[1:(i-1)])):sum(dim[1:i])],customer_num[i],chap_num[i]);
      gender_t=to_matrix(gender[(1+sum(dim[1:(i-1)])):sum(dim[1:i])],customer_num[i],chap_num[i]);
      register_age_t=to_matrix(register_age[(1+sum(dim[1:(i-1)])):sum(dim[1:i])],customer_num[i],chap_num[i]);
      bul_count_t=to_matrix(bul_count[(1+sum(dim[1:(i-1)])):sum(dim[1:i])],customer_num[i],chap_num[i]);
      cost_t=to_matrix(cost[(1+sum(dim[1:(i-1)])):sum(dim[1:i])],customer_num[i],chap_num[i]);
      Choice_t=to_matrix(Choice[(1+sum(dim[1:(i-1)])):sum(dim[1:i])],customer_num[i],chap_num[i]);
    }
    A_t .*= signal_t;
    for(j in 1:customer_num[i]){
      q[j,1]=q0;
      q_sd[j,1]=varq0;
    }
    if(chap_num[i]>1){
     for (t in 2:chap_num[i]) {
          q[,t]=(1-signal_t[,t-1]).*q[,t-1]+signal_t[,t-1] .* (q[,t-1] ./ q_sd[,t-1] + A_t[,t-1]/varad) ./ (1 ./ q_sd[,t-1]+1/varad);
          
          q_sd[,t]=(1-signal_t[,t-1]) .* q_sd[,t-1]+signal_t[,t-1] .* (1 ./ (1 ./ q_sd[,t-1]+1/varad));
          
        }   
    }
    //utility
    U=a*q + b*price_t +c*vote_t+d*gender_t+e*log10(1+register_age_t)+f*log10(1+bul_count_t)+g*cost_t;
    Pr=inv_logit(U);
    for(j in 1:customer_num[i]){
      for(t in 1:chap_num[i]){
        if(t>=2){
          //print(i);
          //print(Choice_t);
        if(Choice_t[j,t-1]==0 && Choice_t[j,t]==0){
          continue;
        }}
        Choice[sum(dim[1:(i-1)])+(j-1)*chap_num[i]+t] ~ bernoulli (Pr[j,t]);
      }
    }
}
}

Hmm I don’t know why cmdstanr is giving warnings and not rstan (probably a version thing, cmdstanr is a bit ahead of rstan now).

Use bernoulli_logit instead of bernoulli though and it’ll probably go away. bernoulli_logit is more numerically stable in any case.

bernoulli_logit(theta) is equivalent to bernoulli(inv_logit(theta))

If you need to generate the probabilities, still use bernoulli_logit and then generate them separately.

1 Like

Thanks for the advice! Unfortunately that didn’t solve my problem but I managed too find an Rstan version which doesn’t need libv8.