{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Variational API quickstart\n", "\n", "The variational inference (VI) API is focused on approximating posterior distributions for Bayesian models. Common use cases to which this module can be applied include:\n", "\n", "* Sampling from model posterior and computing arbitrary expressions\n", "* Conduct Monte Carlo approximation of expectation, variance, and other statistics\n", "* Remove symbolic dependence on PyMC3 random nodes and evaluate expressions (using `eval`)\n", "* Provide a bridge to arbitrary Theano code\n", "\n", "Sounds good, doesn't it?\n", "\n", "The module provides an interface to a variety of inference methods, so you are free to choose what is most appropriate for the problem." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import pymc3 as pm\n", "import theano\n", "import numpy as np\n", "\n", "np.random.seed(42)\n", "pm.set_tt_rng(42)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic setup\n", "\n", "We do not need complex models to play with the VI API; let's begin with a simple mixture model:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "w = pm.floatX([.2, .8])\n", "mu = pm.floatX([-.3, .5])\n", "sd = pm.floatX([.1, .1])\n", "\n", "with pm.Model() as model:\n", " x = pm.NormalMixture('x', w=w, mu=mu, sigma=sd, dtype=theano.config.floatX)\n", " x2 = x ** 2\n", " sin_x = pm.math.sin(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can't compute analytical expectations for this model. However, we can obtain an approximation using Markov chain Monte Carlo methods; let's use NUTS first. \n", "\n", "To allow samples of the expressions to be saved, we need to wrap them in `Deterministic` objects:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "with model:\n", " pm.Deterministic('x2', x2)\n", " pm.Deterministic('sin_x', sin_x)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Auto-assigning NUTS sampler...\n", "Initializing NUTS using jitter+adapt_diag...\n", "Multiprocess sampling (4 chains in 4 jobs)\n", "NUTS: [x]\n" ] }, { "data": { "text/html": [ "\n", "