Newbie question: difference between real variable[N]; and vector[N] variable;


Is there anywhere an introductory piece on the difference between an array and a vector?

it seems to me that if I tried to use real variable[N];, it just raises syntactic error down the road.

For example, if I change the model in recent StanCon Helsinki 2018 tutorial link line 16 from vector to real, I got:

No matches for: 

  real * real[]

Syntax error.

The real question here is when should I use real[N]; instead of vector?



When you don’t need the elements stored as arbitrary types. Here is the function reference manual (see esp. chapter 3):


@increasechief thank you for your quick reply. I read thru the chapter 3 of the reference manual but still a bit lost as to the root of the problem. If I change the line declaring traps
, the syntax error would creep up on the model section where complaints were modelled using traps. Is it because an array cannot be used in vectorized operation? But an array is more efficient in memory?

functions {
  * Alternative to poisson_log_rng() that 
  * avoids potential numerical problems during warmup
  int poisson_log_safe_rng(real eta) {
    real pois_rate = exp(eta);
    if (pois_rate >= exp(20.79))
      return -9;
    return poisson_rng(pois_rate);
data {
  int<lower=1> N;
  int<lower=0> complaints[N];
  real<lower=0> traps[N]; // the original line vector<lower=0>[N] traps;
parameters {
  real alpha;
  real beta;
model {
  // weakly informative priors:
  // we expect negative slope on traps and a positive intercept,
  // but we will allow ourselves to be wrong
  beta ~ normal(-0.25, 1);
  alpha ~ normal(log(4), 1);
  // poisson_log(eta) is more efficient and stable alternative to poisson(exp(eta))
  complaints ~ poisson_log(alpha + beta * traps);
generated quantities {
  int y_rep[N];
  for (n in 1:N) 
    y_rep[n] = poisson_log_safe_rng(alpha + beta * traps[n]);


the reference manual covers this as well:

(chapter 5, section 4 “Vector and Matrix Data Types”)

no - some vectorized functions can apply to arrays - but not multiplication.

not really.