# Bayesian Estimation Kirmann Model

**URL:** <https://discourse.mc-stan.org/t/bayesian-estimation-kirmann-model/6899>\
**Category:** Modeling\
**Created:** [December 9, 2018, 12:23am UTC](https://discourse.mc-stan.org/t/bayesian-estimation-kirmann-model/6899 "2018-12-09T00:23:03Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![lordp](https://avatars.discourse-cdn.com/v4/letter/l/c67d28/32.png) [@lordp](https://discourse.mc-stan.org/u/lordp)\
**Post date:** [December 9, 2018, 12:23am UTC](https://discourse.mc-stan.org/t/bayesian-estimation-kirmann-model/6899/1 "2018-12-09T00:23:04Z")

</div>

Hello Guys!

I am studying engineering in a France (maybe you will forgive my english!). I have a project where i had to use stan to implement a model called Kirmann Model. I can explain it if it is necessary. I made a code to do it but i don’t understand why it isn’t working. It would be great if someone has experienced this kind of result.

Thank you!!  
(here is the code)

```stan
model_string <- "
functions{
int arrondi(real x){
  int k;
  while (k<x){
    k=k+1;
  }
  k=k-1;
  if (x-k>0.5){
    k=k+1;
  }
  return k;
}
}
data{
  int iteration;
  real N;
  real x[iteration + 2];
  
}

transformed data{

  
}

parameters{
  real<lower=0,upper=1> alpha;
  real<lower=0,upper=1> beta;
  real v;
  real gg;
}

transformed parameters{
  real k[iteration];
  real evolution[iteration-1];
  real plus2[iteration -1];
  real p1[iteration-1];
  real p2[iteration-1];
  real c[iteration-1];
  vector[3] g[iteration-1];
  for (i in 3:iteration){
    k[i]=N*((1+gg)*x[i-1]-x[i]-gg*x[i-2])/((v+gg)*x[i-1]-gg*x[i-2]);
  }
  for (i in 1:(iteration-1)){
    evolution[i]=k[i+1]-k[i];
    plus2[i]=evolution[i]+2;
}
  for (i in 1:(iteration-1)){
    p2[i]=((k[i]/N)*((N-k[i]-1)/(N-1)))*alpha+((N-k[i])/N)*beta;
    p1[i]=(k[i]/(N-1))*((N-k[i])/(N))*alpha+((k[i])/N)*beta;
    c[i]=1-p1[i]-p2[i];
    g[i][1]=p1[i];
    g[i][2]=c[i];
    g[i][3]=p2[i];
    
  }

}

model{
  alpha ~ uniform(0,0.8);
  beta ~ uniform(0,0.19);
  v ~ uniform(0.1,0.8);
  gg ~ uniform (0.4,5);

  for (i in 1:(iteration-1)){
    target += categorical_lpmf(arrondi(plus2[i])|(g[i]));
  }
}

" 

data_list <- list(iteration=96, N=100, x=c(0.37249999999999994,0.21503749999999988,0.08304631249999986,-0.003710859375000125,-0.046161730468750085,-0.05584673339843754,-0.04672755151367189,-0.0306285186315918,-0.014914024796990968,-0.0032456968660817577,0.0033998913158932867,0.00578925723950827,0.005430946000571311,0.00384691064573303,0.0020863769075200674,0.0006988627172226278,-0.00014884840059802647,-0.000507827799815257,-0.0005358448175050636,-0.00040405454491600744,-0.00023503866239640912,-9.331595207714952e-05,-1.0269035838460515e-06,4.262107865772578e-05,5.184104446696721e-05,4.278795649399163e-05,2.719419667578533e-05,1.265866744214526e-05,2.4724218301659355e-06,-2.9703053924583196e-06,-4.810535057278799e-06,-4.443106181500141e-06,-3.1115327139363485e-06,-1.661137014988234e-06,-5.454479994252784e-07,1.066102574200033e-07,3.636650183543263e-07,3.7055403532510217e-07,2.679084125548817e-07,1.4741689718554556e-07,5.359172987885054e-08,-2.697028102173412e-09,-2.613254102489429e-08,-2.8397019555217715e-08,-2.1112964866940412e-08,-1.1963626363903152e-08,-4.651452547095586e-09,-2.3141830537868805e-10,1.7351506355925047e-09,2.086257349066254e-09,1.6287075374960145e-09,9.642114307626695e-10,4.073256549270107e-10,5.884141863092245e-11,-1.0139533674659585e-10,-1.3929012634864073e-10,-1.1588719986815743e-10,-7.279608951912474e-11,-3.352002655609014e-11,-7.07811191349957e-12,6.309173278161753e-12,1.0267591526300023e-11,9.043447788003972e-12,5.939683360955538e-12,2.9135941269180887e-12,8.13387273521899e-13,-2.8764678205949897e-13,-6.5421494414032e-13,-6.18451238413572e-13,-4.240796228684018e-13,-2.194604537075937e-13,-7.082576050792229e-14,1.1008063053780066e-14,4.218593066409883e-14,4.252400683842038e-14,3.0118036052558213e-14,1.6173297867254218e-14,5.684832340439588e-15,-2.924640659840883e-16,-2.65687869315249e-15,-2.805580936074104e-15,-2.0233875524205187e-15,-1.103493933232929e-15,-4.081697721137257e-16,-1.2501568782549998e-17,1.417278348058873e-16,1.5639937937966838e-16,1.1349075871000796e-16,6.200334765543451e-17,2.3217093681511895e-17,1.5527952014842835e-18,-6.952125724593481e-18,-7.909026863995309e-18,-5.7717368234154634e-18,-3.1628424090250505e-18,-1.201256116857707e-18,-9.707351194561253e-20,3.314957496453375e-19));
stan_samples <- stan(model_code = model_string, data=data_list)
print(stan_samples)
plot(stan_samples,show_density = TRUE,pars="alpha")
plot(stan_samples,show_density = TRUE,pars="beta")

```

---

<div class="post-metadata">

**Author:** ![lordp](https://avatars.discourse-cdn.com/v4/letter/l/c67d28/32.png) [@lordp](https://discourse.mc-stan.org/u/lordp)\
**Post date:** [December 11, 2018, 11:16am UTC](https://discourse.mc-stan.org/t/bayesian-estimation-kirmann-model/6899/2 "2018-12-11T11:16:32Z")

</div>

Hello Guys!

I tried to figure out the problem of my code but I can’t find it…

When I run it, it is said “initialization between (-2,2) failed”

Any idea would be appreciated!  
Thanks!!

---

<div class="post-metadata">

**Author:** ![hhau](https://avatars.discourse-cdn.com/v4/letter/h/2bfe46/32.png) [@hhau](https://discourse.mc-stan.org/u/hhau)\
**Post date:** [December 11, 2018, 11:22am UTC](https://discourse.mc-stan.org/t/bayesian-estimation-kirmann-model/6899/3 "2018-12-11T11:22:40Z")

</div>

The bounds in the `parameters` block need to match the support of the distributions specified in the `model` block. So you need:

```
parameters {
  real <lower=0,upper=0.8> alpha;
  real <lower=0,upper=0.19> beta;
  real <lower=0.1,upper=0.8>v;
  real <lower=0.4,upper=5>gg;
}

```

in the parameters block (although I’d be suspicious about the need to constrain the parameters to these intervals). See here for more thoughts about priors: [https://github.com/stan-dev/stan/wiki/Prior-Choice-Recommendations](https://github.com/stan-dev/stan/wiki/Prior-Choice-Recommendations)

---

<div class="post-metadata">

**Author:** ![lordp](https://avatars.discourse-cdn.com/v4/letter/l/c67d28/32.png) [@lordp](https://discourse.mc-stan.org/u/lordp)\
**Post date:** [December 11, 2018, 11:37am UTC](https://discourse.mc-stan.org/t/bayesian-estimation-kirmann-model/6899/4 "2018-12-11T11:37:53Z")

</div>

Hey!

Thank you for your answer.  
I changed the parameters block as you advised, however I stil have the same problem.

I will look the link you sent to see if changing my priors will solve the problem

---

<div class="post-metadata">

**Author:** ![Ara\_Winter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ara_winter/32/4284_2.png) [@Ara\_Winter](https://discourse.mc-stan.org/u/Ara_Winter)\
**Post date:** [December 13, 2018, 6:20pm UTC](https://discourse.mc-stan.org/t/bayesian-estimation-kirmann-model/6899/5 "2018-12-13T18:20:32Z")

</div>

I suspect it’s this part that’s giving you an error.

> while (k\<x){  
> k=k+1;  
> }  
> k=k-1;  
> if (x-k\>0.5){  
> k=k+1;

At least when I run your model I get:

> ```
> DIAGNOSTIC(S) FROM PARSER:
> Info: left-hand side variable (name=k) occurs on right-hand side of assignment, causing inefficient deep copy to avoid aliasing.
> Info: left-hand side variable (name=k) occurs on right-hand side of assignment, causing inefficient deep copy to avoid aliasing.
> Info: left-hand side variable (name=k) occurs on right-hand side of assignment, causing inefficient deep copy to avoid aliasing.
> 
> ```

See the deep copy warning here

> [@Deep copy warning](https://discourse.mc-stan.org/t/deep-copy-warning/885/2):
>
> edit: Actually, I don’t get that warning with codes below, I could have sworn that I got before… See below. I was just wondering the same thing. Simplest case where we get this warning is when doing something like for(t in 1:10) { x[t+1] = x[t]; } If you just want to get rid of the warning, you could use temporaries yourself: for(t in 1:10) { tmp = x[t]; x[t+1] = tmp; } But whether this is clever in terms of efficiency, I don’t know. The warning suggests that that is what is happening…

---

<div class="post-metadata">

**Author:** ![lordp](https://avatars.discourse-cdn.com/v4/letter/l/c67d28/32.png) [@lordp](https://discourse.mc-stan.org/u/lordp)\
**Post date:** [December 14, 2018, 8:04pm UTC](https://discourse.mc-stan.org/t/bayesian-estimation-kirmann-model/6899/6 "2018-12-14T20:04:53Z")

</div>

Hey Aya!

Thanks for your answer!

When i run my model, I don’t have the same message,

i get this:

```stan
SAMPLING FOR MODEL '78a280b77e742b140c0416f111969089' NOW (CHAIN 1).
Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: categorical_lpmf: Number of categories is 2147483647, but must be in the interval [1, 3] (in 'model148373115d_78a280b77e742b140c0416f111969089' at line 69)

Initialization between (-2, 2) failed after 100 attempts. 
 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"

```

But i didn’t know about the problem you highlighted, thanks for that! I’ll try to write my code with the “tmp” thing  
thanks again!

---

<div class="post-metadata">

**Author:** ![Ara\_Winter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ara_winter/32/4284_2.png) [@Ara\_Winter](https://discourse.mc-stan.org/u/Ara_Winter)\
**Post date:** [December 18, 2018, 6:20pm UTC](https://discourse.mc-stan.org/t/bayesian-estimation-kirmann-model/6899/7 "2018-12-18T18:20:06Z")

</div>

Hmm ok. Let me try and rerun the model and get around the error I am getting.

---

<div class="post-metadata">

**Author:** ![Apurva](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Apurva](https://discourse.mc-stan.org/u/Apurva)\
**Post date:** [October 25, 2019, 2:41pm UTC](https://discourse.mc-stan.org/t/bayesian-estimation-kirmann-model/6899/8 "2019-10-25T14:41:50Z")

</div>

> [@lordp](#):
>
> Rejecting initial value: Error evaluating the log probability at the initial value.

Did you get any solution for this kind of error?

---

<div class="post-metadata">

**Author:** ![Fabian\_Crespo](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/fabian_crespo/32/11263_2.png) [@Fabian\_Crespo](https://discourse.mc-stan.org/u/Fabian_Crespo)\
**Post date:** [April 12, 2021, 11:06am UTC](https://discourse.mc-stan.org/t/bayesian-estimation-kirmann-model/6899/9 "2021-04-12T11:06:31Z")

</div>

I am getting the same error:  
Chain 1: Rejecting initial value:  
Chain 1: Error evaluating the log probability at the initial value.  
Chain 1: Exception: Exception: categorical\_lpmf: Number of categories is -2147483648, but must be in the interval [1, 2]

When I export the functions from Stan to R works:

> categorical\_test\_rng( c(1, 0.00000000134757))  
> [1] 1  
> categorical\_test\_lpmf(1, c(1, 0.00000000134757))  
> [1] 0

Does anyone knows how to solve it ?

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [April 12, 2021, 12:44pm UTC](https://discourse.mc-stan.org/t/bayesian-estimation-kirmann-model/6899/10 "2021-04-12T12:44:32Z")

</div>

This indicates an issue with your model specification. Can you post the model that you’re trying to run?

---

<div class="post-metadata">

**Author:** ![Fabian\_Crespo](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/fabian_crespo/32/11263_2.png) [@Fabian\_Crespo](https://discourse.mc-stan.org/u/Fabian_Crespo)\
**Post date:** [April 12, 2021, 1:28pm UTC](https://discourse.mc-stan.org/t/bayesian-estimation-kirmann-model/6899/12 "2021-04-12T13:28:28Z")

</div>

Yes, sure, I removed several functions for privacy:[post\_error.stan](https://discourse.mc-stan.org/uploads/short-url/ozkpVLXAVb27kduMwBNbNhLkw81.stan) (5.1 KB) :

```stan
functions{
  int categorical_test_rng(vector theta){
    int selected_index;
    selected_index=categorical_rng(to_vector(theta));
    return(selected_index);
  }
  real categorical_test_lpmf(int y, vector theta){
    real result;
    result= categorical_lpmf( y | theta);
    return(result);
  }

}
data{
  real<lower=0.0, upper=2.0> K;
  int<lower=1> N;
  int<lower=2> total_sample_size;
  vector<lower = 0.0>[total_sample_size-1] sorted_coalescent_times_in_model_time_oldest_population;//not including the 0.0 at the beginning
  int indexes_father_populations[N];
  simplex[N] pop_sizes_proportion;
  int<lower=0,upper=2*total_sample_size> topology[total_sample_size-1,3];
}
transformed data{
    int number_inmigrants_per_population[N];
    vector<lower=0.0>[N] input_lower_bound_torigin_in_model_time_oldest_population;
    vector<lower=0.0>[N] input_upper_bound_torigin_in_model_time_oldest_population;
    vector<lower=0.0>[N] input_lower_bound_torigin_in_model_time;
    vector<lower=0.0>[N] input_upper_bound_torigin_in_model_time;
    int number_of_tips_below[2*total_sample_size-1];
    matrix [(total_sample_size-1),(total_sample_size-1)] Q_coeff_hypoexponential= rep_matrix(0.0, (total_sample_size-1), (total_sample_size-1));
    int number_of_coalescent_times_below[2*total_sample_size-1] ;

    //compute transformed data in terms of data
}
parameters{
  vector<lower=0>[N] alphas;
  vector<lower=0>[N] deltaTs;
  real<lower=input_lower_bound_torigin_in_model_time[1], upper = input_upper_bound_torigin_in_model_time[1] > torigins_in_model_time1;
  real<lower=input_lower_bound_torigin_in_model_time[N]> torigin_oldest_population;
}
transformed parameters {
  vector<lower=0>[N] deltas;
  vector<lower=0>[N-1] torigins_in_model_time_oldest_population;
  vector<lower=0>[N] torigins_in_model_time;

  deltas = deltaTs .* pop_sizes_proportion;
  torigins_in_model_time_oldest_population[1] = torigins_in_model_time1* pop_sizes_proportion[1];
  torigins_in_model_time_oldest_population = (1.0/pop_sizes_proportion[N])*
                                             torigins_in_model_time_oldest_population;

  torigins_in_model_time[1]= torigins_in_model_time1;
  torigins_in_model_time[N] = torigin_oldest_population;
}
model{
   vector[N] MRCA_times_in_model_time_oldest_population;
   int selected_indexes[N-1];
   alphas~gamma(rep_vector(0.001,N), rep_vector(0.001,N));
   deltaTs~exponential(alphas);
   torigins_in_model_time1~conditionalDensityTOrigin(deltas[1], total_sample_size);
   torigin_oldest_population~conditionalDensityTOrigin(deltas[N], total_sample_size);

    for(i in 1:(N-1)){
       int idx_first_coal_time_above_torigin=get_index_coal_time_above_MRCA_in_oldest_population_time_less_than( torigins_in_model_time_oldest_population[i], sorted_coalescent_times_in_model_time_oldest_population);
       vector [idx_first_coal_time_above_torigin-1] candidate_time_MRCAs_in_model_time_oldest_population= get_candidate_time_MRCAs(torigins_in_model_time_oldest_population[i], sorted_coalescent_times_in_model_time_oldest_population,
                   topology, number_of_tips_below,idx_first_coal_time_above_torigin );
       vector[rows(candidate_time_MRCAs_in_model_time_oldest_population)] candidate_time_MRCAs_in_model_time= (pop_sizes_proportion[N] / pop_sizes_proportion[i]) * candidate_time_MRCAs_in_model_time_oldest_population;

       int number_candidate_MRCAs= count_valid_elements_vector(candidate_time_MRCAs_in_model_time_oldest_population);
       vector [idx_first_coal_time_above_torigin-1] candidate_time_MRCAs_Kingman_coal = from_model_time_to_Kingman_coalescent_time(candidate_time_MRCAs_in_model_time_oldest_population, torigins_in_model_time[i], deltas[i], K);
      
       real p= 1.0/number_candidate_MRCAs;
       vector[number_candidate_MRCAs] theta;
       vector[number_candidate_MRCAs] probab;

       probab= compute_probab_vector(total_sample_size, number_candidate_MRCAs,candidate_time_MRCAs_in_model_time, candidate_time_MRCAs_in_model_time_oldest_population,
                     Q_coeff_hypoexponential, deltas[i], torigins_in_model_time[i], number_of_tips_below,
                     sorted_coalescent_times_in_model_time_oldest_population, topology) ;

       print("probab=", to_vector(probab));
       theta = rep_vector(p, number_candidate_MRCAs);

       selected_indexes[i]~categorical_test(to_vector(probab));

       MRCA_times_in_model_time_oldest_population[i]=candidate_time_MRCAs_in_model_time_oldest_population[selected_indexes[i]];
     }

     MRCA_times_in_model_time_oldest_population[N] = max(sorted_coalescent_times_in_model_time_oldest_population);

    target +=log_likelihood(total_sample_size, N, K, deltas,
                [torigins_in_model_time1, torigin_oldest_population]', append_row(torigins_in_model_time_oldest_population, torigin_oldest_population),
                sorted_coalescent_times_in_model_time_oldest_population,
                number_inmigrants_per_population, MRCA_times_in_model_time_oldest_population,
               indexes_father_populations, pop_sizes_proportion, torigin_oldest_population, topology, number_of_tips_below, number_of_coalescent_times_below);
}

```

The error appears in line 82: selected\_indexes[i]~categorical\_test(to\_vector(probab));

EDIT: @maxbiostat edited this post for readability and syntax highlighting.

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [April 12, 2021, 1:33pm UTC](https://discourse.mc-stan.org/t/bayesian-estimation-kirmann-model/6899/13 "2021-04-12T13:33:55Z")

</div>

The error occurs because you’re trying to give a prior to an uninitialised array.

You’ve declared the integer array:

```stan
   int selected_indexes[N-1];

```

And then placed a prior on it:

```stan
  selected_indexes[i]~categorical_test(to_vector(probab));

```

But you haven’t put any integers in the array (you haven’t put anything in `selected_indexes`), so Stan errors

---

<div class="post-metadata">

**Author:** ![Fabian\_Crespo](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/fabian_crespo/32/11263_2.png) [@Fabian\_Crespo](https://discourse.mc-stan.org/u/Fabian_Crespo)\
**Post date:** [April 12, 2021, 1:40pm UTC](https://discourse.mc-stan.org/t/bayesian-estimation-kirmann-model/6899/14 "2021-04-12T13:40:01Z")

</div>

Thanks for the quick reply and help!. Yes, selected\_indexes vector doesn’t have a prior . What would be a non informative candidate prior for integer array of indexes over the set of indexes  
{1,…,rows(sorted\_coalescent\_times\_in\_model\_time\_oldest\_population)} where sorted\_coalescent\_times\_in\_model\_time\_oldest\_population is an input data vector ?

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [April 12, 2021, 1:45pm UTC](https://discourse.mc-stan.org/t/bayesian-estimation-kirmann-model/6899/15 "2021-04-12T13:45:13Z")

</div>

Unfortunately Stan doesn’t support sampling integer values as parameters. You need to either provide the outcomes as data, or marginalise them out of the likelihood. There’s more information in this section of the User’s Guide: [7 Latent Discrete Parameters | Stan User’s Guide](https://mc-stan.org/docs/2_26/stan-users-guide/latent-discrete-chapter.html)
