On Lisp and Stan

2016 March 22
by Daniel Lakeland

Over at the Bayesian Philosophy blog "Laplace" made the comparison between Cox/Jaynes Bayesian probability theory, and the Lisp programming language (and SAS as the antichrist/anti-lisp which seems apt).

I then proceeded to +1 the Lisp programming language and even go so far as to say that I wished Stan was written in Lisp.

Commenter Wayne made the point: "Daniel: I’d love it if Stan were done in Lisp as well. This would unfortunately doom Stan."

Which may well be the case, given how Lisp seems to be a polarizing thing, either you love it, or typically you don't get it. But, I suspect that Stan could be done in Lisp internally, without much need for direct interaction with Lisp. For the most part Stan works as follows:

1) Write your model in the Stan language

2) Run some function in your favorite language which invokes the Stan compiler.

3) Stan compiler reads the Stan code and compiles it to C++ code.

4) Stan compiler runs a C++ compiler on the compiled code (g++ etc) which generates an executable

5) Someone (either your favorite language's function, or whatever) runs the executable and gets samples. Note, this can also be a function call to a shared library generated by the C++ compiler, I think.

Just like you as a user never interact with C++, you as a user could potentially never interact with Lisp, even if Stan were done in Lisp.

Ultimately, since we're talking about numerical high performance computing code, I think C++ was probably the right choice. But Lisp is capable of doing high performance computing (with a good compiler like SBCL). However, the libraries available in C++ are probably better and certainly more well maintained.

Still, the process of converting a Stan program to a C++ program and compiling the C++ program "feels like" the kind of thing that would be cleaner and more easily hooked into in Lisp. If you wanted to do something like write your own mathematical functions to hook into Stan, I think C++ is a clunky way.

I suspect Julia is headed this direction, but I haven't had enough time to play with Julia and figure it out. based on what I have seen, I hope Julia thrives, it brings some of the flexibility of a Lisp like system while maintaining speed+efficiency, and looking more familiar to C++ users etc.


No comments yet

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS