A.6 Further information on WebPPL

WebPPL (pronounced “web people”) is a probabilistic programming language embedded in JavaScript. Unlike JavaScript, WebPPL does not support looping constructs (such as for or while). Instead, it encourages a functional way of programming, using recursion and higher-order functions. Please refer to this tutorial for examples and further explanations.

A.6.1 Primitives and sampling functions

We can use WebPPL to (easily) sample from probability distributions, many of which are already implemented and ready to use. A full list of built-in primitive distributions can be found in the documentation. If we would like to draw one sample from, say, a standard normal distribution, we could run sample(Gaussian({mu: 0, sigma: 1})). A more convenient expression would be to just use the respective sampling function, in this case gaussian({mu: 0, sigma: 1}) (notice the lowercase letter in the function name). Sampling functions can be combined with the repeat() function to take more than one sample, ultimately leading to better approximations.

Let’s look at a simple example to see how repeated sampling from a primitive distribution works. In the code box below, we take \(1000\) samples from a beta distribution with parameters \(\alpha = 4\) and \(\beta = 6\) and visualize them (more on this below).

viz(repeat(1000, function() {beta({a: 4, b: 6})}));

A.6.2 Inference with Infer()

We might also want to create our own distribution objects (= probability distributions). For this purpose, the built-in function Infer() comes in pretty handy. It takes as input a function with no arguments and returns a distribution object. The function passed to Infer() is the sampling function that should be turned into a distribution object. Additionally, Infer() can take on another optional argument, namely the method for performing inference. If this argument is not specified, WebPPL will automatically choose a reasonable method for inference. More on this function and different methods here.

Here’s an example of how to perform inference using the MCMC method. The example is one of a logistic regression (based on very little data) and the model returns samples from the posterior predictive distribution for a previously unseen data point. Click on the yellowish box to check what the code does and how Infer() is used. Please re-visit Chapter 9.3.1 for more information on MCMC algorithms.

// training data 
var xs = [-10, -5, 2, 6, 10]
var labels = [false, false, true, true, true]
// new data point to predict a label for
var x_new = 1

///fold:
var model = function() {
  // priors of regression parameters
  var beta_1 = gaussian(0, 1)
  var beta_0 = gaussian(0, 1)

  var sigmoid = function(x) {
    return 1 / (1 + Math.exp(-1 * (beta_1 * x + beta_0)))
  }

  map2(
    function(x, label) {
      factor(Bernoulli({p: sigmoid(x)}).score(label))
    },
    xs,
    labels)

  return bernoulli(sigmoid(x_new))
}

viz.auto(Infer({method: 'MCMC', samples: 10000, burn: 2000}, model))
///

A.6.3 Visualization

WebPPL comes with a major benefit in that it makes plotting as easy as pie. All we have to do is basically wrap the viz() function of the viz-package around our data, and depending on the nature of the data (continuous or discrete), WebPPL will automatically come up with a visualization of it. Of course, we can also explicitly tell WebPPL how we want our data to be plotted. Much like in ggplot, we just add the (abbreviated) plotting method to the function name. An explicit way of plotting a histogram, for instance, would be to call viz.hist(). The supported methods for data visualization are documented here.

In the example below, the data stored in variable xs is plotted once with the default viz() function and once with the explicit viz.hist() function. What do you notice with regard to the output?

var xs = [-2, -1, 1, 2, 3, 4, 4, 5]; 

viz(xs);
viz.hist(xs);

A.6.4 Installation

You can run WebPPL code directly from within the editor on webppl.org. If you want to install WebPPL locally, follow the steps below:

  1. Install git.
  2. Install Node.js.
  3. Run npm install -g webppl in your command line.

Run npm update -g webppl to update your current version of WebPPL.

These steps are also mentioned in the documentation.

A.6.5 Usage

Run WebPPL programs locally with webppl FILE_NAME.wppl.

A.6.6 Keyboard shortcuts (for in-browser use)

  • Press Ctrl + Enter to run code.
  • Select code and press the Tab key to fix indentations.
  • Press Ctrl + / to comment or uncomment code (apparently, this shortcut only works with an English keyboard).