transparent BUGS transpilation

30 views
Skip to first unread message

Allen B. Riddell

unread,
Feb 12, 2016, 3:59:19 PM2/12/16
to stan...@googlegroups.com
Hi all,

I was surprised to see some code in Richard McElreath's Statistical
Rethinking that translates from BUGS into Stan.

Here's an example from https://github.com/rmcelreath/rethinking

```r
library(rethinking)

f <- alist(
y ~ dnorm( mu , sigma ),
mu ~ dnorm( 0 , 10 ),
sigma ~ dcauchy( 0 , 1 )
)

fit <- map(
f ,
data=list(y=c(-1,1)) ,
start=list(mu=0,sigma=1)
)
```

At first I was a bit dismayed to see this. After some reflection it
occurred to me that for simple models CmdStan/RStan/PyStan could
transparently detect BUGS code and, in simple cases, just translate it
directly into Stan code (replacing all the obvious distributional
BUG-isms with appropriate Stan replacements). It would throw an error on
any complicated models but if it worked on 90% of simple BUGS/JAGS
models I think it might be welcomed by users as they transition to Stan.

This could also make sense as a stand-alone website where you paste in
your BUGS/JAGS code into a form. The site would spit out Stan code (or
an error if it can't translate the code), along with helpful annotations
about helpful changes.

Best,

Allen

Bob Carpenter

unread,
Feb 12, 2016, 4:09:11 PM2/12/16
to stan...@googlegroups.com
To translate a BUGS model to Stan, you need to know which
variables are data and which are parameters and you
need to know the shape (not size) of the data objects.
So McElreath must be using the formula list and data to convert
the BUGS-like spec.

McElreath must be leaning on R's internal formula syntax
for expressing a BUGS program as a sequence of formulas.
To do this outside of R would require a parser for BUGS.

BUGS also has deterministic nodes.

And there's also JAGS, which is slightly different syntactically.

We support most, but not quite all of the features of the
BUGS/JAGS languages --- they'd be easy to add (like
interpolation and the F distribution).

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

Krzysztof Sakrejda

unread,
Feb 12, 2016, 4:16:55 PM2/12/16
to stan development mailing list, a...@ariddell.org

Need another project? :) K

Allen B. Riddell

unread,
Feb 12, 2016, 4:23:42 PM2/12/16
to stan...@googlegroups.com
On 02/12, Bob Carpenter wrote:
> To translate a BUGS model to Stan, you need to know which
> variables are data and which are parameters and you
> need to know the shape (not size) of the data objects.
> So McElreath must be using the formula list and data to convert
> the BUGS-like spec.
>
> McElreath must be leaning on R's internal formula syntax
> for expressing a BUGS program as a sequence of formulas.
> To do this outside of R would require a parser for BUGS.
>
> BUGS also has deterministic nodes.
>
> And there's also JAGS, which is slightly different syntactically.
>
> We support most, but not quite all of the features of the
> BUGS/JAGS languages --- they'd be easy to add (like
> interpolation and the F distribution).
>

I suppose I like the idea of a `pandoc` for probabilistic programming
languages. It would be cool to be able to go back and forth between
arbitrary language pairs (with simple models).

And I also can't help but take McElreath's map2stan as a critique of the
language+interface as it currently exists!

Ben Goodrich

unread,
Feb 12, 2016, 5:07:29 PM2/12/16
to stan development mailing list, a...@ariddell.org
On Friday, February 12, 2016 at 4:23:42 PM UTC-5, Allen B. Riddell wrote:
And I also can't help but take McElreath's map2stan as a critique of the
language+interface as it currently exists!

For his target audience perhaps. I tend to think of it as a rejection of the idea that his audience should start by writing models in the Stan language but mostly because they are somewhat new to modeling.

Ben

 

Andrew Gelman

unread,
Feb 12, 2016, 5:50:34 PM2/12/16
to stan...@googlegroups.com, Allen B. Riddell
It should also grep for inverse-gamma and uniform priors and spit out all caps warning messages where they appear.

Jonah Gabry

unread,
Feb 12, 2016, 6:17:07 PM2/12/16
to stan development mailing list, a...@ariddell.org, gel...@stat.columbia.edu
For the distribution names, I don't get why he uses the R/Bugs conventions of dnorm, dcauchy, etc., rather than Stan's normal, cauchy, etc. It can't be that dnorm is easier to understand than normal (the actual name of the distribution). Unless the goal is for people to never move beyond rethinking and into Stan, why not just use Stan's distribution names, which typically correspond to the actual names of the distributions?

Michael Betancourt

unread,
Feb 12, 2016, 6:21:15 PM2/12/16
to stan...@googlegroups.com
+1

Bob Carpenter

unread,
Feb 12, 2016, 6:32:09 PM2/12/16
to stan...@googlegroups.com
I think the "re" in "rethinking" comes from this point
McElreath makes on his site:

The book is aimed at PhD students and researchers in the natural
and social sciences.

This audience has had some calculus and linear algebra,
and one or two joyless undergraduate courses in statistics.
I've been teaching applied statistics to this audience for
about a decade now, and this book has evolved from that experience.

I'd like to write a book aimed at replacing that joyless undergrad
class with something at the level of Gelman and Hill.

- Bob

> On Feb 12, 2016, at 6:17 PM, Jonah Gabry <jga...@gmail.com> wrote:
>
> For the distribution names, I don't get why he uses the R/Bugs conventions of dnorm, dcauchy, etc., rather than Stan's normal, cauchy, etc. It can't be that dnorm is easier to understand than normal (the actual name of the distribution). Unless the goal is for people to never move beyond rethinking and into Stan, why not just use Stan's distribution names, which typically correspond to the actual names of the distributions?
>
Reply all
Reply to author
Forward
0 new messages