Running Stan inside your Browser

Try it!

To put the important stuff up front: at https://brianward.dev/stan-web-demo/ you can try out a web app which can sample from the bernoulli example model.

All computation is done completely locally, and it should work on all major browsers including your phone.

How it works

The code is open source, of course, but it does depend on some pretty “beta-quality” code I’ve been working on, so it might not quite be ready for prime time. By far the biggest contribution is the ability to build a WebAssembly module out of a specific Stan model.

The front end is a basic React app I built with the help of @JSoules, who had previously shared tools like MCMC Monitor

What else can we do with this?

  • The first question I’ve gotten is if you could let users edit the model. The answer is “maybe”. There are projects which run entire C++ toolchains in the browser, and the Stan Compiler can run in the browser,, so it’s possible. This would need a bunch more engineering, and may hit some technical limitations (e.g., you can’t use more than 2GB of memory in a WASM module, currently), so I have no plans to actively work on it. If I did, I think my goal would be something like @magland’s Stan Playground but entirely clientside.
  • I am very interested in doing some more things in the style of “interactive notebooks” that run locally/without an install step. If you have a class or tutorial built around a relatively fixed set of models, it would now be totally reasonable to have them run on your student’s computers without needing to install anything or use Google Colab.
  • I’m looking for suggestions on a better model for the demo. Obviously the bernoulli coin flip example is conjugate and not that impressive. If you have ideas for models that better show off Stan, but still have an intuitive data block that lends itself toward playing around with different inputs, please let me know!
8 Likes

Linear regression with a univariate covariate is nice if you set up an interface where you let users click and add points and then draw the regression lines for 50 or 100 draws from the posterior. Then you can let users play with (a) the prior scales, and (b) the width of tails in the likelihood if you have something like a student-t for “robust” regression.

You could do the same thing with a logistic regression only now you’d be fitting the posterior logistic response curves. You’d let users assign data points.

One-dimensional Gaussian processes are even more fun for this as you can let users play with the length scale and/or smoothness parameters on the prior and then see the fits which can be arbitrarily wiggly. Either a Matern prior (which controls smoothness) or an square/exponentail would make sense. But the model’s not so easy to understand from just glancing at it.

1 Like

That would be fun. It would be similar to one of the demos of using FORTRAN in the browser that inspired me to try this which showed a polynomial interpolation by letting you click around to add points

I was hoping for something a bit more interesting like an ODE-based model, but even the simplest of those seem to have data blocks which are difficult to invent observations for on the fly. The planetary motion example in the workflow paper could have a nice interactive component that let you change the true masses and eccentricities, but it’s also in the workflow paper because it’s hard to fit, so maybe it isn’t a good demo.