# Initialisation error

**URL:** <https://discourse.mc-stan.org/t/initialisation-error/29224>\
**Category:** Modeling\
**Created:** [October 24, 2022, 2:46pm UTC](https://discourse.mc-stan.org/t/initialisation-error/29224 "2022-10-24T14:46:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![erin1](https://avatars.discourse-cdn.com/v4/letter/e/a6a055/32.png) [@erin1](https://discourse.mc-stan.org/u/erin1)\
**Post date:** [October 24, 2022, 2:46pm UTC](https://discourse.mc-stan.org/t/initialisation-error/29224/1 "2022-10-24T14:46:48Z")

</div>

I am new to using Stan and I am trying to model a simple ancestry problem but keep encountering an initialisation error. My model and code is provided below.

```stan
data {
  int<lower = 1> K;
  int<lower = 1> N;
  matrix[K,N] beta;
  vector[N] x;
}

parameters {
  vector[K] f;
}

transformed parameters {
  vector[N] alpha;
  for(i in 1:N){
    alpha[i]=0;
    for(j in 1:K){
      alpha[i] = alpha[i] + f[j]*beta[j,i];
    }
  }
}
model {
  f~dirichlet(rep_vector(1, K));
  x~dirichlet(alpha);
}

```

The above runs fine with no errors. However, below produces the error :  
[1] “Error in sampler$call\_sampler(args\_list[[i]]) : Initialization failed.”  
[1] “error occurred during calling the sampler; sampling not done”

```stan
# Firstly, prepare the data for stan

beta = matrix(
  c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0.5, 0.5, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0.3, 0.4, 0.3),nrow=3,ncol=10,byrow=T)+0.05
beta=beta/rowSums(beta)

# This is the actual frequencies of the populations
ftrue=c(0.5,0.5,0)

# This function produces a sample child, based on the parent pop profiles 
# and the true frequencies of the populations, sampled from multinomial
# distribution.
make_population_data<-function(beta,ftrue,N=100){
  xi=rmultinom(1,N,ftrue)[,1]
  x=rowSums(do.call("cbind",sapply(1:length(xi),function(i){
    rmultinom(xi[i],1,beta[i,])
  })))
  list(K=dim(beta)[1],N=dim(beta)[2],beta=beta,x=x)
}

# Here we assign the output of the above function to the pop data we
# will be feeding into our stan model.
population_data=make_population_data(beta,ftrue)

# Next, we need to call stan function to draw posterior 
# samples

#my_file <- file.path("C:", "Users", "Joach", "Desktop", "my_file.csv")
#"C:\Users\Team Knowhow\Documents\YEAR 4\Project\STAN models\01-script.R"
# use forward slashes in file path otherwise leads to an error
library(rstan)
fit1 <- stan(
  file = "C:/Users/Team Knowhow/Documents/YEAR 4/Project/STAN models/01-model.stan", # Stan program
  data = population_data, # named list of data
  chains = 4, # number of Markov chains
  warmup = 1000, # number of warmup iterations per chain
  iter = 2000, # total number of iterations per chain
  cores = 1, # number of cores (could use one per chain)
  refresh = 0 # no progress shown
)

```

Please advise.

---

<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:** [October 24, 2022, 2:53pm UTC](https://discourse.mc-stan.org/t/initialisation-error/29224/2 "2022-10-24T14:53:52Z")

</div>

Your parameter declaration should be:

```stan
simplex[K] f;

```

For a dirichlet

---

<div class="post-metadata">

**Author:** ![erin1](https://avatars.discourse-cdn.com/v4/letter/e/a6a055/32.png) [@erin1](https://discourse.mc-stan.org/u/erin1)\
**Post date:** [October 24, 2022, 3:15pm UTC](https://discourse.mc-stan.org/t/initialisation-error/29224/3 "2022-10-24T15:15:59Z")

</div>

Hi, thanks for your reply.  
Unfortunately, after changing the parameter as you said I am receiving the same error.

```stan
data {
  int<lower = 1> K;
  int<lower = 1> N;
  matrix[K,N] beta;
  vector[N] x;
}

// The parameters accepted by the model. Our model
// accepts one parameters 'f' which is a vector representing
// the mixture probabilities of each ref pop.
parameters {
  simplex[K] f;
}

transformed parameters {
  vector[N] alpha;
  for(i in 1:N){
    alpha[i]=0;
    for(j in 1:K){
      alpha[i] = alpha[i] + f[j]*beta[j,i];
    }
  }
}

// The model to be estimated. We model the output
// 'f' (mixture probability) to be dirichlet distributed 
// with parameter (1,1,1), with length p (=3 in this case).
// x is dirichlet distributed with parameter alpha
model {
  f~dirichlet(rep_vector(1, K));
  x~dirichlet(alpha);
}

```

---

<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:** [October 24, 2022, 3:21pm UTC](https://discourse.mc-stan.org/t/initialisation-error/29224/4 "2022-10-24T15:21:56Z")

</div>

What is the error shown if you remove the `refresh = 0` from your `stan` call?

---

<div class="post-metadata">

**Author:** ![erin1](https://avatars.discourse-cdn.com/v4/letter/e/a6a055/32.png) [@erin1](https://discourse.mc-stan.org/u/erin1)\
**Post date:** [October 24, 2022, 3:29pm UTC](https://discourse.mc-stan.org/t/initialisation-error/29224/5 "2022-10-24T15:29:14Z")

</div>

Chain 1: Rejecting initial value:  
Chain 1: Error evaluating the log probability at the initial value.  
Chain 1: Exception: dirichlet\_lpdf: probabilities is not a valid simplex. sum(probabilities) = 100, but should be 1 (in ‘string’, line 43, column 2 to column 21)  
.  
.  
.  
Chain 1: Initialization between (-2, 2) failed after 100 attempts.  
Chain 1: 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”

---

<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:** [October 24, 2022, 3:31pm UTC](https://discourse.mc-stan.org/t/initialisation-error/29224/6 "2022-10-24T15:31:08Z")

</div>

That’s referring to the input data `x`, you need to double check that it is a vector that sums to 1 (i.e., a simplex) to use the dirichlet prior
