Using Stan with Google Colab


Using Stan with Google Colab is fairly straightforward. You just need to be sure to install CmdStanPy and Stan at the top of your notebook.

Installation of most recent version of Stan

CmdStanPy has an install_cmdstan() function that will install the latest version of CmdStan, which provides the Stan engine for all of your calculations. At the top of a Colab notebook, you can install CmdStanPy by running

!pip install --upgrade cmdstanpy

After CmdStanPy is installed, you can install CmdStan using

import cmdstanpy
cmdstanpy.install_cmdstan()

A complete Colab setup cell at the top of the notebook would then look something like this:

[1]:
# Colab setup ------------------
import os, sys, subprocess
if "google.colab" in sys.modules:
    cmd = "pip install --upgrade iqplot bebi103 arviz cmdstanpy watermark"
    process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = process.communicate()
    import cmdstanpy; cmdstanpy.install_cmdstan()
    data_path = "https://s3.amazonaws.com/bebi103.caltech.edu/data/"
else:
    data_path = "../data/"
# ------------------------------

There is another method for installing CmdStan shown below. In all of the lesson notebooks in this course, we will use the above method.

Installation of CmdStan binaries

The drawback with using the above method is that the installation of CmdStan takes several minutes. As an alternative, if you want to quick installation, you can use pre-built binaries of CmdStan for Colab. Unfortunately, binaries are not built for the most recent version of Stan (as of December 2021), but for version 2.23. This version should be sufficient, and you can install it quickly as follows.

Again, you will need to install CmdStanPy

!pip install --upgrade cmdstanpy

Next, you need to install the pre-built binary

import os
import urllib
import shutil

fname = "colab-cmdstan-2.23.0.tar.gz"
cmdstan_url = "https://github.com/stan-dev/cmdstan/releases/download/v2.23.0/" + fname

if not os.path.exists(fname):
    urllib.request.urlretrieve(cmdstan_url, fname)
    shutil.unpack_archive(fname)

# Let cmdstanpy know where CmdStan is
os.environ["CMDSTAN"] = "./cmdstan-2.23.0"

So, your Colab setup cell would look something like this:

[2]:
# Colab setup ------------------
import os, shutil, sys, subprocess, urllib.request
if "google.colab" in sys.modules:
    cmd = "pip install --upgrade iqplot bebi103 arviz cmdstanpy watermark"
    process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = process.communicate()
    fname = "colab-cmdstan-2.23.0.tar.gz"
    cmdstan_url = "https://github.com/stan-dev/cmdstan/releases/download/v2.23.0/" + fname
    urllib.request.urlretrieve(cmdstan_url, fname)
    shutil.unpack_archive(fname)
    os.environ["CMDSTAN"] = "./cmdstan-2.23.0"
    data_path = "https://s3.amazonaws.com/bebi103.caltech.edu/data/"
else:
    data_path = "../data/"
# ------------------------------