Rejection Initial value

Hi Everyone,

I am new to Stan. I am trying to run my code in Matlab but every time I run it, I face this error “Rejecting initial value:
Error evaluating the log probability at the initial value.” I put part of my code here. Any suggestion? Thank you in advance.

evol_code = {
‘data {’
’ int<lower=0> T; ‘
’ int<lower=0> N; ‘
’ real<lower=0,upper=1> Fhat[N,T]; ‘
’ int<lower=0,upper=1> Tr[N,N]; ‘
’ int<lower=0,upper=1> U[N,N]; ‘
’}’
‘parameters {’
’ real mu[N]; ‘
’ real<lower=0> delta_tau[N]; ‘
’}’
‘transformed parameters {’
’ real<lower=0> tau[N];’
’ real<lower=0> M[N,T];’
’ real<lower=0,upper=1> F[N,T];’
’ real<lower=0> den;’
’ for (n in 1:N){’
’ tau[n] = 0; ‘
’ for (s in 1:N)’
’ tau[n] = tau[n] + U[s,n]delta_tau[s];’
’ }’
‘for(i in 1:T){’
’ den = 0.01;
’ for (n in 1:N){’
’ if (tau[n] > i - 1){’
’ M[n,i] = 0.01;’
’ }else{’
’ M[n,i] = exp(mu[n]
(i-1 - tau[n]));’
’ }’
’ den = den + M[n,i];’
’ }’
’ for (n in 1:N){’
’ M[n,i] = M[n,i]/ den;’
’ }’
’}’
‘for (i in 1:T){’
‘for (n in 1:N){’
‘F[n,i]=0;’
‘for (k in 1:N){’
’ F[n,i]= F[n,i] + U[i,k]*M[k,i];’
’}’
’}’
’}’
’}’
‘model {’
’ for(n in 1:N) {’
’ mu[n] ~ normal(0, 0.1);’
’ delta_tau[n] ~ exponential(2);’
’ for (i in 1:T){’
’ Fhat[n,i] ~ normal(F[n,i],0.01);’
’ }’
’ }’
’}’
};
evol_dat = struct(‘T’,11,‘N’,6,…
‘Fhat’,Fdata,…
‘Tr’,Trdata,‘U’,Udata);
model = StanModel(‘verbose’,true,‘model_code’,evol_code,‘data’,evol_dat);
model.compile();

fit_vb = model.vb();

print(fit_vb);
‘Tr’,Trdata,‘U’,Udata);

Hi guys,

I tried to make my code as simple as possible. But still I have the same error. Can you please help me with that? Many thanks. I put the simple version of my code here as well.

evol_code = {
‘data {’
’ int<lower=0> T; ‘
’ int<lower=0> N; ‘
’ real<lower=0,upper=1> Fhat[N,T]; ‘
’ int<lower=0,upper=1> U[N,N]; ‘
’}’
‘parameters {’
’ simplex[N] M[T]; ‘
’}’
‘transformed parameters {’
’ real<lower=0,upper=1> F[N,T]; ‘
’ for (i in 1:T){’
’ for (n in 1:N){’
’ F[n,i]=0.0;’
’ for (k in 1:N){’
’ F[n,i]= F[n,i] + U[i,k]*M[i][k];’
’ }’
’ }’
’ }’
’}’
‘model {’
‘for (i in 1:T){’
’ M[i] ~ dirichlet([0.5, 0.5, 0.5, 0.5, 0.5, 0.5]’’);’
’}’
’ for(n in 1:N) {’
’ for (i in 1:T){’
’ Fhat[n,i] ~ normal(F[n,i],0.01);’
’ }’
’ }’
’}’
};

%%
model = StanModel(‘verbose’,true,‘model_code’,evol_code,‘data’,evol_dat);
model.compile();

fit_vb = model.vb();

print(fit_vb);

Usually what those initial value rejected errors mean is something has gone out of bounds or a function is being called with invalid inputs. I think 2.15 versions of Stan haven’t been doing so well making these errors clear, I think 2.16 will improve that.

But first off, it’s worth keeping your Stan models in separate files. Stan models in strings in code just end up being hard to debug.

So this is just a copy of the second model you gave above:

data {
  int T;
  int N;
  real Fhat[N,T];
  int U[N,N];
}

parameters {
  simplex[N] M[T];
}

transformed parameters {
  real F[N,T];
  for (i in 1:T){
    for (n in 1:N){
      F[n,i]=0.0;
      for (k in 1:N){
        F[n,i]= F[n,i] + U[i,k] * M[i][k];
      }
    }
  }
}

model {
  for (i in 1:T){
    M[i] ~ dirichlet([0.5, 0.5, 0.5, 0.5, 0.5, 0.5]);
  }
  
  for(n in 1:N) {
    for (i in 1:T){
      Fhat[n,i] ~ normal(F[n,i], 0.01);
    }
  }
}

One issue I see is that in this loop:

 for (i in 1:T){
    for (n in 1:N){
      F[n,i]=0.0;
      for (k in 1:N){
        F[n,i]= F[n,i] + U[i,k] * M[i][k];
      }
    }
  }

The i index goes from 1 to T, but U is defined as an NxN matrix. That will definitely cause problems.

Also, (and these are probably not the things stopping your model from initializing), but:

You can problem replace the line:
dirichlet([0.5, 0.5, 0.5, 0.5, 0.5, 0.5]);

with:

dirichlet(rep_vector(0.5, N));

And Fhat[n,i] ~ normal(F[n,i], 0.01); seems suspicious. It’s probably better to just estimate this parameter online instead of fixing it, especially to such a small value. What’s the reason for making it a really small value?

Hope this helps!

Thank you for your help.

Were you able to get the model to run?

Yes, Thank you.