Complex matrix structure

stanc

#1

Is it possible to declare custom structures in Stan? For example, I am working with a dynamic HMM problem. There are 3 states, so I need a 3 x 3 matrix to contain all the parameters pertinent to each transition. Each transition has one intercept term xi, and 6 coefficients beta[6]. Is there an efficient way to capture all these in one matrix declaration and be able to work with it?


#2

I think

matrix[3, 3] beta[k]

would work.


#3

That gives you indexing beta[k, i, j] if that’s what you want, or beta[k] to pick out a matrix.

The main problem with HMMs is that those matrices have to be simplex constrained if they’re the transition matrices, so you want to use something like:

simplex[3] beta[K, 3];

Same indexing structure, beta[k, i, j], but now you are guaranteed to have a simplex. If you’re defining the matrix yourself using something like softmax over predictors, this would only give you error checking.


#4

Vector[3,3] beta is exactly what I need for this. I don’t really need the matrix properties except an efficient way to access the beta vectors using row and column.


#5

I’m confused. That won’t work. A vector only has one index.


#6

to be exact, I am using

vector[K] beta[S,S];