Search code examples
arraysvectorstan

Array format for vector in Rstan


Please create a stan file, for example aaa.stan . And in this aaa.stan file, please write as follows:

transformed parameters { 
        vector  [11] xxx [21];
      xxx[33][4]=2;
}

when I push the Check button, I encountered the following odd output

> rstan:::rstudio_stanc("aaa.stan")
aaa.stan is syntactically correct.

Even though, my code is no correct, why does computer say correct?


Solution

  • That just means that the Stan file parses to a C++ file. But the parser does not check whether things stay within the bounds of the indices, in part because you could declare vector[K] x; and it does not know until runtime how large K is. In general, the Stan parser has almost no checking of logic, although in this case you will get a runtime error when you assign 2 to xxx[33][4].