Writing a user defined column sums function in Stan

I want to write a Stan function that is analogous to colSums() in R, for some matrix modeling. My understanding is that this doesn’t exist in Stan (see Rowsum or columnsum of a matrix - #2 by bgoodri). I wrote the following function in the functions block, which is syntactically correct:

  vector colSums(matrix M){
    int ncol; 
    vector[ncol] sums; 
    ncol = cols(M); 
    for(i in 1:ncol){
      sums[i] = sum(M[,i]); //sums[i] = sum(col(M,i)); 

However, when I actually apply this function to a matrix in a Stan model, I get this error:

Exception: Exception: Found negative dimension size in variable declaration; variable=sums; dimension size expression=ncol; expression value=-2147483648 

… at the line where I ran colSums(M). Hopefully this is just a naïve error, but can anyone tell what I’ve done wrong?

You’re declaring a variable int ncol but not initializing it. By default, Stan will initialize it to the smallest possible int (~ - 2^31). When you then use this as the size of the vector in the next line, you get this error.

If you move the line ncol = cols(M) above the vector declaration the error should go away

Thanks, that helps. But if I do that the vector declaration won’t work (since all the variables have to be declared before I initialize anything).

If you’re using a recent version of Stan you can do declare-defines in one line and mix declarations and statements, so the alternative is valid. This would only not be true if you’re using RStan from CRAN.

I believe you could do vector[cols(M)] sums;

1 Like