lm model in Stan manual - why it is not vectorized?

167 views
Skip to first unread message

t.wol...@ibe.edu.pl

unread,
Jul 7, 2014, 9:59:52 AM7/7/14
to stan-...@googlegroups.com
Hi!

Stan is performs vectorized operations. If so, why in the Stan manual the linear regression model is not vectorized?

The model:

(...)

model {

for (n in 1:N)

   y[n] ~ normal(alpha + beta * x[n], sigma);

}


while it could be:

y ~ normal(alpha + beta * x, sigma);

Is there any reason for that? Both models seem to work. Are there any advantages of non-vectorized form?

Thanks in advance!

Timothy

Bob Carpenter

unread,
Jul 7, 2014, 1:17:14 PM7/7/14
to stan-...@googlegroups.com
There are big advantages to the vectorized forms.
See the discussion in the section following the introduction
of linear regression (p. 22 in version 2.3 manual).

We're going to rewrite all of these in a best-practices
first way rather than the other way around. I started
by trying to make them easy to understand for BUGS/JAGS
users and most of these examples were written before we'd
finished implementing the vectorization.

- 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.
> For more options, visit https://groups.google.com/d/optout.

Tymoteusz Wołodźko

unread,
Jul 7, 2014, 6:17:21 PM7/7/14
to stan-...@googlegroups.com
OK, I thought so, but didn't find clear confirmation of that in manual and was a little bit confused. In the manual there are also some mentions on that "not everything" is yet vectorized, so it can add up to confusion.

Greetings,
Timothy


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/hxV6WVXp-_I/unsubscribe.
To unsubscribe from this group and all its topics, send an email to stan-users+...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Tymoteusz Wołodźko
Zespół Analiz Osiągnięć Uczniów Instytut Badań Edukacyjnych | ul. Górczewska 8 | 01-180 Warszawa
tel.  +48
695 370 391 | www.ibe.edu.pl

Bob Carpenter

unread,
Jul 7, 2014, 6:27:32 PM7/7/14
to stan-...@googlegroups.com

On Jul 7, 2014, at 6:17 PM, Tymoteusz Wołodźko <t.wol...@ibe.edu.pl> wrote:

> OK, I thought so, but didn't find clear confirmation of that in manual and was a little bit confused. In the manual there are also some mentions on that "not everything" is yet vectorized, so it can add up to confusion.
>

Sorry it's confusing.

The manual says after presenting the vectorized form:

"The main difference is that the vectorized form is much faster."

I guess I need to more clear about what the other differences are. The
example is complicated due to matrix arithmetic, which is only defined
for vector and matrix types.

But we're going to change the presentation eventually so at least this issue
shouldn't be a problem, starting with the vectorized version then presenting
the unvectorized one as an equivalent semantically but slower alternative.

In the lists of functions and distributions in the manual, the pseudo-types
"reals" and "ints" indicate what can be vectorized. There's a discussion of
the notation before the functions --- maybe we should point to it on every
function definition (hundreds of references), because it's come up before,
because obviously not everyone's going to read our manual cover-to-cover.

reals: real, 1D array of real, vector, row vector

ints: int, 1D array of int

There's also a discussion in the chapter on vectors and arrays as to what
the advantages are. For instance, using the vectorized exp() is no faster
than just using a loop, whereas vectorized sum() [and all the distributions]
are much faster in vectorized form for vectors with more than a few entries.
The point is whether we can speed things up with analytic derivatives.

- Bob

Reply all
Reply to author
Forward
0 new messages