jrnold / ssmodels-in-stan

State space models (dynamic linear models, hidden Markov models) implemented in Stan.
https://jrnold.github.io/ssmodels-in-stan/
BSD 3-Clause "New" or "Revised" License
47 stars 16 forks source link

Use of ssm_lpdf() gives "Rejecting initial value" #32

Open ksvanhorn opened 7 years ago

ksvanhorn commented 7 years ago

I have a Stan model using ssmodels-in-stan that worked fine with ssm_constant_lpdf(), but when I switched to ssm_lpdf() I started getting the error "Rejecting initial value: Error evaluating the log probability at the initial value." Some experimentation showed that it was in fact the call to ssm_lpdf() that is the problem. Digging in further, the problem appears to be the line

K = ssm_update_K(P, T_t, Z_t, Finv);

near the end of the function. The definition of ssm_update_K looks like this:

matrix ssm_update_k(matrix P, matrix Z, matrix T, matrix Finv) {
  matrix[cols(Z), rows(Z)] K;
  K = T * P * Z' * Finv;
  return K;
}

So you have Z and T swapped in the call to ssm_update_K, leading to non-conforming matrices in the matrix multiply.