# Problem with initialization

I am learning to run a Hidden Markov Model with PyStan. I try to specify a model with 3 hidden states with prior transition probabilities as the posterior transition probabilities from a 2 hidden states. The model is like this (it’s incomplete as I leave out parts probably not related to the error encountered):

stan_model =

``````functions {
vector normalize(vector x) {
return x / sum(x);
}
}

data {
int<lower=1> T;                   // number of observations (length)
int<lower=1> K;                   // number of hidden states
int<lower=1> M;                   // size of the input vector
int<lower=1> S;                   // number of stocks
int<lower=1> L;                   // length of flatten array

real x_t[L];
int<lower=1,upper=S> S_idx[L];
int<lower=1,upper=T> T_idx[L];

vector[M] u_tm[L];
}

parameters {
// Discrete state model
simplex[K] p_1k;                           // initial state probabilities
simplex[K] A[K];                           // transistion matrix

// Continuous observation model
vector[M] b_km[K];                         // mean regressors
real<lower=0.001, upper=20> s_k[K];        // residual standard deviations
real<lower=1,upper=20> nu_k[K];            // residual degrees of freedom

// Prior transition matrix
vector[2] tran_prob_1;
vector[2] tran_prob_2;
}

model {
for(j in 1:K) {
b_km[j] ~ normal(0, 0.5);
s_k[j] ~ uniform(0.001, 20);
nu_k[j] ~ uniform(1,20);
}

A[1][1:2] ~ dirichlet(tran_prob_1);
A[2][1:2] ~ dirichlet(tran_prob_2);

target += log_sum_exp(unalpha_tk[T]); // Note: update based only on last unalpha_tk
}
``````

So the transition probabilities is a 3x3 matrix `A`. Arrays `tran_prob_1` and `tran_prob_2` are 2x2 arrays that store dirichlet parameters from the 2 hidden state models.

The StanModel code works OK:
`stan_model = pystan.StanModel(model_code=stan_model, verbose=True)`

But the model fit encounters a RuntimeError that Initialization failed.
`model_fit = stan_model.sampling(data=model_data_dict, iter=800, chains=4, seed=2017)`

If I don’t specify the use of posterior transition probabilities, i.e., deleting the part:

``````  A[1][1:2] ~ dirichlet(tran_prob_1);
A[2][1:2] ~ dirichlet(tran_prob_2);
``````

then the model runs just fine.
Can you suggest where the problem may be?

Dirichlet parameters should be positive so `tran_prob`s need `<lower=0>` but that alone won’t stop initialization.
The problem is that the simplex constraint on `A[1][1:3]` does not guarantee that `A[1][1:2]` is a simplex (and in fact strongly suggests it is not) so it can’t have dirichlet distribution.
What exactly are you trying to do? Is the intended effect that `A[1][3]=A[2][3]=0`? If so then you’ll have to construct `A` as a transformed parameter

``````paramters {
simplex[2] A1;
simplex[2] A2;
simplex[3] A3;
}
transformed parameters {
simplex[3] A[3];
A[1] = append_row(A1, 0.0);
A[2] = append_row(A2, 0.0);
A[3] = A3;
}
``````