Extracting M N-vectors from N-array of M-vectors

322 views
Skip to first unread message

Thomas

unread,
Feb 20, 2015, 10:38:52 AM2/20/15
to stan-...@googlegroups.com
Hi all,

if I have an N-array of say 2 vectors that is sampled from a multivariate normal in a vectorized way:
vector[2] y[N];
...
y ~ multi_normal(mu_y,Sigma_y);

Can I extract two vectors of length N y1 and y2 (to work with them in the generated quantities block) without giving up the vectorized sampling of y? I can see that this might be possible with the head or segment functions, but I am not sure.

Thanks,
Thomas

Thomas

unread,
Feb 20, 2015, 2:14:06 PM2/20/15
to stan-...@googlegroups.com
I meanwhile tried
y1 <- to_matrix(y)'[1];
but this throws an error suggesting that arg 0 type=vector[1].
According to the manual 2-d arrays can be used as argument to to_matrix?

Thanks,
Thomas

Daniel Lee

unread,
Feb 20, 2015, 2:34:01 PM2/20/15
to stan-...@googlegroups.com
Can you provide the whole error message? I feel like that's the wrong interpretation of the error.

Also, y is not a 2-d array. It's an array of 2-d vectors.


Daniel


--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rob Trangucci

unread,
Feb 20, 2015, 2:41:28 PM2/20/15
to stan-...@googlegroups.com
Also, if possible, you should try to use multi_normal_cholesky (especially if Sigma_y isn't modeled), as it does no costly matrix decompositions internally.


Rob

Thomas Daeubler

unread,
Feb 20, 2015, 2:48:32 PM2/20/15
to stan-...@googlegroups.com
Thanks, I also have a re-parameterized version.

The error is:
Error in stanc(file = file, model_code = model_code, model_name = model_name,  :
  failed to parse Stan model 'stan.code' with error message:
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
no matches for function name="to_matrix"
    arg 0 type=vector[1]
available function signatures for to_matrix:
0.  to_matrix(matrix) : matrix
1.  to_matrix(vector) : matrix
2.  to_matrix(row vector) : matrix
3.  to_matrix(real[2]) : matrix
4.  to_matrix(int[2]) : matrix
no matches for function name="transpose"
    arg 0 type=ill formed
available function signatures for transpose:
0.  transpose(vector) : row vector
1.  transpose(row vector) : vector
2.  transpose(matrix) : matrix
indexes inappropriate for expression.
no matches for function name="to_matrix"
    arg 0 type=vector[1]
available function signatures for to_matrix:
0.  to_matrix(matrix) : matrix
1.  to_matrix(vector) : matrix
2.  to_matrix(row vector) : matrix
3.  to_matrix(real[2]) : matrix
4.  to_matrix(int[2]) : matrix
no matches for function name="transpose"
    arg 0 type=ill formed
available function signatures for

--
You received this message because you are subscribed to a topic in the Google Groups "Stan users mailing list" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-users/8dCXIl1cxV8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to stan-users+...@googlegroups.com.

Rob Trangucci

unread,
Feb 20, 2015, 3:00:38 PM2/20/15
to stan-...@googlegroups.com
In generated quantities, you should do something like the following:

generated quantities {
  matrix[2,N] y_matrix;

  for(n in 1:N)
   col(y_matrix,n) <- y[n];

...
...
}


Rob

Thomas Daeubler

unread,
Feb 20, 2015, 4:04:37 PM2/20/15
to stan-...@googlegroups.com
Thanks! Col mustn't be on the lefthandside, but this seems to do the job:

matrix[N,2] y_matrix;

for(n in 1:N)
    y_matrix[n] <- to_row_vector(y[t]);

Thomas

Bob Carpenter

unread,
Feb 20, 2015, 8:50:13 PM2/20/15
to stan-...@googlegroups.com
Does anyone have suggestions on how to make these
messages clearer? I'm taking a pass through errors now
and this particular one seems to confuse a lot of our
users.

The problem you're having is that you're passing an
array of vectors to to_matrix(). The only valid arguments
are either a matrix, a vector, a row vector, a 2D array
of real or a 2D array of int. An array of vectors doesn't
match any of those.

For a start, I think I'll replace "vector[1]" with just
"vector[]". That will at least make the type print out match
the way way types are defined in functions. I think vector[1]
is confusing with the declaration. So "int[2]" would look
like "int[,]".

- Bob
Reply all
Reply to author
Forward
0 new messages