{ "cells": [ { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2018-06-18T15:27:28.707896Z", "start_time": "2018-06-18T15:27:28.702766Z" } }, "source": [ "# Compound Steps in Sampling\n", "This notebook explains how the compound steps work in `pymc3.sample` function when sampling multiple random variables. We are going to answer the following questions associated with compound steps:\n", "\n", "- How does compound steps work?\n", "- What will happen when PyMC3 assign step methods by default?\n", "- How to specify the step methods? What is the order to apply the step methods at each iteration? Is there a way to specify the order of the step methods? \n", "- What are the issues with mixing discrete and continuous samplers, especially with HMC/NUTS?" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2018-07-28T21:53:54.050085Z", "start_time": "2018-07-28T21:53:52.045480Z" } }, "outputs": [], "source": [ "import pymc3 as pm\n", "import numpy as np\n", "import theano" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compound steps" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When sampling a model with multiple free random variables, the compound steps will be needed in the `pm.sample` function. When the compound steps are involved, the function takes a list of `step` to generate a list of `methods` for different random variables. So for example in the following code,\n", "```python\n", "with pm.Model() as m:\n", " rv1 = ... # random variable 1 (continuous)\n", " rv2 = ... # random variable 2 (continuous)\n", " rv3 = ... # random variable 3 (categorical)\n", " ...\n", " step1 = pm.Metropolis([rv1, rv2])\n", " step2 = pm.CategoricalGibbsMetropolis([rv3])\n", " trace = pm.sample(..., step=[step1, step2]...)\n", "```\n", "the compound step is now contain a list of `methods`. And at each sampling step, it iterates each method, which takes a `point` as input, and generates a new `point` as output. The new `point` is proposed within each step via a stochastic kernel, and if the proposal was rejected by the Metropolis-Hastings criteria it just outputs the original input `point`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compound steps by default\n", "When we call `pm.sample()`, `PyMC3` assigns the best step method to each of the free random variables. Take the following example:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2018-07-28T21:54:23.727052Z", "start_time": "2018-07-28T21:53:56.768369Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Multiprocess sampling (4 chains in 4 jobs)\n", "CompoundStep\n", ">NUTS: [p]\n", ">BinaryGibbsMetropolis: [ni]\n" ] }, { "data": { "text/html": [ "\n", "