Vector vs. Array

226 views
Skip to first unread message

Luca Billi

unread,
Jun 11, 2013, 10:50:16 PM6/11/13
to stan-...@googlegroups.com
I was trying to run an example from the User Manual, but I encountered compilation errors.
In particular, at the bottom of page 78, there's the following sample code:

data {
 
int<lower=0> N;
  real x
[N];
  real y
[N];
}

parameters
{
  real alpha
;
  real beta
;
  real
<lower=0> sigma;
}

model
{
  y
~ normal(alpha + beta * x, sigma);
 
...
}

The compiler complains that it cannot multiply "beta * x".

However, the example seems to work if the declaration "real x[N]" is replaced by "vector[N] x".

So, I'm wondering whether I'm missing something or the Manual needs to be updated.

Luca

Ben Goodrich

unread,
Jun 11, 2013, 10:56:58 PM6/11/13
to stan-...@googlegroups.com
On Tuesday, June 11, 2013 10:50:16 PM UTC-4, Luca Billi wrote:
However, the example seems to work if the declaration "real x[N]" is replaced by "vector[N] x".

So, I'm wondering whether I'm missing something or the Manual needs to be updated.

The manual is wrong and replacing the array with a vector is reasonable. For the most part, a vector is preferable to a one-dimensional array of reals in the sense that operations like beta * x are defined. However, I think there are some operations that work elementwise on an array but don't work for a vector. The array is necessary for integer data and sometimes a multidimensional array is useful.

Ben

Dieter Menne

unread,
Jun 14, 2013, 2:21:01 AM6/14/13
to stan-...@googlegroups.com
In case the manual should be edited:

It would be nice to have some comments on "When to use real[] and when to use a vector" in the manual. For me it was unexpected that setting priors on real[] was possible without a loop, so this is vector-like behavior. And why are (all? sorry, if I should have missed it) parameters to/from R are passed as real/int[], and not as vectors.

Dieter

Ben Goodrich

unread,
Jun 14, 2013, 9:49:08 AM6/14/13
to stan-...@googlegroups.com
On Friday, June 14, 2013 2:21:01 AM UTC-4, Dieter Menne wrote:
In case the manual should be edited:

It should be.
 
It would be nice to have some comments on "When to use real[] and when to use a vector" in the manual. For me it was unexpected that setting priors on real[] was possible without a loop, so this is vector-like behavior. And why are (all? sorry, if I should have missed it) parameters to/from R are passed as real/int[], and not as vectors.

I would say: Use a vector unless you have to use an array. The cases where you have to use an array are when the data are integer valued and when you want to use a 3+ dimensional structure.  For two dimensional, you can use a matrix (of reals only).

It is sometimes useful to utilize an array that contains something other than a real, i.e. you can have an array of vectors (which is somewhat like a matrix but without many matrix algebra capabilities) an array of matrices, etc. But a one dimensional array of reals is not that useful. Sometimes you will run into a function that operates elementwise over the whole array of reals while that function does not operate elementwise over a vector, matrix, etc. In that case, you have to write the loop explicitly in your .stan file, but that doesn't really waste any time.

Ben

Reply all
Reply to author
Forward
0 new messages