{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Adaptive rejection sampling\n", "\n", "Rejection sampling (RS) is a useful method for sampling intractable distributions. It defines an envelope function which upper-bounds the target unnormalised probability density to be sampled. It then proceeds to sample points in the area under the envelope, rejecting those points which fall above the target and accepting the rest. The accepted points are independent and identically distributed samples from the target distribution. There are two important issues with RS. The first is that if the envelope is a very loose upper bound, then most samples will be rejected and the scheme will be slow. The second is that for rejection sampling to work, we must be certain that the envelope is an upper bound to the target, which in practice may be a challenging task.\n", "\n", "Adaptive rejection sampling (ARS) {cite}`gilks1992ars` is an efficient method for sampling log-concave targets, which deals with both of these issues. It is origially defined for univariate distributions, but can also be extended to multivariate distributions via Gibbs sampling.{cite}`bishop2006PRML` ARS maintains an envelope which adapts as more points are sampled, becoming a progressively tighter bound to the target, thereby avoiding the inefficiency of regular RS. Further, the way that ARS constructs this envelope guarantees that the envelope is in fact an upper bound to the target, which sidesteps the second difficulty described above." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "tags": [ "remove-cell" ] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/var/folders/w_/5zj48w1d0xb7ycgdm6pk40v00000gn/T/ipykernel_98848/165326261.py:5: DeprecationWarning: `set_matplotlib_formats` is deprecated since IPython 7.23, directly use `matplotlib_inline.backend_inline.set_matplotlib_formats()`\n", " set_matplotlib_formats('pdf', 'svg')\n" ] } ], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "from IPython.display import HTML, set_matplotlib_formats\n", "set_matplotlib_formats('pdf', 'svg')\n", "#css_style = open('../../../_static/custom_style.css', 'r').read()\n", "#HTML(f'')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## An adaptive envelope function\n", "\n", "Suppose we wish to sample from a log-concave univariate distribution with unnormalised distribution function $f$. Whereas RS defines a fixed envelope, ARS will define an envelope $g_u$ that upper bounds $f$ and adapts its shape as the sampling procedure progresses. By adapting its shape, using the infromation that $f$ is log-concave, the envelope reduces the probability of future rejections. In addition the envelope function, ARS can use an optional function $g_l$ which lower bounds $f$, called the squeezing function. The squeezing function can be used to avoid evaluating $f$ in the rejection step, which can be especially useful if $f$ is computationally expensive to evaluate.\n", "\n", "Given the an ordered set of points $x_1 < x_2 < ... < x_K$, ARS defines the log-envelope $\\log ~ g_u$ to be the minimum over the tangents to $h = \\log f$ at these points. The log-squeezing function is defined to be the piecewise linear function which joins the points $(x_k, h(x_k))$ inside the interval $[x_1, x_K]$ and is equal to $-\\infty$ outside this innterval. Examples of envelope and squeezing functions are shown below.\n", "\n", "