Column Sums or Row Sums of Matrix

3,468 views
Skip to first unread message

Allan Debertin

unread,
Feb 12, 2015, 3:22:24 PM2/12/15
to
Hi All,

I'm looking for a way to sum the rows of a matrix in STAN.

For example in JAGS this command is valid:

for(i in 1:I){
   
for (j in 1:J){
       X
[i]<- sum(Y[i,j]);
   }
}
   

What would be the equivalent in STAN?

Many thanks,

Allan

Jonah

unread,
Feb 12, 2015, 4:52:03 PM2/12/15
to
I forget nearly everything about JAGS syntax, but if it's like R then Y[i,j] is just a single element of the matrix Y, in which case sum(Y[i,j]) = Y[i,j]. Is that not the case in JAGS? 

Either way, if you want the sum of the nth column or nth row of a matrix M then you can do sum(col(M, n)) or sum(row(M, n)). That will give you the sum of the entire column or row. If you want sub-columns or sub-rows then there are sub_col and sub_row functions you can use. 

On Thursday, February 12, 2015 at 3:22:24 PM UTC-5, Allan Debertin wrote:
Hi All,

I'm looking for a way to sum the rows of a column in STAN.

Andrew Gelman

unread,
Feb 12, 2015, 7:06:45 PM2/12/15
to stan-...@googlegroups.com
I see this on p.238 of the manual:
If Sigma is a variable of type matrix, then Sigma[1] denotes the first row of Sigma and has the type row_vector.
So this suggests that you can do something like this:

real[rows(Y)] X;
for (i in 1:rows(Y))
  X[i] <- sum(Y[i]);

On Feb 12, 2015, at 3:22 PM, Allan Debertin <alland...@gmail.com> wrote:

Hi All,

I'm looking for a way to sum the rows of a column in STAN.

For example in JAGS this command is valid:

for(i in 1:I){
   for (j in 1:J){
       X[i]<- sum(Y[i,j]);
   }
}
   

What would be the equivalent in STAN?

Many thanks,

Allan

--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jonah Sol Gabry

unread,
Feb 12, 2015, 7:26:28 PM2/12/15
to stan-...@googlegroups.com
For rows that should work, but for columns I think col(Y, i) is the only option, right? That is, unlike in R, I don't think you can do Y[,i] to get the ith column in Stan. 

--
You received this message because you are subscribed to a topic in the Google Groups "Stan users mailing list" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-users/wMEK0DuAZDs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to stan-users+...@googlegroups.com.

Ben Goodrich

unread,
Feb 12, 2015, 11:12:27 PM2/12/15
to stan-...@googlegroups.com
On Thursday, February 12, 2015 at 3:22:24 PM UTC-5, Allan Debertin wrote:
I'm looking for a way to sum the rows of a column in STAN.

Multiply from the left by a row_vector of ones.

Ben

Andrew Gelman

unread,
Feb 12, 2015, 11:13:21 PM2/12/15
to stan-...@googlegroups.com
Ugh, that’s a bit of a hack, though.

Jonah

unread,
Feb 13, 2015, 12:31:19 AM2/13/15
to stan-...@googlegroups.com, gel...@stat.columbia.edu
Here are a few functions you can dump into the functions block and then use in your code:

  vector col_sums(matrix X) {
vector[cols(X)] s ;
for (j in 1:cols(X)) s[j] <- sum(col(X, j)) ;
return s ;
  }

  vector row_sums(matrix X) {
vector[rows(X)] s ;
for (i in 1:rows(X)) s[i] <- sum(row(X, i)) ;
return s ;
  }

Or using Ben's method you could do 

  row_vector col_sums(matrix X) {
    row_vector[cols(X)] s ;
    s <- rep_row_vector(1, rows(X)) * X ;
    return s ;
  }

  vector row_sums(matrix X) {
    vector[rows(X)] s ;  
    s <- X * rep_vector(1, cols(X)) ;
    return s ;
  }


Andrew Gelman

unread,
Feb 13, 2015, 12:34:52 AM2/13/15
to Jonah, stan-...@googlegroups.com
We should allow users to have their own function libraries.

