
Just in time for Halloween, we have a scarily effective implementation of Stan on the web, full of a veritable haunted house of delicious treats.
Brian Ward, Jeff Soules, and Jeremy Magland write:
Stan Playground is a new open-source, browser-based editor and runtime environment for Stan models. Users can edit, compile, and run models, as well as analyze the results using built-in plots and statistics or custom analysis code in Python or R, all with no local installation required. . . .
Whether you’re a new user, an educator trying to teach Stan, or an experienced user who just doesn’t have their new laptop configured yet, we hope to make your life just a bit easier.
You can visit the live website here: https://stan-playground.flatironinstitute.org
Feature Overview
For users familiar with tools like the Compiler Explorer 1, repl.it or JSFiddle, Stan Playground hopes to provide a similar experience for Stan models.
Stan editor
The site features an editor for Stan code with syntax highlighting and provides warnings and errors from the Stan compiler for instant feedback.
Compiling models
Compilation of the models is the only part of Stan Playground which is not run locally. We provide a public server for convenience, but you can also host your own.
Preparing data
Data can be provided in JSON format in its own editor, or can be generated from code written in R (using webR) or Python (using pyodide), including code that imports published datasets.
Sampling
After a model has been compiled, sampling can be run entirely in your local browser.
Viewing and analyzing results
Stan Playground has several built-in ways of viewing the samples, but also supports performing your own analysis, again in R or Python.
Sharing
Stan Playground has built-in sharing features to allow you to download a copy of your project, upload an existing project, or share via a Github Gist . . . You can also prepare custom links . . . For example, this link will load the “golf” case study from the example models repository: https://stan-playground.flatironinstitute.org/?title=Knitr%20-%20Golf%20-%20Golf%20Angle&stan=https://raw.githubusercontent.com/stan-dev/example-models/master/knitr/golf/golf_angle.stan&data=https://raw.githubusercontent.com/stan-dev/example-models/master/knitr/golf/golf1.data.json
I tried it out and it really worked!
I wrote a simple example that simulates fake data from a linear regression with known parameters (in the transformed data block) and then expresses the log posterior density (in the model block):
data {
int N; // sample size
real a_, b_; // true values of parameters
real<lower=0> s_; // for the simulation
}
transformed data {
vector[N] x = linspaced_vector(N, 0, 1);
vector[N] y;
for (n in 1:N) y[n] = normal_rng(a_ + b_*x[n], s_);
}
parameters {
real a, b;
real<lower=0> s;
}
model {
y ~ normal(a + b*x, s);
}
With data:
{
"N": 1000,
"a_": 0.2,
"b_": 0.3,
"s_": 0.5
}
It’s so easy
Setting this up a and running it in Stan Playground is the simplest thing in the world:
1. Put the Stan program in the Stan program window.
2. Enter the data (in this case, the sample size of the regression and true parameter values) as a .json in the data window and click to save.
3. In the command window, click once to compile and click again to run.
4. The output (in tabular and graphical form) appears in the output window.
That’s it! The image at the top of this post shows the results.
Also, it catches many bugs in the compilation and sampling stages.
Wow. Easy to use. What a great tool for trying out Stan or for some kinds of teaching.
Haven’t looked at it yet, but does this compile the model to WebAsm and run it on the browser, or does it run on the backend on flatiron servers?
Daniel:
See above: “Compilation of the models is the only part of Stan Playground which is not run locally. . . . After a model has been compiled, sampling can be run entirely in your local browser.”
This is the best of both worlds. Compilation at Flatiron so that you don’t need do download a C++ compiler onto your computer, execution on your browser so that you can fit models without Flatiron needing to budget tons of server time. Also, they write: “the use of web technologies does place some constraints on what is (currently) possible. One a few early users have run into is memory constraints – Web Assembly is currently limited to somewhere between 2 and 4GB of RAM usage, depending on your browser. You can still do a lot of cool modeling within those limits, but it’s still important to be aware of.”
This is indeed a great way to go. Thanks for the explanation.
Just taught my undergrad statistical computation course about Stan using this site—a fantastic teaching tool. Huge thanks to the team that put this together.
This is amazing, Brian’s Stan Con talk about it is very good too
https://www.youtube.com/watch?v=2Vl7AWaypCA&list=PLCrWEzJgSUqzNzh6mjWsWUu-lSK59VXP6&index=1
A cool thing that Brian mentioned is that you can run the compilation on your own server, so you can set something up to do a tutorial to a large audience. Another cool thing is that because of the way Stan separates code & data, only your code needs to be transferred to a server (your data never needs to leave your computer). I had never previously thought that the code/data separation as a privacy thing.
Fwiw:
I know. I kinda just want to repeat this post every day for the next year, just to spread the word!
PS this might interest you; https://www.youtube.com/watch?v=RmXrwKydM9k Does Gladwell often admit he was wrong?
Sorry, one more, just so people don’t have to watch the video if they don’t want to:
I told a story about how crime fell in New York,
and I told the story like the story was over.
And like I knew what the answer to this story was.
And it wasn’t over
and I didn’t know the answer, right?
I wrote, “I know this is what happened,”
and what I should have said is
“This is what I believe happened now,” right?
And those words “I believe happened now”
have to be at the center of any understanding
of how the world works.
We have to acknowledge that we are representing
the position of this very moment,
and that that position could change if the facts change, right?
The great desire of any writer is to write a book for the ages,
that will forever explain the way things are,
but that’s not possible,
and no one should ever try.
That was my mistake.
And I’m sorry.
Next step is for Gladwell to address this one.
But, yeah, good on Gladwell for admitting error. We can learn a lot from our mistakes! I’d really like to see the Freakonomics crew and the editors of Psychological Science confront some of their notorious howlers.