sparse matrix syntax

443 views
Skip to first unread message

Eaton, Jeffrey W

unread,
Nov 15, 2015, 5:54:30 PM11/15/15
to stan-...@googlegroups.com
Dear Stan developers,

Thanks very much for the inclusion of sparse matrix multiplication in Stan 2.8. This is a very helpful addition to the language. I have a small question regarding how I am using the syntax.

I would like to pass a matrix A as data, parse this matrix as a sparse matrix in the transformed data block, and then use the sparse matrix in the model block. The question is about how to declare the sizes of the w, v, and u objects. The best I can figure out (without calculating this outside Stan and passing in the data list) is to call the csr_extract functions twice:

data {
int m;
int n;
matrix[m, n] A;
}
transformed data {
vector[rows(csr_extract_w(A))] Aw;
int Av[rows(Aw)];
int Au[size(csr_extract_u(A))];

Aw <- csr_extract_w(A);
Av <- csr_extract_v(A);
Au <- csr_extract_u(A);
}
model {

csr_matrix_times_vector(m, m, Aw, Av, Au, …)

}


This works fine and doesn’t have computational implications because it’s done in transformed data, but seems inelegant because it calls the csr_extract functions twice. So I wanted to check if I am missing a better syntax for this?

Thanks,
Jeff

Bob Carpenter

unread,
Nov 16, 2015, 5:10:18 PM11/16/15
to stan-...@googlegroups.com
I don't know a better way to do that. We require sizes
for everything, which is a pain for these derived quantities,
but does allow for error checking.

It's not pretty, but I wouldn't worry about efficiency
because this will only be done once when the data's loaded.

We might eventually move to allow dynamically sized variables
along the lines of how the Eigen matrix library works. We
already do that in functions.

- Bob
> --
> 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.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
>

Yannick

unread,
Mar 23, 2016, 7:20:13 AM3/23/16
to Stan users mailing list
Hi

just checking: is there an update to this, or are we at the same point in stan 2.9?

I'll use this occasion to make two feature requests:
- add the size of u,v,w in the docs
- support for operation on sparse matrices (but as I read, it's something you've already got in the radar).

thanks!

Krzysztof Sakrejda

unread,
Mar 23, 2016, 8:32:33 AM3/23/16
to Stan users mailing list


On Wednesday, March 23, 2016 at 7:20:13 AM UTC-4, Yannick wrote:
Hi

just checking: is there an update to this, or are we at the same point in stan 2.9?

Same point.
 

I'll use this occasion to make two feature requests:
- add the size of u,v,w in the docs 
- support for operation on sparse matrices (but as I read, it's something you've already got in the radar).

One difficulty about adding sparse matrices as a new variable type that all the operations work with is that you need to plumb it through.. all the operations.  If you only add true sparse versions of some operations and allow the type to also work with the dense versions of operations you introduce a lot of opportunities for users to do something naive and kill performance or run out of memory.    I'm enthusiastic about adding a sparse data type but it's not a one person part time project.

Krzysztof

Bob Carpenter

unread,
Mar 23, 2016, 2:02:58 PM3/23/16
to stan-...@googlegroups.com
If they're a separate type, we can limit their use
to a set of functions rather than having them auto-promote
to dense matrices (the way int autopromotes to real in Stan).
But I've also not taken them on due to all the plumbing
through I/O. Once we get the refactor in place, we'll revisit
it. Eigen's getting better sparse matrix funcitonality all the
time.

I added a note to add sizes of u, v, and w in the docs:

https://github.com/stan-dev/stan/issues/1709#issuecomment-200467751

- Bob
Reply all
Reply to author
Forward
0 new messages