Skip to content

Instantly share code, notes, and snippets.

@fonnesbeck
Created April 22, 2019 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fonnesbeck/f8b6d485b67436cbd08ed46e35bb22bb to your computer and use it in GitHub Desktop.
Save fonnesbeck/f8b6d485b67436cbd08ed46e35bb22bb to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import pymc3 as pm\n",
"import arviz as az\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"np.random.seed(42)\n",
"x = np.random.randn(100)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"with pm.Model() as m1:\n",
" m = pm.Normal('m', 0, 1)\n",
" l = pm.Normal('l', m, observed=x)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"with pm.Model() as m2:\n",
" m = pm.Normal('m', 0, 1.1)\n",
" l = pm.Normal('l', m, observed=x)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"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: [m]\n",
"Sampling 4 chains: 100%|██████████| 4000/4000 [00:00<00:00, 7948.32draws/s]\n"
]
}
],
"source": [
"with m1:\n",
" tr1 = pm.sample()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"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: [m]\n",
"Sampling 4 chains: 100%|██████████| 4000/4000 [00:00<00:00, 7828.57draws/s]\n"
]
}
],
"source": [
"with m2:\n",
" tr2 = pm.sample()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>waic</th>\n",
" <th>p_waic</th>\n",
" <th>d_waic</th>\n",
" <th>weight</th>\n",
" <th>se</th>\n",
" <th>dse</th>\n",
" <th>warning</th>\n",
" <th>waic_scale</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>model1</th>\n",
" <td>267.215</td>\n",
" <td>0.797262</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>11.1965</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>deviance</td>\n",
" </tr>\n",
" <tr>\n",
" <th>model2</th>\n",
" <td>267.219</td>\n",
" <td>0.79825</td>\n",
" <td>0.0040196</td>\n",
" <td>2.22045e-16</td>\n",
" <td>11.1885</td>\n",
" <td>0.0648026</td>\n",
" <td>0</td>\n",
" <td>deviance</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" waic p_waic d_waic weight se dse warning \\\n",
"model1 267.215 0.797262 0 1 11.1965 0 0 \n",
"model2 267.219 0.79825 0.0040196 2.22045e-16 11.1885 0.0648026 0 \n",
"\n",
" waic_scale \n",
"model1 deviance \n",
"model2 deviance "
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"az.compare({'model1': tr1, 'model2': tr2})"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment