Hello,
I am trying to fit a Hidden Markov Model in CmdStan.
I specified a transition matrix Gamma
that depends on the parameters of the model, and it is such that Gamma[j, ]
is simplex for each state j
.
Since I assume that my system is at steady state, I would like to define the initial state vector rho
as a stationary probability vector, hence an eigenvector of Gamma
with eigenvalue 1.
In the transformed parameter block I wrote the following:
complex_vector[n_states] eigvals = eigenvalues(Gamma);
complex_matrix[n_states, n_states] eigvecs = eigenvectors(Gamma);
simplex[n_states] rho;
for (v in 1 : n_states){
if (eigvals[v] == to_complex(1, 0)){
rho = get_real(eigvecs[1 : n_states, v]); // column vector
rho = rho / sum(rho); // normalize
}
}
I do find a eigenvector with eigenvalue 1, but I repeatedly get the following error for rho
:
Exception: test_new_func_v4_model_namespace::log_prob: rho is not a valid simplex. sum(rho) = nan, but should be 1 (in 'test_new_func_v4.stan', line 195, column 4 to column 26)
and if I print the values of rho
is indeed all nan
entries.
// ------------------------------- //
In particular, what I found is that the evaluation eigvals[v] == to_complex(1, 0)
is always failing, even if eigvals[v]
appears to have real part 1 and imaginary part 0. If I print the output of
for (i in 1 : n_max){
print(get_real(eigvals[i]));
print(get_imag(eigvals[i]));
print(eigvals[i] == to_complex(1.0, 0.0));
print( get_real(eigvals[i]) == 1.0 );
print( get_imag(eigvals[i]) == 0.0 );
}
I get for i=0
1
0
0
0
1
I am not sure what I am not understanding…
Thank you in advance for your help!
CmdStan v2.34.1, CmdStanPy 1.2.2