Skip to content

Instantly share code, notes, and snippets.

@dboyliao
Last active March 5, 2018 06:48
Show Gist options
  • Select an option

  • Save dboyliao/536434a348e31c419278d5ee5c7ea210 to your computer and use it in GitHub Desktop.

Select an option

Save dboyliao/536434a348e31c419278d5ee5c7ea210 to your computer and use it in GitHub Desktop.
Pinkoi sharing material: Google Optimize and Bayesian estimation
*.pdf
private_*

Pinkoi Sharing Material: Google Optimize and Bayesian Estimation

Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"heading_collapsed": true
},
"source": [
"## Imports"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"hidden": true,
"init_cell": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"from ipywidgets import interactive"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Helper Functions"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def normal_kernel(x, mean, sigma=1):\n",
" return np.exp(-(x-mean)**2/2*sigma**2)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# the prior\n",
"def prior(mu, mu_mean, mu_sigma):\n",
" return normal_kernel(mu, mu_mean, mu_sigma)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# the likelihood\n",
"def lh(x, mu):\n",
" return np.exp(-np.sum((x - mu)**2)/(2*10))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# the data source\n",
"def gen_data(num=10, mu=np.pi, sigma=1.0):\n",
" return sigma*np.random.randn(num) + mu"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Basic Case"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"# Bayesian Updating\n",
"num_iter = 150\n",
"mus = np.linspace(-5, 5, 1000)\n",
"init_mean = -3\n",
"init_std = 1\n",
"num_data = 5"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"post_dists = [prior(mus, init_mean, init_std)]\n",
"for i in range(num_iter):\n",
" x = gen_data(num_data)\n",
" prior_d = post_dists[-1]\n",
" # compute posterior\n",
" post_d = np.array([pri*lh(x, mu) for pri, mu in zip(prior_d, mus)])\n",
" post_dists.append(post_d/post_d.sum()) # normalize posterior to make it a distribution"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"def plot(frame):\n",
" post_d = post_dists[frame]\n",
" plt.figure('dist')\n",
" plt.plot(mus, post_d)\n",
" plt.xlabel('$\\mu$')\n",
" plt.title('Posterio Distribution of $\\mu$')\n",
" plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"interactive_plot = interactive(plot, frame=(0, len(post_dists)-1))\n",
"interactive_plot.children[0].value=0"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "971c68f534de412398109f4eec9322ac",
"version_major": 2,
"version_minor": 0
},
"text/html": [
"<p>Failed to display Jupyter Widget of type <code>interactive</code>.</p>\n",
"<p>\n",
" If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n",
" that the widgets JavaScript is still loading. If this message persists, it\n",
" likely means that the widgets JavaScript library is either not installed or\n",
" not enabled. See the <a href=\"https://ipywidgets.readthedocs.io/en/stable/user_install.html\">Jupyter\n",
" Widgets Documentation</a> for setup instructions.\n",
"</p>\n",
"<p>\n",
" If you're reading this message in another frontend (for example, a static\n",
" rendering on GitHub or <a href=\"https://nbviewer.jupyter.org/\">NBViewer</a>),\n",
" it may mean that your frontend doesn't currently support widgets.\n",
"</p>\n"
],
"text/plain": [
"interactive(children=(IntSlider(value=0, description='frame', max=150), Output()), _dom_classes=('widget-interact',))"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"interactive_plot"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.0980980980980988"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mus[post_dists[-1].argmax()]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Source Data Distribution Shift"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"shift_at = 50\n",
"num_iter = 400\n",
"mus = np.linspace(-7, 7, 1000)\n",
"init_mean = -5\n",
"init_std = 1\n",
"num_data = 5"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"post_dists = [prior(mus, init_mean, init_std)]\n",
"for i in range(num_iter):\n",
" if i < shift_at:\n",
" x = gen_data(num_data, 1)\n",
" else:\n",
" x = gen_data(num_data, -1)\n",
" prior_d = post_dists[-1]\n",
" # compute posterior\n",
" post_d = np.array([pri*lh(x, mu) for pri, mu in zip(prior_d, mus)])\n",
" post_dists.append(post_d/post_d.sum()) # normalize posterior to make it a distribution"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"interactive_plot = interactive(plot, frame=(0, len(post_dists)-1))\n",
"interactive_plot.children[0].value = 0"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "3fc0824a1cb848a986e8a8579a5f9f23",
"version_major": 2,
"version_minor": 0
},
"text/html": [
"<p>Failed to display Jupyter Widget of type <code>interactive</code>.</p>\n",
"<p>\n",
" If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n",
" that the widgets JavaScript is still loading. If this message persists, it\n",
" likely means that the widgets JavaScript library is either not installed or\n",
" not enabled. See the <a href=\"https://ipywidgets.readthedocs.io/en/stable/user_install.html\">Jupyter\n",
" Widgets Documentation</a> for setup instructions.\n",
"</p>\n",
"<p>\n",
" If you're reading this message in another frontend (for example, a static\n",
" rendering on GitHub or <a href=\"https://nbviewer.jupyter.org/\">NBViewer</a>),\n",
" it may mean that your frontend doesn't currently support widgets.\n",
"</p>\n"
],
"text/plain": [
"interactive(children=(IntSlider(value=0, description='frame', max=400), Output()), _dom_classes=('widget-interact',))"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"interactive_plot # Bayesian estimation is adaptive"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(0.85585585585585555, -0.7757757757757755)"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mus[post_dists[shift_at].argmax()], mus[post_dists[-1].argmax()]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.6.4"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"toc_cell": false,
"toc_position": {},
"toc_section_display": "block",
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment