Declaring a Vector and adding elements in it

Hi everyone,

I have 3 1D real data types which I wanna add as elements to a vector as follows,

real p1;
real p2;
real p3;
vector[3] p;

p1 = 43;
p2 = 4;
p3 = 5;

p= [p1,p2,p3]; - this line is giving me an error in stan. What is the correct syntax for appending p1,p2 and p3 to the vector p of length 3?

@Bob_Carpenter I saw your response in one of the links as,

append_col(a, append_col(b, c))

will append three vectors (or matrices) by column.

The next version of Stan (2.15) will let you construct
vector and matrix literals. It’ll look like:

real a;
row_vector[3] x = [ 1, 2, a ];
vector[3] xt = [ 1, 2, a ]’; // note transpose
matrix[3, 3] = [ [ 1, 2, 3],
[4, 5, 6],
[7, 8, 9] ];

Arrays use { 1, 2, 3 } instead of [1, 2, 3].

  • Bob

which will work in the current stan version?

What’s the error message? Wait, let me guess! It’s

Variable definition base type mismatch, variable declared
as base type vector variable definition has base type row_vector error

isn’t it?

Stan makes difference between row_vectors and (column) vectors. You a prime ' to transpose to turn the row literal into a column vector.

p = [p1, p2, p3]' ;
1 Like

yes, [p1,p2,p3]’; worked

’ - was the problem before

@nhuurre in the following user defined function, the length of vector y passed to this functions is 8. I am declaring a vector alk_dic of length 2 inside the fn and I want to assign the first 2 values of this vector to the first two values of vector y.

vector dynamics(real[] t, vector[] y, data real x_r, data int x_i) {

vector[8] dydt;
vector[2] alk_dic;

 alk_dic[1] = y[1];
 alk_dic[2] = y[2];

When I assign it this way as shown above, I am getting a dimension mismatch error saying left side is type real and right side as type vector.

Any suggestions on assigning it the right way?

Also, can we have just vector y in the function argument instead of vector[] y?

Yes, it’s rather confusing. Variable declaration that saysvector[3] means a vector of size 3. In function argument vector means it’s a vector of unknown size and vector[] means it’s an array of vectors, i.e. what you would get if you declared e.g. vector[3] v[3].

can we have just vector y in the function argument instead of vector y?

Yes.

1 Like

@nhuurre Hi Niko,

I have another quick doubt. can a vector data type hold a real data type and another vector type as follows?

Let’s say,

model{
real a1;
vector[8] Co;
vector[2] parms;

parms[1] = a1;
parts[2] = Co;


}

@nhuurre can a real data type store an int data type in stan?


real x[2];
int n;

x[1] = n;

is this valid?

You can store the value. It’s not possible to make it an integer again.

real x[2];
int n;

x[1] = n;
int m = x[1]; // invalid!

That means it can’t be used as an index or on the left-hand side of ~ poisson( or any other context where a real value is not accepted.

(Maybe you’ve already tried this out. If you want a speedy answer you’ll have to ask when it’s not midnight for me.)

1 Like

@nhuurre I am getting this error for using the algebraic solver,

[2] " Exception: Exception: Exception: algebra_solver: size of the algebraic system’s output (1) and size of the vector of unknowns, x, (4) must match in size (in ‘model290628a15da_v7_coralMCMC’ at line 54)"

How should I debug this?

The error is pretty clear: You have only one equation but you’re trying to solve for four unknowns. There’s no unique solution.
If you have only one equation then all but one parameters must go in the theta vector.

This thread has drifted from the original topic. If you need more help with the algebraic solver you should start a new thread.

Okay. Yeah the return type length was not matching with the guess values vector length.

I fixed it. Thanks