Bob:  how hard would this be?  The idea would be that, the call to stan could include links to function libraries, and the stan program would then compile in any function in the library that was used in the calling program (or any of the library functions being called; it would have to search recursively).  In R, for example, the call might look like this:
stan(“myprogram.stan”, data=…, chains=…, iter=…, library=c(“mylib.stan”,”mylib2.stan”))

This would make life a lot easier in that we could have big ugly functions without having to fill up our stan models with them.

A

Jonah

unread,
Feb 13, 2015, 12:46:22 AM2/13/15
to stan-...@googlegroups.com, jga...@gmail.com, gel...@stat.columbia.edu
On Friday, February 13, 2015 at 12:34:52 AM UTC-5, Andrew Gelman wrote:

We should allow users to have their own function libraries.



This would be a great feature.  

Andrew Gelman

unread,
Feb 13, 2015, 12:47:57 AM2/13/15
to Jonah, stan-...@googlegroups.com
Do you have the R chops to implement it?

Or, I guess it makes more sense to implement within Stan itself, because it’s the Stan programs that will have to get parsed for dependencies.

Bob Carpenter

unread,
Feb 13, 2015, 4:53:52 AM2/13/15
to stan-...@googlegroups.com
I changed the subject rather than hijacking the existing
thread.

I think everyone agrees function libraries would be nice.
One approach we've been considering is a general include
functionality like that of C++, but that's a pretty substantial
amount of work on the parser side we've never gotten to.

Andrew's suggested approach to just include libraries,
is much less general, but could be workable in much less
time.

Comments?

- Bob

Allan Debertin

unread,
Feb 13, 2015, 8:19:49 AM2/13/15
to stan-...@googlegroups.com
Thanks All for the discussion.

This will be helpful. Allan

Allan Debertin

unread,
Feb 13, 2015, 11:10:12 AM2/13/15
to stan-...@googlegroups.com, gel...@stat.columbia.edu
Thanks Andrew,

I got this code to work to allow me to  sum rows across a matrix:

row_vector[J] x[I];
vector
[I] y;

   
for(i in 1:I){
      y
[i] <- sum(x[i]);
   
}

To sum columns of matrix, I think the functions  that Jonah and Ben pointed out would be easier. Would it be possible to implement a column_vector in STAN? or I guess you could just switch rows and column of a matrix, so above would be row_vector[I] x[J]; ?

Daniel Lee

unread,
Feb 13, 2015, 11:12:03 AM2/13/15
to stan-...@googlegroups.com
Hi Allan,

On Fri, Feb 13, 2015 at 11:10 AM, Allan Debertin <alland...@gmail.com> wrote:
Thanks Andrew,

I got this code to work to allow me to  sum rows across a matrix:

row_vector[J] x[I];
vector
[I] y;

   
for(i in 1:I){
      y
[i] <- sum(x[i]);
   
}

To sum columns of matrix, I think the functions  that Jonah and Ben pointed out would be easier. Would it be possible to implement a column_vector in STAN?

The 'vector' type in Stan is a column vector.

Allan Debertin

unread,
Feb 13, 2015, 11:16:49 AM2/13/15
to stan-...@googlegroups.com
Right! Must be Friday.

Bob Carpenter

unread,
Feb 14, 2015, 3:29:55 AM2/14/15
to stan-...@googlegroups.com
I created an issue with a translation of Andrew's
suggested implementation into what we'd need to do
at the C++ level in Stan:

https://github.com/stan-dev/stan/issues/1294

- Bob

> On Feb 14, 2015, at 12:19 AM, Allan Debertin <alland...@gmail.com> wrote:
>
> Thanks All for the discussion.
>
> This will be helpful. Allan
>

Ben Goodrich

unread,
Feb 13, 2015, 8:26:06 AM2/13/15
to stan-...@googlegroups.com, jga...@gmail.com, gel...@stat.columbia.edu
On Friday, February 13, 2015 at 12:47:57 AM UTC-5, Andrew Gelman wrote:
Do you have the R chops to implement it?

Or, I guess it makes more sense to implement within Stan itself, because it’s the Stan programs that will have to get parsed for dependencies.

It does make more sense to implement it within Stan, but I can put it into RStan. The only problem is that the line numbers will seem off if you get an error message because they will be offset by the number of lines in your library file.

Ben
 
Reply all
Reply to author
Forward
0 new messages