Skip to content

Instantly share code, notes, and snippets.

@ferrine
Last active November 3, 2018 11:10
Show Gist options
  • Save ferrine/57c12e2cd99eb21fa18568e3befa15fb to your computer and use it in GitHub Desktop.
Save ferrine/57c12e2cd99eb21fa18568e3befa15fb to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"import pymc3 as pm\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import theano.tensor as tt\n",
"import theano\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [],
"source": [
"with pm.Model() as model:\n",
" dummy = pm.Normal('norm', 0, 1)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 10000/10000 [00:22<00:00, 447.28it/s]\n"
]
}
],
"source": [
"# the below code is model agnostic\n",
"with model:\n",
" approx = pm.fit(method='svgd', inf_kwargs={'n_particles': 100})"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<pymc3.variational.approximations.Empirical at 0x117666d30>"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# this is an empirical approximatoin\n",
"approx"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"histogram"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# let's introspect what is inside\n",
"# every empirical has the corresponding histogram storage\n",
"# the histogram is of size (n_samples, model_dim)\n",
"approx.histogram # histogram storage"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"theano.tensor.sharedvar.TensorSharedVariable"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# this is regular theano variable\n",
"type(approx.histogram)"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [],
"source": [
"# this is a link to all plotting functions and those who accept trace object as input\n",
"trace = approx.sample(100)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.axes._subplots.AxesSubplot at 0x1175afe10>"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAbMAAAEYCAYAAADWNhiqAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAGzNJREFUeJzt3Xt8FPW9//H3bHaTAE2UMBhciuAFvFTwAgptELkJVqoghhFrKcLR/rxQpajVIyJoreg5NSoe+aGCKK0FB7R4xAqiWCmg1IJYLygqAgpBTQMkhFw2yZw/iHmwBshOyGb3m7yej0cej+zsZ775zJedeTOzk43leZ4AADBZINENAABwpAgzAIDxCDMAgPEIMwCA8QgzAIDxCDMAgPEIMwCA8QgzIAlYlhVMdA+AySx+aRpoHJZldZH0iqRVkn4iabuk4ZJOljRLUmtJn0sa73neLsuy/iZpg6S+kuZL6i6pVNJZko6RNF7SLyX9WNJaz/OuarKNAQzDmRnQuLpKeszzvB9J2i3pMknzJN3meV4PSe9LmnpAfarneb08z3uw5nFb7Q+v30j6X0kPSfqRpO6WZZ3ZRNsAGIcwAxrXF57nbaj5fp2kEyUd7XnemzXLnpHU74D65763/kve/ssl70v62vO89z3Pq5b0oaQu8WsbMBthBjSu8gO+r5J0dD31JYdYv/p7Y1VL4n014BAIMyC+9kjaZVnWeTWPx0h68zD1ABqA/+kB8TdW0izLslpL2ixpXIL7AZod7mYEABiPy4wAAOMRZgAA4xFmAADjEWYAAOPF5W7GHTt2NJu7SrKyslRYWJjoNp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YpNzdXlmWpZ8+e2rNnj77++uuomq+//lrFxcXq2bOnLMtSbm6uli5dKknKyMiordu3b58sy2qMaQKanThemAESY8uWLXr88ceVl5eniy66SIsXL9bixYv16quv6tFHH1XXrl2Vk5OjvLw87dmzR8OGDdN5550n27Y1f/58paena/Pmzbrhhhv0yiuvSJI+ev9fmuD+XRntO2jWuGHaumGtupzVR0v+cKc2/3NVnR4KLh+lSddeo507d0b9z/LYY4/Vzp07lZ2dXbts586dOvbYY+vUfOf+++/XokWLlJmZqYULF9YuX7dunQYPHqwOHTpoypQpOvnkkxt1HgGTEGZodjp16qRTTz1VktStWzf17dtXlmXplFNO0Zdffqn8/HwtX75cs2bNkiSVl5dr+/btys7O1uTJk/XRRx8pEAho8+bNtWP2OLunjsreH0rhk0/Xrh1fqstZffSzW+49aA+XHZcuNdJlz9tvv1233367Hn30Uc2dO1e33HKLunfvrn/84x9q06aNXn/9dY0fP16rV69ulJ8HmIgwQ7OTlpZW+30gEFBqamrt91VVVUpJSdETTzyhk046KWq9Bx98UO3bt9fy5ctVXV2tE044ofa578aQJCsQUHVVpSTVe2bWoUMH7dixo3Z5fn6+OnToEFXboUMH5efnH7ZGkkaOHKkxY8bolltuibr8OGjQIN1xxx0qLCxUVlbW4ScHaKYIM7Q4559/vubOnat7771XlmXpgw8+0Omnn66ioiIde+yxCgQCWrhwoaqqquodq74zsyFDhujpp5/W8OHDtX79emVmZkZdYpSk7OxsZWRkaN26dTr77LO1aNEijRs3TpK0efPm2lBdtmyZTjzxREnSN998o/bt28uyLL377ruqrq5W27Ztj2RaAKMRZmhxJk6cqKlTp2rw4MGqrq5Wp06dNG/ePI0dO1a/+tWvtGjRIg0YMECtW7c+4p81aNAgrVixQjk5OWrVqpXy8vJqn7vgggu0fPlySdJ9992n3/zmNyorK9OAAQM0cOBASdL06dP1+eefKxAIqGPHjrr//vslSS+//LLmzZunlJQUpaena+bMmdwcghbNivWuLD927NjR+IMmCLfU1mXbtrYWV/i+db6Nj/eQGnJrv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NAko3jOP8t6WJJFZI+lzTOdd3die0qcRzHGSVpmqRTJZ3ruu4/E9tRYjiOc6GkRySlSJrtuu79CW4poRzHeUrSzyR947ru6YnuJ9Ecx+kkaZ6kbEmepCdc130ksV0ljuM46ZJWSkrT/uxZ5Lru1MYanzOz2CyXdLrruj0kbZL0nwnuJ9E+kDRS+1+YLZLjOCmSHpP0U0mnSbrCcZzTEttVwj0t6cJEN5FEKiXd7LruaZL6SLqhhb9GyiUNdF33DElnSrrQcZw+jTU4Z2YxcF331QMevi0pN1G9JAPXdTdKkuM4iW4lkc6V9JnrupslyXGcBZKGS/oooV0lkOu6Kx3H6ZLoPpKF67r5kvJrvi92HGejpI5qoa8R13U9SXtrHoZqvhrtdnrCzL/xkp5LdBNIuI6Svjzg8VeSeieoFyS5mpA/S9LaBLeSUDVXNNZJOknSY67rNtp8EGY1HMd5TVKHgzw12XXdF2tqJmv/pYNnm7K3RIhlPgDUz3GcH0h6XtJE13Vb9Mfyu65bJelMx3GOlvQXx3FOd133g8YYmzCr4bru4MM97zjOVdr/5vagmtPlZq2++YC2S+p0wOMf1iwDajmOE9L+IHvWdd0XEt1PsnBdd7fjOG9o/3ushFlTqblr7beSznddd1+i+0FSeEdSV8dxjtf+EBst6eeJbQnJxHEcS9IcSRtd181LdD+J5jhOe0mRmiBrJekCSQ801vh8nFUMHMf5TPtvJ/13zaK3Xde9NoEtJZTjOJdKelRSe0m7JW1wXXdoYrtqeo7jXCTpYe2/Nf8p13V/n+CWEspxnPmS+kuyJX0taarrunMS2lQCOY7TV9LfJb0vqbpm8R2u6/41cV0ljuM4PSQ9o/37S0CS67ruPY01PmEGADAev2cGADAeYQYAMB5hBgAwHmEGADAeYQYAMB5hBgAwHmEGADDe/wHo+8zHfiOV+AAAAABJRU5ErkJggg==\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"pm.plot_posterior(trace)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Trace object can be converted to Empirical approx that can manipulate the graph. This is useful to wrap over NUTS output"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [],
"source": [
"approx2 = pm.Empirical(trace, model=model)\n",
"# in our case it is just an example"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So the question was about model averaging"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [],
"source": [
"# suppose you have a graph\n",
"out = tt.sin(dummy * 100.)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Basicaly there are two ways to do do model averaging\n",
"\n",
"* $\\bar{f} = \\mathbb{E}{f(\\xi)}$ -- model averaging\n",
"* $\\bar{f} = f(\\mathbb{E}{\\xi})$ -- mean propagation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# model averaging"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [],
"source": [
"mout = approx.sample_node(out, 100).mean()"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [],
"source": [
"samples1 = [mout.eval() for _ in range(1000)]"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD8CAYAAAB5Pm/hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAADbZJREFUeJzt3W+MXOdVx/Hv4m0jBJSQDhhvbOFKNahuVYwwSRGlCkoKSRXZQSqnLlLrlMjLi0QI0TemfdEKhOSKQhsBCthtVRtB40OgzaqxWqgRlLxIS1v1T0qAmOISezc221ohKFKrmOHFXFsTZ3ZnvDt/1me+H2m19z73mbknJzO/vXv3zvVMu91GklTX90y6AEnSaBn0klScQS9JxRn0klScQS9JxRn0klScQS9J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npEvwDsBw413x/uGr8vIh4Ebgae6TrFI0magEEur/wYcAvQiogzwHvoBHxGxD3AN4Fopp+gc2nlKTqXV75jBDVLkq5C36DPzLeusOnWHnPbwL3rLUqSNDx+MlaSijPoJak4g16SijPoJak4g16SijPoJak4g16SijPoJak4g16SijPoJak4g16SijPoJak4g16SijPoJak4g16SijPoJak4g16SijPoJak4g16SijPoJak4g16SijPoJak4g16SijPoJak4g16SijPoJak4g16SijPoJak4g16SijPoJak4g16SijPoJak4g16SijPoJak4g16SijPoJam42fU8OCJOA88CF4HnM3N3RNwAHAe2A6eByMwL6ytTkrRWwzii/4XM3JWZu5v1g8DJzNwBnGzWJUkTMopTN3uBo83yUeCuEexDkjSg9QZ9G/jbiPhiRMw3Y5szc6lZfhrYvM59SJLWYV3n6IHXZ+bZiPgR4O8i4l+7N2ZmOyLavR7Y/GCYb+bRarV6Fzg7u+K2aTXJnpybyF41LUb5up7mLJlpt3vm8FWLiPcC/wscAG7JzKWI2AL8Q2b+RJ+HtxcXF3tuaLVaLC8vD6XGKi4e2DPpEqSR2HRkYWTPXTFL5ubmAGb6zVvzqZuI+L6I+IFLy8AvAo8DC8D+Ztp+4OG17kOStH7rOUe/GXg0Ir4CfB54JDM/BRwC3hgRTwK3NeuSpAlZ8zn6zPwG8JM9xr8F3LqeoiRJw7PeP8ZK0tCM8u9Pq11IMMq/DWwE3gJBkooz6CWpOINekooz6CWpOINekooz6CWpOINekooz6CWpOINekooz6CWpOINekooz6CWpOINekooz6CWpOINekooz6CWpOINekooz6CWpOINekooz6CWpOINekooz6CWpOINekooz6CWpOINekooz6CWpOINekooz6CWpuNlJFyBJk3bxwJ6J7XvTkYWR78OgX4dJvjgkaVCeupGk4gx6SSrOoJek4gx6SSrOoJek4kZ21U1E3A7cD2wCPpSZh0axH698kaTVjeSIPiI2AX8C3AHsBN4aETtHsS9J0upGdermJuBUZn4jM78LPAjsHdG+JEmrGFXQ3wg81bV+phmTJI3ZxD4ZGxHzwDxAZjI3N7fi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RukHxHxMxFxBvgV4M8iYipuzeInYyWpuI14RC9JGiKDXpKKM+glqTiDXpKKM+glqTiDXpKKM+glqTiDXpKK+3/Gl7TYxOP4tAAAAABJRU5ErkJggg==\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.hist(samples1);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The above still has some distribution as we take few MC samples"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [],
"source": [
"mout1 = approx.sample_node(out, 10000).mean()"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [],
"source": [
"samples2 = [mout1.eval() for _ in range(1000)]"
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD8CAYAAAB5Pm/hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAADlRJREFUeJzt3W+sZPVdx/H3utPWpJAgGVn3siRbzfYBkoDJBjT6YBVKoZJdNPEbsKnbluz6AGIqPJAWExppdB+0GpIa9G6tXWIFvkoN1xaLsAZrY7D/UrUtJsW6hP3DrreQFoN/wu34YM7CAJe9c+fMuXPvd9+v5GbmnPObc777u2c+89vfnJm7aTAYIEmq64dmXYAkqVsGvSQVZ9BLUnEGvSQVZ9BLUnEGvSQVZ9BLUnEGvSQVZ9BLUnG9WRfQ8OO5kjSZTSs1WC9Bz/Hjx1s9vt/vs7i4OKVqNjb74hX2xZD98IpKfTE3NzdWO6duJKk4g16SijPoJak4g16SijPoJak4g16SijPoJak4g16SijPoJam4dfPJWOlMlvbtnuhxJ6dw7M0HF6awF2l2DHppBZO+yLTlC4ymxakbSSrOoJek4gx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XQy/b+fLwO80604LagR92774LeDDEfEvDKcqblvj+qepTV+8BXik6YevMxzlHVz7f8JUTOP5UUWbvngrsDByTpwC/mjt/wnT4ydjJam4jTailyStkkEvScUZ9JJUnEEvScUZ9JJUnEEvScUZ9JJUnEEvScX9Py86h8/Sta1QAAAAAElFTkSuQmCC\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.hist(samples2);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This distribution is much more concentrated. CLT is in work"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# mean propagation\n",
"This is a bit more tricky. The procedure is not mathematically correct in sence that it may give unexpected results"
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [],
"source": [
"mean_theta = approx.histogram.mean(0)"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [],
"source": [
"out_prop = theano.clone(approx.to_flat_input(out), {approx.input: mean_theta})"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array(0.4845134, dtype=float32)"
]
},
"execution_count": 96,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"out_prop.eval()"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.legend.Legend at 0x114ad7da0>"
]
},
"execution_count": 98,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD8CAYAAAB5Pm/hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAGBlJREFUeJzt3XtwlPW9x/F3LpRghXLZSklgxlZgWsACioDDTSRHLqGmjvSnHM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GcW+mc69XE/U1S0Is0wzn3H8AQImOj3jCzX5vZRcCPgbvSXU8yOOeyiQxDzU53LSnye+DC6HDiOv41StAiBX3iWpzuofE2zrlc4EtEXpQ92wXpLRMF6ss5VwjcCVxtZpnySSStfc6eAr6d0oqSp6XeOgIDgD865yqB4UC5c25Im1UYvyDTxnzU6OfwYSIXdwSiq24SF2S6h3JgGrAFmAJsMLNMGBf1dSqLFvtyzg0GlgITzOyDti8xbkF662Nmu6KLRcAuMkOzvZnZISB0etk590fgRxly1U2Q562Hme2LLl5NZNaBQHRGn6AzTffgnPuZc+7q6GaPAN2cc7uB24G56am2dYL05py7zDm3B/gOsNQ5d9ZPdRHwOfslcD7wv865151z5Wkqt1UC9nabc+4t59zrRH4ep6Wp3FYJ2FtGCtjbzOjz9lcir6vcGHT/emesiIjndEYvIuI5Bb2IiOcU9CIinlPQi4h4TkEvIuI5Bb2IiOcU9CIinlPQi4h47v8BXPjrB5X59QAAAAAASUVORK5CYII=\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.hist(samples2, label='model averaging', color='r');\n",
"plt.axvline(out_prop.eval(), label='mean prop', color='b');\n",
"plt.legend()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We are done, hope I gave the full answer"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "pymc4",
"language": "python",
"name": "pymc4"
},
"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.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment