# StatSim overview

Learn how StatSim works and how to build a simple model using main building blocks (data block, random variable, expression and other)

In [3]:
from iframer import iframer


StatSim is a multipurpose app. It wraps WebPPL (a probabilistic programming language) and Z3 (theorem prover) in a user-friendly graphical interface based on Vue.js. It also used a lot of awesome JavaScript libraries. You can find a full list of dependencies here. StatSim can be used to create basic programs, however its main purpose is probabilistic simulations, modeling and problem solving.

Let's open a simple example that just sums two variables x and y and displays results:

In [4]:
iframer('https://statsim.com/app/?a=%5B%7B%22b%22%3A%5B%7B%22n%22%3A%22x%22%2C%22sh%22%3Afalse%2C%22t%22%3A2%2C%22u%22%3Atrue%2C%22dims%22%3A%22%22%2C%22v%22%3A%222%22%7D%2C%7B%22n%22%3A%22y%22%2C%22sh%22%3Afalse%2C%22t%22%3A2%2C%22u%22%3Atrue%2C%22dims%22%3A%22%22%2C%22v%22%3A%223%22%7D%2C%7B%22n%22%3A%22z%22%2C%22h%22%3Afalse%2C%22sh%22%3Atrue%2C%22t%22%3A1%2C%22v%22%3A%22x%2By%22%7D%5D%2C%22mod%22%3A%7B%22n%22%3A%22Sum%22%2C%22e%22%3A%22%22%2C%22s%22%3A1%2C%22m%22%3A%22deterministic%22%7D%2C%22met%22%3A%7B%22sm%22%3A1000%7D%7D%5D')


There are 2 key areas of the StatSim window: block stack on the left and preview with results on the right. To actually build a model use the left part. To run the model and get results click the blue button Compile and Run. Each model (program) consists of building blocks. Our new simple program just sums two variables and displays results. What we need is two data blocks for each input variable and one expression block that describes a formula: x + y.

Another important thing to understand before creating your first model is a simulation method. Our first simple example just added value of one deterministic (non random) variable to another. The main feature of StatSim is processing of random variables. The next example creates such variable and adds it as a third value to first two we created before. That new variable has Gaussian distribution with parameters Mu (mean) equal to 5 and Sigma (standard deviation) equal to 3.

In [3]:
iframer('https://statsim.com/app/?a=%5B%7B%22b%22%3A%5B%7B%22n%22%3A%22x%22%2C%22sh%22%3Afalse%2C%22t%22%3A2%2C%22u%22%3Atrue%2C%22dims%22%3A%22%22%2C%22v%22%3A%222%22%7D%2C%7B%22n%22%3A%22y%22%2C%22sh%22%3Afalse%2C%22t%22%3A2%2C%22u%22%3Atrue%2C%22dims%22%3A%22%22%2C%22v%22%3A%223%22%7D%2C%7B%22d%22%3A%22Gaussian%22%2C%22n%22%3A%22r%22%2C%22o%22%3Afalse%2C%22p%22%3A%7B%22mu%22%3A%225%22%2C%22sigma%22%3A%223%22%7D%2C%22sh%22%3Afalse%2C%22t%22%3A0%2C%22dims%22%3A%221%22%7D%2C%7B%22n%22%3A%22z%22%2C%22h%22%3Afalse%2C%22sh%22%3Atrue%2C%22t%22%3A1%2C%22v%22%3A%22x%2By%2Br%22%7D%5D%2C%22mod%22%3A%7B%22n%22%3A%22Sum%22%2C%22e%22%3A%22%22%2C%22s%22%3A1%2C%22m%22%3A%22deterministic%22%7D%2C%22met%22%3A%7B%22sm%22%3A1000%7D%7D%5D')

Out[3]:

Each time you click Run you get a new random result centered around 10

One of the best things about StatSim is that you can run each model multiple times producing thousand of samples. The Deterministic method we used in previous examples produces only one output value. Let's change it to Rejection Sampling and click Run:

In [4]:
iframer('https://statsim.com/app/?a=%5B%7B%22b%22%3A%5B%7B%22n%22%3A%22x%22%2C%22sh%22%3Afalse%2C%22t%22%3A2%2C%22u%22%3Atrue%2C%22dims%22%3A%22%22%2C%22v%22%3A%222%22%7D%2C%7B%22n%22%3A%22y%22%2C%22sh%22%3Afalse%2C%22t%22%3A2%2C%22u%22%3Atrue%2C%22dims%22%3A%22%22%2C%22v%22%3A%223%22%7D%2C%7B%22d%22%3A%22Gaussian%22%2C%22n%22%3A%22r%22%2C%22o%22%3Afalse%2C%22p%22%3A%7B%22mu%22%3A%225%22%2C%22sigma%22%3A%223%22%7D%2C%22sh%22%3Afalse%2C%22t%22%3A0%2C%22dims%22%3A%221%22%7D%2C%7B%22n%22%3A%22z%22%2C%22h%22%3Afalse%2C%22sh%22%3Atrue%2C%22t%22%3A1%2C%22v%22%3A%22x%2By%2Br%22%7D%5D%2C%22mod%22%3A%7B%22n%22%3A%22Sum%22%2C%22e%22%3A%22%22%2C%22s%22%3A1%2C%22m%22%3A%22rejection%22%7D%2C%22met%22%3A%7B%22sm%22%3A1000%7D%7D%5D')

Out[4]:

Now instead of a scalar value, the output contains some charts and stats that describe distibution of our probabilistic results.

That are quite simple and not really useful models, however you can create complex programs with StatSim using iterative models, bayesian inference and neural nets!

The last thing i'd like to show in this post is the Preview mode. When the model is done and its inner structure is not needed anymore it's possible to switch the app from Editor mode to Preview hiding the program architecture. Links generated in Preview mode automatically include the preview flag.

Previous model in the Preview mode:

In [5]:
iframer('https://statsim.com/app/?a=%5B%7B%22b%22%3A%5B%7B%22n%22%3A%22x%22%2C%22sh%22%3Afalse%2C%22t%22%3A2%2C%22u%22%3Atrue%2C%22dims%22%3A%22%22%2C%22v%22%3A%222%22%7D%2C%7B%22n%22%3A%22y%22%2C%22sh%22%3Afalse%2C%22t%22%3A2%2C%22u%22%3Atrue%2C%22dims%22%3A%22%22%2C%22v%22%3A%223%22%7D%2C%7B%22d%22%3A%22Gaussian%22%2C%22n%22%3A%22r%22%2C%22o%22%3Afalse%2C%22p%22%3A%7B%22mu%22%3A%225%22%2C%22sigma%22%3A%223%22%7D%2C%22sh%22%3Afalse%2C%22t%22%3A0%2C%22dims%22%3A%221%22%7D%2C%7B%22n%22%3A%22z%22%2C%22h%22%3Afalse%2C%22sh%22%3Atrue%2C%22t%22%3A1%2C%22v%22%3A%22x%2By%2Br%22%7D%5D%2C%22mod%22%3A%7B%22n%22%3A%22Sum%22%2C%22e%22%3A%22%22%2C%22s%22%3A1%2C%22m%22%3A%22rejection%22%7D%2C%22met%22%3A%7B%22sm%22%3A1000%7D%7D%5D&preview=1')

Out[5]:

To switch between views click on the ☰ button (top-left corner) and select corresponding mode.

By Anton Zemlyansky in