Rejecting initial value: Log probability evaluates to log(0), i.e. negative infinity

My stan model is as following:

data {
int<lower=1> n;
int<lower=1> J;
int<lower=1> m[J];
int<lower=1> maxm;
int<lower=1> D;
int<lower=0, upper=1> Q[J, D];
int<lower=1> sumQ;
int<lower=1> n_obs;
int<lower=1> ii[n_obs];
int<lower=1> jj[n_obs];
int<lower=1, upper=maxm> Y_obs[n_obs];
}

parameters {
real<lower=0> a[J];
ordered[maxm-1] b[J];
vector[n] theta[D];
real epsilon[n];
real<lower=0> lambda[D];
}

transformed parameters {
simplex[maxm] prob[n_obs];
real summatory;
real Pmais[n_obs, maxm];
for (h in 1:n_obs) {
summatory ← 0;
for (d in 1:D)
summatory ← Q[jj[h], d] ? summatory + lambda[d] * theta[d, ii[h]] : summatory;
for (k in 1:(m[jj[h]]-1))
Pmais[h, k] ← inv_logit(b[jj[h], k] - a[jj[h]] * (summatory + epsilon[ii[h]]));
Pmais[h, m[jj[h]]] ← 1;
prob[h, 1] ← Pmais[h, 1];
for (k in 2:m[jj[h]])
prob[h, k] ← Pmais[h, k] - Pmais[h, k-1];
}
}

model {
epsilon ~ normal(0, 1);
for (j in 1:J) {
a[j] ~ normal(1, 2.5) T[0,];
b[j] ~ normal(0, 2.5);
}
for (d in 1:D) {
theta[d] ~ normal(0, 1);
lambda[d] ~ normal(1, 1) T[0,];
}
for (h in 1:n_obs)
Y_obs[h] ~ categorical(prob[h, 1:m[jj[h]]]);
}

generated quantities {
real dev; //deviance
vector[n_obs] log_lik;
dev ← 0;
for (h in 1:n_obs) {
log_lik[h] ← categorical_log(Y_obs[h], prob[h, 1:m[jj[h]]]);
dev ← dev + (-2) * log_lik[h];
}
}

The data is simulated. After I run the stan model, it often shows the problem:
Rejecting initial value:
Log probability evaluates to log(0), i.e. negative infinity.
Stan can’t start sampling from this initial value.

Nothing obviously wrong there but inv_logit() could cause rounding errors. It’s better to write this kind of model using the ordered logistic distribution.

model {
  ...
  for (h in 1 : n_obs) {
    real summatory = 0;
    for (d in 1 : D) {
      if (Q[jj[h], d])
        summatory += lambda[d] * theta[d, ii[h]];
    }
    Y_obs[h] ~ ordered_logistic(a[jj[h]] * (summatory + epsilon[ii[h]]),
                                b[jj[h], 1 : m[jj[h]]]);
  }
}

I didn’t know the ordered logistic function in Stan. It’s working! Thanks a million!