{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Updating priors" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this notebook, I will show how it is possible to update the priors as new data becomes available. The example is a slightly modified version of the linear regression in the [Getting started with PyMC3](https://github.com/pymc-devs/pymc3/blob/master/docs/source/notebooks/getting_started.ipynb) notebook." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on PyMC3 v3.10.0\n" ] } ], "source": [ "%matplotlib inline\n", "import warnings\n", "\n", "import arviz as az\n", "import matplotlib as mpl\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import pymc3 as pm\n", "import theano.tensor as tt\n", "\n", "from pymc3 import Model, Normal, Slice, sample\n", "from pymc3.distributions import Interpolated\n", "from scipy import stats\n", "from theano import as_op\n", "\n", "plt.style.use(\"seaborn-darkgrid\")\n", "print(f\"Running on PyMC3 v{pm.__version__}\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "warnings.filterwarnings(\"ignore\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generating data" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Initialize random number generator\n", "np.random.seed(93457)\n", "\n", "# True parameter values\n", "alpha_true = 5\n", "beta0_true = 7\n", "beta1_true = 13\n", "\n", "# Size of dataset\n", "size = 100\n", "\n", "# Predictor variable\n", "X1 = np.random.randn(size)\n", "X2 = np.random.randn(size) * 0.2\n", "\n", "# Simulate outcome variable\n", "Y = alpha_true + beta0_true * X1 + beta1_true * X2 + np.random.randn(size)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Model specification" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Our initial beliefs about the parameters are quite informative (sigma=1) and a bit off the true values." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "WARNING (theano.gof.compilelock): Overriding existing lock by dead process '73117' (I am process '73202')\n", "Auto-assigning NUTS sampler...\n", "Initializing NUTS using jitter+adapt_diag...\n", "Multiprocess sampling (2 chains in 2 jobs)\n", "NUTS: [beta1, beta0, alpha]\n" ] }, { "data": { "text/html": [ "\n", "