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?