Can all distributions in Stan be vectorized? Is vectorizing distributions improve speed?

217 views
Skip to first unread message

Jiang Du

unread,
Oct 19, 2015, 11:27:37 PM10/19/15
to Stan users mailing list
Hi, I'm a beginner of Stan, and I'm not that confident to vectorize all my distribution statements in Stan, for example, I know 

when mu is a vector, delta is a scalar, 
for(i in 1:N){
y
[n]~dnorm(mu,delta)
}

is the same as

y~dnorm(mu, delta)
, is the sense that delta is broadcasted to a vector with the same size as mu, or y, i.e., N.

Can I do the same thing for all the distributions in Stan?
For example,
Vector[N] x;
x
~uniform(a,b) # a, b are both scalar


matrix
[K,K] sigma[N];
sigma
~ inv_wishart(m, S) # S is a single matrix with the same dim as each element of sigma array, broadcast??

Also, for increment_log_prob(), it can take a vector, suppose I have a vector of log_likelihood log_vec, which way is better:
increment_log_prob(sum(log_vec))
#or
increment_log_prob
(log_vec)

Thanks for your patience and hope you don't mind if this question is too simple. I went over the menu 2 times but I'm still not feeling confident when writing programs.

Best!



Daniel Lee

unread,
Oct 20, 2015, 8:20:02 AM10/20/15
to stan-...@googlegroups.com
Hi Jiang Du,

On Oct 19, 2015, at 11:27 PM, Jiang Du <jiang...@gmail.com> wrote:

Hi, I'm a beginner of Stan, and I'm not that confident to vectorize all my distribution statements in Stan, for example, I know 

when mu is a vector, delta is a scalar, 
for(i in 1:N){
y
[n]~dnorm(mu,delta)
}

is the same as

y~dnorm(mu, delta)
, is the sense that delta is broadcasted to a vector with the same size as mu, or y, i.e., N.

I see you're coming from R. Stan uses "normal" instead of "dnorm."

Can I do the same thing for all the distributions in Stan?
For example,
Vector[N] x;
x
~uniform(a,b) # a, b are both scalar


matrix
[K,K] sigma[N];
sigma
~ inv_wishart(m, S) # S is a single matrix with the same dim as each element of sigma array, broadcast??



No, we don't have all the multivariate distributions vectorized. The way to determine if it is vectorized is to look at the function signature either in the manual or through rstan (I forget the function to look up Stan functions). 

Plural arguments indicate that it can be given either a scalar or a vector type. For example, "reals" means that argument can accept a real, a row_vector, a vector, or a 1-d array of reals.


Also, for increment_log_prob(), it can take a vector, suppose I have a vector of log_likelihood log_vec, which way is better:
increment_log_prob(sum(log_vec))
#or
increment_log_prob
(log_vec)


My guess is the second is better, but this won't impact the overall readability or runtime either way. 



Thanks for your patience and hope you don't mind if this question is too simple. I went over the menu 2 times but I'm still not feeling confident when writing programs.

No problem. Good luck!


Best!



--
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.

Bob Carpenter

unread,
Oct 20, 2015, 10:01:08 AM10/20/15
to stan-...@googlegroups.com
See inline.

> On Oct 20, 2015, at 8:19 AM, Daniel Lee <bea...@alum.mit.edu> wrote:
>
> Hi Jiang Du,
>
> On Oct 19, 2015, at 11:27 PM, Jiang Du <jiang...@gmail.com> wrote:
>
> No, we don't have all the multivariate distributions vectorized.

Some of them are, and when we say vectors (plural), it means the argument
can accept an array of vectors.

> The way to determine if it is vectorized is to look at the function signature either in the manual or through rstan (I forget the function to look up Stan functions).
>
> Plural arguments indicate that it can be given either a scalar or a vector type. For example, "reals" means that argument can accept a real, a row_vector, a vector, or a 1-d array of reals.
>
>
>> Also, for increment_log_prob(), it can take a vector, suppose I have a vector of log_likelihood log_vec, which way is better:
>> increment_log_prob(sum(log_vec))
>> #or
>> increment_log_prob(log_vec)

Second is very slightly more efficient.

- Bob

Jiang Du

unread,
Oct 20, 2015, 12:25:51 PM10/20/15
to Stan users mailing list
Thank you Daniel.

I didn't even notice the arguments are expressed as plural of singular when I was reading the menu. That's really good to know.

Jiang

在 2015年10月20日星期二 UTC-5上午7:20:02,Daniel Lee写道:

Jiang Du

unread,
Oct 20, 2015, 12:26:38 PM10/20/15
to Stan users mailing list
Thank you Bob.

I'll check the menu again to make my mind clear.

JIang

在 2015年10月20日星期二 UTC-5上午9:01:08,Bob Carpenter写道:
Reply all
Reply to author
Forward
0 new messages